69 lines · 1.8 KB
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 {