1 | #![allow (deprecated)]slint::slint!{#[style="material" #] |
2 | #[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/widgets"# ] |
3 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
4 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
5 | |
6 | import { ComboBox } from "std-widgets.slint" ; |
7 | export component TestCase inherits Window { |
8 | width: 200px; |
9 | height: 200px; |
10 | |
11 | in-out property <string> output; |
12 | TouchArea { |
13 | clicked => { output += "clicked-under \n" ; } |
14 | } |
15 | |
16 | VerticalLayout { |
17 | alignment: center; |
18 | box := ComboBox { |
19 | model: ["Aaa" , "Bbb" , "Ccc" ]; |
20 | selected => { |
21 | output += "selected(" +self.current-value+"," +self.current-index+") \n" ; |
22 | } |
23 | } |
24 | } |
25 | |
26 | in-out property current-index <=> box.current-index; |
27 | in-out property current-value <=> box.current-value; |
28 | out property has-focus <=> box.has-focus; |
29 | } |
30 | |
31 | /* |
32 | |
33 | |
34 | ```rust |
35 | use slint::platform::Key; |
36 | use slint::SharedString; |
37 | |
38 | let instance = TestCase::new().unwrap(); |
39 | |
40 | assert_eq!(instance.get_current_value(), "Aaa"); |
41 | assert_eq!(instance.get_current_index(), 0); |
42 | assert_eq!(instance.get_has_focus(), false); |
43 | |
44 | // Change the index programmatically |
45 | instance.set_current_index(1); |
46 | assert_eq!(instance.get_current_value(), "Bbb"); |
47 | assert_eq!(instance.get_current_index(), 1); |
48 | assert_eq!(instance.get_output(), ""); |
49 | instance.set_current_index(0); |
50 | assert_eq!(instance.get_current_value(), "Aaa"); |
51 | assert_eq!(instance.get_current_index(), 0); |
52 | assert_eq!(instance.get_output(), ""); |
53 | assert_eq!(instance.get_has_focus(), false); |
54 | |
55 | // Open the combobox |
56 | slint_testing::send_mouse_click(&instance, 100., 100.); |
57 | assert_eq!(instance.get_output(), ""); |
58 | assert_eq!(instance.get_has_focus(), true); |
59 | |
60 | // click outside of the combobox, this should close it |
61 | slint_testing::send_mouse_click(&instance, 100., 10.); |
62 | assert_eq!(instance.get_output(), ""); |
63 | assert_eq!(instance.get_current_value(), "Aaa"); |
64 | assert_eq!(instance.get_current_index(), 0); |
65 | assert_eq!(instance.get_has_focus(), true); |
66 | |
67 | // click outside of the combobox again |
68 | slint_testing::send_mouse_click(&instance, 100., 10.); |
69 | assert_eq!(instance.get_output(), "clicked-under\n"); |
70 | instance.set_output(Default::default()); |
71 | assert_eq!(instance.get_current_value(), "Aaa"); |
72 | assert_eq!(instance.get_current_index(), 0); |
73 | assert_eq!(instance.get_has_focus(), true); |
74 | |
75 | |
76 | // The arrow change the values |
77 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
78 | assert_eq!(instance.get_current_value(), "Bbb"); |
79 | assert_eq!(instance.get_current_index(), 1); |
80 | assert_eq!(instance.get_output(), "selected(Bbb,1)\n"); |
81 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
82 | assert_eq!(instance.get_current_value(), "Ccc"); |
83 | assert_eq!(instance.get_current_index(), 2); |
84 | assert_eq!(instance.get_output(), "selected(Bbb,1)\nselected(Ccc,2)\n"); |
85 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow)); |
86 | assert_eq!(instance.get_current_value(), "Bbb"); |
87 | assert_eq!(instance.get_current_index(), 1); |
88 | assert_eq!(instance.get_output(), "selected(Bbb,1)\nselected(Ccc,2)\nselected(Bbb,1)\n"); |
89 | instance.set_output(Default::default()); |
90 | |
91 | |
92 | ``` |
93 | |
94 | */ |
95 | } |
96 | |
97 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
98 | use i_slint_backend_testing as slint_testing; |
99 | slint_testing::init(); |
100 | use slint::platform::Key; |
101 | use slint::SharedString; |
102 | |
103 | let instance = TestCase::new().unwrap(); |
104 | |
105 | assert_eq!(instance.get_current_value(), "Aaa" ); |
106 | assert_eq!(instance.get_current_index(), 0); |
107 | assert_eq!(instance.get_has_focus(), false); |
108 | |
109 | // Change the index programmatically |
110 | instance.set_current_index(1); |
111 | assert_eq!(instance.get_current_value(), "Bbb" ); |
112 | assert_eq!(instance.get_current_index(), 1); |
113 | assert_eq!(instance.get_output(), "" ); |
114 | instance.set_current_index(0); |
115 | assert_eq!(instance.get_current_value(), "Aaa" ); |
116 | assert_eq!(instance.get_current_index(), 0); |
117 | assert_eq!(instance.get_output(), "" ); |
118 | assert_eq!(instance.get_has_focus(), false); |
119 | |
120 | // Open the combobox |
121 | slint_testing::send_mouse_click(&instance, 100., 100.); |
122 | assert_eq!(instance.get_output(), "" ); |
123 | assert_eq!(instance.get_has_focus(), true); |
124 | |
125 | // click outside of the combobox, this should close it |
126 | slint_testing::send_mouse_click(&instance, 100., 10.); |
127 | assert_eq!(instance.get_output(), "" ); |
128 | assert_eq!(instance.get_current_value(), "Aaa" ); |
129 | assert_eq!(instance.get_current_index(), 0); |
130 | assert_eq!(instance.get_has_focus(), true); |
131 | |
132 | // click outside of the combobox again |
133 | slint_testing::send_mouse_click(&instance, 100., 10.); |
134 | assert_eq!(instance.get_output(), "clicked-under \n" ); |
135 | instance.set_output(Default::default()); |
136 | assert_eq!(instance.get_current_value(), "Aaa" ); |
137 | assert_eq!(instance.get_current_index(), 0); |
138 | assert_eq!(instance.get_has_focus(), true); |
139 | |
140 | |
141 | // The arrow change the values |
142 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
143 | assert_eq!(instance.get_current_value(), "Bbb" ); |
144 | assert_eq!(instance.get_current_index(), 1); |
145 | assert_eq!(instance.get_output(), "selected(Bbb,1) \n" ); |
146 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
147 | assert_eq!(instance.get_current_value(), "Ccc" ); |
148 | assert_eq!(instance.get_current_index(), 2); |
149 | assert_eq!(instance.get_output(), "selected(Bbb,1) \nselected(Ccc,2) \n" ); |
150 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow)); |
151 | assert_eq!(instance.get_current_value(), "Bbb" ); |
152 | assert_eq!(instance.get_current_index(), 1); |
153 | assert_eq!(instance.get_output(), "selected(Bbb,1) \nselected(Ccc,2) \nselected(Bbb,1) \n" ); |
154 | instance.set_output(Default::default()); |
155 | |
156 | |
157 | Ok(()) |
158 | } |