iconset_test.go (1369B)
1 package main 2 3 import ( 4 "image" 5 "image/color" 6 "testing" 7 8 "github.com/jackmordaunt/icns/v4" 9 ) 10 11 func solid(side int, c color.NRGBA) *image.NRGBA { 12 img := image.NewNRGBA(image.Rect(0, 0, side, side)) 13 for i := 0; i < len(img.Pix); i += 4 { 14 img.Pix[i], img.Pix[i+1], img.Pix[i+2], img.Pix[i+3] = c.R, c.G, c.B, c.A 15 } 16 return img 17 } 18 19 // TestPixelSizes covers the slots that land on the same number of pixels, 20 // where only one drawing can be kept. 21 func TestPixelSizes(t *testing.T) { 22 t.Parallel() 23 var ( 24 plain = color.NRGBA{R: 0xFF, A: 0xFF} 25 retina = color.NRGBA{B: 0xFF, A: 0xFF} 26 ) 27 images := map[icns.Slot]image.Image{ 28 {Points: 16, Scale: 1}: solid(16, plain), 29 {Points: 16, Scale: 2}: solid(32, retina), 30 {Points: 32, Scale: 1}: solid(32, plain), 31 {Points: 512, Scale: 2}: solid(1024, retina), 32 } 33 got := pixelSizes(images) 34 if len(got) != 3 { 35 t.Fatalf("mapped to %d sizes, want 3", len(got)) 36 } 37 for size, want := range map[uint]color.NRGBA{16: plain, 32: plain, 1024: retina} { 38 img, ok := got[size] 39 if !ok { 40 t.Errorf("no drawing at %d pixels", size) 41 continue 42 } 43 if img.Bounds().Dx() != int(size) { 44 t.Errorf("the drawing at %d pixels is %d wide", size, img.Bounds().Dx()) 45 } 46 if c := color.NRGBAModel.Convert(img.At(0, 0)).(color.NRGBA); c != want { 47 t.Errorf("the drawing at %d pixels is %v, want %v", size, c, want) 48 } 49 } 50 }