1#![allow(deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/focus"#]
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// Test that the correct item indices are computed when focusing
6// children of sub-components or their roots.
7
8SubComponentWithFocusableChild := Rectangle {
9 property <bool> has-focus: input.has-focus;
10
11 callback activate();
12 activate => {
13 input.focus();
14 }
15
16 input := TextInput {
17 width: 100%;
18 height: 100%;
19 }
20}
21
22SubComponentWithRootFocusable := TextInput {
23 callback activate();
24 activate => {
25 root.focus();
26 }
27}
28
29FocusInNestedSubComponent := Rectangle {
30 property <bool> has-focus: compo.has-focus;
31 callback activate();
32 activate => {
33 compo.activate();
34 }
35 compo := SubComponentWithRootFocusable {
36 }
37}
38
39
40TestCase := Rectangle {
41 width: 400phx;
42 height: 400phx;
43
44 callback focus_subcompo1();
45 focus_subcompo1 => { input1.activate(); }
46 callback focus_subcompo2();
47 focus_subcompo2 => { input2.activate(); }
48 callback focus_subcompo3();
49 focus_subcompo3 => { input3.activate(); }
50
51 input1 := SubComponentWithFocusableChild {
52 y: 0phx;
53 height: 150phx;
54 }
55
56 input2 := SubComponentWithRootFocusable {
57 y: 150phx;
58 height: 150phx;
59 }
60
61 input3 := FocusInNestedSubComponent {
62 y: 250px;
63 height: 150px;
64 }
65
66 property<bool> input1_focused: input1.has_focus;
67 property<bool> input2_focused: input2.has_focus;
68 property<bool> input3_focused: input3.has_focus;
69
70}
71
72/*
73```rust
74let instance = TestCase::new().unwrap();
75assert!(!instance.get_input1_focused());
76assert!(!instance.get_input2_focused());
77assert!(!instance.get_input3_focused());
78
79instance.invoke_focus_subcompo1();
80assert!(instance.get_input1_focused());
81assert!(!instance.get_input2_focused());
82assert!(!instance.get_input3_focused());
83
84instance.invoke_focus_subcompo2();
85assert!(!instance.get_input1_focused());
86assert!(instance.get_input2_focused());
87assert!(!instance.get_input3_focused());
88
89instance.invoke_focus_subcompo3();
90assert!(!instance.get_input1_focused());
91assert!(!instance.get_input2_focused());
92assert!(instance.get_input3_focused());
93```
94
95```cpp
96auto handle = TestCase::create();
97const TestCase &instance = *handle;
98assert(!instance.get_input1_focused());
99assert(!instance.get_input2_focused());
100assert(!instance.get_input3_focused());
101
102instance.invoke_focus_subcompo1();
103assert(instance.get_input1_focused());
104assert(!instance.get_input2_focused());
105assert(!instance.get_input3_focused());
106
107instance.invoke_focus_subcompo2();
108assert(!instance.get_input1_focused());
109assert(instance.get_input2_focused());
110assert(!instance.get_input3_focused());
111
112instance.invoke_focus_subcompo3();
113assert(!instance.get_input1_focused());
114assert(!instance.get_input2_focused());
115assert(instance.get_input3_focused());
116```
117
118```js
119var instance = new slint.TestCase();
120assert(!instance.input1_focused);
121assert(!instance.input2_focused);
122assert(!instance.input3_focused);
123
124instance.focus_subcompo1();
125assert(instance.input1_focused);
126assert(!instance.input2_focused);
127assert(!instance.input3_focused);
128
129instance.focus_subcompo2();
130assert(!instance.input1_focused);
131assert(instance.input2_focused);
132assert(!instance.input3_focused);
133
134instance.focus_subcompo3();
135assert(!instance.input1_focused);
136assert(!instance.input2_focused);
137assert(instance.input3_focused);
138```
139*/
140}
141
142#[test] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> {
143 use i_slint_backend_testing as slint_testing;
144 slint_testing::init();
145 let instance = TestCase::new().unwrap();
146 assert!(!instance.get_input1_focused());
147 assert!(!instance.get_input2_focused());
148 assert!(!instance.get_input3_focused());
149
150 instance.invoke_focus_subcompo1();
151 assert!(instance.get_input1_focused());
152 assert!(!instance.get_input2_focused());
153 assert!(!instance.get_input3_focused());
154
155 instance.invoke_focus_subcompo2();
156 assert!(!instance.get_input1_focused());
157 assert!(instance.get_input2_focused());
158 assert!(!instance.get_input3_focused());
159
160 instance.invoke_focus_subcompo3();
161 assert!(!instance.get_input1_focused());
162 assert!(!instance.get_input2_focused());
163 assert!(instance.get_input3_focused());
164 Ok(())
165}