1package tray
2
3import "testing"
4
5// pixmap builds an ARGB32 icon from pixels of the given colors, n of each.
6func pixmap(colors map[[4]uint8]int) []byte {
7 var b []byte
8 for c, n := range colors {
9 for range n {
10 b = append(b, c[:]...)
11 }
12 }
13 return b
14}
15
16func TestDominant(t *testing.T) {
17 var h histogram
18 // A blue logo with a larger grey outline and a transparent background.
19 got, ok := h.dominant(pixmap(map[[4]uint8]int{
20 {255, 30, 60, 200}: 40, // blue
21 {255, 128, 128, 128}: 100,
22 {0, 255, 0, 0}: 500, // transparent red
23 }))
24 if !ok || got != (RGB{30, 60, 200}) {
25 t.Fatalf("dominant = %v, %v", got, ok)
26 }
27 if _, ok := h.dominant(pixmap(map[[4]uint8]int{{255, 200, 200, 200}: 10})); ok {
28 t.Fatal("a grey icon has no dominant color")
29 }
30 if _, ok := h.dominant(nil); ok {
31 t.Fatal("an empty icon has no dominant color")
32 }
33 icon := pixmap(map[[4]uint8]int{{255, 30, 60, 200}: 1024})
34 if allocs := testing.AllocsPerRun(50, func() { h.dominant(icon) }); allocs != 0 {
35 t.Fatalf("dominant allocates %v times", allocs)
36 }
37}
38
39func TestDominantLargeIcon(t *testing.T) {
40 // 512x512 of one saturated color: summing every pixel would overflow.
41 var h histogram
42 icon := pixmap(map[[4]uint8]int{{255, 250, 20, 20}: 512 * 512})
43 if got, ok := h.dominant(icon); !ok || got != (RGB{250, 20, 20}) {
44 t.Fatalf("dominant = %v, %v", got, ok)
45 }
46}
47
48func TestDominantLargeIconOffColumns(t *testing.T) {
49 // 512x512, transparent but for a red logo in columns 10..49: a stride of
50 // 64 pixels would sample only columns 0, 64, 128... and miss it.
51 const side = 512
52 icon := make([]byte, 0, side*side*4)
53 for range side {
54 for x := range side {
55 if x >= 10 && x < 50 {
56 icon = append(icon, 255, 250, 20, 20)
57 } else {
58 icon = append(icon, 0, 0, 0, 0)
59 }
60 }
61 }
62 var h histogram
63 if got, ok := h.dominant(icon); !ok || got != (RGB{250, 20, 20}) {
64 t.Fatalf("dominant = %v, %v", got, ok)
65 }
66}
67
68func TestReadable(t *testing.T) {
69 bg, fg := RGB{0x1e, 0x1e, 0x2e}, RGB{0xcd, 0xd6, 0xf4}
70 dark := RGB{0x17, 0x1a, 0x21} // Steam's near-black blue
71 got := readable(dark, bg, fg)
72 if contrast(got, bg) < 3 {
73 t.Fatalf("readable(%v) = %v, contrast %.2f", dark, got, contrast(got, bg))
74 }
75 bright := RGB{0xf9, 0xe2, 0xaf}
76 if readable(bright, bg, fg) != bright {
77 t.Fatal("a readable color must not change")
78 }
79}