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
11component Container {
12 in property<component-factory> c1 <=> cont1.component-factory;
13
14 cont1 := ComponentContainer { }
15}
16
17export component TestCase inherits Rectangle {
18
19 in property<component-factory> c1;
20 out property<bool> outside-focus <=> outside.has-focus;
21
22 outside := Button { text: "Outside button"; }
23
24 Container {
25 c1 <=> root.c1;
26 }
27}
28
29/*
30```cpp
31// ComponentFactory not supported yet!
32```
33
34```rust
35let factory = slint::ComponentFactory::new(|ctx| {
36
37 let mut compiler = slint_interpreter::ComponentCompiler::new();
38 let e = spin_on::spin_on(compiler.build_from_source(
39 r#"import { Button } from "std-widgets.slint";
40
41export component E1 inherits Rectangle {
42 background: Colors.red;
43 forward-focus: b;
44 b := Button {
45 text: "red";
46 }
47}"#.into(),
48 std::path::PathBuf::from("embedded.slint"),
49 )).unwrap();
50 e.create_embedded(ctx).ok()
51});
52
53let instance = TestCase::new().unwrap();
54assert!(!instance.get_outside_focus()); // Nothing has focus be default
55slint_testing::send_keyboard_string_sequence(&instance, "\t");
56assert!(instance.get_outside_focus()); // The outside button is the only thing
57 // accepting focus at this point.
58
59instance.set_c1(factory);
60
61assert!(instance.get_outside_focus()); // embedding does not move the focus
62
63// focus the two embedded buttons:
64slint_testing::send_keyboard_string_sequence(&instance, "\t");
65assert!(!instance.get_outside_focus());
66
67// Go back to outside button
68slint_testing::send_keyboard_string_sequence(&instance, "\t");
69assert!(instance.get_outside_focus());
70
71// Focus backwards over the embedded buttons
72slint_testing::send_keyboard_string_sequence(&instance, "\u{0019}");
73assert!(!instance.get_outside_focus());
74
75slint_testing::send_keyboard_string_sequence(&instance, "\u{0019}");
76assert!(instance.get_outside_focus());
77```
78
79```js
80var _instance = new slint.TestCase();
81```
82*/
83}
84
85#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
86 use i_slint_backend_testing as slint_testing;
87 slint_testing::init();
88 let factory = slint::ComponentFactory::new(|ctx| {
89
90 let mut compiler = slint_interpreter::ComponentCompiler::new();
91 let e = spin_on::spin_on(compiler.build_from_source(
92 r#"import { Button } from "std-widgets.slint";
93
94 export component E1 inherits Rectangle {
95 background: Colors.red;
96 forward-focus: b;
97 b := Button {
98 text: "red";
99 }
100 }"#.into(),
101 std::path::PathBuf::from("embedded.slint"),
102 )).unwrap();
103 e.create_embedded(ctx).ok()
104 });
105
106 let instance = TestCase::new().unwrap();
107 assert!(!instance.get_outside_focus()); // Nothing has focus be default
108 slint_testing::send_keyboard_string_sequence(&instance, "\t");
109 assert!(instance.get_outside_focus()); // The outside button is the only thing
110 // accepting focus at this point.
111
112 instance.set_c1(factory);
113
114 assert!(instance.get_outside_focus()); // embedding does not move the focus
115
116 // focus the two embedded buttons:
117 slint_testing::send_keyboard_string_sequence(&instance, "\t");
118 assert!(!instance.get_outside_focus());
119
120 // Go back to outside button
121 slint_testing::send_keyboard_string_sequence(&instance, "\t");
122 assert!(instance.get_outside_focus());
123
124 // Focus backwards over the embedded buttons
125 slint_testing::send_keyboard_string_sequence(&instance, "\u{0019}");
126 assert!(!instance.get_outside_focus());
127
128 slint_testing::send_keyboard_string_sequence(&instance, "\u{0019}");
129 assert!(instance.get_outside_focus());
130 Ok(())
131}