1use crate::{
2 Action, AnyElement, AnyView, AnyWindowHandle, AppCell, AppContext, AsyncAppContext,
3 AvailableSpace, BackgroundExecutor, BorrowAppContext, Bounds, ClipboardItem, Context, Empty,
4 Entity, EventEmitter, ForegroundExecutor, Global, InputEvent, Keystroke, Model, ModelContext,
5 Modifiers, ModifiersChangedEvent, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
6 Pixels, Platform, Point, Render, Result, Size, Task, TestDispatcher, TestPlatform, TestWindow,
7 TextSystem, View, ViewContext, VisualContext, WindowContext, WindowHandle, WindowOptions,
8};
9use anyhow::{anyhow, bail};
10use futures::{Stream, StreamExt};
11use std::{cell::RefCell, future::Future, ops::Deref, rc::Rc, sync::Arc, time::Duration};
12
13/// A TestAppContext is provided to tests created with `#[gpui::test]`, it provides
14/// an implementation of `Context` with additional methods that are useful in tests.
15#[derive(Clone)]
16pub struct TestAppContext {
17 #[doc(hidden)]
18 pub app: Rc<AppCell>,
19 #[doc(hidden)]
20 pub background_executor: BackgroundExecutor,
21 #[doc(hidden)]
22 pub foreground_executor: ForegroundExecutor,
23 #[doc(hidden)]
24 pub dispatcher: TestDispatcher,
25 test_platform: Rc<TestPlatform>,
26 text_system: Arc<TextSystem>,
27 fn_name: Option<&'static str>,
28 on_quit: Rc<RefCell<Vec<Box<dyn FnOnce() + 'static>>>>,
29}
30
31impl Context for TestAppContext {
32 type Result<T> = T;
33
34 fn new_model<T: 'static>(
35 &mut self,
36 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
37 ) -> Self::Result<Model<T>>
38 where
39 T: 'static,
40 {
41 let mut app = self.app.borrow_mut();
42 app.new_model(build_model)
43 }
44
45 fn reserve_model<T: 'static>(&mut self) -> Self::Result<crate::Reservation<T>> {
46 let mut app = self.app.borrow_mut();
47 app.reserve_model()
48 }
49
50 fn insert_model<T: 'static>(
51 &mut self,
52 reservation: crate::Reservation<T>,
53 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
54 ) -> Self::Result<Model<T>> {
55 let mut app = self.app.borrow_mut();
56 app.insert_model(reservation, build_model)
57 }
58
59 fn update_model<T: 'static, R>(
60 &mut self,
61 handle: &Model<T>,
62 update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
63 ) -> Self::Result<R> {
64 let mut app = self.app.borrow_mut();
65 app.update_model(handle, update)
66 }
67
68 fn read_model<T, R>(
69 &self,
70 handle: &Model<T>,
71 read: impl FnOnce(&T, &AppContext) -> R,
72 ) -> Self::Result<R>
73 where
74 T: 'static,
75 {
76 let app = self.app.borrow();
77 app.read_model(handle, read)
78 }
79
80 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
81 where
82 F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
83 {
84 let mut lock = self.app.borrow_mut();
85 lock.update_window(window, f)
86 }
87
88 fn read_window<T, R>(
89 &self,
90 window: &WindowHandle<T>,
91 read: impl FnOnce(View<T>, &AppContext) -> R,
92 ) -> Result<R>
93 where
94 T: 'static,
95 {
96 let app = self.app.borrow();
97 app.read_window(window, read)
98 }
99}
100
101impl TestAppContext {
102 /// Creates a new `TestAppContext`. Usually you can rely on `#[gpui::test]` to do this for you.
103 pub fn new(dispatcher: TestDispatcher, fn_name: Option<&'static str>) -> Self {
104 let arc_dispatcher = Arc::new(dispatcher.clone());
105 let background_executor = BackgroundExecutor::new(arc_dispatcher.clone());
106 let foreground_executor = ForegroundExecutor::new(arc_dispatcher);
107 let platform = TestPlatform::new(background_executor.clone(), foreground_executor.clone());
108 let asset_source = Arc::new(());
109 let http_client = util::http::FakeHttpClient::with_404_response();
110 let text_system = Arc::new(TextSystem::new(platform.text_system()));
111
112 Self {
113 app: AppContext::new(platform.clone(), asset_source, http_client),
114 background_executor,
115 foreground_executor,
116 dispatcher: dispatcher.clone(),
117 test_platform: platform,
118 text_system,
119 fn_name,
120 on_quit: Rc::new(RefCell::new(Vec::default())),
121 }
122 }
123
124 /// The name of the test function that created this `TestAppContext`
125 pub fn test_function_name(&self) -> Option<&'static str> {
126 self.fn_name
127 }
128
129 /// Checks whether there have been any new path prompts received by the platform.
130 pub fn did_prompt_for_new_path(&self) -> bool {
131 self.test_platform.did_prompt_for_new_path()
132 }
133
134 /// returns a new `TestAppContext` re-using the same executors to interleave tasks.
135 pub fn new_app(&self) -> TestAppContext {
136 Self::new(self.dispatcher.clone(), self.fn_name)
137 }
138
139 /// Called by the test helper to end the test.
140 /// public so the macro can call it.
141 pub fn quit(&self) {
142 self.on_quit.borrow_mut().drain(..).for_each(|f| f());
143 self.app.borrow_mut().shutdown();
144 }
145
146 /// Register cleanup to run when the test ends.
147 pub fn on_quit(&mut self, f: impl FnOnce() + 'static) {
148 self.on_quit.borrow_mut().push(Box::new(f));
149 }
150
151 /// Schedules all windows to be redrawn on the next effect cycle.
152 pub fn refresh(&mut self) -> Result<()> {
153 let mut app = self.app.borrow_mut();
154 app.refresh();
155 Ok(())
156 }
157
158 /// Returns an executor (for running tasks in the background)
159 pub fn executor(&self) -> BackgroundExecutor {
160 self.background_executor.clone()
161 }
162
163 /// Returns an executor (for running tasks on the main thread)
164 pub fn foreground_executor(&self) -> &ForegroundExecutor {
165 &self.foreground_executor
166 }
167
168 /// Gives you an `&mut AppContext` for the duration of the closure
169 pub fn update<R>(&self, f: impl FnOnce(&mut AppContext) -> R) -> R {
170 let mut cx = self.app.borrow_mut();
171 cx.update(f)
172 }
173
174 /// Gives you an `&AppContext` for the duration of the closure
175 pub fn read<R>(&self, f: impl FnOnce(&AppContext) -> R) -> R {
176 let cx = self.app.borrow();
177 f(&cx)
178 }
179
180 /// Adds a new window. The Window will always be backed by a `TestWindow` which
181 /// can be retrieved with `self.test_window(handle)`
182 pub fn add_window<F, V>(&mut self, build_window: F) -> WindowHandle<V>
183 where
184 F: FnOnce(&mut ViewContext<V>) -> V,
185 V: 'static + Render,
186 {
187 let mut cx = self.app.borrow_mut();
188
189 // Some tests rely on the window size matching the bounds of the test display
190 let bounds = Bounds::maximized(None, &mut cx);
191 cx.open_window(
192 WindowOptions {
193 bounds: Some(bounds),
194 ..Default::default()
195 },
196 |cx| cx.new_view(build_window),
197 )
198 }
199
200 /// Adds a new window with no content.
201 pub fn add_empty_window(&mut self) -> &mut VisualTestContext {
202 let mut cx = self.app.borrow_mut();
203 let bounds = Bounds::maximized(None, &mut cx);
204 let window = cx.open_window(
205 WindowOptions {
206 bounds: Some(bounds),
207 ..Default::default()
208 },
209 |cx| cx.new_view(|_| Empty),
210 );
211 drop(cx);
212 let cx = VisualTestContext::from_window(*window.deref(), self).as_mut();
213 cx.run_until_parked();
214 cx
215 }
216
217 /// Adds a new window, and returns its root view and a `VisualTestContext` which can be used
218 /// as a `WindowContext` for the rest of the test. Typically you would shadow this context with
219 /// the returned one. `let (view, cx) = cx.add_window_view(...);`
220 pub fn add_window_view<F, V>(&mut self, build_window: F) -> (View<V>, &mut VisualTestContext)
221 where
222 F: FnOnce(&mut ViewContext<V>) -> V,
223 V: 'static + Render,
224 {
225 let mut cx = self.app.borrow_mut();
226 let bounds = Bounds::maximized(None, &mut cx);
227 let window = cx.open_window(
228 WindowOptions {
229 bounds: Some(bounds),
230 ..Default::default()
231 },
232 |cx| cx.new_view(build_window),
233 );
234 drop(cx);
235 let view = window.root_view(self).unwrap();
236 let cx = VisualTestContext::from_window(*window.deref(), self).as_mut();
237 cx.run_until_parked();
238
239 // it might be nice to try and cleanup these at the end of each test.
240 (view, cx)
241 }
242
243 /// returns the TextSystem
244 pub fn text_system(&self) -> &Arc<TextSystem> {
245 &self.text_system
246 }
247
248 /// Simulates writing to the platform clipboard
249 pub fn write_to_clipboard(&self, item: ClipboardItem) {
250 self.test_platform.write_to_clipboard(item)
251 }
252
253 /// Simulates reading from the platform clipboard.
254 /// This will return the most recent value from `write_to_clipboard`.
255 pub fn read_from_clipboard(&self) -> Option<ClipboardItem> {
256 self.test_platform.read_from_clipboard()
257 }
258
259 /// Simulates choosing a File in the platform's "Open" dialog.
260 pub fn simulate_new_path_selection(
261 &self,
262 select_path: impl FnOnce(&std::path::Path) -> Option<std::path::PathBuf>,
263 ) {
264 self.test_platform.simulate_new_path_selection(select_path);
265 }
266
267 /// Simulates clicking a button in an platform-level alert dialog.
268 pub fn simulate_prompt_answer(&self, button_ix: usize) {
269 self.test_platform.simulate_prompt_answer(button_ix);
270 }
271
272 /// Returns true if there's an alert dialog open.
273 pub fn has_pending_prompt(&self) -> bool {
274 self.test_platform.has_pending_prompt()
275 }
276
277 /// All the urls that have been opened with cx.open_url() during this test.
278 pub fn opened_url(&self) -> Option<String> {
279 self.test_platform.opened_url.borrow().clone()
280 }
281
282 /// Simulates the user resizing the window to the new size.
283 pub fn simulate_window_resize(&self, window_handle: AnyWindowHandle, size: Size<Pixels>) {
284 self.test_window(window_handle).simulate_resize(size);
285 }
286
287 /// Returns all windows open in the test.
288 pub fn windows(&self) -> Vec<AnyWindowHandle> {
289 self.app.borrow().windows().clone()
290 }
291
292 /// Run the given task on the main thread.
293 pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut) -> Task<R>
294 where
295 Fut: Future<Output = R> + 'static,
296 R: 'static,
297 {
298 self.foreground_executor.spawn(f(self.to_async()))
299 }
300
301 /// true if the given global is defined
302 pub fn has_global<G: Global>(&self) -> bool {
303 let app = self.app.borrow();
304 app.has_global::<G>()
305 }
306
307 /// runs the given closure with a reference to the global
308 /// panics if `has_global` would return false.
309 pub fn read_global<G: Global, R>(&self, read: impl FnOnce(&G, &AppContext) -> R) -> R {
310 let app = self.app.borrow();
311 read(app.global(), &app)
312 }
313
314 /// runs the given closure with a reference to the global (if set)
315 pub fn try_read_global<G: Global, R>(
316 &self,
317 read: impl FnOnce(&G, &AppContext) -> R,
318 ) -> Option<R> {
319 let lock = self.app.borrow();
320 Some(read(lock.try_global()?, &lock))
321 }
322
323 /// sets the global in this context.
324 pub fn set_global<G: Global>(&mut self, global: G) {
325 let mut lock = self.app.borrow_mut();
326 lock.update(|cx| cx.set_global(global))
327 }
328
329 /// updates the global in this context. (panics if `has_global` would return false)
330 pub fn update_global<G: Global, R>(
331 &mut self,
332 update: impl FnOnce(&mut G, &mut AppContext) -> R,
333 ) -> R {
334 let mut lock = self.app.borrow_mut();
335 lock.update(|cx| cx.update_global(update))
336 }
337
338 /// Returns an `AsyncAppContext` which can be used to run tasks that expect to be on a background
339 /// thread on the current thread in tests.
340 pub fn to_async(&self) -> AsyncAppContext {
341 AsyncAppContext {
342 app: Rc::downgrade(&self.app),
343 background_executor: self.background_executor.clone(),
344 foreground_executor: self.foreground_executor.clone(),
345 }
346 }
347
348 /// Wait until there are no more pending tasks.
349 pub fn run_until_parked(&mut self) {
350 self.background_executor.run_until_parked()
351 }
352
353 /// Simulate dispatching an action to the currently focused node in the window.
354 pub fn dispatch_action<A>(&mut self, window: AnyWindowHandle, action: A)
355 where
356 A: Action,
357 {
358 window
359 .update(self, |_, cx| cx.dispatch_action(action.boxed_clone()))
360 .unwrap();
361
362 self.background_executor.run_until_parked()
363 }
364
365 /// simulate_keystrokes takes a space-separated list of keys to type.
366 /// cx.simulate_keystrokes("cmd-shift-p b k s p enter")
367 /// in Zed, this will run backspace on the current editor through the command palette.
368 /// This will also run the background executor until it's parked.
369 pub fn simulate_keystrokes(&mut self, window: AnyWindowHandle, keystrokes: &str) {
370 for keystroke in keystrokes
371 .split(' ')
372 .map(Keystroke::parse)
373 .map(Result::unwrap)
374 {
375 self.dispatch_keystroke(window, keystroke);
376 }
377
378 self.background_executor.run_until_parked()
379 }
380
381 /// simulate_input takes a string of text to type.
382 /// cx.simulate_input("abc")
383 /// will type abc into your current editor
384 /// This will also run the background executor until it's parked.
385 pub fn simulate_input(&mut self, window: AnyWindowHandle, input: &str) {
386 for keystroke in input.split("").map(Keystroke::parse).map(Result::unwrap) {
387 self.dispatch_keystroke(window, keystroke);
388 }
389
390 self.background_executor.run_until_parked()
391 }
392
393 /// dispatches a single Keystroke (see also `simulate_keystrokes` and `simulate_input`)
394 pub fn dispatch_keystroke(&mut self, window: AnyWindowHandle, keystroke: Keystroke) {
395 self.update_window(window, |_, cx| cx.dispatch_keystroke(keystroke))
396 .unwrap();
397 }
398
399 /// Returns the `TestWindow` backing the given handle.
400 pub(crate) fn test_window(&self, window: AnyWindowHandle) -> TestWindow {
401 self.app
402 .borrow_mut()
403 .windows
404 .get_mut(window.id)
405 .unwrap()
406 .as_mut()
407 .unwrap()
408 .platform_window
409 .as_test()
410 .unwrap()
411 .clone()
412 }
413
414 /// Returns a stream of notifications whenever the View or Model is updated.
415 pub fn notifications<T: 'static>(&mut self, entity: &impl Entity<T>) -> impl Stream<Item = ()> {
416 let (tx, rx) = futures::channel::mpsc::unbounded();
417 self.update(|cx| {
418 cx.observe(entity, {
419 let tx = tx.clone();
420 move |_, _| {
421 let _ = tx.unbounded_send(());
422 }
423 })
424 .detach();
425 cx.observe_release(entity, move |_, _| tx.close_channel())
426 .detach()
427 });
428 rx
429 }
430
431 /// Retuens a stream of events emitted by the given Model.
432 pub fn events<Evt, T: 'static + EventEmitter<Evt>>(
433 &mut self,
434 entity: &Model<T>,
435 ) -> futures::channel::mpsc::UnboundedReceiver<Evt>
436 where
437 Evt: 'static + Clone,
438 {
439 let (tx, rx) = futures::channel::mpsc::unbounded();
440 entity
441 .update(self, |_, cx: &mut ModelContext<T>| {
442 cx.subscribe(entity, move |_model, _handle, event, _cx| {
443 let _ = tx.unbounded_send(event.clone());
444 })
445 })
446 .detach();
447 rx
448 }
449
450 /// Runs until the given condition becomes true. (Prefer `run_until_parked` if you
451 /// don't need to jump in at a specific time).
452 pub async fn condition<T: 'static>(
453 &mut self,
454 model: &Model<T>,
455 mut predicate: impl FnMut(&mut T, &mut ModelContext<T>) -> bool,
456 ) {
457 let timer = self.executor().timer(Duration::from_secs(3));
458 let mut notifications = self.notifications(model);
459
460 use futures::FutureExt as _;
461 use smol::future::FutureExt as _;
462
463 async {
464 loop {
465 if model.update(self, &mut predicate) {
466 return Ok(());
467 }
468
469 if notifications.next().await.is_none() {
470 bail!("model dropped")
471 }
472 }
473 }
474 .race(timer.map(|_| Err(anyhow!("condition timed out"))))
475 .await
476 .unwrap();
477 }
478}
479
480impl<T: 'static> Model<T> {
481 /// Block until the next event is emitted by the model, then return it.
482 pub fn next_event<Evt>(&self, cx: &mut TestAppContext) -> Evt
483 where
484 Evt: Send + Clone + 'static,
485 T: EventEmitter<Evt>,
486 {
487 let (tx, mut rx) = futures::channel::mpsc::unbounded();
488 let _subscription = self.update(cx, |_, cx| {
489 cx.subscribe(self, move |_, _, event, _| {
490 tx.unbounded_send(event.clone()).ok();
491 })
492 });
493
494 // Run other tasks until the event is emitted.
495 loop {
496 match rx.try_next() {
497 Ok(Some(event)) => return event,
498 Ok(None) => panic!("model was dropped"),
499 Err(_) => {
500 if !cx.executor().tick() {
501 break;
502 }
503 }
504 }
505 }
506 panic!("no event received")
507 }
508
509 /// Returns a future that resolves when the model notifies.
510 pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
511 use postage::prelude::{Sink as _, Stream as _};
512
513 let (mut tx, mut rx) = postage::mpsc::channel(1);
514 let mut cx = cx.app.app.borrow_mut();
515 let subscription = cx.observe(self, move |_, _| {
516 tx.try_send(()).ok();
517 });
518
519 let duration = if std::env::var("CI").is_ok() {
520 Duration::from_secs(5)
521 } else {
522 Duration::from_secs(1)
523 };
524
525 async move {
526 let notification = crate::util::timeout(duration, rx.recv())
527 .await
528 .expect("next notification timed out");
529 drop(subscription);
530 notification.expect("model dropped while test was waiting for its next notification")
531 }
532 }
533}
534
535impl<V: 'static> View<V> {
536 /// Returns a future that resolves when the view is next updated.
537 pub fn next_notification(&self, cx: &TestAppContext) -> impl Future<Output = ()> {
538 use postage::prelude::{Sink as _, Stream as _};
539
540 let (mut tx, mut rx) = postage::mpsc::channel(1);
541 let mut cx = cx.app.app.borrow_mut();
542 let subscription = cx.observe(self, move |_, _| {
543 tx.try_send(()).ok();
544 });
545
546 let duration = if std::env::var("CI").is_ok() {
547 Duration::from_secs(5)
548 } else {
549 Duration::from_secs(1)
550 };
551
552 async move {
553 let notification = crate::util::timeout(duration, rx.recv())
554 .await
555 .expect("next notification timed out");
556 drop(subscription);
557 notification.expect("model dropped while test was waiting for its next notification")
558 }
559 }
560}
561
562impl<V> View<V> {
563 /// Returns a future that resolves when the condition becomes true.
564 pub fn condition<Evt>(
565 &self,
566 cx: &TestAppContext,
567 mut predicate: impl FnMut(&V, &AppContext) -> bool,
568 ) -> impl Future<Output = ()>
569 where
570 Evt: 'static,
571 V: EventEmitter<Evt>,
572 {
573 use postage::prelude::{Sink as _, Stream as _};
574
575 let (tx, mut rx) = postage::mpsc::channel(1024);
576 let timeout_duration = Duration::from_millis(100);
577
578 let mut cx = cx.app.borrow_mut();
579 let subscriptions = (
580 cx.observe(self, {
581 let mut tx = tx.clone();
582 move |_, _| {
583 tx.blocking_send(()).ok();
584 }
585 }),
586 cx.subscribe(self, {
587 let mut tx = tx.clone();
588 move |_, _: &Evt, _| {
589 tx.blocking_send(()).ok();
590 }
591 }),
592 );
593
594 let cx = cx.this.upgrade().unwrap();
595 let handle = self.downgrade();
596
597 async move {
598 crate::util::timeout(timeout_duration, async move {
599 loop {
600 {
601 let cx = cx.borrow();
602 let cx = &*cx;
603 if predicate(
604 handle
605 .upgrade()
606 .expect("view dropped with pending condition")
607 .read(cx),
608 cx,
609 ) {
610 break;
611 }
612 }
613
614 cx.borrow().background_executor().start_waiting();
615 rx.recv()
616 .await
617 .expect("view dropped with pending condition");
618 cx.borrow().background_executor().finish_waiting();
619 }
620 })
621 .await
622 .expect("condition timed out");
623 drop(subscriptions);
624 }
625 }
626}
627
628use derive_more::{Deref, DerefMut};
629#[derive(Deref, DerefMut, Clone)]
630/// A VisualTestContext is the test-equivalent of a `WindowContext`. It allows you to
631/// run window-specific test code.
632pub struct VisualTestContext {
633 #[deref]
634 #[deref_mut]
635 /// cx is the original TestAppContext (you can more easily access this using Deref)
636 pub cx: TestAppContext,
637 window: AnyWindowHandle,
638}
639
640impl VisualTestContext {
641 /// Get the underlying window handle underlying this context.
642 pub fn handle(&self) -> AnyWindowHandle {
643 self.window
644 }
645
646 /// Provides the `WindowContext` for the duration of the closure.
647 pub fn update<R>(&mut self, f: impl FnOnce(&mut WindowContext) -> R) -> R {
648 self.cx.update_window(self.window, |_, cx| f(cx)).unwrap()
649 }
650
651 /// Creates a new VisualTestContext. You would typically shadow the passed in
652 /// TestAppContext with this, as this is typically more useful.
653 /// `let cx = VisualTestContext::from_window(window, cx);`
654 pub fn from_window(window: AnyWindowHandle, cx: &TestAppContext) -> Self {
655 Self {
656 cx: cx.clone(),
657 window,
658 }
659 }
660
661 /// Wait until there are no more pending tasks.
662 pub fn run_until_parked(&self) {
663 self.cx.background_executor.run_until_parked();
664 }
665
666 /// Dispatch the action to the currently focused node.
667 pub fn dispatch_action<A>(&mut self, action: A)
668 where
669 A: Action,
670 {
671 self.cx.dispatch_action(self.window, action)
672 }
673
674 /// Read the title off the window (set by `WindowContext#set_window_title`)
675 pub fn window_title(&mut self) -> Option<String> {
676 self.cx.test_window(self.window).0.lock().title.clone()
677 }
678
679 /// Simulate a sequence of keystrokes `cx.simulate_keystrokes("cmd-p escape")`
680 /// Automatically runs until parked.
681 pub fn simulate_keystrokes(&mut self, keystrokes: &str) {
682 self.cx.simulate_keystrokes(self.window, keystrokes)
683 }
684
685 /// Simulate typing text `cx.simulate_input("hello")`
686 /// Automatically runs until parked.
687 pub fn simulate_input(&mut self, input: &str) {
688 self.cx.simulate_input(self.window, input)
689 }
690
691 /// Simulate a mouse move event to the given point
692 pub fn simulate_mouse_move(&mut self, position: Point<Pixels>, modifiers: Modifiers) {
693 self.simulate_event(MouseMoveEvent {
694 position,
695 modifiers,
696 pressed_button: None,
697 })
698 }
699
700 /// Simulate a primary mouse click at the given point
701 pub fn simulate_click(&mut self, position: Point<Pixels>, modifiers: Modifiers) {
702 self.simulate_event(MouseDownEvent {
703 position,
704 modifiers,
705 button: MouseButton::Left,
706 click_count: 1,
707 first_mouse: false,
708 });
709 self.simulate_event(MouseUpEvent {
710 position,
711 modifiers,
712 button: MouseButton::Left,
713 click_count: 1,
714 });
715 }
716
717 /// Simulate a modifiers changed event
718 pub fn simulate_modifiers_change(&mut self, modifiers: Modifiers) {
719 self.simulate_event(ModifiersChangedEvent { modifiers })
720 }
721
722 /// Simulates the user resizing the window to the new size.
723 pub fn simulate_resize(&self, size: Size<Pixels>) {
724 self.simulate_window_resize(self.window, size)
725 }
726
727 /// debug_bounds returns the bounds of the element with the given selector.
728 pub fn debug_bounds(&mut self, selector: &'static str) -> Option<Bounds<Pixels>> {
729 self.update(|cx| cx.window.rendered_frame.debug_bounds.get(selector).copied())
730 }
731
732 /// Draw an element to the window. Useful for simulating events or actions
733 pub fn draw(
734 &mut self,
735 origin: Point<Pixels>,
736 space: Size<AvailableSpace>,
737 f: impl FnOnce(&mut WindowContext) -> AnyElement,
738 ) {
739 self.update(|cx| {
740 cx.with_element_context(|cx| {
741 let mut element = f(cx);
742 element.layout(origin, space, cx);
743 element.paint(cx);
744 });
745
746 cx.refresh();
747 })
748 }
749
750 /// Simulate an event from the platform, e.g. a SrollWheelEvent
751 /// Make sure you've called [VisualTestContext::draw] first!
752 pub fn simulate_event<E: InputEvent>(&mut self, event: E) {
753 self.test_window(self.window)
754 .simulate_input(event.to_platform_input());
755 self.background_executor.run_until_parked();
756 }
757
758 /// Simulates the user blurring the window.
759 pub fn deactivate_window(&mut self) {
760 if Some(self.window) == self.test_platform.active_window() {
761 self.test_platform.set_active_window(None)
762 }
763 self.background_executor.run_until_parked();
764 }
765
766 /// Simulates the user closing the window.
767 /// Returns true if the window was closed.
768 pub fn simulate_close(&mut self) -> bool {
769 let handler = self
770 .cx
771 .update_window(self.window, |_, cx| {
772 cx.window
773 .platform_window
774 .as_test()
775 .unwrap()
776 .0
777 .lock()
778 .should_close_handler
779 .take()
780 })
781 .unwrap();
782 if let Some(mut handler) = handler {
783 let should_close = handler();
784 self.cx
785 .update_window(self.window, |_, cx| {
786 cx.window.platform_window.on_should_close(handler);
787 })
788 .unwrap();
789 should_close
790 } else {
791 false
792 }
793 }
794
795 /// Get an &mut VisualTestContext (which is mostly what you need to pass to other methods).
796 /// This method internally retains the VisualTestContext until the end of the test.
797 pub fn as_mut(self) -> &'static mut Self {
798 let ptr = Box::into_raw(Box::new(self));
799 // safety: on_quit will be called after the test has finished.
800 // the executor will ensure that all tasks related to the test have stopped.
801 // so there is no way for cx to be accessed after on_quit is called.
802 let cx = Box::leak(unsafe { Box::from_raw(ptr) });
803 cx.on_quit(move || unsafe {
804 drop(Box::from_raw(ptr));
805 });
806 cx
807 }
808}
809
810impl Context for VisualTestContext {
811 type Result<T> = <TestAppContext as Context>::Result<T>;
812
813 fn new_model<T: 'static>(
814 &mut self,
815 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
816 ) -> Self::Result<Model<T>> {
817 self.cx.new_model(build_model)
818 }
819
820 fn reserve_model<T: 'static>(&mut self) -> Self::Result<crate::Reservation<T>> {
821 self.cx.reserve_model()
822 }
823
824 fn insert_model<T: 'static>(
825 &mut self,
826 reservation: crate::Reservation<T>,
827 build_model: impl FnOnce(&mut ModelContext<'_, T>) -> T,
828 ) -> Self::Result<Model<T>> {
829 self.cx.insert_model(reservation, build_model)
830 }
831
832 fn update_model<T, R>(
833 &mut self,
834 handle: &Model<T>,
835 update: impl FnOnce(&mut T, &mut ModelContext<'_, T>) -> R,
836 ) -> Self::Result<R>
837 where
838 T: 'static,
839 {
840 self.cx.update_model(handle, update)
841 }
842
843 fn read_model<T, R>(
844 &self,
845 handle: &Model<T>,
846 read: impl FnOnce(&T, &AppContext) -> R,
847 ) -> Self::Result<R>
848 where
849 T: 'static,
850 {
851 self.cx.read_model(handle, read)
852 }
853
854 fn update_window<T, F>(&mut self, window: AnyWindowHandle, f: F) -> Result<T>
855 where
856 F: FnOnce(AnyView, &mut WindowContext<'_>) -> T,
857 {
858 self.cx.update_window(window, f)
859 }
860
861 fn read_window<T, R>(
862 &self,
863 window: &WindowHandle<T>,
864 read: impl FnOnce(View<T>, &AppContext) -> R,
865 ) -> Result<R>
866 where
867 T: 'static,
868 {
869 self.cx.read_window(window, read)
870 }
871}
872
873impl VisualContext for VisualTestContext {
874 fn new_view<V>(
875 &mut self,
876 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
877 ) -> Self::Result<View<V>>
878 where
879 V: 'static + Render,
880 {
881 self.window
882 .update(&mut self.cx, |_, cx| cx.new_view(build_view))
883 .unwrap()
884 }
885
886 fn update_view<V: 'static, R>(
887 &mut self,
888 view: &View<V>,
889 update: impl FnOnce(&mut V, &mut ViewContext<'_, V>) -> R,
890 ) -> Self::Result<R> {
891 self.window
892 .update(&mut self.cx, |_, cx| cx.update_view(view, update))
893 .unwrap()
894 }
895
896 fn replace_root_view<V>(
897 &mut self,
898 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
899 ) -> Self::Result<View<V>>
900 where
901 V: 'static + Render,
902 {
903 self.window
904 .update(&mut self.cx, |_, cx| cx.replace_root_view(build_view))
905 .unwrap()
906 }
907
908 fn focus_view<V: crate::FocusableView>(&mut self, view: &View<V>) -> Self::Result<()> {
909 self.window
910 .update(&mut self.cx, |_, cx| {
911 view.read(cx).focus_handle(cx).clone().focus(cx)
912 })
913 .unwrap()
914 }
915
916 fn dismiss_view<V>(&mut self, view: &View<V>) -> Self::Result<()>
917 where
918 V: crate::ManagedView,
919 {
920 self.window
921 .update(&mut self.cx, |_, cx| {
922 view.update(cx, |_, cx| cx.emit(crate::DismissEvent))
923 })
924 .unwrap()
925 }
926}
927
928impl AnyWindowHandle {
929 /// Creates the given view in this window.
930 pub fn build_view<V: Render + 'static>(
931 &self,
932 cx: &mut TestAppContext,
933 build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
934 ) -> View<V> {
935 self.update(cx, |_, cx| cx.new_view(build_view)).unwrap()
936 }
937}