-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathdesktop_test.go
More file actions
396 lines (313 loc) · 11.2 KB
/
desktop_test.go
File metadata and controls
396 lines (313 loc) · 11.2 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package src
import (
"fmt"
"os"
"os/user"
"testing"
)
func TestStringifyEnv(t *testing.T) {
if Xorg.stringify() != constEnvXorg {
t.Error("TestStringifyEnv: wrong value for Xorg env")
}
if Wayland.stringify() != constEnvWayland {
t.Error("TestStringifyEnv: wrong value for Wayland env")
}
if Custom.stringify() != constEnvXorg {
t.Error("TestStringifyEnv: wrong value for Custom env")
}
}
func TestStringEnv(t *testing.T) {
if Undefined.string() != constEnvSUndefined {
t.Error("TestStringEnv: wrong value for Xorg env")
}
if Xorg.string() != constEnvSXorg {
t.Error("TestStringEnv: wrong value for Xorg env")
}
if Wayland.string() != constEnvSWayland {
t.Error("TestStringEnv: wrong value for Xorg env")
}
if Custom.string() != constEnvSCustom {
t.Error("TestStringEnv: wrong value for Xorg env")
}
if UserCustom.string() != constEnvSUserCustom {
t.Error("TestStringEnv: wrong value for Xorg env")
}
}
func TestPrintDesktops(t *testing.T) {
desktops := []*desktop{{name: "a", envOrigin: Xorg},
{name: "b", envOrigin: Wayland},
{name: "c", envOrigin: Custom},
{name: "d", envOrigin: UserCustom},
{name: "e", envOrigin: Xorg},
{name: "f", envOrigin: Wayland},
{name: "g", envOrigin: Custom},
{name: "h", envOrigin: UserCustom},
{name: "i", envOrigin: Xorg},
{name: "j", envOrigin: Wayland},
{name: "k", envOrigin: Custom}}
var result string
conf := &config{}
conf.VerticalSelection = false
conf.IdentifyEnvs = false
result = readOutput(func() {
printDesktops(conf, desktops)
})
if result != "[0] a, [1] b, [2] c, [3] d, [4] e, [5] f, [6] g, [7] h, [8] i, [9] j, [10] k" {
t.Error("TestPrintDesktops: wrong output for VerticalSelection=false, IdentifyEnvs=false")
}
conf.VerticalSelection = true
conf.IdentifyEnvs = false
conf.IndentSelection = 0
result = readOutput(func() {
printDesktops(conf, desktops)
})
if result != "[0] a\n[1] b\n[2] c\n[3] d\n[4] e\n[5] f\n[6] g\n[7] h\n[8] i\n[9] j\n[10] k" {
t.Error("TestPrintDesktops: wrong output for VerticalSelection=true, IdentifyEnvs=false, IndentSelection=0")
}
conf.VerticalSelection = false
conf.IdentifyEnvs = true
result = readOutput(func() {
printDesktops(conf, desktops)
})
if result != "|Xorg| [0] a |Wayland| [1] b |Custom| [2] c |User Custom| [3] d |Xorg| [4] e |Wayland| [5] f |Custom| [6] g |User Custom| [7] h |Xorg| [8] i |Wayland| [9] j |Custom| [10] k" {
t.Error("TestPrintDesktops: wrong output for VerticalSelection=false, IdentifyEnvs=true")
}
conf.VerticalSelection = true
conf.IdentifyEnvs = true
conf.IndentSelection = 0
result = readOutput(func() {
printDesktops(conf, desktops)
})
if result != "|Xorg|\n[0] a\n\n|Wayland|\n[1] b\n\n|Custom|\n[2] c\n\n|User Custom|\n[3] d\n\n|Xorg|\n[4] e\n\n|Wayland|\n[5] f\n\n|Custom|\n[6] g\n\n|User Custom|\n[7] h\n\n|Xorg|\n[8] i\n\n|Wayland|\n[9] j\n\n|Custom|\n[10] k" {
t.Error("TestPrintDesktops: wrong output for VerticalSelection=true, IdentifyEnvs=true, IndentSelection=0")
}
conf.VerticalSelection = true
conf.IdentifyEnvs = false
conf.IndentSelection = 4
result = readOutput(func() {
printDesktops(conf, desktops)
})
if result != " [0] a\n [1] b\n [2] c\n [3] d\n [4] e\n [5] f\n [6] g\n [7] h\n [8] i\n [9] j\n [10] k" {
t.Error("TestPrintDesktops: wrong output for VerticalSelection=true, IndentSelection=4")
}
}
func TestParseEnv(t *testing.T) {
var env enEnvironment
env = parseEnv("", "xorg")
if env != Xorg {
t.Error("TestParseEnv: wrong default value")
}
env = parseEnv("xorg", "wayland")
if env != Xorg {
t.Error("TestParseEnv: wrong parsed value for wayland")
}
env = parseEnv("wayland", "xorg")
if env != Wayland {
t.Error("TestParseEnv: wrong parsed value for wayland")
}
env = parseEnv("aaa", "bbb")
if env != Xorg {
t.Error("TestParseEnv: wrong fallback value")
}
}
func TestLoadUserDesktop(t *testing.T) {
loadUserDesktop(getTestingPath("userHome2"))
d, _ := loadUserDesktop(getTestingPath("userHome"))
fmt.Println(d.exec)
if d.exec != "none" {
t.Error("TestLoadUserDesktop: wrong EXEC value")
}
if d.selection != SelectionFalse {
t.Error("TestLoadUserDesktop: wrong SELECTION value")
}
if d.env != Wayland {
t.Error("TestLoadUserDesktop: wrong ENVIRONMENT value")
}
if d.name != "window-manager" {
t.Error("TestLoadUserDesktop: wrong NAME value")
}
if !d.isUser {
t.Error("TestLoadUserDesktop: wrong isUser value")
}
if d.loginShell == "" {
t.Error("TestLoadUserDesktop: wrong loginShell value")
}
readOutput(func() {
d, _ = loadUserDesktop(getTestingPath("userHome3"))
if d == nil || d.exec != "" || d.name != "" {
t.Error("TestLoadUserDesktop: No desktop returned, selection does not return empty desktop or exec/name are not empty.")
}
})
d, _ = loadUserDesktop("/dev/null")
if d != nil {
t.Error("TestLoadUserDesktop: No desktop should be returned, no data available")
}
}
func TestGetDesktop(t *testing.T) {
d := getDesktop(getTestingPath("desktops/desktop2.desktop"), Custom)
if d.exec != "/usr/bin/desktop2" {
t.Error("TestLoadUserDesktop: wrong EXEC value")
}
if d.selection != SelectionFalse {
t.Error("TestLoadUserDesktop: wrong SELECTION value")
}
if d.env != Wayland {
t.Error("TestLoadUserDesktop: wrong ENVIRONMENT value")
}
if d.name != "Desktop2" {
t.Error("TestLoadUserDesktop: wrong NAME value")
}
if d.isUser {
t.Error("TestLoadUserDesktop: wrong isUser value")
}
}
func TestGetUserLastSession(t *testing.T) {
usr := &sysuser{}
usr.homedir = getTestingPath("userHome2")
getUserLastSession(usr)
usr.homedir = getTestingPath("userHome")
s := getUserLastSession(usr)
if s.env != Wayland {
t.Error("TestGetUserLastSession: wrong env value")
}
if s.exec != "/usr/bin/none" {
t.Error("TestGetUserLastSession: wrong exec value")
}
}
func TestGetLastDesktop(t *testing.T) {
usr := &sysuser{}
usr.homedir = getTestingPath("userHome")
desktops := []*desktop{{exec: "/usr/bin/none", env: Xorg}, {exec: "/usr/bin/none", env: Wayland}, {exec: "/usr/bin/none2", env: Wayland}}
if getLastDesktop(usr, desktops) != 1 {
t.Error("TestGetLastDesktop: expected different index")
}
}
func TestListDesktops(t *testing.T) {
desktops := listDesktops(Custom, getTestingPath("userHome"))
if len(desktops) > 0 {
t.Error("TestListDesktops: no desktop was expected")
}
desktops = listDesktops(Custom, getTestingPath("desktops"))
if len(desktops) == 0 {
t.Error("TestListDesktops: desktops were expected")
}
for _, d := range desktops {
if d.name == "Desktop1" && (d.exec != "/usr/bin/desktop1" || d.desktopNames != "Desk1:DESKTOP_1") {
t.Error("TestListDesktops: wrongly loaded desktop")
}
if d.name == "Desktop2" && d.env != Wayland {
t.Error("TestListDesktops: wrongly loaded desktop, environment is not parsed correctly")
}
}
}
func TestIsLastDesktopForSave(t *testing.T) {
currentDesktop := &desktop{exec: "/usr/bin/none", env: Wayland}
lastDesktop := &desktop{exec: "/usr/bin/none", env: Wayland}
usr := &sysuser{}
usr.homedir = "/dev/null"
if !isLastDesktopForSave(usr, lastDesktop, currentDesktop) {
t.Error("TestIsLastDesktopForSave: file not exists and doesn't need to save")
}
usr.homedir = getTestingPath("userHome")
if isLastDesktopForSave(usr, lastDesktop, currentDesktop) {
t.Error("TestIsLastDesktopForSave: desktops should not need to save")
}
lastDesktop.env = Xorg
if !isLastDesktopForSave(usr, lastDesktop, currentDesktop) {
t.Error("TestIsLastDesktopForSave: desktop should be saved, env is different")
}
}
func TestSetUserLastSession(t *testing.T) {
d := &desktop{exec: "/usr/bin/none", env: Wayland}
currentUser, _ := user.Current()
usr := getSysuser(currentUser)
usr.homedir = "/tmp/emptty-test/"
setUserLastSession(usr, d)
if !fileExists(usr.homedir + pathLastSession) {
t.Error("TestSetUserLastSession: last session is not being saved")
}
os.RemoveAll(usr.homedir)
}
func TestListAllDesktops(t *testing.T) {
usr := &sysuser{}
usr.homedir = getTestingPath("userHome2")
desktops := listAllDesktops(usr, getTestingPath("desktops"), getTestingPath("desktops"))
if len(desktops) != 6 {
t.Error("TestListAllDesktops: unexpected count of desktops, 6 expected")
}
usr.homedir = "/dev/null"
desktops = listAllDesktops(usr, "/dev/null", "/dev/null")
if len(desktops) != 0 {
t.Error("TestListAllDesktops: unexpected count of desktops, 0 expected")
}
}
func TestFindAutoselectDesktop(t *testing.T) {
usr := &sysuser{}
usr.homedir = getTestingPath("userHome2")
desktops := listAllDesktops(usr, getTestingPath("desktops"), getTestingPath("desktops"))
d1 := findAutoselectDesktop("CustomDesktop1", Undefined, desktops)
if d1 == nil || d1.name != "CustomDesktop1" {
t.Error("TestFindAutoselectDesktop: could not find desktop by its name")
}
d2 := findAutoselectDesktop("custom-desktop2", Xorg, desktops)
if d2 == nil || d2.name != "CustomDesktop2" {
t.Error("TestFindAutoselectDesktop: could not find desktop by its exec")
}
d3 := findAutoselectDesktop("unknowndESktop", Undefined, desktops)
if d3 != nil {
t.Error("TestFindAutoselectDesktop: found desktop, that should be unknown")
}
d4 := findAutoselectDesktop("UnknownDesktop", Wayland, desktops)
if d4 != nil {
t.Error("TestFindAutoselectDesktop: found desktop, that should be unknown")
}
}
func TestGetStrExec(t *testing.T) {
d := &desktop{path: "/dev/null", exec: "/usr/bin/none"}
cmd, isExec := d.getStrExec()
if !isExec || cmd != "/usr/bin/none" {
t.Errorf("TestGetStrExec: unexpected result: %s; %t", cmd, isExec)
}
d.exec = ""
cmd, isExec = d.getStrExec()
if isExec || cmd != "/dev/null" {
t.Errorf("TestGetStrExec: unexpected result: %s; %t", cmd, isExec)
}
d = &desktop{path: "/dev/null", exec: "/usr/bin/none", selection: SelectionAuto, child: d}
cmd, isExec = d.getStrExec()
if isExec || cmd != d.path+" "+d.child.exec {
t.Errorf("TestGetStrExec: unexpected result: %s; %t", cmd, isExec)
}
}
func TestGetDesktopBaseExec(t *testing.T) {
exec1, args1 := getDesktopBaseExec("/usr/bin/shell ")
if exec1 != "shell" || args1 != "" {
t.Error("TestGetDesktopBaseExec: wrong value for exec1")
}
exec2, args2 := getDesktopBaseExec("/usr/bin/shell --argument1 --argument2='none' -a3 some")
if exec2 != "shell" || args2 != "--argument1 --argument2='none' -a3 some" {
t.Error("TestGetDesktopBaseExec: wrong value for exec2")
}
exec3, args3 := getDesktopBaseExec("shell --argument1 --argument2='none' -a3 some")
if exec3 != "shell" || args3 != "--argument1 --argument2='none' -a3 some" {
t.Error("TestGetDesktopBaseExec: wrong value for exec3")
}
exec4, args4 := getDesktopBaseExec("shell")
if exec4 != "shell" || args4 != "" {
t.Error("TestGetDesktopBaseExec: wrong value for exec4")
}
exec5, args5 := getDesktopBaseExec(" / ")
if exec5 != "" || args5 != "" {
t.Error("TestGetDesktopBaseExec: wrong value for exec5")
}
}
func TestGetDesktopName(t *testing.T) {
d := getDesktop(getTestingPath("desktops/desktop1.desktop"), Custom)
if desktopName := d.getDesktopName(); desktopName != "Desk1" {
t.Errorf("TestGetDesktopName: desktop1 got unexpected desktop name '%s'", d.getDesktopName())
}
d = getDesktop(getTestingPath("desktops/desktop2.desktop"), Custom)
if desktopName := d.getDesktopName(); desktopName != "Desktop2" {
t.Errorf("TestGetDesktopName: desktop2 got unexpected desktop name '%s'", d.getDesktopName())
}
}