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
9import { Button } from "std-widgets.slint";
10
11export 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
34use std::cell::RefCell;
35use std::rc::Rc;
36
37use slint_interpreter::ComponentInstance;
38
39let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default();
40
41let ei = embedded_instance.clone();
42
43let 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
69let instance = TestCase::new().unwrap();
70
71let ei = embedded_instance.take();
72
73assert_eq!(instance.get_container_x(), 15.0);
74assert_eq!(instance.get_container_y(), 20.0);
75assert_eq!(instance.get_container_width(), 0.0);
76assert_eq!(instance.get_container_height(), 0.0);
77assert!(ei.is_none());
78drop(ei);
79
80instance.set_cc(dyn_factory.clone());
81
82assert_eq!(instance.get_container_x(), 15.0);
83assert_eq!(instance.get_container_y(), 20.0);
84assert_eq!(instance.get_container_width(), 50.0);
85assert_eq!(instance.get_container_height(), 50.0);
86
87let ei = embedded_instance.take().unwrap();
88assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into());
89assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into());
90assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0));
91assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0));
92
93// Set a valid size:
94instance.set_container_width(20.0);
95instance.set_container_height(70.0);
96
97assert_eq!(instance.get_container_x(), 15.0);
98assert_eq!(instance.get_container_y(), 20.0);
99assert_eq!(instance.get_container_width(), 20.0);
100assert_eq!(instance.get_container_height(), 70.0);
101
102assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into());
103assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into());
104assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0));
105assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0));
106
107// Set an invalid size:
108instance.set_container_width(5.0);
109instance.set_container_height(700.0);
110
111assert_eq!(instance.get_container_x(), 15.0);
112assert_eq!(instance.get_container_y(), 20.0);
113assert_eq!(instance.get_container_width(), 5.0);
114assert_eq!(instance.get_container_height(), 700.0);
115
116assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into());
117assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into());
118assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0));
119assert_eq!(ei.get_property("embedded-y").unwrap(), slint_interpreter::Value::Number(0.0));
120```
121
122// Now with fixed size
123```rust
124use std::cell::RefCell;
125use std::rc::Rc;
126
127use slint_interpreter::ComponentInstance;
128
129let embedded_instance: Rc<RefCell<Option<ComponentInstance>>> = Default::default();
130
131let ei = embedded_instance.clone();
132
133let 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
154let instance = TestCase::new().unwrap();
155
156assert_eq!(instance.get_container_x(), 15.0);
157assert_eq!(instance.get_container_y(), 20.0);
158assert_eq!(instance.get_container_width(), 0.0);
159assert_eq!(instance.get_container_height(), 0.0);
160assert!(embedded_instance.take().is_none());
161
162instance.set_cc(fixed_factory.clone());
163
164assert_eq!(instance.get_container_x(), 15.0);
165assert_eq!(instance.get_container_y(), 20.0);
166assert_eq!(instance.get_container_width(), 100.0);
167assert_eq!(instance.get_container_height(), 100.0);
168
169let ei = embedded_instance.take().unwrap();
170assert_eq!(ei.get_property("embedded-width").unwrap(), instance.get_container_width().into());
171assert_eq!(ei.get_property("embedded-height").unwrap(), instance.get_container_height().into());
172assert_eq!(ei.get_property("embedded-x").unwrap(), slint_interpreter::Value::Number(0.0));
173assert_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}