1use crate::{
2 AnyWindowHandle, AppContext, AsyncAppContext, Context, Executor, Handle, MainThread,
3 ModelContext, Result, Task, TestDispatcher, TestPlatform, WindowContext,
4};
5use parking_lot::Mutex;
6use std::{any::Any, future::Future, sync::Arc};
7
8#[derive(Clone)]
9pub struct TestAppContext {
10 pub(crate) app: Arc<Mutex<AppContext>>,
11 pub(crate) executor: Executor,
12}
13
14impl Context for TestAppContext {
15 type EntityContext<'a, 'w, T> = ModelContext<'a, T>;
16 type Result<T> = T;
17
18 fn entity<T: 'static>(
19 &mut self,
20 build_entity: impl FnOnce(&mut Self::EntityContext<'_, '_, T>) -> T,
21 ) -> Self::Result<Handle<T>>
22 where
23 T: Any + Send + Sync,
24 {
25 let mut lock = self.app.lock();
26 lock.entity(build_entity)
27 }
28
29 fn update_entity<T: 'static, R>(
30 &mut self,
31 handle: &Handle<T>,
32 update: impl FnOnce(&mut T, &mut Self::EntityContext<'_, '_, T>) -> R,
33 ) -> Self::Result<R> {
34 let mut lock = self.app.lock();
35 lock.update_entity(handle, update)
36 }
37}
38
39impl TestAppContext {
40 pub fn new(dispatcher: TestDispatcher) -> Self {
41 let executor = Executor::new(Arc::new(dispatcher));
42 let platform = Arc::new(TestPlatform::new(executor.clone()));
43 let asset_source = Arc::new(());
44 let http_client = util::http::FakeHttpClient::with_404_response();
45 Self {
46 app: AppContext::new(platform, asset_source, http_client),
47 executor,
48 }
49 }
50
51 pub fn refresh(&mut self) -> Result<()> {
52 let mut lock = self.app.lock();
53 lock.refresh();
54 Ok(())
55 }
56
57 pub fn executor(&self) -> &Executor {
58 &self.executor
59 }
60
61 pub fn update<R>(&self, f: impl FnOnce(&mut AppContext) -> R) -> R {
62 let mut lock = self.app.lock();
63 f(&mut *lock)
64 }
65
66 pub fn read_window<R>(
67 &self,
68 handle: AnyWindowHandle,
69 read: impl FnOnce(&WindowContext) -> R,
70 ) -> R {
71 let mut app_context = self.app.lock();
72 app_context.read_window(handle.id, read).unwrap()
73 }
74
75 pub fn update_window<R>(
76 &self,
77 handle: AnyWindowHandle,
78 update: impl FnOnce(&mut WindowContext) -> R,
79 ) -> R {
80 let mut app = self.app.lock();
81 app.update_window(handle.id, update).unwrap()
82 }
83
84 pub fn spawn<Fut, R>(&self, f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static) -> Task<R>
85 where
86 Fut: Future<Output = R> + Send + 'static,
87 R: Send + 'static,
88 {
89 let cx = self.to_async();
90 self.executor.spawn(async move { f(cx).await })
91 }
92
93 pub fn spawn_on_main<Fut, R>(
94 &self,
95 f: impl FnOnce(AsyncAppContext) -> Fut + Send + 'static,
96 ) -> Task<R>
97 where
98 Fut: Future<Output = R> + 'static,
99 R: Send + 'static,
100 {
101 let cx = self.to_async();
102 self.executor.spawn_on_main(|| f(cx))
103 }
104
105 pub fn run_on_main<R>(
106 &self,
107 f: impl FnOnce(&mut MainThread<AppContext>) -> R + Send + 'static,
108 ) -> Task<R>
109 where
110 R: Send + 'static,
111 {
112 let mut app_context = self.app.lock();
113 app_context.run_on_main(f)
114 }
115
116 pub fn has_global<G: 'static>(&self) -> bool {
117 let lock = self.app.lock();
118 lock.has_global::<G>()
119 }
120
121 pub fn read_global<G: 'static, R>(&self, read: impl FnOnce(&G, &AppContext) -> R) -> R {
122 let lock = self.app.lock();
123 read(lock.global(), &lock)
124 }
125
126 pub fn try_read_global<G: 'static, R>(
127 &self,
128 read: impl FnOnce(&G, &AppContext) -> R,
129 ) -> Option<R> {
130 let lock = self.app.lock();
131 Some(read(lock.try_global()?, &lock))
132 }
133
134 pub fn update_global<G: 'static, R>(
135 &mut self,
136 update: impl FnOnce(&mut G, &mut AppContext) -> R,
137 ) -> R {
138 let mut lock = self.app.lock();
139 lock.update_global(update)
140 }
141
142 fn to_async(&self) -> AsyncAppContext {
143 AsyncAppContext {
144 app: Arc::downgrade(&self.app),
145 executor: self.executor.clone(),
146 }
147 }
148}