mod.rs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Utilities for unit testing on Tauri applications.
  5. //!
  6. //! # Stability
  7. //!
  8. //! This module is unstable.
  9. //!
  10. //! # Examples
  11. //!
  12. //! ```rust
  13. //! #[tauri::command]
  14. //! fn my_cmd() {}
  15. //!
  16. //! fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
  17. //! builder
  18. //! .setup(|app| {
  19. //! // do something
  20. //! Ok(())
  21. //! })
  22. //! .invoke_handler(tauri::generate_handler![my_cmd])
  23. //! // remove the string argument on your app
  24. //! .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  25. //! .expect("failed to build app")
  26. //! }
  27. //!
  28. //! fn main() {
  29. //! let app = create_app(tauri::Builder::default());
  30. //! // app.run(|_handle, _event| {});
  31. //! }
  32. //!
  33. //! //#[cfg(test)]
  34. //! mod tests {
  35. //! use tauri::Manager;
  36. //! //#[cfg(test)]
  37. //! fn something() {
  38. //! let app = super::create_app(tauri::test::mock_builder());
  39. //! let window = app.get_window("main").unwrap();
  40. //! // do something with the app and window
  41. //! // in this case we'll run the my_cmd command with no arguments
  42. //! tauri::test::assert_ipc_response(
  43. //! &window,
  44. //! tauri::InvokePayload {
  45. //! cmd: "my_cmd".into(),
  46. //! callback: tauri::api::ipc::CallbackFn(0),
  47. //! error: tauri::api::ipc::CallbackFn(1),
  48. //! inner: serde_json::Value::Null,
  49. //! },
  50. //! Ok(())
  51. //! );
  52. //! }
  53. //! }
  54. //! ```
  55. #![allow(unused_variables)]
  56. mod mock_runtime;
  57. pub use mock_runtime::*;
  58. use serde::Serialize;
  59. use serde_json::Value as JsonValue;
  60. use std::{
  61. borrow::Cow,
  62. collections::HashMap,
  63. fmt::Debug,
  64. hash::{Hash, Hasher},
  65. sync::{
  66. mpsc::{channel, Sender},
  67. Arc, Mutex,
  68. },
  69. };
  70. use crate::hooks::window_invoke_responder;
  71. use crate::{api::ipc::CallbackFn, App, Builder, Context, InvokePayload, Manager, Pattern, Window};
  72. use tauri_utils::{
  73. assets::{AssetKey, Assets, CspHash},
  74. config::{Config, PatternKind, TauriConfig},
  75. };
  76. #[derive(Eq, PartialEq)]
  77. struct IpcKey {
  78. callback: CallbackFn,
  79. error: CallbackFn,
  80. }
  81. impl Hash for IpcKey {
  82. fn hash<H: Hasher>(&self, state: &mut H) {
  83. self.callback.0.hash(state);
  84. self.error.0.hash(state);
  85. }
  86. }
  87. struct Ipc(Mutex<HashMap<IpcKey, Sender<std::result::Result<JsonValue, JsonValue>>>>);
  88. /// An empty [`Assets`] implementation.
  89. pub struct NoopAsset {
  90. csp_hashes: Vec<CspHash<'static>>,
  91. }
  92. impl Assets for NoopAsset {
  93. fn get(&self, key: &AssetKey) -> Option<Cow<'_, [u8]>> {
  94. None
  95. }
  96. fn csp_hashes(&self, html_path: &AssetKey) -> Box<dyn Iterator<Item = CspHash<'_>> + '_> {
  97. Box::new(self.csp_hashes.iter().copied())
  98. }
  99. }
  100. /// Creates a new empty [`Assets`] implementation.
  101. pub fn noop_assets() -> NoopAsset {
  102. NoopAsset {
  103. csp_hashes: Default::default(),
  104. }
  105. }
  106. /// Creates a new [`crate::Context`] for testing.
  107. pub fn mock_context<A: Assets>(assets: A) -> crate::Context<A> {
  108. Context {
  109. config: Config {
  110. schema: None,
  111. package: Default::default(),
  112. tauri: TauriConfig {
  113. pattern: PatternKind::Brownfield,
  114. windows: Vec::new(),
  115. bundle: Default::default(),
  116. security: Default::default(),
  117. system_tray: None,
  118. macos_private_api: false,
  119. },
  120. build: Default::default(),
  121. plugins: Default::default(),
  122. namespaces: Default::default(),
  123. },
  124. assets: Arc::new(assets),
  125. default_window_icon: None,
  126. app_icon: None,
  127. #[cfg(desktop)]
  128. system_tray_icon: None,
  129. package_info: crate::PackageInfo {
  130. name: "test".into(),
  131. version: "0.1.0".parse().unwrap(),
  132. authors: "Tauri",
  133. description: "Tauri test",
  134. crate_name: "test",
  135. },
  136. _info_plist: (),
  137. pattern: Pattern::Brownfield(std::marker::PhantomData),
  138. runtime_authority: Default::default(),
  139. }
  140. }
  141. /// Creates a new [`Builder`] using the [`MockRuntime`].
  142. ///
  143. /// To use a dummy [`Context`], see [`mock_app`].
  144. ///
  145. /// # Examples
  146. ///
  147. /// ```rust
  148. /// #[cfg(test)]
  149. /// fn do_something() {
  150. /// let app = tauri::test::mock_builder()
  151. /// // remove the string argument to use your app's config file
  152. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  153. /// .unwrap();
  154. /// }
  155. /// ```
  156. pub fn mock_builder() -> Builder<MockRuntime> {
  157. let mut builder = Builder::<MockRuntime>::new().manage(Ipc(Default::default()));
  158. builder.invoke_responder = Arc::new(|window, response, callback, error| {
  159. let window_ = window.clone();
  160. let ipc = window_.state::<Ipc>();
  161. let mut ipc_ = ipc.0.lock().unwrap();
  162. if let Some(tx) = ipc_.remove(&IpcKey { callback, error }) {
  163. tx.send(response.into_result()).unwrap();
  164. } else {
  165. window_invoke_responder(window, response, callback, error)
  166. }
  167. });
  168. builder
  169. }
  170. /// Creates a new [`App`] for testing using the [`mock_context`] with a [`noop_assets`].
  171. pub fn mock_app() -> App<MockRuntime> {
  172. mock_builder().build(mock_context(noop_assets())).unwrap()
  173. }
  174. /// Executes the given IPC message and assert the response matches the expected value.
  175. ///
  176. /// # Examples
  177. ///
  178. /// ```rust
  179. /// #[tauri::command]
  180. /// fn ping() -> &'static str {
  181. /// "pong"
  182. /// }
  183. ///
  184. /// fn create_app<R: tauri::Runtime>(mut builder: tauri::Builder<R>) -> tauri::App<R> {
  185. /// builder
  186. /// .invoke_handler(tauri::generate_handler![ping])
  187. /// // remove the string argument on your app
  188. /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json"))
  189. /// .expect("failed to build app")
  190. /// }
  191. ///
  192. /// fn main() {
  193. /// let app = create_app(tauri::Builder::default());
  194. /// // app.run(|_handle, _event| {});}
  195. /// }
  196. ///
  197. /// //#[cfg(test)]
  198. /// mod tests {
  199. /// use tauri::Manager;
  200. ///
  201. /// //#[cfg(test)]
  202. /// fn something() {
  203. /// let app = super::create_app(tauri::test::mock_builder());
  204. /// let window = app.get_window("main").unwrap();
  205. ///
  206. /// // run the `ping` command and assert it returns `pong`
  207. /// tauri::test::assert_ipc_response(
  208. /// &window,
  209. /// tauri::InvokePayload {
  210. /// cmd: "ping".into(),
  211. /// callback: tauri::api::ipc::CallbackFn(0),
  212. /// error: tauri::api::ipc::CallbackFn(1),
  213. /// inner: serde_json::Value::Null,
  214. /// },
  215. /// // the expected response is a success with the "pong" payload
  216. /// // we could also use Err("error message") here to ensure the command failed
  217. /// Ok("pong")
  218. /// );
  219. /// }
  220. /// }
  221. /// ```
  222. pub fn assert_ipc_response<T: Serialize + Debug>(
  223. window: &Window<MockRuntime>,
  224. payload: InvokePayload,
  225. expected: Result<T, T>,
  226. ) {
  227. let callback = payload.callback;
  228. let error = payload.error;
  229. let ipc = window.state::<Ipc>();
  230. let (tx, rx) = channel();
  231. ipc.0.lock().unwrap().insert(IpcKey { callback, error }, tx);
  232. window.clone().on_message(payload).unwrap();
  233. assert_eq!(
  234. rx.recv().unwrap(),
  235. expected
  236. .map(|e| serde_json::to_value(e).unwrap())
  237. .map_err(|e| serde_json::to_value(e).unwrap())
  238. );
  239. }
  240. #[cfg(test)]
  241. mod tests {
  242. use crate::WindowBuilder;
  243. use std::time::Duration;
  244. use super::mock_app;
  245. #[test]
  246. fn run_app() {
  247. let app = mock_app();
  248. let w = WindowBuilder::new(&app, "main", Default::default())
  249. .build()
  250. .unwrap();
  251. std::thread::spawn(move || {
  252. std::thread::sleep(Duration::from_secs(1));
  253. w.close().unwrap();
  254. });
  255. app.run(|_app, event| {
  256. println!("{:?}", event);
  257. });
  258. }
  259. }