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.
35#[unsafe(no_mangle)]
36pub extern "C" fn slint_send_mouse_click(
37    x: f32,
38    y: f32,
39    window_adapter: &crate::window::WindowAdapterRc,
40) {
41    let position = LogicalPosition::new(x, y);
42    let button = 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(50);
47    window_adapter.window().dispatch_event(WindowEvent::PointerReleased { position, button });
48}
49
50/// Simulate a character input event (pressed or released).
51#[unsafe(no_mangle)]
52pub extern "C" fn slint_send_keyboard_char(
53    string: &crate::SharedString,
54    pressed: bool,
55    window_adapter: &crate::window::WindowAdapterRc,
56) {
57    for ch 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#[unsafe(no_mangle)]
68pub extern "C" fn send_keyboard_string_sequence(
69    sequence: &crate::SharedString,
70    window_adapter: &crate::window::WindowAdapterRc,
71) {
72    for ch in sequence.chars() {
73        if ch.is_ascii_uppercase() {
74            window_adapter
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
85                .window()
86                .dispatch_event(WindowEvent::KeyReleased { text: Key::Shift.into() });
87        }
88    }
89}
90
91/// implementation details for debug_log()
92#[doc(hidden)]
93pub fn debug_log_impl(args: core::fmt::Arguments) {
94    crate::context::GLOBAL_CONTEXT.with(|p| match p.get() {
95        Some(ctx) => ctx.platform().debug_log(args),
96        None => default_debug_log(args),
97    });
98}
99
100#[doc(hidden)]
101pub 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.
123macro_rules! debug_log {
124    ($($t:tt)*) => ($crate::tests::debug_log_impl(format_args!($($t)*)))
125}