1package bootstrap
2
3import (
4 "bytes"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8)
9
10func TestApplyGTKIMModuleFallback(t *testing.T) {
11 tests := []struct {
12 name string
13 gtkIMModule string
14 xdgSessionType string
15 waylandDisplay string
16 wantEnvSet bool
17 wantMessageSent bool
18 }{
19 {
20 name: "GTK_IM_MODULE already set => no override, no message",
21 gtkIMModule: "ibus",
22 xdgSessionType: "wayland",
23 waylandDisplay: "",
24 wantEnvSet: false,
25 wantMessageSent: false,
26 },
27 {
28 name: "non-Wayland with GTK_IM_MODULE unset => no override, no message",
29 gtkIMModule: "",
30 xdgSessionType: "x11",
31 waylandDisplay: "",
32 wantEnvSet: false,
33 wantMessageSent: false,
34 },
35 {
36 name: "Wayland via XDG_SESSION_TYPE with GTK_IM_MODULE unset => override + message",
37 gtkIMModule: "",
38 xdgSessionType: "wayland",
39 waylandDisplay: "",
40 wantEnvSet: true,
41 wantMessageSent: true,
42 },
43 {
44 name: "Wayland via WAYLAND_DISPLAY with GTK_IM_MODULE unset => override + message",
45 gtkIMModule: "",
46 xdgSessionType: "",
47 waylandDisplay: "wayland-0",
48 wantEnvSet: true,
49 wantMessageSent: true,
50 },
51 }
52
53 for _, tt := range tests {
54 t.Run(tt.name, func(t *testing.T) {
55 var stderr bytes.Buffer
56 env := map[string]string{
57 "GTK_IM_MODULE": tt.gtkIMModule,
58 "XDG_SESSION_TYPE": tt.xdgSessionType,
59 "WAYLAND_DISPLAY": tt.waylandDisplay,
60 }
61 getter := func(key string) string { return env[key] }
62 var setKey, setValue string
63 setter := func(key, val string) error {
64 setKey = key
65 setValue = val
66 return nil
67 }
68
69 ApplyGTKIMModuleFallback(&stderr, getter, setter)
70
71 msg := stderr.String()
72 if tt.wantMessageSent {
73 assert.Contains(t, msg, "GTK_IM_MODULE unset", "expected stderr message")
74 assert.Equal(t, "GTK_IM_MODULE", setKey)
75 assert.Equal(t, "gtk-im-context-simple", setValue)
76 } else {
77 assert.Empty(t, msg, "expected no stderr message")
78 assert.Empty(t, setKey, "expected no env var set")
79 }
80 })
81 }
82}