1#![allow(deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/models"#]
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
5export global Glob {
6 in-out property <[string]> model: ["Hello", "World"];
7 in-out property <bool> condition: true;
8 callback clicked(string);
9 clicked => {
10 model = ["oops"];
11 }
12 in-out property <string> value;
13}
14
15component Button {
16 callback clicked();
17 in property <string> text;
18 Rectangle {
19 background: yellow;
20 Text { text: text; }
21 TouchArea {
22 clicked => {clicked();}
23 }
24 }
25}
26
27
28component TestCase inherits Window {
29 width: 300px;
30 height: 300px;
31 VerticalLayout {
32 for string in Glob.model: Rectangle {
33 if Glob.condition: Button {
34 text: string;
35 width: 80%;
36 height: 80%;
37 clicked => {
38 Glob.value = string;
39 Glob.clicked(string);
40 //Glob.value = string; // FIXME: this still crashes (#3464)
41 }
42 }
43 }
44 Rectangle {}
45 }
46}
47
48/*
49```rust
50let instance = TestCase::new().unwrap();
51let model = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from(
52 vec!["hello".into(), "world".into()]));
53instance.global::<Glob<'_>>().set_model(model.clone().into());
54instance.global::<Glob<'_>>().on_clicked(move |val|{
55 assert_eq!(val, "world");
56 model.remove(1);
57});
58slint_testing::send_mouse_click(&instance, 150., 150.);
59assert_eq!(instance.global::<Glob<'_>>().get_value(), "world");
60```
61
62```cpp
63auto handle = TestCase::create();
64TestCase &instance = *handle;
65
66std::vector<slint::SharedString> array;
67array.push_back("hello");
68array.push_back("world");
69auto model = std::make_shared<slint::VectorModel<slint::SharedString>>(std::move(array));
70instance.global<Glob>().set_model(model);
71instance.global<Glob>().on_clicked([=](slint::SharedString val){
72 assert_eq(val, "world");
73 model->erase(1);
74});
75slint_testing::send_mouse_click(&instance, 150., 150.);
76assert_eq(instance.global<Glob>().get_value(), "world");
77```
78
79
80```js
81var instance = new slint.TestCase({});
82let model = new slintlib.ArrayModel(["hello", "world"]);
83instance.Glob.model = model;
84instance.Glob.clicked = (val) => {
85 assert.equal(val, "world");
86 model.remove(1, 1);
87};
88slintlib.private_api.send_mouse_click(instance, 150., 150.);
89assert.equal(instance.Glob.value, "world");
90```
91
92
93*/
94}
95
96#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
97 use i_slint_backend_testing as slint_testing;
98 slint_testing::init();
99 let instance = TestCase::new().unwrap();
100 let model: Rc> = std::rc::Rc::new(slint::VecModel::<slint::SharedString>::from(
101 vec!["hello".into(), "world".into()]));
102 instance.global::<Glob<'_>>().set_model(model.clone().into());
103 instance.global::<Glob<'_>>().on_clicked(move |val|{
104 assert_eq!(val, "world");
105 model.remove(index:1);
106 });
107 slint_testing::send_mouse_click(&instance, x:150., y:150.);
108 assert_eq!(instance.global::<Glob<'_>>().get_value(), "world");
109 Ok(())
110}