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 | |
6 | TestCase := 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 |
47 | use slint::Model; |
48 | let instance = TestCase::new().unwrap(); |
49 | |
50 | slint_testing::send_mouse_click(&instance, 15., 5.); |
51 | assert_eq!(instance.get_clicked_score(), 792); |
52 | |
53 | assert_eq!(instance.get_model().row_data(1).unwrap().2, 792.); |
54 | instance.invoke_manual_score_update(1, 100); |
55 | assert_eq!(instance.get_model().row_data(1).unwrap().2, 892.); |
56 | |
57 | type ModelData = (slint::SharedString, slint::SharedString, f32); |
58 | let 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 | |
65 | instance.set_model(slint::ModelRc::from(another_model.clone())); |
66 | |
67 | slint_testing::send_mouse_click(&instance, 25., 5.); |
68 | assert_eq!(instance.get_clicked_score(), 336); |
69 | assert_eq!(another_model.row_data(2).unwrap().2, 336.); |
70 | |
71 | instance.invoke_manual_score_update(2, 100); |
72 | slint_testing::send_mouse_click(&instance, 25., 5.); |
73 | assert_eq!(instance.get_clicked_score(), 439); |
74 | assert_eq!(another_model.row_data(2).unwrap().2, 439.); |
75 | assert_eq!(another_model.row_data(2).unwrap().1, slint::SharedString::from("world")); |
76 | |
77 | instance.invoke_manual_score_update(200, 100); |
78 | assert_eq!(another_model.row_count(), 3); |
79 | |
80 | another_model.insert(1, ("new_a".into(), "inserted".into(), 444.)); |
81 | slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a2" |
82 | assert_eq!(instance.get_clicked_score(), 222 + 3); |
83 | slint_testing::send_mouse_click(&instance, 15., 5.); // click on "new_a" |
84 | assert_eq!(instance.get_clicked_score(), 444 + 3); |
85 | |
86 | another_model.remove(2); // remove "a2" |
87 | slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a3" |
88 | assert_eq!(instance.get_clicked_score(), 439 + 3); |
89 | |
90 | |
91 | ``` |
92 | |
93 | ```cpp |
94 | auto handle = TestCase::create(); |
95 | const TestCase &instance = *handle; |
96 | slint_testing::send_mouse_click(&instance, 15., 5.); |
97 | assert_eq(instance.get_clicked_score(), 792); |
98 | |
99 | assert_eq(std::get<2>(*instance.get_model()->row_data(1)), 792.); |
100 | instance.invoke_manual_score_update(1, 100); |
101 | assert_eq(std::get<2>(*instance.get_model()->row_data(1)), 892.); |
102 | |
103 | using ModelData = std::tuple<slint::SharedString, slint::SharedString, float>; |
104 | std::vector<ModelData> array; |
105 | array.push_back(ModelData{"a1", "hello", 111.}); |
106 | array.push_back(ModelData{"a2", "cruel", 222.}); |
107 | array.push_back(ModelData{"a3", "world", 333.}); |
108 | auto another_model = std::make_shared<slint::VectorModel<ModelData>>(std::move(array)); |
109 | instance.set_model(another_model); |
110 | |
111 | slint_testing::send_mouse_click(&instance, 25., 5.); |
112 | assert_eq(instance.get_clicked_score(), 336); |
113 | assert_eq(std::get<2>(*another_model->row_data(2)), 336.); |
114 | |
115 | instance.invoke_manual_score_update(2, 100); |
116 | slint_testing::send_mouse_click(&instance, 25., 5.); |
117 | assert_eq(instance.get_clicked_score(), 439); |
118 | assert_eq(std::get<2>(*another_model->row_data(2)), 439.); |
119 | assert_eq(std::get<1>(*another_model->row_data(2)), "world"); |
120 | |
121 | instance.invoke_manual_score_update(200, 100); |
122 | assert_eq(another_model->row_count(), 3); |
123 | |
124 | another_model->insert(1, ModelData{"new_a", "inserted", 444.}); |
125 | slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a2" |
126 | assert_eq(instance.get_clicked_score(), 222 + 3); |
127 | slint_testing::send_mouse_click(&instance, 15., 5.); // click on "new_a" |
128 | assert_eq(instance.get_clicked_score(), 444 + 3); |
129 | |
130 | another_model->erase(2); // remove "a2" |
131 | slint_testing::send_mouse_click(&instance, 25., 5.); // click on "a3" |
132 | assert_eq(instance.get_clicked_score(), 439 + 3); |
133 | |
134 | ``` |
135 | |
136 | |
137 | ```js |
138 | var instance = new slint.TestCase({}); |
139 | slintlib.private_api.send_mouse_click(instance, 15., 5.); |
140 | assert.equal(instance.clicked_score, 792); |
141 | |
142 | assert.equal(instance.model.rowData(1).score, 792.); |
143 | instance.manual_score_update(1, 100); |
144 | assert.equal(instance.model.rowData(1).score, 892.); |
145 | |
146 | let 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 | ]); |
151 | instance.model = another_model; |
152 | |
153 | slintlib.private_api.send_mouse_click(instance, 25., 5.); |
154 | assert.equal(instance.clicked_score, 336); |
155 | assert.equal(another_model.rowData(2).score, 336.); |
156 | |
157 | instance.manual_score_update(2, 100); |
158 | slintlib.private_api.send_mouse_click(instance, 25., 5.); |
159 | assert.equal(instance.clicked_score, 439); |
160 | assert.equal(another_model.rowData(2).score, 439.); |
161 | assert.equal(another_model.rowData(2).name, "world"); |
162 | |
163 | instance.manual_score_update(200, 100); // should do nothing |
164 | assert.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 | } |