Skip to main content

selection_capture/
windows_subscriber.rs

1use crate::native_subscriber::define_native_subscriber_core;
2use crate::WindowsObserverBridge;
3
4define_native_subscriber_core!(
5    observer_bridge = WindowsObserverBridge,
6    runtime_adapter_type = WindowsNativeRuntimeAdapter,
7    stats_type = WindowsNativeSubscriberStats,
8    ensure_hook_fn = ensure_windows_native_subscriber_hook_installed,
9    stats_fn = windows_native_subscriber_stats,
10    set_adapter_fn = set_windows_native_runtime_adapter,
11    adapter_registered_fn = windows_native_runtime_adapter_registered,
12    reset_fn = reset_windows_native_subscriber_for_tests,
13);
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18    use crate::windows_observer::windows_observer_test_lock;
19
20    #[test]
21    fn lifecycle_hook_drives_subscriber_stats() {
22        let _guard = windows_observer_test_lock()
23            .lock()
24            .expect("test lock poisoned");
25        let _ = WindowsObserverBridge::stop();
26        WindowsObserverBridge::set_lifecycle_hook(None);
27        reset_windows_native_subscriber_for_tests();
28
29        ensure_windows_native_subscriber_hook_installed();
30        assert_eq!(windows_native_subscriber_stats().starts, 0);
31
32        assert!(WindowsObserverBridge::start());
33        let started = windows_native_subscriber_stats();
34        assert!(started.active);
35        assert_eq!(started.starts, 1);
36        assert_eq!(started.stops, 0);
37
38        assert!(WindowsObserverBridge::stop());
39        let stopped = windows_native_subscriber_stats();
40        assert!(!stopped.active);
41        assert_eq!(stopped.starts, 1);
42        assert_eq!(stopped.stops, 1);
43    }
44
45    #[test]
46    fn runtime_adapter_failures_are_recorded() {
47        fn adapter(_active: bool) -> bool {
48            false
49        }
50
51        let _guard = windows_observer_test_lock()
52            .lock()
53            .expect("test lock poisoned");
54        let _ = WindowsObserverBridge::stop();
55        WindowsObserverBridge::set_lifecycle_hook(None);
56        reset_windows_native_subscriber_for_tests();
57        set_windows_native_runtime_adapter(Some(adapter));
58        ensure_windows_native_subscriber_hook_installed();
59
60        assert!(WindowsObserverBridge::start());
61        assert!(WindowsObserverBridge::stop());
62
63        let stats = windows_native_subscriber_stats();
64        assert_eq!(stats.adapter_attempts, 2);
65        assert_eq!(stats.adapter_failures, 2);
66    }
67}