utils.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use dioxus::prelude::*;
  2. use dioxus_core::Element;
  3. pub fn check_app_exits(app: fn() -> Element) {
  4. use dioxus_desktop::tao::window::WindowBuilder;
  5. use dioxus_desktop::Config;
  6. // This is a deadman's switch to ensure that the app exits
  7. let should_panic = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(true));
  8. let should_panic_clone = should_panic.clone();
  9. std::thread::spawn(move || {
  10. std::thread::sleep(std::time::Duration::from_secs(60));
  11. if should_panic_clone.load(std::sync::atomic::Ordering::SeqCst) {
  12. eprintln!("App did not exit in time");
  13. std::process::exit(exitcode::SOFTWARE);
  14. }
  15. });
  16. LaunchBuilder::desktop()
  17. .with_cfg(Config::new().with_window(WindowBuilder::new().with_visible(true)))
  18. .launch(app);
  19. // Stop deadman's switch
  20. should_panic.store(false, std::sync::atomic::Ordering::SeqCst);
  21. }
  22. pub static EXPECTED_EVENTS: GlobalSignal<usize> = Signal::global(|| 0);
  23. pub fn mock_event(id: &'static str, value: &'static str) {
  24. mock_event_with_extra(id, value, "");
  25. }
  26. pub fn mock_event_with_extra(id: &'static str, value: &'static str, extra: &'static str) {
  27. use_hook(move || {
  28. EXPECTED_EVENTS.with_mut(|x| *x += 1);
  29. spawn(async move {
  30. tokio::time::sleep(std::time::Duration::from_millis(5000)).await;
  31. let js = format!(
  32. r#"
  33. let event = {value};
  34. let element = document.getElementById('{id}');
  35. {extra}
  36. element.dispatchEvent(event);
  37. "#
  38. );
  39. eval(&js).await.unwrap();
  40. });
  41. })
  42. }