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 pure callback test_func(int) -> int;
7 pure callback test_func2(string, int) -> string;
8 test_func2(str, val) => { str + "=" + (val + some_value) }
9
10 property <int> test_prop: 4 + test_func(2);
11 property <string> test_prop2: test_func2("hello", 42);
12 property <int> some_value: 8;
13
14 callback dummy() -> int;
15 dummy => { 4.4 }
16 callback returns_void;
17 returns_void => { test_func2("haha", 8) }
18
19 property <bool> test: test_prop == 4 && test_prop2 == "hello=50";
20}
21
22/*
23```rust
24let instance = TestCase::new().unwrap();
25instance.on_test_func({
26 let weak = instance.as_weak();
27 move |a| weak.upgrade().unwrap().get_some_value() * a
28});
29assert_eq!(instance.get_test_prop(), 4 + 16);
30assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=50"));
31instance.set_some_value(2);
32assert_eq!(instance.get_test_prop(), 4 + 4);
33assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=44"));
34
35assert_eq!(instance.invoke_test_func2("xxx".into(), 1), slint::SharedString::from("xxx=3"));
36```
37
38```cpp
39auto handle = TestCase::create();
40const TestCase &instance = *handle;
41instance.on_test_func([weak = slint::ComponentWeakHandle(handle)](int a) {
42 return (*weak.lock())->get_some_value() * a;
43});
44assert_eq(instance.get_test_prop(), 4 + 16);
45assert_eq(instance.get_test_prop2(), slint::SharedString("hello=50"));
46instance.set_some_value(2);
47assert_eq(instance.get_test_prop(), 4 + 4);
48assert_eq(instance.get_test_prop2(), slint::SharedString("hello=44"));
49
50assert_eq(instance.invoke_test_func2("xxx", 1), slint::SharedString("xxx=3"));
51```
52
53
54```js
55var instance = new slint.TestCase({
56 test_func: function(a) { return instance.some_value * a; }
57});
58assert.equal(instance.test_prop, 4 + 16);
59assert.equal(instance.test_prop2, "hello=50");
60instance.some_value = 2;
61assert.equal(instance.test_prop, 4 + 4);
62assert.equal(instance.test_prop2, "hello=44");
63
64assert.equal(instance.test_func2("xxx", 1), "xxx=3");
65```
66*/
67}
68
69#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
70 use i_slint_backend_testing as slint_testing;
71 slint_testing::init();
72 let instance = TestCase::new().unwrap();
73 instance.on_test_func({
74 let weak = instance.as_weak();
75 move |a| weak.upgrade().unwrap().get_some_value() * a
76 });
77 assert_eq!(instance.get_test_prop(), 4 + 16);
78 assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=50"));
79 instance.set_some_value(2);
80 assert_eq!(instance.get_test_prop(), 4 + 4);
81 assert_eq!(instance.get_test_prop2(), slint::SharedString::from("hello=44"));
82
83 assert_eq!(instance.invoke_test_func2("xxx".into(), 1), slint::SharedString::from("xxx=3"));
84 Ok(())
85}