package ui import ( "image" "image/color" "slices" "testing" "github.com/egoist/mygo/internal/platform" ) // A square in black, and a check mark in currentColor, as icon sets draw // them. const ( squareSVG = `` checkSVG = `` // A logo of two colors, and a third that follows the text. logoSVG = `` ) // colorIn returns the color at the center of r in img, at scale. func colorIn(img *image.RGBA, r Rect, scale float32) color.RGBA { return img.RGBAAt(int((r.X+r.W/2)*scale), int((r.Y+r.H/2)*scale)) } func TestIconTakesTheTextColorAndSize(t *testing.T) { square := MustParseSVG([]byte(squareSVG)) blue := RGB(0, 0, 255) view := func(c *context) { coreColumn(c).Fill().Padding(10).Gap(10).Background(RGB(255, 255, 255)).Children(func() { coreIcon(c, square).Label("plain") coreRow(c).TextColor(blue).FontSize(24).Children(func() { coreIcon(c, square).Label("inherited") }) coreIcon(c, square).Size(40, 20).TextColor(blue).Label("sized") }) } tt := coreNewTester(view, 200, 200) theme := tt.rt.c.theme plain, _ := tt.Find("plain") if plain.W != theme.FontSize || plain.H != theme.FontSize { t.Errorf("an icon is %vx%v, not the font size, %v", plain.W, plain.H, theme.FontSize) } if plain.X != 10 { t.Errorf("an icon stretches across its column: at %v", plain.X) } inherited, _ := tt.Find("inherited") if inherited.W != 24 || inherited.H != 24 { t.Errorf("an icon is %vx%v, not its row's font size", inherited.W, inherited.H) } img := tt.Image() if c := colorIn(img, plain, 1); c != (color.RGBA{theme.Text.R, theme.Text.G, theme.Text.B, 255}) { t.Errorf("an icon is %v, not the theme's text color, %v", c, theme.Text) } if c := colorIn(img, inherited, 1); c != (color.RGBA{0, 0, 255, 255}) { t.Errorf("an icon is %v, not its row's text color", c) } // A size of another aspect ratio fits the icon in, centered. sized, _ := tt.Find("sized") if sized.W != 40 || sized.H != 20 { t.Fatalf("a sized icon is %vx%v", sized.W, sized.H) } if c := img.RGBAAt(int(sized.X+2), int(sized.Y+10)); c != (color.RGBA{255, 255, 255, 255}) { t.Errorf("the icon does not keep its aspect ratio: %v beside it", c) } if c := img.RGBAAt(int(sized.X+20), int(sized.Y+10)); c != (color.RGBA{0, 0, 255, 255}) { t.Errorf("the icon is not centered: %v", c) } } func TestIconDrawsItsShapeOnce(t *testing.T) { check := MustParseSVG([]byte(checkSVG)) hot := false view := func(c *context) { col := RGB(0, 0, 0) if hot { col = RGB(255, 0, 0) } coreIcon(c, check).FontSize(32).TextColor(col).Label("check") } tt := coreNewTester(view, 100, 100) tt.SetScale(2) draws := 0 j := &tt.rt.svgs.job j.draw = func() (int, int, []byte) { draws++; return tt.rt.rasterizeIcon() } for i := range 6 { hot = i%2 == 1 tt.Frame() r, _ := tt.Find("check") // The corner of the check mark, at (9, 17) of 24. c := tt.Image().RGBAAt(int((r.X+r.W*9/24)*2), int((r.Y+r.H*17/24)*2)) if want := uint8(0); hot { want = 255 if c.R != want || c.G != 0 { t.Errorf("frame %d: the check mark is %v, not red", i, c) } } else if c.R != 0 || c.A != 255 { t.Errorf("frame %d: the check mark is %v, not black", i, c) } } // The mask is drawn for the first frame, and once more as the frame // after it finds it drawn recently and keeps it: changes of color // leave it be. if draws > 2 { t.Errorf("the icon's shape was drawn %d times for changes of color", draws) } r, _ := tt.Find("check") if r.W != 32 || r.H != 32 { t.Errorf("the icon is %vx%v DIPs", r.W, r.H) } } func TestImageOfAnSVG(t *testing.T) { logo := MustParseSVG([]byte(logoSVG)) if w, h := logo.Size(); w != 30 || h != 10 { t.Fatalf("Size %vx%v", w, h) } view := func(c *context) { coreColumn(c).Fill().Background(RGB(255, 255, 255)).TextColor(RGB(0, 0, 255)).AlignItems(Start).Children(func() { coreImage(c, logo).Label("logo") coreImage(c, logo).Size(60, 60).Fit(Cover).Label("cover") coreImage(c, logo).Size(60, 10).Fit(FillBox).Label("stretched") }) } tt := coreNewTester(view, 200, 200) img := tt.Image() r, _ := tt.Find("logo") if r.W != 30 || r.H != 10 { t.Errorf("the logo is %vx%v, not its size", r.W, r.H) } for i, want := range []color.RGBA{{255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}} { if c := img.RGBAAt(int(r.X)+i*10+5, int(r.Y)+5); c != want { t.Errorf("part %d of the logo is %v, want %v", i, c, want) } } // Cover fills the box with the middle of the logo. r, _ = tt.Find("cover") if c := img.RGBAAt(int(r.X)+2, int(r.Y)+30); c != (color.RGBA{0, 255, 0, 255}) { t.Errorf("the covering logo shows %v at its left", c) } r, _ = tt.Find("stretched") if c := img.RGBAAt(int(r.X)+55, int(r.Y)+5); c != (color.RGBA{0, 0, 255, 255}) { t.Errorf("the stretched logo shows %v at its right", c) } } // ObjectPosition places a picture where Fit crops it or leaves room // around it, an SVG's and a bitmap's alike, as CSS's object-position. func TestObjectPosition(t *testing.T) { logo := MustParseSVG([]byte(logoSVG)) // red, green and currentColor, 30×10 strip := image.NewRGBA(image.Rect(0, 0, 3, 1)) for i, c := range []color.RGBA{{255, 0, 0, 255}, {0, 255, 0, 255}, {0, 0, 255, 255}} { strip.SetRGBA(i, 0, c) } bitmap := NewBitmap(strip) red, green, blue, white := color.RGBA{255, 0, 0, 255}, color.RGBA{0, 255, 0, 255}, color.RGBA{0, 0, 255, 255}, color.RGBA{255, 255, 255, 255} for _, src := range []ImageSource{logo, bitmap} { var x, y float32 set := false fit := Cover tt := coreNewTester(func(c *context) { coreColumn(c).Fill().Background(RGB(255, 255, 255)).TextColor(RGB(0, 0, 255)).AlignItems(Start).Children(func() { e := coreImage(c, src).Size(60, 60).Fit(fit).Label("picture") if set { e.ObjectPosition(x, y) } }) }, 100, 100) at := func(px, py float32) color.RGBA { r, _ := tt.Find("picture") return tt.Image().RGBAAt(int(r.X+px), int(r.Y+py)) } check := func(what string, px, py float32, want color.RGBA) { t.Helper() if c := at(px, py); c != want { t.Errorf("%T, %s: %v at %v, %v, want %v", src, what, c, px, py, want) } } check("covering, in the middle by default", 2, 30, green) set, x, y = true, 0, 0.5 tt.Frame() // 180×60, of which the left third shows. check("covering, by the left edge", 2, 30, red) check("covering, by the left edge", 58, 30, red) x = 1 tt.Frame() check("covering, by the right edge", 2, 30, blue) check("covering, by the right edge", 58, 30, blue) x = 0.5 tt.Frame() check("covering, at 50% 50%", 2, 30, green) // Contained, 60×20, at the top and at the bottom. fit, x, y = Contain, 0.5, 0 tt.Frame() check("contained, at the top", 2, 5, red) check("contained, at the top", 2, 50, white) y = 1 tt.Frame() check("contained, at the bottom", 2, 55, red) check("contained, at the bottom", 2, 5, white) // Past the edge, clipped to the box. y = 2 tt.Frame() check("contained, past the bottom", 2, 55, white) } } func TestPictureFollowsTheTextColor(t *testing.T) { logo := MustParseSVG([]byte(logoSVG)) col := RGB(0, 0, 255) view := func(c *context) { coreImage(c, logo).TextColor(col).Label("logo") } tt := coreNewTester(view, 100, 100) first := tt.rt.svgs.pictures if len(first) != 1 { t.Fatalf("%d pictures", len(first)) } var img any for _, p := range first { img = p.img } col = RGB(255, 0, 255) tt.Frame() r, _ := tt.Find("logo") if c := tt.Image().RGBAAt(int(r.X+r.W*25/30), int(r.Y+r.H/2)); c != (color.RGBA{255, 0, 255, 255}) { t.Errorf("currentColor is %v after the text color changed", c) } pics := tt.rt.svgs.pictures if len(pics) != 1 { t.Fatalf("%d pictures after a change of color", len(pics)) } for _, p := range pics { if any(p.img) != img { t.Error("a change of color made another picture, and another texture") } } // Pictures no frame draws go. col = RGB(1, 2, 3) for range 40 { tt.Frame() } if n := len(tt.rt.svgs.pictures); n != 1 { t.Errorf("%d pictures kept", n) } } func TestIconsInAccessibility(t *testing.T) { check := MustParseSVG([]byte(checkSVG)) tt := coreNewTester(func(c *context) { coreRow(c).Children(func() { coreIcon(c, check) coreIcon(c, check).Label("Done") }) }, 100, 50) tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) var images []string for _, n := range tt.h.access.Nodes { if n.Role == platform.RoleImage { images = append(images, n.Label) } } if len(images) != 1 || images[0] != "Done" { t.Errorf("images seen: %q", images) } } func TestPainterDrawsSVGs(t *testing.T) { check, logo := MustParseSVG([]byte(checkSVG)), MustParseSVG([]byte(logoSVG)) img := coreRender(func(c *context) { coreBox(c).Fill().Background(RGB(255, 255, 255)).Draw(func(p *Painter, r Rect) { p.Icon(check, Rect{0, 0, 48, 48}, RGB(0, 128, 0)) p.Image(logo, Rect{0, 60, 30, 10}, Contain) }) }, 100, 100, 1) if c := img.RGBAAt(18, 34); c.G < 100 || c.R > 50 { t.Errorf("the icon is %v", c) } if c := img.RGBAAt(5, 65); c != (color.RGBA{255, 0, 0, 255}) { t.Errorf("the image is %v", c) } } func TestParseSVGErrors(t *testing.T) { if _, err := ParseSVG([]byte("")); err == nil { t.Error("ParseSVG takes a document that is not SVG") } defer func() { if recover() == nil { t.Error("MustParseSVG takes a document that is not SVG") } }() MustParseSVG([]byte("nope")) } // A diagram of two colors that a prefers-color-scheme media query sets. const schemeSVG = ` ` // TestSVGCustomProperties is #1691's repro: a fill of var() draws its // color, not the black of a fill MyGo did not understand. func TestSVGCustomProperties(t *testing.T) { const plain = `` const custom = `` for _, src := range []string{plain, custom} { s := MustParseSVG([]byte(src)) tt := coreNewTester(func(c *context) { coreImage(c, s).Size(100, 100) }, 100, 100) if c := tt.Image().RGBAAt(50, 50); c != (color.RGBA{255, 255, 255, 255}) { t.Errorf("%s: the center is %v, want white", src, c) } } } // TestSVGFollowsTheTheme draws an SVG's dark colors in a dark theme, // unless WithDark says otherwise. func TestSVGFollowsTheTheme(t *testing.T) { s := MustParseSVG([]byte(schemeSVG)) light, dark := color.RGBA{255, 255, 255, 255}, color.RGBA{0x0d, 0x11, 0x17, 255} view := func(c *context) { coreRow(c).Children(func() { coreImage(c, s).Label("theme") coreImage(c, s.WithDark(false)).Label("light") coreImage(c, s.WithDark(true)).Label("dark") }) } tt := coreNewTester(view, 100, 100) check := func(label string, want color.RGBA) { t.Helper() r, _ := tt.Find(label) if c := colorIn(tt.Image(), r, 1); c != want { t.Errorf("%s: %v, want %v", label, c, want) } } check("theme", light) check("light", light) check("dark", dark) tt.SetDark(true) check("theme", dark) check("light", light) check("dark", dark) // Image, the method, draws light unless told. if c := s.Image(4, 4, nil).RGBAAt(2, 2); c != light { t.Errorf("Image: %v, want light", c) } if c := s.WithDark(true).Image(4, 4, nil).RGBAAt(2, 2); c != dark { t.Errorf("Image of WithDark(true): %v, want dark", c) } } func TestSVGUnsupported(t *testing.T) { s := MustParseSVG([]byte(``)) if got := s.Unsupported(); !slices.Equal(got, []string{"", "filter"}) { t.Errorf("Unsupported() = %q", got) } for _, src := range []string{squareSVG, checkSVG, logoSVG, schemeSVG, textSVG} { if got := MustParseSVG([]byte(src)).Unsupported(); len(got) != 0 { t.Errorf("Unsupported() = %q for an SVG drawn whole", got) } } var none *SVG if none.Unsupported() != nil { t.Error("a nil SVG has something unsupported") } } // A label centered on x = 100, in the system's fonts. const textSVG = ` HIH` // TestSVGText draws text with the system's fonts, centered where // text-anchor says. func TestSVGText(t *testing.T) { s := MustParseSVG([]byte(textSVG)) tt := coreNewTester(func(c *context) { coreImage(c, s).Background(RGB(255, 255, 255)) }, 200, 60) img := tt.Image() x0, x1, y0, y1 := 200, 0, 60, 0 for y := range 60 { for x := range 200 { if c := img.RGBAAt(x, y); c.B > 200 && c.R < 100 { x0, x1, y0, y1 = min(x0, x), max(x1, x+1), min(y0, y), max(y1, y+1) } } } if x1 <= x0 { t.Fatal("no text drawn") } // The letters, as wide as 32 pixels' three capitals, stand on the // baseline at 40, centered on 100. if mid := float32(x0+x1) / 2; mid < 98 || mid > 102 { t.Errorf("the text is from x %d to %d: centered on %v, not 100", x0, x1, mid) } if w := x1 - x0; w < 40 || w > 90 { t.Errorf("the text is %d pixels wide", w) } if y1 < 38 || y1 > 41 || y0 > 22 || y0 < 10 { t.Errorf("the text is from y %d to %d, not on the baseline at 40", y0, y1) } }