Skip to main content

i_slint_core/
tests.rs

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
8use crate::api::LogicalPosition;
9use crate::input::key_codes::Key;
10use 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#[unsafe(no_mangle)]
17pub extern "C" fn slint_mock_elapsed_time(time_in_ms: u64) {
18    let tick = crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver| {
19        let mut tick = 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(tick);
25    crate::properties::ChangeTracker::run_change_handlers();
26}
27
28/// Return the current mocked time.
29#[unsafe(no_mangle)]
30pub extern "C" fn slint_get_mocked_time() -> u64 {
31    crate::animations::CURRENT_ANIMATION_DRIVER.with(|driver| driver.current_tick()).as_millis()
32}
33
34/// Simulate a click on a position within the component and releasing after some time.
35/// The time until the release is hardcoded to 50ms
36#[unsafe(no_mangle)]
37pub extern "C" fn slint_send_mouse_click(
38    x: f32,
39    y: f32,
40    window_adapter: &crate::window::WindowAdapterRc,
41) {
42    let position = LogicalPosition::new(x, y);
43    let button = crate::items::PointerEventButton::Left;
44
45    window_adapter.window().dispatch_event(WindowEvent::PointerMoved { position });
46    window_adapter.window().dispatch_event(WindowEvent::PointerPressed { position, button });
47    slint_mock_elapsed_time(50);
48    window_adapter.window().dispatch_event(WindowEvent::PointerReleased { position, button });
49}
50
51/// Simulate a character input event (pressed or released).
52#[unsafe(no_mangle)]
53pub extern "C" fn slint_send_keyboard_char(
54    string: &crate::SharedString,
55    pressed: bool,
56    window_adapter: &crate::window::WindowAdapterRc,
57) {
58    for ch in string.chars() {
59        window_adapter.window().dispatch_event(if pressed {
60            WindowEvent::KeyPressed { text: ch.into() }
61        } else {
62            WindowEvent::KeyReleased { text: ch.into() }
63        })
64    }
65}
66
67/// Simulate a character input event.
68#[unsafe(no_mangle)]
69pub extern "C" fn send_keyboard_string_sequence(
70    sequence: &crate::SharedString,
71    window_adapter: &crate::window::WindowAdapterRc,
72) {
73    for ch in sequence.chars() {
74        if ch.is_ascii_uppercase() {
75            window_adapter
76                .window()
77                .dispatch_event(WindowEvent::KeyPressed { text: Key::Shift.into() });
78        }
79
80        let text: crate::SharedString = ch.into();
81        window_adapter.window().dispatch_event(WindowEvent::KeyPressed { text: text.clone() });
82        window_adapter.window().dispatch_event(WindowEvent::KeyReleased { text });
83
84        if ch.is_ascii_uppercase() {
85            window_adapter
86                .window()
87                .dispatch_event(WindowEvent::KeyReleased { text: Key::Shift.into() });
88        }
89    }
90}
91
92/// implementation details for debug_log()
93#[doc(hidden)]
94pub fn debug_log_impl(args: core::fmt::Arguments) {
95    crate::context::GLOBAL_CONTEXT.with(|p| match p.get() {
96        Some(ctx) => ctx.platform().debug_log(args),
97        None => default_debug_log(args),
98    });
99}
100
101#[doc(hidden)]
102pub fn default_debug_log(_arguments: core::fmt::Arguments) {
103    cfg_if::cfg_if! {
104        if #[cfg(target_arch = "wasm32")] {
105            use wasm_bindgen::prelude::*;
106            use std::string::ToString;
107
108            #[wasm_bindgen]
109            extern "C" {
110                #[wasm_bindgen(js_namespace = console)]
111                pub fn log(s: &str);
112            }
113
114            log(&_arguments.to_string());
115        } else if #[cfg(feature = "std")] {
116            std::eprintln!("{_arguments}");
117        }
118    }
119}
120
121#[macro_export]
122/// This macro allows producing debug output that will appear on stderr in regular builds
123/// and in the console log for wasm builds.
124macro_rules! debug_log {
125    ($($t:tt)*) => ($crate::tests::debug_log_impl(format_args!($($t)*)))
126}