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
5
6TestCase := Rectangle {
7 width: 300phx;
8 height: 300phx;
9 property<[{name: string, account: string, score: float}]> model: [
10 {
11 name: "Olivier",
12 account: "ogoffart",
13 score: 456,
14 },
15 {
16 name: "Simon",
17 account: "tronical",
18 score: 789,
19 }
20 ];
21
22 property <int> clicked_score;
23
24 for person[i] in model: TouchArea {
25 x: i*10phx;
26 width: 10phx;
27 height: 10phx;
28 property <int> score: person.score;
29 clicked => {
30 person.score += 1;
31 person.score = person.score + 1;
32 person.score += 1;
33 clicked_score = score;
34 }
35 }
36
37 callback manual_score_update(int, int);
38 manual_score_update(i, val) => {
39 model[i].score += val;
40 }
41
42
43}
44
45/*
46```rust
47use slint::Model;
48let instance = TestCase::new().unwrap();
49
50slint_testing::send_mouse_click(&instance, 15., 5.);
51assert_eq!(instance.get_clicked_score(), 792);
52
53assert_eq!(instance.get_model().row_data(1).unwrap().2, 792.);
54instance.invoke_manual_score_update(1, 100);
55assert_eq!(instance.get_model().row_data(1).unwrap().2, 892.);
56
57type ModelData = (slint::SharedString, slint::SharedString, f32);
58let another_model = std::rc::Rc::new(slint::VecModel::<ModelData>::from(
59 vec![
60 ("a1".into(), "hello".into(), 111.),
61 ("a2".into(), "cruel".into(), 222.),
62 ("a3".into(), "world".into(), 333.),
63 ]));
64
65instance.set_model(slint::ModelRc::from(another_model.clone()));
66
67slint_testing::send_mouse_click(&instance, 25., 5.);
68assert_eq!(instance.get_clicked_score(), 336);
69assert_eq!(another_model.row_data(2).unwrap().2, 336.);
70
71instance.invoke_manual_score_update(2, 100);
72slint_testing::send_mouse_click(&instance, 25., 5.);
73assert_eq!(instance.get_clicked_score(), 439);
74assert_eq!(another_model.row_data(2).unwrap().2, 439.);
75assert_eq!(another_model.row_data(2).unwrap().1, slint::SharedString::from("world"));
76
77instance.invoke_manual_score_update(200, 100);
78assert_eq!(another_model.row_count(), 3);
79
80another_model.insert(1, ("new_a".into(), "inserted".into(), 444.));
81slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a2"
82assert_eq!(instance.get_clicked_score(), 222 + 3);
83slint_testing::send_mouse_click(&instance, 15., 5.); // click on "new_a"
84assert_eq!(instance.get_clicked_score(), 444 + 3);
85
86another_model.remove(2); // remove "a2"
87slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a3"
88assert_eq!(instance.get_clicked_score(), 439 + 3);
89
90
91```
92
93```cpp
94auto handle = TestCase::create();
95const TestCase &instance = *handle;
96slint_testing::send_mouse_click(&instance, 15., 5.);
97assert_eq(instance.get_clicked_score(), 792);
98
99assert_eq(std::get<2>(*instance.get_model()->row_data(1)), 792.);
100instance.invoke_manual_score_update(1, 100);
101assert_eq(std::get<2>(*instance.get_model()->row_data(1)), 892.);
102
103using ModelData = std::tuple<slint::SharedString, slint::SharedString, float>;
104std::vector<ModelData> array;
105array.push_back(ModelData{"a1", "hello", 111.});
106array.push_back(ModelData{"a2", "cruel", 222.});
107array.push_back(ModelData{"a3", "world", 333.});
108auto another_model = std::make_shared<slint::VectorModel<ModelData>>(std::move(array));
109instance.set_model(another_model);
110
111slint_testing::send_mouse_click(&instance, 25., 5.);
112assert_eq(instance.get_clicked_score(), 336);
113assert_eq(std::get<2>(*another_model->row_data(2)), 336.);
114
115instance.invoke_manual_score_update(2, 100);
116slint_testing::send_mouse_click(&instance, 25., 5.);
117assert_eq(instance.get_clicked_score(), 439);
118assert_eq(std::get<2>(*another_model->row_data(2)), 439.);
119assert_eq(std::get<1>(*another_model->row_data(2)), "world");
120
121instance.invoke_manual_score_update(200, 100);
122assert_eq(another_model->row_count(), 3);
123
124another_model->insert(1, ModelData{"new_a", "inserted", 444.});
125slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a2"
126assert_eq(instance.get_clicked_score(), 222 + 3);
127slint_testing::send_mouse_click(&instance, 15., 5.); // click on "new_a"
128assert_eq(instance.get_clicked_score(), 444 + 3);
129
130another_model->erase(2); // remove "a2"
131slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a3"
132assert_eq(instance.get_clicked_score(), 439 + 3);
133
134```
135
136
137```js
138var instance = new slint.TestCase({});
139slintlib.private_api.send_mouse_click(instance, 15., 5.);
140assert.equal(instance.clicked_score, 792);
141
142assert.equal(instance.model.rowData(1).score, 792.);
143instance.manual_score_update(1, 100);
144assert.equal(instance.model.rowData(1).score, 892.);
145
146let another_model = new slintlib.ArrayModel([
147 {account: "a1", name: "hello", score: 111.},
148 {account: "a2", name: "cruel", score: 222.},
149 {account: "a3", name: "world", score: 333.},
150]);
151instance.model = another_model;
152
153slintlib.private_api.send_mouse_click(instance, 25., 5.);
154assert.equal(instance.clicked_score, 336);
155assert.equal(another_model.rowData(2).score, 336.);
156
157instance.manual_score_update(2, 100);
158slintlib.private_api.send_mouse_click(instance, 25., 5.);
159assert.equal(instance.clicked_score, 439);
160assert.equal(another_model.rowData(2).score, 439.);
161assert.equal(another_model.rowData(2).name, "world");
162
163instance.manual_score_update(200, 100); // should do nothing
164assert.equal(another_model.length, 3);
165```
166
167
168*/
169}
170
171#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
172 use i_slint_backend_testing as slint_testing;
173 slint_testing::init();
174 use slint::Model;
175 let instance = TestCase::new().unwrap();
176
177 slint_testing::send_mouse_click(&instance, 15., 5.);
178 assert_eq!(instance.get_clicked_score(), 792);
179
180 assert_eq!(instance.get_model().row_data(1).unwrap().2, 792.);
181 instance.invoke_manual_score_update(1, 100);
182 assert_eq!(instance.get_model().row_data(1).unwrap().2, 892.);
183
184 type ModelData = (slint::SharedString, slint::SharedString, f32);
185 let another_model = std::rc::Rc::new(slint::VecModel::<ModelData>::from(
186 vec![
187 ("a1".into(), "hello".into(), 111.),
188 ("a2".into(), "cruel".into(), 222.),
189 ("a3".into(), "world".into(), 333.),
190 ]));
191
192 instance.set_model(slint::ModelRc::from(another_model.clone()));
193
194 slint_testing::send_mouse_click(&instance, 25., 5.);
195 assert_eq!(instance.get_clicked_score(), 336);
196 assert_eq!(another_model.row_data(2).unwrap().2, 336.);
197
198 instance.invoke_manual_score_update(2, 100);
199 slint_testing::send_mouse_click(&instance, 25., 5.);
200 assert_eq!(instance.get_clicked_score(), 439);
201 assert_eq!(another_model.row_data(2).unwrap().2, 439.);
202 assert_eq!(another_model.row_data(2).unwrap().1, slint::SharedString::from("world"));
203
204 instance.invoke_manual_score_update(200, 100);
205 assert_eq!(another_model.row_count(), 3);
206
207 another_model.insert(1, ("new_a".into(), "inserted".into(), 444.));
208 slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a2"
209 assert_eq!(instance.get_clicked_score(), 222 + 3);
210 slint_testing::send_mouse_click(&instance, 15., 5.); // click on "new_a"
211 assert_eq!(instance.get_clicked_score(), 444 + 3);
212
213 another_model.remove(2); // remove "a2"
214 slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a3"
215 assert_eq!(instance.get_clicked_score(), 439 + 3);
216
217
218 Ok(())
219}