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
#[cfg(test)]
mod core_tests {
use std::{thread, time::Duration};
use serial_test::serial;
use raw_input::{Core};
/// Test the full start-to-stop lifecycle of the Core.
#[serial]
#[test]
fn test_core_lifecycle_management() {
// 1. Pre-check: Core should not be running initially
assert!(!Core::is_runing(), "Core should not be running before start");
// 2. Start Core in a background thread because it's a blocking call
let core_handle = thread::spawn(|| {
println!("Core thread: Calling Core::start()...");
let result = Core::start();
println!("Core thread: Core::start() has exited.");
result
});
// 3. Wait for the Core to initialize (Window creation and Hook setup)
thread::sleep(Duration::from_millis(800));
// 4. Assert: Core should be running now
assert!(Core::is_runing(), "Core::is_runing() should return true after start");
// 5. Action: Stop the Core
// This triggers unhooking and posts WM_QUIT to the message loop
println!("Main thread: Calling Core::stop()...");
Core::stop();
// 6. Join the thread to ensure the message loop actually terminated
let join_result = core_handle.join().expect("Core thread panicked");
// 7. Final Assertions
assert!(join_result.is_ok(), "Core::start() should return Ok(()) after stopping");
assert!(!Core::is_runing(), "Core::is_runing() should return false after stop");
}
/// Test the pause and resume functionality via Core's state flags.
#[serial]
#[test]
fn test_core_pause_resume_logic() {
// Start core
thread::spawn(|| { let _ = Core::start(); });
thread::sleep(Duration::from_millis(500));
// Test Pause
Core::pause();
// Here we assume IS_CORE_RUNNING is the backing store for is_runing()
assert!(!Core::is_runing(), "is_runing should be false after pause");
// Test Resume
Core::resume();
assert!(Core::is_runing(), "is_runing should be true after resume");
// Cleanup
Core::stop();
}
/// Verify that multiple calls to start() are handled gracefully.
#[serial]
#[test]
fn test_core_reentrancy_protection() {
// First start
thread::spawn(|| { let _ = Core::start(); });
thread::sleep(Duration::from_millis(500));
assert!(Core::is_runing());
// Second start should return Ok(()) immediately due to is_run() check
let second_start = Core::start();
assert!(second_start.is_ok(), "Subsequent Core::start() should not fail or re-register");
Core::stop();
}
}