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
5TestCase := 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
17auto handle = TestCase::create();
18const TestCase &instance = *handle;
19int callback_3_emitted = 0;
20std::string callback_3_string_value;
21int callback_3_int_value = 0;
22instance.on_test_callback3([&](int a, auto b) {
23 callback_3_int_value = a;
24 callback_3_string_value = b;
25 callback_3_emitted ++;
26});
27instance.set_callback_emission_count(0);
28assert_eq(instance.get_callback_emission_count(), 0);
29instance.invoke_test_callback(42);
30assert_eq(instance.get_callback_emission_count(), 1);
31assert_eq(callback_3_emitted, 0);
32instance.invoke_test_callback2("hello");
33assert_eq(instance.get_callback_emission_count(), 88);
34assert_eq(callback_3_emitted, 1);
35assert_eq(callback_3_int_value, 55);
36assert_eq(callback_3_string_value, "hello");
37```
38
39```rust
40let instance = TestCase::new().unwrap();
41let callback_3_emitted = std::rc::Rc::new(std::cell::RefCell::new((0, String::new())));
42instance.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});
46instance.set_callback_emission_count(0);
47assert_eq!(instance.get_callback_emission_count(), 0);
48instance.invoke_test_callback(42);
49assert_eq!(instance.get_callback_emission_count(), 1);
50assert_eq!(*callback_3_emitted.borrow(), (0, "".into()));
51instance.invoke_test_callback2("hello".into());
52assert_eq!(instance.get_callback_emission_count(), 88);
53assert_eq!(*callback_3_emitted.borrow(), (55, "hello".into()));
54```
55
56
57```js
58var callback_3_emitted = 0;
59var callback_3_string_value;
60var callback_3_int_value;
61var 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});
68instance.callback_emission_count = 0;
69assert.equal(instance.callback_emission_count, 0);
70instance.test_callback(42);
71assert.equal(instance.callback_emission_count, 1);
72instance.test_callback2("hello");
73assert.equal(instance.callback_emission_count, 88);
74assert.equal(callback_3_emitted, 1);
75assert.equal(callback_3_string_value, "hello");
76assert.equal(callback_3_int_value, 55);
77instance.callback_emission_count = 0;
78// Calling a callback with a wrong number of arg
79try {
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}
85assert.equal(instance.callback_emission_count, 0);
86
87
88/// also test assigning with a function
89instance.test_callback2 = function(a) {
90 callback_3_emitted += 100;
91 callback_3_string_value = a;
92};
93instance.test_callback2("salùt")
94assert.equal(callback_3_emitted, 101);
95assert.equal(callback_3_string_value, "salùt");
96assert.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}