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;
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
21auto handle = TestCase::create();
22const TestCase &instance = *handle;
23int callback_3_emitted = 0;
24instance.on_test_callback3([&]{ callback_3_emitted++; });
25instance.set_callback_emission_count(0);
26assert_eq(instance.get_callback_emission_count(), 0);
27instance.invoke_test_callback();
28assert_eq(instance.get_callback_emission_count(), 1);
29instance.invoke_test_callback();
30assert_eq(instance.get_callback_emission_count(), 2);
31assert_eq(callback_3_emitted, 0);
32instance.invoke_test_callback2();
33assert_eq(instance.get_callback_emission_count(), 88);
34assert_eq(callback_3_emitted, 1);
35```
36
37
38```rust
39let instance = TestCase::new().unwrap();
40let callback_3_emitted = std::rc::Rc::new(std::cell::Cell::new(0));
41// test that mutable variables can be captured
42let mut _mutable_var = String::from("1");
43instance.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});
50instance.set_callback_emission_count(0);
51assert_eq!(instance.get_callback_emission_count(), 0);
52instance.invoke_test_callback();
53assert_eq!(instance.get_callback_emission_count(), 1);
54instance.invoke_test_callback();
55assert_eq!(instance.get_callback_emission_count(), 2);
56assert_eq!(callback_3_emitted.get(), 0);
57instance.invoke_test_callback2();
58assert_eq!(instance.get_callback_emission_count(), 88);
59assert_eq!(callback_3_emitted.get(), 1);
60```
61
62```js
63var callback_3_emitted = 0;
64var instance = new slint.TestCase({
65 test_callback3: function() { callback_3_emitted++; }
66});
67instance.callback_emission_count = 0;
68assert.equal(instance.callback_emission_count, 0);
69instance.test_callback();
70assert.equal(instance.callback_emission_count, 1);
71let x = instance.test_callback;
72assert.equal(instance.callback_emission_count, 1);
73x()
74assert.equal(instance.callback_emission_count, 2);
75assert.equal(callback_3_emitted, 0);
76instance.test_callback2();
77assert.equal(instance.callback_emission_count, 88);
78assert.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}