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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Test-only accessors on `AppState`, gated on the `test-hooks` feature.
//!
//! These wrappers expose `pub(crate)` setters and synthetic event dispatchers
//! so integration tests can drive the runtime without going through the
//! platform layer. None of these are reachable in a release build of a
//! downstream crate unless the `test-hooks` feature is explicitly enabled.
#![cfg(any(test, feature = "test-hooks"))]
use slate_platform::{Key, KeyCode, Modifiers, Window, WindowId};
use crate::event::{
ImeCommitHandler, ImeLifecycleHandler, ImePreeditHandler, KeyHandler, TextInputHandler,
};
use crate::types::ElementId;
use super::state::AppState;
use super::types::{AppSignal, RecoveryState};
impl AppState {
// -------------------------------------------------------------------------
// Window setup helpers — public to integration tests under test-hooks.
// -------------------------------------------------------------------------
/// Register a window's redraw requester. Test-only public wrapper around
/// the `pub(crate)` `register_redraw_requester` so integration tests in
/// `tests/` can call it without a private-items import.
pub fn register_redraw_requester_for_test(
&self,
window: WindowId,
req: crate::executor::RedrawRequester,
) {
self.register_redraw_requester(window, req);
}
// -------------------------------------------------------------------------
// Window-id helper — returns first registered window id for single-window tests.
// -------------------------------------------------------------------------
/// Return the `WindowId` of the first registered window. Test-only.
/// Panics if no window is registered (harness not yet set up).
pub fn window_id_for_test(&self) -> WindowId {
self.windows
.borrow()
.keys()
.next()
.copied()
.expect("no windows registered — call make_state() which inserts a test window")
}
// -------------------------------------------------------------------------
// Per-window renderer accessors
// -------------------------------------------------------------------------
/// Current renderer generation for a window. `None` if not yet initialized.
pub fn renderer_generation_for(&self, window: WindowId) -> Option<u64> {
self.windows
.borrow()
.get(&window)
.and_then(|w| w.renderer.borrow().as_ref().map(|r| r.current_generation()))
}
/// Convenience: renderer generation for the first registered window.
pub fn renderer_generation(&self) -> Option<u64> {
self.renderer_generation_for(self.window_id_for_test())
}
/// True if the renderer reports the device as lost.
pub fn renderer_is_device_lost_for(&self, window: WindowId) -> bool {
self.windows
.borrow()
.get(&window)
.and_then(|w| w.renderer.borrow().as_ref().map(|r| r.is_device_lost()))
.unwrap_or(false)
}
/// Convenience: device-lost status for the first registered window.
pub fn renderer_is_device_lost(&self) -> bool {
self.renderer_is_device_lost_for(self.window_id_for_test())
}
/// Snapshot of the current recovery state for a window.
pub fn current_recovery_state(&self) -> RecoveryState {
let window = self.window_id_for_test();
self.windows
.borrow()
.get(&window)
.map(|w| w.recovery_state.borrow().clone())
.unwrap_or(RecoveryState::NotLost)
}
/// Trigger a synthetic device-lost. Returns `false` if no renderer initialized.
#[cfg(all(target_os = "windows", feature = "test-hooks"))]
pub fn force_renderer_device_lost(&self, window: WindowId) -> bool {
self.windows
.borrow()
.get(&window)
.and_then(|w| {
w.renderer.borrow().as_ref().map(|r| {
r.force_device_lost();
true
})
})
.unwrap_or(false)
}
/// Synthetic `LuidMigration`-origin device loss.
#[cfg(all(target_os = "windows", feature = "test-hooks"))]
pub fn force_renderer_device_lost_luid_migration_for_test(&self) -> bool {
let window = self.window_id_for_test();
self.windows
.borrow()
.get(&window)
.and_then(|w| {
w.renderer.borrow().as_ref().map(|r| {
r.force_device_lost_luid_migration();
true
})
})
.unwrap_or(false)
}
/// Consume the renderer's `wgpu_callback_fired` flag.
#[cfg(feature = "test-hooks")]
pub fn consume_wgpu_callback_fired_for_test(&self) -> bool {
let window = self.window_id_for_test();
self.windows
.borrow()
.get(&window)
.and_then(|w| {
w.renderer
.borrow()
.as_ref()
.map(|r| r.consume_wgpu_callback_fired())
})
.unwrap_or(false)
}
/// Fire the device-lost callback logic for testing.
#[cfg(feature = "test-hooks")]
pub fn fire_renderer_device_lost_callback(
&self,
reason: wgpu::DeviceLostReason,
message: String,
) -> bool {
let window = self.window_id_for_test();
self.windows
.borrow()
.get(&window)
.and_then(|w| {
w.renderer
.borrow()
.as_ref()
.map(|r| r.fire_device_lost_callback_for_test(reason, message))
})
.unwrap_or(false)
}
// -------------------------------------------------------------------------
// Misc accessors
// -------------------------------------------------------------------------
/// Read the sync-path quit signal.
pub fn pending_quit(&self) -> bool {
self.pending_quit.get()
}
/// Request a redraw on the first registered window.
pub fn request_redraw(&self) {
let window = self.window_id_for_test();
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.window.request_redraw();
}
}
// -------------------------------------------------------------------------
// Handler installation helpers
// -------------------------------------------------------------------------
/// Install App-level keyboard handlers. Test-only wrapper.
pub fn install_key_handlers_for_test(
&self,
on_key_down: Vec<KeyHandler>,
on_key_up: Vec<KeyHandler>,
on_text_input: Vec<TextInputHandler>,
) {
self.install_key_handlers(on_key_down, on_key_up, on_text_input);
}
// -------------------------------------------------------------------------
// Per-window element registration helpers
// -------------------------------------------------------------------------
/// Register a per-element keyboard handler bundle. Test-only.
pub fn install_element_key_handlers_for_test(
&self,
window: WindowId,
id: ElementId,
handlers: crate::event::KeyHandlers,
) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.key_handler_map.borrow_mut().insert(id, handlers);
}
}
/// Register a focusable entry directly into the registry. Test-only.
pub fn register_focusable_for_test(
&self,
window: WindowId,
entry: crate::focus::FocusableEntry,
) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.focus_registry.borrow_mut().register(entry);
}
}
/// Set focus to the given element. Test-only.
pub fn set_focus_for_test(&self, window: WindowId, id: ElementId) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.focus_registry.borrow_mut().set_focus(id);
}
}
/// Insert a parent_map edge. Test-only.
pub fn set_parent_for_test(&self, window: WindowId, child: ElementId, parent: ElementId) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.parent_map.borrow_mut().insert(child, parent);
}
}
/// Read the current focused element. Test-only.
pub fn focused_for_test(&self, window: WindowId) -> Option<ElementId> {
self.windows
.borrow()
.get(&window)
.and_then(|w| w.focus_registry.borrow().focused())
}
/// Register a per-element mouse handler bundle. Test-only.
pub fn install_element_mouse_handlers_for_test(
&self,
window: WindowId,
id: ElementId,
handlers: crate::event::MouseHandlers,
) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.mouse_handler_map.borrow_mut().insert(id, handlers);
}
}
// -------------------------------------------------------------------------
// Synthetic dispatch helpers
// -------------------------------------------------------------------------
/// Dispatch a synthetic `KeyDown`. Test-only.
pub fn dispatch_key_down_for_test(
&self,
window: WindowId,
code: KeyCode,
key: Key,
modifiers: Modifiers,
is_repeat: bool,
) -> AppSignal {
self.dispatch_key_down(window, code, key, modifiers, is_repeat)
}
/// Dispatch a synthetic `KeyUp`. Test-only.
pub fn dispatch_key_up_for_test(
&self,
window: WindowId,
code: KeyCode,
key: Key,
modifiers: Modifiers,
) -> AppSignal {
self.dispatch_key_up(window, code, key, modifiers)
}
/// Dispatch a synthetic `TextInput`. Test-only.
pub fn dispatch_text_input_for_test(&self, window: WindowId, text: String) -> AppSignal {
self.dispatch_text_input(window, text)
}
/// Dispatch a synthetic `MouseDown`. Test-only.
pub fn dispatch_mouse_down_for_test(
&self,
window: WindowId,
position: (f32, f32),
button: crate::event::MouseButton,
modifiers: Modifiers,
) -> AppSignal {
self.dispatch_mouse_down(window, position, button, modifiers)
}
/// Dispatch a synthetic `MouseMoved` then flush the coalesced move. Test-only.
pub fn dispatch_mouse_move_for_test(
&self,
window: WindowId,
position: (f32, f32),
) -> AppSignal {
let _ = self.dispatch_mouse_moved(window, position, Modifiers::default());
self.flush_coalesced_move(window);
AppSignal::RequestRedraw { window }
}
/// Dispatch a synthetic `MouseMoved` returning the raw `AppSignal`. Test-only.
pub fn dispatch_mouse_moved_raw_for_test(
&self,
window: WindowId,
position: (f32, f32),
) -> AppSignal {
self.dispatch_mouse_moved(window, position, Modifiers::default())
}
/// Dispatch a synthetic `MouseUp`. Test-only.
pub fn dispatch_mouse_up_for_test(
&self,
window: WindowId,
position: (f32, f32),
button: crate::event::MouseButton,
modifiers: Modifiers,
) -> AppSignal {
self.dispatch_mouse_up(window, position, button, modifiers)
}
/// Dispatch a synthetic `CaptureLost`. Test-only.
pub fn dispatch_capture_lost_for_test(&self, window: WindowId) -> AppSignal {
self.dispatch_capture_lost(window)
}
/// Run the unmount-driven capture release. Test-only.
pub fn release_capture_if_unmounted_for_test(&self, window: WindowId) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
let hit = win.hit_test_list.borrow();
AppState::release_capture_if_unmounted(win, &hit);
}
}
/// Read the current capture target for a window. Test-only.
pub fn capture_target_for_test(&self, window: WindowId) -> Option<ElementId> {
self.windows
.borrow()
.get(&window)
.and_then(|w| *w.capture_target.borrow())
}
/// Read the explicit-capture flag for a window. Test-only.
pub fn explicit_capture_for_test(&self, window: WindowId) -> bool {
self.windows
.borrow()
.get(&window)
.map(|w| *w.explicit_capture.borrow())
.unwrap_or(false)
}
/// Clear the hit-test list for a window. Test-only.
pub fn clear_hit_test_list_for_test(&self, window: WindowId) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.hit_test_list.borrow_mut().clear();
}
}
/// Push a hit region directly into the window's hit-test list. Test-only.
pub fn push_hit_region_for_test(&self, window: WindowId, region: crate::hit_test::HitRegion) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.hit_test_list.borrow_mut().push(region);
}
}
// -------------------------------------------------------------------------
// IME test-hook accessors
// -------------------------------------------------------------------------
/// Install App-level IME handlers. Test-only.
pub fn install_ime_handlers_for_test(
&self,
on_ime_preedit: Vec<ImePreeditHandler>,
on_ime_commit: Vec<ImeCommitHandler>,
on_ime_enabled: Vec<ImeLifecycleHandler>,
on_ime_disabled: Vec<ImeLifecycleHandler>,
) {
self.install_ime_handlers(
on_ime_preedit,
on_ime_commit,
on_ime_enabled,
on_ime_disabled,
);
}
/// Register a per-element IME handler bundle. Test-only.
pub fn install_element_ime_handlers_for_test(
&self,
window: WindowId,
id: ElementId,
handlers: crate::event::ImeHandlers,
) {
let guard = self.windows.borrow();
if let Some(win) = guard.get(&window) {
win.ime_handler_map.borrow_mut().insert(id, handlers);
}
}
/// Register an IME-capable element. Test-only.
pub fn register_ime_state_for_test(
&self,
window: WindowId,
id: ElementId,
) -> std::rc::Rc<std::cell::RefCell<crate::ime::ImeState>> {
let guard = self.windows.borrow();
guard
.get(&window)
.expect("window not found")
.ime_registry
.borrow_mut()
.register(id)
}
/// Write an `ImeState` and republish the cache. Test-only.
pub fn set_ime_state_for_test(
&self,
window: WindowId,
id: ElementId,
new_state: crate::ime::ImeState,
) {
let rc = {
let guard = self.windows.borrow();
guard
.get(&window)
.expect("window not found")
.ime_registry
.borrow_mut()
.register(id)
};
*rc.borrow_mut() = new_state;
self.republish_ime_cache(window);
}
/// Dispatch a synthetic `ImeEnabled`. Test-only.
pub fn dispatch_ime_enabled_for_test(&self, window: WindowId) -> AppSignal {
self.dispatch_ime_enabled(window)
}
/// Dispatch a synthetic `ImeDisabled`. Test-only.
pub fn dispatch_ime_disabled_for_test(&self, window: WindowId) -> AppSignal {
self.dispatch_ime_disabled(window)
}
/// Dispatch a synthetic `ImePreedit`. Test-only.
pub fn dispatch_ime_preedit_for_test(
&self,
window: WindowId,
text: String,
cursor_byte_offset: usize,
selection: Option<std::ops::Range<usize>>,
) -> AppSignal {
self.dispatch_ime_preedit(window, text, cursor_byte_offset, selection)
}
/// Dispatch a synthetic `ImeCommit`. Test-only.
pub fn dispatch_ime_commit_for_test(&self, window: WindowId, text: String) -> AppSignal {
self.dispatch_ime_commit(window, text)
}
/// Re-publish the IME cache snapshot. Test-only.
pub fn republish_ime_cache_for_test(&self, window: WindowId) {
self.republish_ime_cache(window);
}
/// Query the cached caret rect. Test-only.
pub fn ime_caret_rect_query_for_test(
&self,
window: WindowId,
) -> Option<slate_platform::PhysicalRect> {
use slate_platform::WindowImeDelegate;
WindowImeDelegate::ime_caret_rect(self, window)
}
/// Query a text substring from the cached IME snapshot. Test-only.
pub fn ime_text_query_for_test(
&self,
window: WindowId,
range: std::ops::Range<usize>,
) -> Option<String> {
use slate_platform::WindowImeDelegate;
WindowImeDelegate::ime_text(self, window, range)
}
}