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(int); |
7 | callback test_callback2(string); |
8 | callback test_callback3(int, string); |
9 | property<int> callback_emission_count; |
10 | test_callback => { callback_emission_count += 1; } |
11 | test_callback2(xx) => { callback_emission_count = 88; root.test_callback3(55, xx); } |
12 | } |
13 | |
14 | /* |
15 | |
16 | ```cpp |
17 | auto handle = TestCase::create(); |
18 | const TestCase &instance = *handle; |
19 | int callback_3_emitted = 0; |
20 | std::string callback_3_string_value; |
21 | int callback_3_int_value = 0; |
22 | instance.on_test_callback3([&](int a, auto b) { |
23 | callback_3_int_value = a; |
24 | callback_3_string_value = b; |
25 | callback_3_emitted ++; |
26 | }); |
27 | instance.set_callback_emission_count(0); |
28 | assert_eq(instance.get_callback_emission_count(), 0); |
29 | instance.invoke_test_callback(42); |
30 | assert_eq(instance.get_callback_emission_count(), 1); |
31 | assert_eq(callback_3_emitted, 0); |
32 | instance.invoke_test_callback2("hello"); |
33 | assert_eq(instance.get_callback_emission_count(), 88); |
34 | assert_eq(callback_3_emitted, 1); |
35 | assert_eq(callback_3_int_value, 55); |
36 | assert_eq(callback_3_string_value, "hello"); |
37 | ``` |
38 | |
39 | ```rust |
40 | let instance = TestCase::new().unwrap(); |
41 | let callback_3_emitted = std::rc::Rc::new(std::cell::RefCell::new((0, String::new()))); |
42 | instance.on_test_callback3({ |
43 | let callback_3_emitted = callback_3_emitted.clone(); |
44 | move |a, b| *callback_3_emitted.borrow_mut() = (a, b.into()) |
45 | }); |
46 | instance.set_callback_emission_count(0); |
47 | assert_eq!(instance.get_callback_emission_count(), 0); |
48 | instance.invoke_test_callback(42); |
49 | assert_eq!(instance.get_callback_emission_count(), 1); |
50 | assert_eq!(*callback_3_emitted.borrow(), (0, "".into())); |
51 | instance.invoke_test_callback2("hello".into()); |
52 | assert_eq!(instance.get_callback_emission_count(), 88); |
53 | assert_eq!(*callback_3_emitted.borrow(), (55, "hello".into())); |
54 | ``` |
55 | |
56 | |
57 | ```js |
58 | var callback_3_emitted = 0; |
59 | var callback_3_string_value; |
60 | var callback_3_int_value; |
61 | var instance = new slint.TestCase({ |
62 | test_callback3: function(a, b) { |
63 | callback_3_emitted++; |
64 | callback_3_string_value = b; |
65 | callback_3_int_value = a; |
66 | } |
67 | }); |
68 | instance.callback_emission_count = 0; |
69 | assert.equal(instance.callback_emission_count, 0); |
70 | instance.test_callback(42); |
71 | assert.equal(instance.callback_emission_count, 1); |
72 | instance.test_callback2("hello"); |
73 | assert.equal(instance.callback_emission_count, 88); |
74 | assert.equal(callback_3_emitted, 1); |
75 | assert.equal(callback_3_string_value, "hello"); |
76 | assert.equal(callback_3_int_value, 55); |
77 | instance.callback_emission_count = 0; |
78 | // Calling a callback with a wrong number of arg |
79 | try { |
80 | instance.test_callback(); |
81 | assert(false); |
82 | } catch(e) { |
83 | assert.equal(e.toString(), "Error: test_callback expect 1 arguments, but 0 where provided"); |
84 | } |
85 | assert.equal(instance.callback_emission_count, 0); |
86 | |
87 | |
88 | /// also test assigning with a function |
89 | instance.test_callback2 = function(a) { |
90 | callback_3_emitted += 100; |
91 | callback_3_string_value = a; |
92 | }; |
93 | instance.test_callback2("salùt") |
94 | assert.equal(callback_3_emitted, 101); |
95 | assert.equal(callback_3_string_value, "salùt"); |
96 | assert.equal(callback_3_int_value, 55); // same as before |
97 | |
98 | ``` |
99 | |
100 | |
101 | |
102 | */ |
103 | } |
104 | |
105 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
106 | use i_slint_backend_testing as slint_testing; |
107 | slint_testing::init(); |
108 | let instance = TestCase::new().unwrap(); |
109 | let callback_3_emitted: Rc> = std::rc::Rc::new(std::cell::RefCell::new((0, String::new()))); |
110 | instance.on_test_callback3({ |
111 | let callback_3_emitted: Rc> = callback_3_emitted.clone(); |
112 | move |a: i32, b| *callback_3_emitted.borrow_mut() = (a, b.into()) |
113 | }); |
114 | instance.set_callback_emission_count(0); |
115 | assert_eq!(instance.get_callback_emission_count(), 0); |
116 | instance.invoke_test_callback(42); |
117 | assert_eq!(instance.get_callback_emission_count(), 1); |
118 | assert_eq!(*callback_3_emitted.borrow(), (0, "" .into())); |
119 | instance.invoke_test_callback2("hello" .into()); |
120 | assert_eq!(instance.get_callback_emission_count(), 88); |
121 | assert_eq!(*callback_3_emitted.borrow(), (55, "hello" .into())); |
122 | Ok(()) |
123 | } |