package overflow import ( "image" "image/color" "testing" ) // page draws a rendering w x h image pixels on a flat background, with an // optional block of a different colour at x. func page(w, h int, ink *image.Rectangle) image.Image { img := image.NewRGBA(image.Rect(0, 0, w, h)) bg := color.RGBA{0xf4, 0xf4, 0xf5, 0xff} for y := 0; y < h; y++ { for x := 0; x < w; x++ { img.Set(x, y, bg) } } if ink != nil { for y := ink.Min.Y; y < ink.Max.Y; y++ { for x := ink.Min.X; x < ink.Max.X; x++ { img.Set(x, y, color.RGBA{0x11, 0x88, 0x44, 0xff}) } } } return img } func TestFits(t *testing.T) { // 390 CSS px at 2x is 780 image px: exactly the viewport. got := Measure(page(780, 1200, nil), 390, 2) if got.Overflows() { t.Fatalf("a page the width of its viewport overflows: %+v", got) } } func TestOverflowWithNothingDrawn(t *testing.T) { // 430 CSS px wide, blank past the edge: the tooltip-bubble case. got := Measure(page(860, 1200, nil), 390, 2) if got.Past != 40 { t.Errorf("past = %d, want 40", got.Past) } if got.Ink { t.Error("blank space past the edge counted as ink") } } func TestOverflowWithContent(t *testing.T) { // A badge sitting past the edge: the case a person reports. ink := image.Rect(800, 300, 850, 340) got := Measure(page(910, 1200, &ink), 390, 2) if got.Past != 65 { t.Errorf("past = %d, want 65", got.Past) } if !got.Ink { t.Error("a badge drawn past the edge was not seen") } } // A border drawn hard against the viewport edge is anti-aliased across it. // Half a border pixel is not content that fell off the screen. func TestEdgeBorderIsNotInk(t *testing.T) { ink := image.Rect(779, 0, 781, 1200) got := Measure(page(860, 1200, &ink), 390, 2) if got.Ink { t.Error("a border on the edge counted as content past it") } }