forked from kenn-io/agentsview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesktop_icon_test.go
More file actions
123 lines (104 loc) · 3.3 KB
/
Copy pathdesktop_icon_test.go
File metadata and controls
123 lines (104 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package agentsview_test
import (
"bytes"
"encoding/binary"
"image"
"image/png"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDesktopAppIconIncludesTransparentDockPadding(t *testing.T) {
iconPaths := []string{
"desktop/src-tauri/icons/icon.png",
"desktop/src-tauri/icons/32x32.png",
"desktop/src-tauri/icons/128x128.png",
"desktop/src-tauri/icons/128x128@2x.png",
}
for _, path := range iconPaths {
t.Run(filepath.Base(path), func(t *testing.T) {
img := decodePNGFile(t, path)
assertIconHasTransparentPadding(t, path, img)
})
}
}
func TestDesktopMacIconBundleIncludesTransparentDockPadding(t *testing.T) {
data, err := os.ReadFile("desktop/src-tauri/icons/icon.icns")
require.NoError(t, err)
require.GreaterOrEqual(t, len(data), 8, "icon.icns should contain an ICNS header")
require.Equal(t, "icns", string(data[:4]))
require.Equal(t, len(data), int(binary.BigEndian.Uint32(data[4:8])))
const pngSignature = "\x89PNG\r\n\x1a\n"
decoded := 0
for offset := 8; offset < len(data); {
require.LessOrEqual(t, offset+8, len(data), "truncated ICNS entry header")
entryType := string(data[offset : offset+4])
entrySize := int(binary.BigEndian.Uint32(data[offset+4 : offset+8]))
require.GreaterOrEqual(t, entrySize, 8, "invalid ICNS entry size for %s", entryType)
require.LessOrEqual(t, offset+entrySize, len(data), "truncated ICNS entry %s", entryType)
payload := data[offset+8 : offset+entrySize]
if bytes.HasPrefix(payload, []byte(pngSignature)) {
img, err := png.Decode(bytes.NewReader(payload))
require.NoError(t, err, "decode ICNS PNG entry %s", entryType)
assertIconHasTransparentPadding(t, entryType, img)
decoded++
}
offset += entrySize
}
require.NotZero(t, decoded, "icon.icns should include PNG icon renditions")
}
func decodePNGFile(t *testing.T, path string) image.Image {
t.Helper()
file, err := os.Open(path)
require.NoError(t, err)
defer file.Close()
img, err := png.Decode(file)
require.NoError(t, err)
return img
}
func assertIconHasTransparentPadding(t *testing.T, name string, img image.Image) {
t.Helper()
bounds := img.Bounds()
left, top, right, bottom, ok := visibleAlphaMargins(img)
require.True(t, ok, "%s should contain visible pixels", name)
minMargin := max(1, bounds.Dx()/12)
assert.GreaterOrEqual(t, left, minMargin, "%s left transparent padding", name)
assert.GreaterOrEqual(t, top, minMargin, "%s top transparent padding", name)
assert.GreaterOrEqual(t, right, minMargin, "%s right transparent padding", name)
assert.GreaterOrEqual(t, bottom, minMargin, "%s bottom transparent padding", name)
}
func visibleAlphaMargins(img image.Image) (int, int, int, int, bool) {
bounds := img.Bounds()
minX, minY := bounds.Max.X, bounds.Max.Y
maxX, maxY := bounds.Min.X-1, bounds.Min.Y-1
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
_, _, _, alpha := img.At(x, y).RGBA()
if alpha == 0 {
continue
}
if x < minX {
minX = x
}
if x > maxX {
maxX = x
}
if y < minY {
minY = y
}
if y > maxY {
maxY = y
}
}
}
if maxX < minX || maxY < minY {
return 0, 0, 0, 0, false
}
return minX - bounds.Min.X,
minY - bounds.Min.Y,
bounds.Max.X - 1 - maxX,
bounds.Max.Y - 1 - maxY,
true
}