| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | //! Functions useful for testing |
| 5 | #![warn (missing_docs)] |
| 6 | #![allow (unsafe_code)] |
| 7 | |
| 8 | use crate::api::LogicalPosition; |
| 9 | use crate::input::key_codes::Key; |
| 10 | use crate::platform::WindowEvent; |
| 11 | |
| 12 | /// Slint animations do not use real time, but use a mocked time. |
| 13 | /// Normally, the event loop update the time of the animation using |
| 14 | /// real time, but in tests, it is more convenient to use the fake time. |
| 15 | /// This function will add some milliseconds to the fake time |
| 16 | #[no_mangle ] |
| 17 | pub extern "C" fn slint_mock_elapsed_time(time_in_ms: u64) { |
| 18 | let tick: Instant = crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver: &AnimationDriver| { |
| 19 | let mut tick: Instant = driver.current_tick(); |
| 20 | tick += core::time::Duration::from_millis(time_in_ms); |
| 21 | driver.update_animations(tick); |
| 22 | tick |
| 23 | }); |
| 24 | crate::timers::TimerList::maybe_activate_timers(now:tick); |
| 25 | crate::properties::ChangeTracker::run_change_handlers(); |
| 26 | } |
| 27 | |
| 28 | /// Return the current mocked time. |
| 29 | #[no_mangle ] |
| 30 | pub extern "C" fn slint_get_mocked_time() -> u64 { |
| 31 | crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver: &AnimationDriver| driver.current_tick()).as_millis() |
| 32 | } |
| 33 | |
| 34 | /// Simulate a click on a position within the component. |
| 35 | #[no_mangle ] |
| 36 | pub extern "C" fn slint_send_mouse_click( |
| 37 | x: f32, |
| 38 | y: f32, |
| 39 | window_adapter: &crate::window::WindowAdapterRc, |
| 40 | ) { |
| 41 | let position: LogicalPosition = LogicalPosition::new(x, y); |
| 42 | let button: PointerEventButton = crate::items::PointerEventButton::Left; |
| 43 | |
| 44 | window_adapter.window().dispatch_event(WindowEvent::PointerMoved { position }); |
| 45 | window_adapter.window().dispatch_event(WindowEvent::PointerPressed { position, button }); |
| 46 | slint_mock_elapsed_time(time_in_ms:50); |
| 47 | window_adapter.window().dispatch_event(WindowEvent::PointerReleased { position, button }); |
| 48 | } |
| 49 | |
| 50 | /// Simulate a character input event (pressed or released). |
| 51 | #[no_mangle ] |
| 52 | pub extern "C" fn slint_send_keyboard_char( |
| 53 | string: &crate::SharedString, |
| 54 | pressed: bool, |
| 55 | window_adapter: &crate::window::WindowAdapterRc, |
| 56 | ) { |
| 57 | for ch: char in string.chars() { |
| 58 | window_adapter.window().dispatch_event(if pressed { |
| 59 | WindowEvent::KeyPressed { text: ch.into() } |
| 60 | } else { |
| 61 | WindowEvent::KeyReleased { text: ch.into() } |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /// Simulate a character input event. |
| 67 | #[no_mangle ] |
| 68 | pub extern "C" fn send_keyboard_string_sequence( |
| 69 | sequence: &crate::SharedString, |
| 70 | window_adapter: &crate::window::WindowAdapterRc, |
| 71 | ) { |
| 72 | for ch: char in sequence.chars() { |
| 73 | if ch.is_ascii_uppercase() { |
| 74 | window_adapter&Window |
| 75 | .window() |
| 76 | .dispatch_event(WindowEvent::KeyPressed { text: Key::Shift.into() }); |
| 77 | } |
| 78 | |
| 79 | let text: crate::SharedString = ch.into(); |
| 80 | window_adapter.window().dispatch_event(WindowEvent::KeyPressed { text: text.clone() }); |
| 81 | window_adapter.window().dispatch_event(WindowEvent::KeyReleased { text }); |
| 82 | |
| 83 | if ch.is_ascii_uppercase() { |
| 84 | window_adapter&Window |
| 85 | .window() |
| 86 | .dispatch_event(WindowEvent::KeyReleased { text: Key::Shift.into() }); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /// implementation details for debug_log() |
| 92 | #[doc (hidden)] |
| 93 | pub fn debug_log_impl(args: core::fmt::Arguments) { |
| 94 | crate::context::GLOBAL_CONTEXT.with(|p: &OnceCell| match p.get() { |
| 95 | Some(ctx: &SlintContext) => ctx.platform().debug_log(_arguments:args), |
| 96 | None => default_debug_log(_arguments:args), |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | #[doc (hidden)] |
| 101 | pub fn default_debug_log(_arguments: core::fmt::Arguments) { |
| 102 | cfg_if::cfg_if! { |
| 103 | if #[cfg(target_arch = "wasm32" )] { |
| 104 | use wasm_bindgen::prelude::*; |
| 105 | use std::string::ToString; |
| 106 | |
| 107 | #[wasm_bindgen] |
| 108 | extern "C" { |
| 109 | #[wasm_bindgen(js_namespace = console)] |
| 110 | pub fn log(s: &str); |
| 111 | } |
| 112 | |
| 113 | log(&_arguments.to_string()); |
| 114 | } else if #[cfg(feature = "std" )] { |
| 115 | std::eprintln!(" {_arguments}" ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[macro_export ] |
| 121 | /// This macro allows producing debug output that will appear on stderr in regular builds |
| 122 | /// and in the console log for wasm builds. |
| 123 | macro_rules! debug_log { |
| 124 | ($($t:tt)*) => ($crate::tests::debug_log_impl(format_args!($($t)*))) |
| 125 | } |
| 126 | |