1 | #![allow (deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/callbacks"# ] |
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 | |
5 | TestCase := Rectangle { |
6 | callback test_callback; |
7 | callback test_callback2; |
8 | callback test_callback3; |
9 | property<int> callback_emission_count; |
10 | test_callback => { |
11 | callback_emission_count += 1; |
12 | // This returns something despite the callback isn't supposed to return anything. |
13 | // Maybe one should produce a warning in the future, but at least we shouldn't |
14 | // result in compilation error in the generated code |
15 | callback_emission_count |
16 | } |
17 | test_callback2 => { callback_emission_count = 88; root.test_callback3(); } |
18 | } |
19 | /* |
20 | ```cpp |
21 | auto handle = TestCase::create(); |
22 | const TestCase &instance = *handle; |
23 | int callback_3_emitted = 0; |
24 | instance.on_test_callback3([&]{ callback_3_emitted++; }); |
25 | instance.set_callback_emission_count(0); |
26 | assert_eq(instance.get_callback_emission_count(), 0); |
27 | instance.invoke_test_callback(); |
28 | assert_eq(instance.get_callback_emission_count(), 1); |
29 | instance.invoke_test_callback(); |
30 | assert_eq(instance.get_callback_emission_count(), 2); |
31 | assert_eq(callback_3_emitted, 0); |
32 | instance.invoke_test_callback2(); |
33 | assert_eq(instance.get_callback_emission_count(), 88); |
34 | assert_eq(callback_3_emitted, 1); |
35 | ``` |
36 | |
37 | |
38 | ```rust |
39 | let instance = TestCase::new().unwrap(); |
40 | let callback_3_emitted = std::rc::Rc::new(std::cell::Cell::new(0)); |
41 | // test that mutable variables can be captured |
42 | let mut _mutable_var = String::from("1"); |
43 | instance.on_test_callback3({ |
44 | let callback_3_emitted = callback_3_emitted.clone(); |
45 | move || { |
46 | callback_3_emitted.set(callback_3_emitted.get() + 1); |
47 | _mutable_var.push_str(&callback_3_emitted.get().to_string()); |
48 | } |
49 | }); |
50 | instance.set_callback_emission_count(0); |
51 | assert_eq!(instance.get_callback_emission_count(), 0); |
52 | instance.invoke_test_callback(); |
53 | assert_eq!(instance.get_callback_emission_count(), 1); |
54 | instance.invoke_test_callback(); |
55 | assert_eq!(instance.get_callback_emission_count(), 2); |
56 | assert_eq!(callback_3_emitted.get(), 0); |
57 | instance.invoke_test_callback2(); |
58 | assert_eq!(instance.get_callback_emission_count(), 88); |
59 | assert_eq!(callback_3_emitted.get(), 1); |
60 | ``` |
61 | |
62 | ```js |
63 | var callback_3_emitted = 0; |
64 | var instance = new slint.TestCase({ |
65 | test_callback3: function() { callback_3_emitted++; } |
66 | }); |
67 | instance.callback_emission_count = 0; |
68 | assert.equal(instance.callback_emission_count, 0); |
69 | instance.test_callback(); |
70 | assert.equal(instance.callback_emission_count, 1); |
71 | let x = instance.test_callback; |
72 | assert.equal(instance.callback_emission_count, 1); |
73 | x() |
74 | assert.equal(instance.callback_emission_count, 2); |
75 | assert.equal(callback_3_emitted, 0); |
76 | instance.test_callback2(); |
77 | assert.equal(instance.callback_emission_count, 88); |
78 | assert.equal(callback_3_emitted, 1); |
79 | ``` |
80 | */ |
81 | } |
82 | |
83 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
84 | use i_slint_backend_testing as slint_testing; |
85 | slint_testing::init(); |
86 | let instance = TestCase::new().unwrap(); |
87 | let callback_3_emitted = std::rc::Rc::new(std::cell::Cell::new(0)); |
88 | // test that mutable variables can be captured |
89 | let mut _mutable_var = String::from("1" ); |
90 | instance.on_test_callback3({ |
91 | let callback_3_emitted = callback_3_emitted.clone(); |
92 | move || { |
93 | callback_3_emitted.set(callback_3_emitted.get() + 1); |
94 | _mutable_var.push_str(&callback_3_emitted.get().to_string()); |
95 | } |
96 | }); |
97 | instance.set_callback_emission_count(0); |
98 | assert_eq!(instance.get_callback_emission_count(), 0); |
99 | instance.invoke_test_callback(); |
100 | assert_eq!(instance.get_callback_emission_count(), 1); |
101 | instance.invoke_test_callback(); |
102 | assert_eq!(instance.get_callback_emission_count(), 2); |
103 | assert_eq!(callback_3_emitted.get(), 0); |
104 | instance.invoke_test_callback2(); |
105 | assert_eq!(instance.get_callback_emission_count(), 88); |
106 | assert_eq!(callback_3_emitted.get(), 1); |
107 | Ok(()) |
108 | } |