1 | #![allow (deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/elements"# ] |
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 | // FIXME: Skip embedding test on C++ and NodeJS since ComponentFactory is not |
6 | // implemented there! |
7 | //ignore: cpp,js |
8 | |
9 | import { Button } from "std-widgets.slint" ; |
10 | |
11 | export component TestCase inherits Rectangle { |
12 | in property <component-factory> cc <=> cc.component-factory; |
13 | out property <length> container-x <=> cc.x; |
14 | out property <length> container-y <=> cc.y; |
15 | in-out property <length> container-width <=> cc.width; |
16 | in-out property <length> container-height <=> cc.height; |
17 | |
18 | width: 500px; |
19 | height: 500px; |
20 | |
21 | cc := ComponentContainer { |
22 | x: 15px; |
23 | y: 20px; |
24 | } |
25 | |
26 | } |
27 | |
28 | /* |
29 | ```cpp |
30 | // ComponentFactory not supported yet! |
31 | ``` |
32 | |
33 | ```rust |
34 | use std::cell::RefCell; |
35 | use std::rc::Rc; |
36 | |
37 | use slint_interpreter::ComponentInstance; |
38 | |
39 | let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default(); |
40 | |
41 | let ei = embedded_instance.clone(); |
42 | |
43 | let dyn_factory = slint::ComponentFactory::new(move |ctx| { |
44 | let mut compiler = slint_interpreter::ComponentCompiler::new(); |
45 | let e = spin_on::spin_on(compiler.build_from_source( |
46 | r#"export component E1 inherits Rectangle { |
47 | out property <length> embedded-width <=> self.width; |
48 | out property <length> embedded-height <=> self.height; |
49 | out property <length> embedded-x <=> self.x; |
50 | out property <length> embedded-y <=> self.y; |
51 | |
52 | min-width: 10px; |
53 | preferred-width: 50px; |
54 | max-width: 100px; |
55 | min-height: 10px; |
56 | preferred-height: 50px; |
57 | max-height: 100px; |
58 | |
59 | }"#.into(), |
60 | std::path::PathBuf::from("embedded.slint"), |
61 | )).unwrap(); |
62 | let instance = e.create_embedded(ctx).ok(); |
63 | |
64 | ei.replace(instance.as_ref().map(|i| i.clone_strong())); |
65 | |
66 | instance |
67 | }); |
68 | |
69 | let instance = TestCase::new().unwrap(); |
70 | |
71 | let ei = embedded_instance.take(); |
72 | |
73 | assert_eq!(instance.get_container_x(), 15.0); |
74 | assert_eq!(instance.get_container_y(), 20.0); |
75 | assert_eq!(instance.get_container_width(), 0.0); |
76 | assert_eq!(instance.get_container_height(), 0.0); |
77 | assert!(ei.is_none()); |
78 | drop(ei); |
79 | |
80 | instance.set_cc(dyn_factory.clone()); |
81 | |
82 | assert_eq!(instance.get_container_x(), 15.0); |
83 | assert_eq!(instance.get_container_y(), 20.0); |
84 | assert_eq!(instance.get_container_width(), 50.0); |
85 | assert_eq!(instance.get_container_height(), 50.0); |
86 | |
87 | let ei = embedded_instance.take().unwrap(); |
88 | assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into()); |
89 | assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into()); |
90 | assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0)); |
91 | assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0)); |
92 | |
93 | // Set a valid size: |
94 | instance.set_container_width(20.0); |
95 | instance.set_container_height(70.0); |
96 | |
97 | assert_eq!(instance.get_container_x(), 15.0); |
98 | assert_eq!(instance.get_container_y(), 20.0); |
99 | assert_eq!(instance.get_container_width(), 20.0); |
100 | assert_eq!(instance.get_container_height(), 70.0); |
101 | |
102 | assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into()); |
103 | assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into()); |
104 | assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0)); |
105 | assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0)); |
106 | |
107 | // Set an invalid size: |
108 | instance.set_container_width(5.0); |
109 | instance.set_container_height(700.0); |
110 | |
111 | assert_eq!(instance.get_container_x(), 15.0); |
112 | assert_eq!(instance.get_container_y(), 20.0); |
113 | assert_eq!(instance.get_container_width(), 5.0); |
114 | assert_eq!(instance.get_container_height(), 700.0); |
115 | |
116 | assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into()); |
117 | assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into()); |
118 | assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0)); |
119 | assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0)); |
120 | ``` |
121 | |
122 | // Now with fixed size |
123 | ```rust |
124 | use std::cell::RefCell; |
125 | use std::rc::Rc; |
126 | |
127 | use slint_interpreter::ComponentInstance; |
128 | |
129 | let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default(); |
130 | |
131 | let ei = embedded_instance.clone(); |
132 | |
133 | let fixed_factory = slint::ComponentFactory::new(move |ctx| { |
134 | let mut compiler = slint_interpreter::ComponentCompiler::new(); |
135 | let e = spin_on::spin_on(compiler.build_from_source( |
136 | r#"export component E1 inherits Rectangle { |
137 | out property <length> embedded-width <=> self.width; |
138 | out property <length> embedded-height <=> self.height; |
139 | out property <length> embedded-x <=> self.x; |
140 | out property <length> embedded-y <=> self.y; |
141 | |
142 | width: 100px; |
143 | height: 100px; |
144 | }"#.into(), |
145 | std::path::PathBuf::from("embedded.slint"), |
146 | )).unwrap(); |
147 | let instance = e.create_embedded(ctx).ok(); |
148 | |
149 | ei.replace(instance.as_ref().map(|i| i.clone_strong())); |
150 | |
151 | instance |
152 | }); |
153 | |
154 | let instance = TestCase::new().unwrap(); |
155 | |
156 | assert_eq!(instance.get_container_x(), 15.0); |
157 | assert_eq!(instance.get_container_y(), 20.0); |
158 | assert_eq!(instance.get_container_width(), 0.0); |
159 | assert_eq!(instance.get_container_height(), 0.0); |
160 | assert!(embedded_instance.take().is_none()); |
161 | |
162 | instance.set_cc(fixed_factory.clone()); |
163 | |
164 | assert_eq!(instance.get_container_x(), 15.0); |
165 | assert_eq!(instance.get_container_y(), 20.0); |
166 | assert_eq!(instance.get_container_width(), 100.0); |
167 | assert_eq!(instance.get_container_height(), 100.0); |
168 | |
169 | let ei = embedded_instance.take().unwrap(); |
170 | assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into()); |
171 | assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into()); |
172 | assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0)); |
173 | assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0)); |
174 | ``` |
175 | |
176 | |
177 | |
178 | ```js |
179 | // Not supported yet |
180 | ``` |
181 | */ |
182 | } |
183 | |
184 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
185 | use i_slint_backend_testing as slint_testing; |
186 | slint_testing::init(); |
187 | use std::cell::RefCell; |
188 | use std::rc::Rc; |
189 | |
190 | use slint_interpreter::ComponentInstance; |
191 | |
192 | let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default(); |
193 | |
194 | let ei = embedded_instance.clone(); |
195 | |
196 | let dyn_factory = slint::ComponentFactory::new(move |ctx| { |
197 | let mut compiler = slint_interpreter::ComponentCompiler::new(); |
198 | let e = spin_on::spin_on(compiler.build_from_source( |
199 | r#"export component E1 inherits Rectangle { |
200 | out property <length> embedded-width <=> self.width; |
201 | out property <length> embedded-height <=> self.height; |
202 | out property <length> embedded-x <=> self.x; |
203 | out property <length> embedded-y <=> self.y; |
204 | |
205 | min-width: 10px; |
206 | preferred-width: 50px; |
207 | max-width: 100px; |
208 | min-height: 10px; |
209 | preferred-height: 50px; |
210 | max-height: 100px; |
211 | |
212 | }"# .into(), |
213 | std::path::PathBuf::from("embedded.slint" ), |
214 | )).unwrap(); |
215 | let instance = e.create_embedded(ctx).ok(); |
216 | |
217 | ei.replace(instance.as_ref().map(|i| i.clone_strong())); |
218 | |
219 | instance |
220 | }); |
221 | |
222 | let instance = TestCase::new().unwrap(); |
223 | |
224 | let ei = embedded_instance.take(); |
225 | |
226 | assert_eq!(instance.get_container_x(), 15.0); |
227 | assert_eq!(instance.get_container_y(), 20.0); |
228 | assert_eq!(instance.get_container_width(), 0.0); |
229 | assert_eq!(instance.get_container_height(), 0.0); |
230 | assert!(ei.is_none()); |
231 | drop(ei); |
232 | |
233 | instance.set_cc(dyn_factory.clone()); |
234 | |
235 | assert_eq!(instance.get_container_x(), 15.0); |
236 | assert_eq!(instance.get_container_y(), 20.0); |
237 | assert_eq!(instance.get_container_width(), 50.0); |
238 | assert_eq!(instance.get_container_height(), 50.0); |
239 | |
240 | let ei = embedded_instance.take().unwrap(); |
241 | assert_eq!(ei.get_property("embedded-width" ).unwrap(), instance.get_container_width().into()); |
242 | assert_eq!(ei.get_property("embedded-height" ).unwrap(), instance.get_container_height().into()); |
243 | assert_eq!(ei.get_property("embedded-x" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
244 | assert_eq!(ei.get_property("embedded-y" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
245 | |
246 | // Set a valid size: |
247 | instance.set_container_width(20.0); |
248 | instance.set_container_height(70.0); |
249 | |
250 | assert_eq!(instance.get_container_x(), 15.0); |
251 | assert_eq!(instance.get_container_y(), 20.0); |
252 | assert_eq!(instance.get_container_width(), 20.0); |
253 | assert_eq!(instance.get_container_height(), 70.0); |
254 | |
255 | assert_eq!(ei.get_property("embedded-width" ).unwrap(), instance.get_container_width().into()); |
256 | assert_eq!(ei.get_property("embedded-height" ).unwrap(), instance.get_container_height().into()); |
257 | assert_eq!(ei.get_property("embedded-x" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
258 | assert_eq!(ei.get_property("embedded-y" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
259 | |
260 | // Set an invalid size: |
261 | instance.set_container_width(5.0); |
262 | instance.set_container_height(700.0); |
263 | |
264 | assert_eq!(instance.get_container_x(), 15.0); |
265 | assert_eq!(instance.get_container_y(), 20.0); |
266 | assert_eq!(instance.get_container_width(), 5.0); |
267 | assert_eq!(instance.get_container_height(), 700.0); |
268 | |
269 | assert_eq!(ei.get_property("embedded-width" ).unwrap(), instance.get_container_width().into()); |
270 | assert_eq!(ei.get_property("embedded-height" ).unwrap(), instance.get_container_height().into()); |
271 | assert_eq!(ei.get_property("embedded-x" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
272 | assert_eq!(ei.get_property("embedded-y" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
273 | Ok(()) |
274 | } |
275 | #[test ] fn t_1() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
276 | use i_slint_backend_testing as slint_testing; |
277 | slint_testing::init(); |
278 | use std::cell::RefCell; |
279 | use std::rc::Rc; |
280 | |
281 | use slint_interpreter::ComponentInstance; |
282 | |
283 | let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default(); |
284 | |
285 | let ei = embedded_instance.clone(); |
286 | |
287 | let fixed_factory = slint::ComponentFactory::new(move |ctx| { |
288 | let mut compiler = slint_interpreter::ComponentCompiler::new(); |
289 | let e = spin_on::spin_on(compiler.build_from_source( |
290 | r#"export component E1 inherits Rectangle { |
291 | out property <length> embedded-width <=> self.width; |
292 | out property <length> embedded-height <=> self.height; |
293 | out property <length> embedded-x <=> self.x; |
294 | out property <length> embedded-y <=> self.y; |
295 | |
296 | width: 100px; |
297 | height: 100px; |
298 | }"# .into(), |
299 | std::path::PathBuf::from("embedded.slint" ), |
300 | )).unwrap(); |
301 | let instance = e.create_embedded(ctx).ok(); |
302 | |
303 | ei.replace(instance.as_ref().map(|i| i.clone_strong())); |
304 | |
305 | instance |
306 | }); |
307 | |
308 | let instance = TestCase::new().unwrap(); |
309 | |
310 | assert_eq!(instance.get_container_x(), 15.0); |
311 | assert_eq!(instance.get_container_y(), 20.0); |
312 | assert_eq!(instance.get_container_width(), 0.0); |
313 | assert_eq!(instance.get_container_height(), 0.0); |
314 | assert!(embedded_instance.take().is_none()); |
315 | |
316 | instance.set_cc(fixed_factory.clone()); |
317 | |
318 | assert_eq!(instance.get_container_x(), 15.0); |
319 | assert_eq!(instance.get_container_y(), 20.0); |
320 | assert_eq!(instance.get_container_width(), 100.0); |
321 | assert_eq!(instance.get_container_height(), 100.0); |
322 | |
323 | let ei = embedded_instance.take().unwrap(); |
324 | assert_eq!(ei.get_property("embedded-width" ).unwrap(), instance.get_container_width().into()); |
325 | assert_eq!(ei.get_property("embedded-height" ).unwrap(), instance.get_container_height().into()); |
326 | assert_eq!(ei.get_property("embedded-x" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
327 | assert_eq!(ei.get_property("embedded-y" ).unwrap(), slint_interpreter::Value::Number(0.0)); |
328 | Ok(()) |
329 | } |