Roll src/third_party/WebKit dbf9be3:8d6c3d5 (svn 202308:202312)
[chromium-blink-merge.git] / ash / virtual_keyboard_controller_unittest.cc
blobc31c6d0424690f8de2d6f129da49a0b359534f04
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/virtual_keyboard_controller.h"
7 #include <vector>
9 #include "ash/shell.h"
10 #include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_observer.h"
11 #include "ash/test/ash_test_base.h"
12 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
13 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h"
14 #include "ash_switches.h"
15 #include "base/command_line.h"
16 #include "ui/events/devices/device_data_manager.h"
17 #include "ui/events/devices/device_hotplug_event_observer.h"
18 #include "ui/events/devices/input_device.h"
19 #include "ui/events/devices/keyboard_device.h"
20 #include "ui/events/devices/touchscreen_device.h"
21 #include "ui/keyboard/keyboard_export.h"
22 #include "ui/keyboard/keyboard_switches.h"
23 #include "ui/keyboard/keyboard_util.h"
25 namespace ash {
26 namespace test {
28 class VirtualKeyboardControllerTest : public AshTestBase {
29 public:
30 VirtualKeyboardControllerTest() {}
31 ~VirtualKeyboardControllerTest() override {}
33 void UpdateTouchscreenDevices(
34 std::vector<ui::TouchscreenDevice> touchscreen_devices) {
35 ui::DeviceHotplugEventObserver* manager =
36 ui::DeviceDataManager::GetInstance();
37 manager->OnTouchscreenDevicesUpdated(touchscreen_devices);
40 void UpdateKeyboardDevices(std::vector<ui::KeyboardDevice> keyboard_devices) {
41 ui::DeviceHotplugEventObserver* manager =
42 ui::DeviceDataManager::GetInstance();
43 manager->OnKeyboardDevicesUpdated(keyboard_devices);
46 // Sets the event blocker on the maximized window controller.
47 void SetEventBlocker(
48 scoped_ptr<ScopedDisableInternalMouseAndKeyboard> blocker) {
49 Shell::GetInstance()->maximize_mode_controller()->event_blocker_ =
50 blocker.Pass();
53 void SetUp() override {
54 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
55 keyboard::switches::kSmartVirtualKeyboard, "disabled");
56 AshTestBase::SetUp();
57 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
58 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
61 private:
62 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest);
65 TEST_F(VirtualKeyboardControllerTest, EnabledDuringMaximizeMode) {
66 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
67 // Toggle maximized mode on.
68 Shell::GetInstance()
69 ->maximize_mode_controller()
70 ->EnableMaximizeModeWindowManager(true);
71 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
72 // Toggle maximized mode off.
73 Shell::GetInstance()
74 ->maximize_mode_controller()
75 ->EnableMaximizeModeWindowManager(false);
76 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
79 // Mock event blocker that enables the internal keyboard when it's destructor
80 // is called.
81 class MockEventBlocker : public ScopedDisableInternalMouseAndKeyboard {
82 public:
83 MockEventBlocker() {}
84 ~MockEventBlocker() override {
85 std::vector<ui::KeyboardDevice> keyboards;
86 keyboards.push_back(ui::KeyboardDevice(
87 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
88 ui::DeviceHotplugEventObserver* manager =
89 ui::DeviceDataManager::GetInstance();
90 manager->OnKeyboardDevicesUpdated(keyboards);
93 private:
94 DISALLOW_COPY_AND_ASSIGN(MockEventBlocker);
97 // Tests that reenabling keyboard devices while shutting down does not
98 // cause the Virtual Keyboard Controller to crash. See crbug.com/446204.
99 TEST_F(VirtualKeyboardControllerTest, RestoreKeyboardDevices) {
100 // Toggle maximized mode on.
101 Shell::GetInstance()
102 ->maximize_mode_controller()
103 ->EnableMaximizeModeWindowManager(true);
104 scoped_ptr<ScopedDisableInternalMouseAndKeyboard> blocker(
105 new MockEventBlocker);
106 SetEventBlocker(blocker.Pass());
109 class VirtualKeyboardControllerAutoTest : public VirtualKeyboardControllerTest,
110 public VirtualKeyboardObserver {
111 public:
112 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
113 ~VirtualKeyboardControllerAutoTest() override {}
115 void SetUp() override {
116 AshTestBase::SetUp();
117 // Set the current list of devices to empty so that they don't interfere
118 // with the test.
119 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
120 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
121 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver(
122 this);
125 void TearDown() override {
126 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver(
127 this);
128 AshTestBase::TearDown();
131 void OnKeyboardSuppressionChanged(bool suppressed) override {
132 notified_ = true;
133 suppressed_ = suppressed;
136 void ResetObserver() {
137 suppressed_ = false;
138 notified_ = false;
141 bool IsVirtualKeyboardSuppressed() { return suppressed_; }
143 bool notified() { return notified_; }
145 private:
146 // Whether the observer method was called.
147 bool notified_;
149 // Whether the keeyboard is suppressed.
150 bool suppressed_;
152 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAutoTest);
155 // Tests that the onscreen keyboard is disabled if an internal keyboard is
156 // present and maximized mode is disabled.
157 TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfInternalKeyboardPresent) {
158 std::vector<ui::TouchscreenDevice> screens;
159 screens.push_back(
160 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
161 "Touchscreen", gfx::Size(1024, 768), 0));
162 UpdateTouchscreenDevices(screens);
163 std::vector<ui::KeyboardDevice> keyboards;
164 keyboards.push_back(ui::KeyboardDevice(
165 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
166 UpdateKeyboardDevices(keyboards);
167 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
168 // Remove the internal keyboard. Virtual keyboard should now show.
169 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
170 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
171 // Replug in the internal keyboard. Virtual keyboard should hide.
172 UpdateKeyboardDevices(keyboards);
173 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
176 TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfNoTouchScreen) {
177 std::vector<ui::TouchscreenDevice> devices;
178 // Add a touchscreen. Keyboard should deploy.
179 devices.push_back(
180 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL,
181 "Touchscreen", gfx::Size(800, 600), 0));
182 UpdateTouchscreenDevices(devices);
183 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
184 // Remove touchscreen. Keyboard should hide.
185 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
186 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
189 TEST_F(VirtualKeyboardControllerAutoTest, SuppressedIfExternalKeyboardPresent) {
190 std::vector<ui::TouchscreenDevice> screens;
191 screens.push_back(
192 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
193 "Touchscreen", gfx::Size(1024, 768), 0));
194 UpdateTouchscreenDevices(screens);
195 std::vector<ui::KeyboardDevice> keyboards;
196 keyboards.push_back(ui::KeyboardDevice(
197 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
198 UpdateKeyboardDevices(keyboards);
199 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
200 ASSERT_TRUE(notified());
201 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
202 // Toggle show keyboard. Keyboard should be visible.
203 ResetObserver();
204 Shell::GetInstance()
205 ->virtual_keyboard_controller()
206 ->ToggleIgnoreExternalKeyboard();
207 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
208 ASSERT_TRUE(notified());
209 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
210 // Toggle show keyboard. Keyboard should be hidden.
211 ResetObserver();
212 Shell::GetInstance()
213 ->virtual_keyboard_controller()
214 ->ToggleIgnoreExternalKeyboard();
215 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
216 ASSERT_TRUE(notified());
217 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
218 // Remove external keyboard. Should be notified that the keyboard is not
219 // suppressed.
220 ResetObserver();
221 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
222 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
223 ASSERT_TRUE(notified());
224 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
227 // Tests handling multiple keyboards. Catches crbug.com/430252
228 TEST_F(VirtualKeyboardControllerAutoTest, HandleMultipleKeyboardsPresent) {
229 std::vector<ui::KeyboardDevice> keyboards;
230 keyboards.push_back(ui::KeyboardDevice(
231 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
232 keyboards.push_back(ui::KeyboardDevice(
233 2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
234 keyboards.push_back(ui::KeyboardDevice(
235 3, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
236 UpdateKeyboardDevices(keyboards);
237 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
240 // Tests maximized mode interaction without disabling the internal keyboard.
241 TEST_F(VirtualKeyboardControllerAutoTest, EnabledDuringMaximizeMode) {
242 std::vector<ui::TouchscreenDevice> screens;
243 screens.push_back(
244 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
245 "Touchscreen", gfx::Size(1024, 768), 0));
246 UpdateTouchscreenDevices(screens);
247 std::vector<ui::KeyboardDevice> keyboards;
248 keyboards.push_back(ui::KeyboardDevice(
249 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
250 UpdateKeyboardDevices(keyboards);
251 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
252 // Toggle maximized mode on.
253 Shell::GetInstance()
254 ->maximize_mode_controller()
255 ->EnableMaximizeModeWindowManager(true);
256 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
257 // Toggle maximized mode off.
258 Shell::GetInstance()
259 ->maximize_mode_controller()
260 ->EnableMaximizeModeWindowManager(false);
261 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
264 // Tests that keyboard gets suppressed in maximized mode.
265 TEST_F(VirtualKeyboardControllerAutoTest, SuppressedInMaximizedMode) {
266 std::vector<ui::TouchscreenDevice> screens;
267 screens.push_back(
268 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
269 "Touchscreen", gfx::Size(1024, 768), 0));
270 UpdateTouchscreenDevices(screens);
271 std::vector<ui::KeyboardDevice> keyboards;
272 keyboards.push_back(ui::KeyboardDevice(
273 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "Keyboard"));
274 keyboards.push_back(ui::KeyboardDevice(
275 2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "Keyboard"));
276 UpdateKeyboardDevices(keyboards);
277 // Toggle maximized mode on.
278 Shell::GetInstance()
279 ->maximize_mode_controller()
280 ->EnableMaximizeModeWindowManager(true);
281 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
282 ASSERT_TRUE(notified());
283 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
284 // Toggle show keyboard. Keyboard should be visible.
285 ResetObserver();
286 Shell::GetInstance()
287 ->virtual_keyboard_controller()
288 ->ToggleIgnoreExternalKeyboard();
289 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
290 ASSERT_TRUE(notified());
291 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
292 // Toggle show keyboard. Keyboard should be hidden.
293 ResetObserver();
294 Shell::GetInstance()
295 ->virtual_keyboard_controller()
296 ->ToggleIgnoreExternalKeyboard();
297 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
298 ASSERT_TRUE(notified());
299 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
300 // Remove external keyboard. Should be notified that the keyboard is not
301 // suppressed.
302 ResetObserver();
303 keyboards.pop_back();
304 UpdateKeyboardDevices(keyboards);
305 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
306 ASSERT_TRUE(notified());
307 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
308 // Toggle maximized mode oFF.
309 Shell::GetInstance()
310 ->maximize_mode_controller()
311 ->EnableMaximizeModeWindowManager(false);
312 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
315 class VirtualKeyboardControllerAlwaysEnabledTest
316 : public VirtualKeyboardControllerAutoTest {
317 public:
318 VirtualKeyboardControllerAlwaysEnabledTest()
319 : VirtualKeyboardControllerAutoTest() {}
320 ~VirtualKeyboardControllerAlwaysEnabledTest() override {}
322 void SetUp() override {
323 base::CommandLine::ForCurrentProcess()->AppendSwitch(
324 keyboard::switches::kEnableVirtualKeyboard);
325 VirtualKeyboardControllerAutoTest::SetUp();
328 private:
329 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAlwaysEnabledTest);
332 // Tests that the controller cannot suppress the keyboard if the virtual
333 // keyboard always enabled flag is active.
334 TEST_F(VirtualKeyboardControllerAlwaysEnabledTest, DoesNotSuppressKeyboard) {
335 std::vector<ui::TouchscreenDevice> screens;
336 screens.push_back(
337 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
338 "Touchscreen", gfx::Size(1024, 768), 0));
339 UpdateTouchscreenDevices(screens);
340 std::vector<ui::KeyboardDevice> keyboards;
341 keyboards.push_back(ui::KeyboardDevice(
342 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
343 UpdateKeyboardDevices(keyboards);
344 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
347 } // namespace test
348 } // namespace ash