i_slint_backend_testing/
lib.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#![doc = include_str!("README.md")]
5#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
6
7mod search_api;
8pub use search_api::*;
9#[cfg(feature = "internal")]
10mod internal_tests;
11#[cfg(feature = "internal")]
12pub use internal_tests::*;
13mod testing_backend;
14#[cfg(feature = "internal")]
15pub use testing_backend::*;
16#[cfg(all(feature = "ffi", not(test)))]
17mod ffi;
18#[cfg(feature = "system-testing")]
19pub mod systest;
20
21/// Initialize the testing backend without support for event loop.
22/// This means that each test thread can use its own backend, but global functions that needs
23/// an event loop such as `slint::invoke_from_event_loop` or `Timer`s won't work.
24/// Must be called before any call that would otherwise initialize the rendering backend.
25/// Calling it when the rendering backend is already initialized will panic.
26///
27/// Note that for animations and timers, the changes in the system time will be disregarded.
28/// Instead, use [`mock_elapsed_time()`] to advance the simulate (mock) time Slint uses.
29pub fn init_no_event_loop() {
30    i_slint_core::platform::set_platform(Box::new(testing_backend::TestingBackend::new(
31        testing_backend::TestingBackendOptions { mock_time: true, threading: false },
32    )))
33    .expect("platform already initialized");
34}
35
36/// Initialize the testing backend with support for simple event loop.
37/// This function can only be called once per process, so make sure to use integration
38/// tests with only one `#[test]` function. (Or in a doc test)
39/// Must be called before any call that would otherwise initialize the rendering backend.
40/// Calling it when the rendering backend is already initialized will panic.
41///
42/// Note that for animations and timers, the changes in the system time will be disregarded.
43/// Instead, use [`mock_elapsed_time()`] to advance the simulate (mock) time Slint uses.
44pub fn init_integration_test_with_mock_time() {
45    i_slint_core::platform::set_platform(Box::new(testing_backend::TestingBackend::new(
46        testing_backend::TestingBackendOptions { mock_time: true, threading: true },
47    )))
48    .expect("platform already initialized");
49}
50
51/// Initialize the testing backend with support for simple event loop.
52/// This function can only be called once per process, so make sure to use integration
53/// tests with only one `#[test]` function. (Or in a doc test)
54/// Must be called before any call that would otherwise initialize the rendering backend.
55/// Calling it when the rendering backend is already initialized will panic.
56pub fn init_integration_test_with_system_time() {
57    i_slint_core::platform::set_platform(Box::new(testing_backend::TestingBackend::new(
58        testing_backend::TestingBackendOptions { mock_time: false, threading: true },
59    )))
60    .expect("platform already initialized");
61}
62
63/// Advance the simulated mock time by the specified duration. Use in combination with
64/// [`init_integration_test_with_mock_time()`] or [`init_no_event_loop()`].
65#[cfg(not(feature = "internal"))]
66pub fn mock_elapsed_time(duration: std::time::Duration) {
67    i_slint_core::tests::slint_mock_elapsed_time(duration.as_millis() as _);
68}
69
70pub use i_slint_core::items::AccessibleRole;