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 | |
8 | SubComponentWithFocusableChild := 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 | |
22 | SubComponentWithRootFocusable := TextInput { |
23 | callback activate(); |
24 | activate => { |
25 | root.focus(); |
26 | } |
27 | } |
28 | |
29 | FocusInNestedSubComponent := 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 | |
40 | TestCase := 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 |
74 | let instance = TestCase::new().unwrap(); |
75 | assert!(!instance.get_input1_focused()); |
76 | assert!(!instance.get_input2_focused()); |
77 | assert!(!instance.get_input3_focused()); |
78 | |
79 | instance.invoke_focus_subcompo1(); |
80 | assert!(instance.get_input1_focused()); |
81 | assert!(!instance.get_input2_focused()); |
82 | assert!(!instance.get_input3_focused()); |
83 | |
84 | instance.invoke_focus_subcompo2(); |
85 | assert!(!instance.get_input1_focused()); |
86 | assert!(instance.get_input2_focused()); |
87 | assert!(!instance.get_input3_focused()); |
88 | |
89 | instance.invoke_focus_subcompo3(); |
90 | assert!(!instance.get_input1_focused()); |
91 | assert!(!instance.get_input2_focused()); |
92 | assert!(instance.get_input3_focused()); |
93 | ``` |
94 | |
95 | ```cpp |
96 | auto handle = TestCase::create(); |
97 | const TestCase &instance = *handle; |
98 | assert(!instance.get_input1_focused()); |
99 | assert(!instance.get_input2_focused()); |
100 | assert(!instance.get_input3_focused()); |
101 | |
102 | instance.invoke_focus_subcompo1(); |
103 | assert(instance.get_input1_focused()); |
104 | assert(!instance.get_input2_focused()); |
105 | assert(!instance.get_input3_focused()); |
106 | |
107 | instance.invoke_focus_subcompo2(); |
108 | assert(!instance.get_input1_focused()); |
109 | assert(instance.get_input2_focused()); |
110 | assert(!instance.get_input3_focused()); |
111 | |
112 | instance.invoke_focus_subcompo3(); |
113 | assert(!instance.get_input1_focused()); |
114 | assert(!instance.get_input2_focused()); |
115 | assert(instance.get_input3_focused()); |
116 | ``` |
117 | |
118 | ```js |
119 | var instance = new slint.TestCase(); |
120 | assert(!instance.input1_focused); |
121 | assert(!instance.input2_focused); |
122 | assert(!instance.input3_focused); |
123 | |
124 | instance.focus_subcompo1(); |
125 | assert(instance.input1_focused); |
126 | assert(!instance.input2_focused); |
127 | assert(!instance.input3_focused); |
128 | |
129 | instance.focus_subcompo2(); |
130 | assert(!instance.input1_focused); |
131 | assert(instance.input2_focused); |
132 | assert(!instance.input3_focused); |
133 | |
134 | instance.focus_subcompo3(); |
135 | assert(!instance.input1_focused); |
136 | assert(!instance.input2_focused); |
137 | assert(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 | } |