aboutsummaryrefslogtreecommitdiffstats
path: root/internal/notify/classify_test.go
blob: bbe7989f59ea6fa1246290ab287d198237f85ae5 (plain)
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
// Copyright (C) 2026 Danilo M. <danix@danix.xyz>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

package notify

import (
	"image"
	"image/png"
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/godbus/dbus/v5"
)

func writeTestPNG(t *testing.T, path string, w, h int) {
	t.Helper()
	f, err := os.Create(path)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	if err := png.Encode(f, image.NewRGBA(image.Rect(0, 0, w, h))); err != nil {
		t.Fatal(err)
	}
}

func TestIsIconImage(t *testing.T) {
	dir := t.TempDir()
	small := filepath.Join(dir, "logo.png")
	large := filepath.Join(dir, "shot.png")
	svg := filepath.Join(dir, "icon.svg")
	exactly := filepath.Join(dir, "256.png")
	over := filepath.Join(dir, "over.png")
	writeTestPNG(t, small, 32, 32)
	writeTestPNG(t, large, 800, 600)
	writeTestPNG(t, exactly, 256, 256)
	writeTestPNG(t, over, 256, 257)
	if err := os.WriteFile(svg, []byte("<svg/>"), 0o644); err != nil {
		t.Fatal(err)
	}
	cases := []struct {
		name          string
		raw, resolved string
		want          bool
	}{
		{"theme name", "utilities-terminal", "/x/apps/16/utilities-terminal.svg", true},
		{"svg", svg, svg, true},
		{"small raster", small, small, true},
		{"at the bound", exactly, exactly, true},
		{"just over the bound", over, over, false},
		{"large raster", large, large, false},
		{"missing file", "/no/such.png", "/no/such.png", false},
		{"empty", "", "", false},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			if got := IsIconImage(c.raw, c.resolved); got != c.want {
				t.Fatalf("IsIconImage(%q, %q) = %v, want %v", c.raw, c.resolved, got, c.want)
			}
		})
	}
}

func TestNotifyRoutesContentImageByKind(t *testing.T) {
	dir := t.TempDir()
	small := filepath.Join(dir, "logo.png")
	large := filepath.Join(dir, "shot.png")
	writeTestPNG(t, small, 32, 32)
	writeTestPNG(t, large, 800, 600)

	run := func(path string) Popup {
		var live []Popup
		s := &Service{dir: t.TempDir(), timers: map[uint32]*time.Timer{}}
		s.store = NewStore(s.emitClosed, func(l, _ []Popup) { live = l }, s.removeImage)
		hints := map[string]dbus.Variant{"image-path": dbus.MakeVariant(path)}
		if _, err := s.Notify("app", 0, "", "s", "b", nil, hints, -1); err != nil {
			t.Fatal(err)
		}
		if len(live) != 1 {
			t.Fatalf("published %d popups", len(live))
		}
		return live[0]
	}

	if p := run(small); p.Icon != small || p.Image != "" {
		t.Fatalf("small image: icon=%q image=%q, want icon only", p.Icon, p.Image)
	}
	if p := run(large); p.Icon != "" || p.Image != large {
		t.Fatalf("large image: icon=%q image=%q, want image only", p.Icon, p.Image)
	}
}