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 single key event with the given text (pressed or released).
52///
53/// Unlike [`slint_send_keyboard_char`], this dispatches a single [`WindowEvent`]
54/// with the complete text. This is important for multi-codepoint grapheme clusters
55/// (e.g. NFD-encoded `é` = `e` + `\u{0301}`).
56#[unsafe(no_mangle)]
57pub extern "C" fn slint_send_keyboard_key_text(
58    text: &crate::SharedString,
59    pressed: bool,
60    window_adapter: &crate::window::WindowAdapterRc,
61) {
62    window_adapter.window().dispatch_event(if pressed {
63        WindowEvent::KeyPressed { text: text.clone() }
64    } else {
65        WindowEvent::KeyReleased { text: text.clone() }
66    })
67}
68
69/// Simulate a character input event (pressed or released).
70///
71/// Each character in the string is dispatched as a separate [`WindowEvent`].
72/// This is useful for modifier keys where each special character code
73/// represents an independent key press.
74#[unsafe(no_mangle)]
75pub extern "C" fn slint_send_keyboard_char(
76    string: &crate::SharedString,
77    pressed: bool,
78    window_adapter: &crate::window::WindowAdapterRc,
79) {
80    for ch in string.chars() {
81        slint_send_keyboard_key_text(&ch.into(), pressed, window_adapter);
82    }
83}
84
85/// Simulate a character input event.
86#[unsafe(no_mangle)]
87pub extern "C" fn send_keyboard_string_sequence(
88    sequence: &crate::SharedString,
89    window_adapter: &crate::window::WindowAdapterRc,
90) {
91    for ch in sequence.chars() {
92        if ch.is_ascii_uppercase() {
93            window_adapter
94                .window()
95                .dispatch_event(WindowEvent::KeyPressed { text: Key::Shift.into() });
96        }
97
98        let text: crate::SharedString = ch.into();
99        window_adapter.window().dispatch_event(WindowEvent::KeyPressed { text: text.clone() });
100        window_adapter.window().dispatch_event(WindowEvent::KeyReleased { text });
101
102        if ch.is_ascii_uppercase() {
103            window_adapter
104                .window()
105                .dispatch_event(WindowEvent::KeyReleased { text: Key::Shift.into() });
106        }
107    }
108}
109
110/// implementation details for debug_log()
111#[doc(hidden)]
112pub fn debug_log_impl(args: core::fmt::Arguments) {
113    crate::context::GLOBAL_CONTEXT.with(|p| match p.get() {
114        Some(ctx) => ctx.platform().debug_log(args),
115        None => default_debug_log(args),
116    });
117}
118
119#[doc(hidden)]
120pub fn default_debug_log(_arguments: core::fmt::Arguments) {
121    cfg_if::cfg_if! {
122        if #[cfg(target_arch = "wasm32")] {
123            use wasm_bindgen::prelude::*;
124            use std::string::ToString;
125
126            #[wasm_bindgen]
127            extern "C" {
128                #[wasm_bindgen(js_namespace = console)]
129                pub fn log(s: &str);
130            }
131
132            log(&_arguments.to_string());
133        } else if #[cfg(feature = "std")] {
134            use std::io::Write;
135            // We were seeing intermittent, albeit very rare, crashes due to `eprintln` panicking
136            // if the write to stderr fails. Since this is just for debug printing, it's safe
137            // to silently drop this if we can't write (since it wouldn't be written to stderr
138            // anyway)
139            let _ = writeln!(std::io::stderr(), "{_arguments}");
140        }
141    }
142}
143
144#[macro_export]
145/// This macro allows producing debug output that will appear on stderr in regular builds
146/// and in the console log for wasm builds.
147macro_rules! debug_log {
148    ($($t:tt)*) => ($crate::tests::debug_log_impl(format_args!($($t)*)))
149}