1#![allow(deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases"#]
2// Copyright © SixtyFPS GmbH <info@slint.dev>
3// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial
4
5export component TestCase { }
6
7/*
8```cpp
9auto handle = TestCase::create();
10const TestCase &instance = *handle;
11
12// Check time mocking without event delivery happening:
13auto start_time = slint_testing::get_mocked_time();
14slint_testing::mock_elapsed_time(512);
15assert(slint_testing::get_mocked_time() == start_time + 512);
16slint_testing::mock_elapsed_time(10333);
17auto pre_event1_time = slint_testing::get_mocked_time();
18assert_eq(pre_event1_time, start_time + 512 + 10333);
19
20// Test that the first mouse event keeps time consistent
21slint_testing::send_mouse_click(&instance, 5., 5.);
22auto post_event1_time = slint_testing::get_mocked_time();
23assert(post_event1_time >= pre_event1_time && post_event1_time < pre_event1_time + 100); // allow for a bit of time for event handling...
24
25// Test that a second mouse event keeps time consistent
26slint_testing::send_mouse_click(&instance, 5., 5.);
27auto post_event2_time = slint_testing::get_mocked_time();
28assert(post_event2_time >= post_event1_time && post_event2_time < post_event1_time + 100);
29
30// Test that mocking works after mouse event delivery:
31slint_testing::mock_elapsed_time(500000);
32auto pre_event3_time = slint_testing::get_mocked_time();
33assert(pre_event3_time == post_event2_time + 500000);
34
35// Test that a new event does not break the mocked time again
36slint_testing::send_mouse_click(&instance, 5., 5.);
37auto post_event3_time = slint_testing::get_mocked_time();
38assert(post_event3_time >= pre_event3_time && post_event3_time <= pre_event3_time + 100);
39```
40
41```rust
42let instance = TestCase::new().unwrap();
43
44// Check time mocking without event delivery happening:
45let start_time = slint_testing::get_mocked_time();
46slint_testing::mock_elapsed_time(512);
47assert_eq!(slint_testing::get_mocked_time(), start_time + 512, "Mocked time is wrong");
48slint_testing::mock_elapsed_time(10333);
49let pre_event1_time = slint_testing::get_mocked_time();
50assert_eq!(pre_event1_time, start_time + 512 + 10333);
51
52// Test that the first mouse event keeps time consistent
53slint_testing::send_mouse_click(&instance, 5., 5.);
54let post_event1_time = slint_testing::get_mocked_time();
55assert!(post_event1_time >= pre_event1_time && post_event1_time < pre_event1_time + 100); // allow for a bit of time for event handling...
56
57// Test that a second mouse event keeps time consistent
58slint_testing::send_mouse_click(&instance, 5., 5.);
59let post_event2_time = slint_testing::get_mocked_time();
60assert!(post_event2_time >= post_event1_time && post_event2_time < post_event1_time + 100);
61
62// Test that mocking works after mouse event delivery:
63slint_testing::mock_elapsed_time(500000);
64let pre_event3_time = slint_testing::get_mocked_time();
65assert_eq!(pre_event3_time, post_event2_time + 500000);
66
67// Test that a new event does not break the mocked time again
68slint_testing::send_mouse_click(&instance, 5., 5.);
69let post_event3_time = slint_testing::get_mocked_time();
70assert!(post_event3_time >= pre_event3_time && post_event3_time <= pre_event3_time + 100);
71```
72
73```js
74var instance = new slint.TestCase({});
75
76let start_time = slintlib.private_api.get_mocked_time();
77slintlib.private_api.mock_elapsed_time(512);
78assert(slintlib.private_api.get_mocked_time() == start_time + 512);
79slintlib.private_api.mock_elapsed_time(122);
80assert(slintlib.private_api.get_mocked_time() == start_time + 512 + 122);
81slintlib.private_api.mock_elapsed_time(10333);
82assert(slintlib.private_api.get_mocked_time() == start_time + 512 + 122 + 10333);
83
84// FIXME: Sending a mouse event undoes the mock_elapsed_time calls on Node at this time!
85// slintlib.private_api.send_mouse_click(instance, 5., 5.);
86// assert(slintlib.private_api.get_mocked_time() == start_time + 512 + 122 + 10333);
87```
88
89*/
90}
91
92#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
93 use i_slint_backend_testing as slint_testing;
94 slint_testing::init();
95 let instance = TestCase::new().unwrap();
96
97 // Check time mocking without event delivery happening:
98 let start_time = slint_testing::get_mocked_time();
99 slint_testing::mock_elapsed_time(512);
100 assert_eq!(slint_testing::get_mocked_time(), start_time + 512, "Mocked time is wrong");
101 slint_testing::mock_elapsed_time(10333);
102 let pre_event1_time = slint_testing::get_mocked_time();
103 assert_eq!(pre_event1_time, start_time + 512 + 10333);
104
105 // Test that the first mouse event keeps time consistent
106 slint_testing::send_mouse_click(&instance, 5., 5.);
107 let post_event1_time = slint_testing::get_mocked_time();
108 assert!(post_event1_time >= pre_event1_time && post_event1_time < pre_event1_time + 100); // allow for a bit of time for event handling...
109
110 // Test that a second mouse event keeps time consistent
111 slint_testing::send_mouse_click(&instance, 5., 5.);
112 let post_event2_time = slint_testing::get_mocked_time();
113 assert!(post_event2_time >= post_event1_time && post_event2_time < post_event1_time + 100);
114
115 // Test that mocking works after mouse event delivery:
116 slint_testing::mock_elapsed_time(500000);
117 let pre_event3_time = slint_testing::get_mocked_time();
118 assert_eq!(pre_event3_time, post_event2_time + 500000);
119
120 // Test that a new event does not break the mocked time again
121 slint_testing::send_mouse_click(&instance, 5., 5.);
122 let post_event3_time = slint_testing::get_mocked_time();
123 assert!(post_event3_time >= pre_event3_time && post_event3_time <= pre_event3_time + 100);
124 Ok(())
125}