-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathtests.rs
442 lines (385 loc) · 13.8 KB
/
tests.rs
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use super::*;
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};
use std::thread;
use std::time::Duration;
use windows::core::PCWSTR;
use windows::Win32::Foundation::HWND;
use windows::Win32::UI::WindowsAndMessaging::FindWindowW;
static SEMAPHORE: Lazy<Arc<Mutex<u32>>> = Lazy::new(|| Arc::new(Mutex::new(0)));
// Run the tests synchronously
pub fn sync_test<T>(test: T)
where
T: FnOnce() -> (),
{
let mut tests_ran = SEMAPHORE.lock().unwrap();
test();
*tests_ran += 1;
drop(tests_ran);
}
#[test]
fn test_desktop_get() {
sync_test(|| {
let desktop = get_desktop(0).get_id().unwrap();
get_desktop(&desktop).get_index().unwrap();
})
}
#[test]
fn test_desktop_moves() {
sync_test(|| {
let current_desktop = get_current_desktop().unwrap().get_index().unwrap();
// Listen for desktop changes
let (tx, rx) = std::sync::mpsc::channel::<DesktopEvent>();
let mut _notifications_thread = listen_desktop_events(tx).unwrap();
let receiver = std::thread::spawn(move || {
let mut count = 0;
for item in rx {
if let DesktopEvent::DesktopChanged { new, old } = item {
count += 1;
println!(
"Desktop changed from {:?} to {:?} count {}",
old, new, count
);
}
if count == 3 {
// Stopping the notification thread drops the sender, and iteration ends
_notifications_thread.stop().unwrap();
}
}
count
});
// Wait for listener to have started
std::thread::sleep(Duration::from_millis(400));
// Go to desktop 0, ensure it worked
switch_desktop(0).unwrap();
assert_eq!(get_current_desktop().unwrap().get_index().unwrap(), 0);
std::thread::sleep(Duration::from_millis(400));
// Go to desktop 1, ensure it worked
switch_desktop(1).unwrap();
assert_eq!(get_current_desktop().unwrap().get_index().unwrap(), 1);
std::thread::sleep(Duration::from_millis(400));
// Go to desktop where it was, ensure it worked
switch_desktop(current_desktop).unwrap();
assert_eq!(
get_current_desktop().unwrap().get_index().unwrap(),
current_desktop
);
// Ensure desktop changed three times
assert_eq!(3, receiver.join().unwrap());
})
}
#[test]
fn test_move_notepad_between_desktops() {
sync_test(|| {
// Get notepad
let notepad_hwnd = unsafe {
let notepad = "notepad\0".encode_utf16().collect::<Vec<_>>();
let pw = PCWSTR::from_raw(notepad.as_ptr());
FindWindowW(pw, PCWSTR::null()).unwrap()
};
assert!(
notepad_hwnd != HWND::default(),
"Notepad requires to be running for this test"
);
let current_desktop = get_current_desktop().unwrap();
assert!(
0 != current_desktop.get_index().unwrap(),
"Current desktop must not be 0"
);
let notepad_is_on_current_desktop = is_window_on_current_desktop(notepad_hwnd).unwrap();
let notepad_is_on_specific_desktop =
is_window_on_desktop(current_desktop, notepad_hwnd).unwrap();
assert!(
notepad_is_on_current_desktop,
"Notepad must be on this desktop"
);
assert!(
notepad_is_on_specific_desktop,
"Notepad must be on this desktop"
);
// Move notepad current desktop -> 0 -> 1 -> current desktop
move_window_to_desktop(0, ¬epad_hwnd).unwrap();
let notepad_desktop = get_desktop_by_window(notepad_hwnd)
.unwrap()
.get_index()
.unwrap();
assert_eq!(notepad_desktop, 0, "Notepad should have moved to desktop 0");
std::thread::sleep(Duration::from_millis(300));
move_window_to_desktop(1, ¬epad_hwnd).unwrap();
let notepad_desktop = get_desktop_by_window(notepad_hwnd)
.unwrap()
.get_index()
.unwrap();
assert_eq!(notepad_desktop, 1, "Notepad should have moved to desktop 1");
std::thread::sleep(Duration::from_millis(300));
move_window_to_desktop(current_desktop, ¬epad_hwnd).unwrap();
let notepad_desktop = get_desktop_by_window(notepad_hwnd).unwrap();
assert!(
notepad_desktop == current_desktop,
"Notepad should have moved to desktop 0"
);
})
}
#[test]
fn test_pin_notepad() {
sync_test(|| {
// Get notepad
let notepad_hwnd = unsafe {
let notepad = "notepad\0".encode_utf16().collect::<Vec<_>>();
let pw = PCWSTR::from_raw(notepad.as_ptr());
FindWindowW(pw, PCWSTR::null()).unwrap()
};
assert!(
notepad_hwnd != HWND::default(),
"Notepad requires to be running for this test"
);
assert_eq!(
is_window_on_current_desktop(notepad_hwnd).unwrap(),
true,
"Notepad must be on current desktop to test this"
);
assert_eq!(
is_pinned_window(notepad_hwnd).unwrap(),
false,
"Notepad must not be pinned at the start of the test"
);
let current_desktop = get_current_desktop().unwrap();
// Pin notepad and go to desktop 0 and back
pin_window(notepad_hwnd).unwrap();
switch_desktop(0).unwrap();
assert_eq!(is_pinned_window(notepad_hwnd).unwrap(), true);
std::thread::sleep(Duration::from_millis(1000));
switch_desktop(current_desktop).unwrap();
unpin_window(notepad_hwnd).unwrap();
assert_eq!(
is_window_on_desktop(current_desktop, notepad_hwnd).unwrap(),
true
);
std::thread::sleep(Duration::from_millis(1000));
})
}
#[test]
fn test_pin_notepad_app() {
sync_test(|| {
// Get notepad
let notepad_hwnd = unsafe {
let notepad = "notepad\0".encode_utf16().collect::<Vec<_>>();
let pw = PCWSTR::from_raw(notepad.as_ptr());
FindWindowW(pw, PCWSTR::null()).unwrap()
};
assert!(
notepad_hwnd != HWND::default(),
"Notepad requires to be running for this test"
);
assert_eq!(
is_window_on_current_desktop(notepad_hwnd).unwrap(),
true,
"Notepad must be on current desktop to test this"
);
assert_eq!(
is_pinned_app(notepad_hwnd).unwrap(),
false,
"Notepad must not be pinned at the start of the test"
);
let current_desktop = get_current_desktop().unwrap();
// Pin notepad and go to desktop 0 and back
pin_app(notepad_hwnd).unwrap();
assert_eq!(is_pinned_app(notepad_hwnd).unwrap(), true);
switch_desktop(0).unwrap();
std::thread::sleep(Duration::from_millis(1000));
switch_desktop(current_desktop).unwrap();
unpin_app(notepad_hwnd).unwrap();
assert_eq!(
is_window_on_desktop(current_desktop, notepad_hwnd).unwrap(),
true
);
std::thread::sleep(Duration::from_millis(1000));
})
}
/// Rename first desktop to Foo, and then back to what it was
#[test]
fn test_rename_desktop() {
sync_test(|| {
let desktops = get_desktops().unwrap();
let first_desktop = desktops.get(0).take().unwrap();
let first_desktop_name_before = first_desktop.get_name().unwrap();
// Pre-condition
assert_ne!(
first_desktop_name_before, "Example Desktop",
"Your first desktop must be something else than \"Example Desktop\" to run this test."
);
// Rename
first_desktop.set_name("Example Desktop").unwrap();
// Ensure it worked
assert_eq!(
first_desktop.get_name().unwrap(),
"Example Desktop",
"Rename failed"
);
// Return to normal
first_desktop.set_name(&first_desktop_name_before).unwrap();
})
}
/// Test some errors
#[test]
fn test_errors() {
sync_test(|| {
// Get notepad
let notepad_hwnd = unsafe {
let notepad = "notepad\0".encode_utf16().collect::<Vec<_>>();
let pw = PCWSTR::from_raw(notepad.as_ptr());
FindWindowW(pw, PCWSTR::null()).unwrap()
};
assert_ne!(
notepad_hwnd.0, 0 as _,
"Notepad must be running for this test"
);
let err = get_desktop(99999).set_name("").unwrap_err();
assert_eq!(err, Error::DesktopNotFound);
let err = switch_desktop(99999).unwrap_err();
assert_eq!(err, Error::DesktopNotFound);
let err = get_desktop_by_window(HWND(9999999 as _)).unwrap_err();
assert_eq!(err, Error::WindowNotFound);
let err = move_window_to_desktop(99999, ¬epad_hwnd).unwrap_err();
assert_eq!(err, Error::DesktopNotFound);
let err = move_window_to_desktop(0, &HWND(999999 as _)).unwrap_err();
assert_eq!(err, Error::WindowNotFound);
});
}
#[test]
fn test_threads() {
sync_test(|| {
// let get_count = || {
// get_desktop_count().unwrap();
// };
let mut threads = vec![];
for _ in 0..555 {
threads.push(std::thread::spawn(|| {
get_desktops().unwrap().iter().for_each(|d| {
let _n = d.get_name().unwrap();
let _i = d.get_index().unwrap();
// println!("Thread {n} {i} {:?}", std::thread::current().id());
})
}));
}
thread::sleep(Duration::from_millis(150));
for t in threads {
t.join().unwrap();
}
})
}
#[test]
fn test_listener_manual() {
// This test can be run only individually
let args = std::env::args().collect::<Vec<_>>();
if !args.contains(&"tests::test_listener_manual".to_owned()) {
return;
}
sync_test(|| {
let (tx, rx) = std::sync::mpsc::channel::<DesktopEvent>();
let _notifications_thread = listen_desktop_events(tx);
std::thread::spawn(|| {
for item in rx {
println!("{:?}", item);
}
});
// Wait for keypress
println!("⛔ Press enter to stop");
let mut input = String::new();
std::io::stdin().read_line(&mut input).unwrap();
})
}
#[test]
fn test_kill_explorer_exe_manually() {
// This test can be run only individually
let args = std::env::args().collect::<Vec<_>>();
if !args.contains(&"tests::test_kill_explorer_exe_manually".to_owned()) {
return;
}
sync_test(|| {
// Kill explorer.exe
let mut cmd = std::process::Command::new("taskkill");
cmd.arg("/F").arg("/IM").arg("explorer.exe");
cmd.output().unwrap();
std::thread::sleep(Duration::from_secs(2));
// Create listener
let (tx, rx) = std::sync::mpsc::channel::<DesktopEvent>();
let _notifications_thread = listen_desktop_events(tx);
let _receiver = std::thread::spawn(|| {
let mut count = 0;
for item in rx {
println!("{:?}", item);
if let DesktopEvent::DesktopChanged { new: _, old: _ } = item {
count += 1;
}
}
count
});
// Try switching desktops, can't work
let error = switch_desktop(0);
assert_eq!(error, Err(Error::ClassNotRegistered));
// Start explorer exe
let mut cmd = std::process::Command::new("explorer.exe");
cmd.spawn().unwrap();
std::thread::sleep(Duration::from_secs(4));
// Try switching desktops, should work now
switch_desktop(0).unwrap();
std::thread::sleep(Duration::from_secs(1));
switch_desktop(1).unwrap();
std::thread::sleep(Duration::from_secs(1));
switch_desktop(2).unwrap();
std::thread::sleep(Duration::from_secs(1));
drop(_notifications_thread);
let count = _receiver.join().unwrap();
assert_eq!(count, 3);
})
}
#[test]
fn test_switch_desktops_rapidly_manual() {
// This test can be run only individually
let args = std::env::args().collect::<Vec<_>>();
if !args.contains(&"tests::test_switch_desktops_rapidly_manual".to_owned()) {
return;
}
sync_test(|| {
let (tx, rx) = std::sync::mpsc::channel::<DesktopEvent>();
// let (tx, rx) = crossbeam_channel::unbounded::<DesktopEvent>();
let mut _notifications_thread = listen_desktop_events(tx).unwrap();
let receiver = std::thread::spawn(move || {
let mut count = 0;
for item in rx {
if let DesktopEvent::DesktopChanged {
new: _new,
old: _old,
} = item
{
count += 1;
}
if count == 7998 {
// This stops the receiver loop as the sender thread ends
_notifications_thread.stop().unwrap();
}
}
count
});
let current_desktop = get_current_desktop().unwrap();
for _ in 0..3999 {
switch_desktop(0).unwrap();
// std::thread::sleep(Duration::from_millis(4));
switch_desktop(1).unwrap();
}
// Finally return to same desktop we were
std::thread::sleep(Duration::from_millis(130));
switch_desktop(current_desktop).unwrap();
let count = receiver.join().unwrap();
assert!(count >= 1999);
println!("End of program, starting to drop stuff...");
})
}
#[test]
fn test_desktop_count() {
sync_test(|| {
let count = get_desktop_count().unwrap();
assert!(count > 1);
})
}