Ensure that by default PDFs are opened in Chrome.
[chromium-blink-merge.git] / ash / virtual_keyboard_controller_unittest.cc
blob7ca7a2aeef814df2cb3c1a6369f1f9241673811f
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()
50 ->maximize_mode_controller()
51 ->event_blocker_ = blocker.Pass();
54 void SetUp() override {
55 base::CommandLine::ForCurrentProcess()->AppendSwitch(
56 keyboard::switches::kDisableSmartVirtualKeyboard);
57 AshTestBase::SetUp();
58 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
59 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
62 private:
63 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest);
66 TEST_F(VirtualKeyboardControllerTest, EnabledDuringMaximizeMode) {
67 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
68 // Toggle maximized mode on.
69 Shell::GetInstance()
70 ->maximize_mode_controller()
71 ->EnableMaximizeModeWindowManager(true);
72 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
73 // Toggle maximized mode off.
74 Shell::GetInstance()
75 ->maximize_mode_controller()
76 ->EnableMaximizeModeWindowManager(false);
77 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
80 // Mock event blocker that enables the internal keyboard when it's destructor
81 // is called.
82 class MockEventBlocker : public ScopedDisableInternalMouseAndKeyboard {
83 public:
84 MockEventBlocker() {}
85 ~MockEventBlocker() override {
86 std::vector<ui::KeyboardDevice> keyboards;
87 keyboards.push_back(
88 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL));
89 ui::DeviceHotplugEventObserver* manager =
90 ui::DeviceDataManager::GetInstance();
91 manager->OnKeyboardDevicesUpdated(keyboards);
94 private:
95 DISALLOW_COPY_AND_ASSIGN(MockEventBlocker);
98 // Tests that reenabling keyboard devices while shutting down does not
99 // cause the Virtual Keyboard Controller to crash. See crbug.com/446204.
100 TEST_F(VirtualKeyboardControllerTest, RestoreKeyboardDevices) {
101 // Toggle maximized mode on.
102 Shell::GetInstance()
103 ->maximize_mode_controller()
104 ->EnableMaximizeModeWindowManager(true);
105 scoped_ptr<ScopedDisableInternalMouseAndKeyboard>
106 blocker(new MockEventBlocker);
107 SetEventBlocker(blocker.Pass());
110 class VirtualKeyboardControllerAutoTest : public VirtualKeyboardControllerTest,
111 public VirtualKeyboardObserver {
112 public:
113 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
114 ~VirtualKeyboardControllerAutoTest() override {}
116 void SetUp() override {
117 AshTestBase::SetUp();
118 // Set the current list of devices to empty so that they don't interfere
119 // with the test.
120 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
121 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
122 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver(
123 this);
126 void TearDown() override {
127 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver(
128 this);
129 AshTestBase::TearDown();
132 void OnKeyboardSuppressionChanged(bool suppressed) override {
133 notified_ = true;
134 suppressed_ = suppressed;
137 void ResetObserver() {
138 suppressed_ = false;
139 notified_ = false;
142 bool IsVirtualKeyboardSuppressed() { return suppressed_; }
144 bool notified() { return notified_; }
146 private:
147 // Whether the observer method was called.
148 bool notified_;
150 // Whether the keeyboard is suppressed.
151 bool suppressed_;
153 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAutoTest);
156 // Tests that the onscreen keyboard is disabled if an internal keyboard is
157 // present and maximized mode is disabled.
158 TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfInternalKeyboardPresent) {
159 std::vector<ui::TouchscreenDevice> screens;
160 screens.push_back(ui::TouchscreenDevice(
161 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, gfx::Size(1024, 768), 0));
162 UpdateTouchscreenDevices(screens);
163 std::vector<ui::KeyboardDevice> keyboards;
164 keyboards.push_back(
165 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL));
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(ui::TouchscreenDevice(
180 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, gfx::Size(800, 600), 0));
181 UpdateTouchscreenDevices(devices);
182 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
183 // Remove touchscreen. Keyboard should hide.
184 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
185 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
188 TEST_F(VirtualKeyboardControllerAutoTest, SuppressedIfExternalKeyboardPresent) {
189 std::vector<ui::TouchscreenDevice> screens;
190 screens.push_back(ui::TouchscreenDevice(
191 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, gfx::Size(1024, 768), 0));
192 UpdateTouchscreenDevices(screens);
193 std::vector<ui::KeyboardDevice> keyboards;
194 keyboards.push_back(
195 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL));
196 UpdateKeyboardDevices(keyboards);
197 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
198 ASSERT_TRUE(notified());
199 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
200 // Toggle show keyboard. Keyboard should be visible.
201 ResetObserver();
202 Shell::GetInstance()
203 ->virtual_keyboard_controller()
204 ->ToggleIgnoreExternalKeyboard();
205 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
206 ASSERT_TRUE(notified());
207 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
208 // Toggle show keyboard. Keyboard should be hidden.
209 ResetObserver();
210 Shell::GetInstance()
211 ->virtual_keyboard_controller()
212 ->ToggleIgnoreExternalKeyboard();
213 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
214 ASSERT_TRUE(notified());
215 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
216 // Remove external keyboard. Should be notified that the keyboard is not
217 // suppressed.
218 ResetObserver();
219 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
220 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
221 ASSERT_TRUE(notified());
222 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
225 // Tests handling multiple keyboards. Catches crbug.com/430252
226 TEST_F(VirtualKeyboardControllerAutoTest, HandleMultipleKeyboardsPresent) {
227 std::vector<ui::KeyboardDevice> keyboards;
228 keyboards.push_back(
229 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL));
230 keyboards.push_back(
231 ui::KeyboardDevice(2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL));
232 keyboards.push_back(
233 ui::KeyboardDevice(3, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL));
234 UpdateKeyboardDevices(keyboards);
235 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
238 // Tests maximized mode interaction without disabling the internal keyboard.
239 TEST_F(VirtualKeyboardControllerAutoTest, EnabledDuringMaximizeMode) {
240 std::vector<ui::TouchscreenDevice> screens;
241 screens.push_back(ui::TouchscreenDevice(
242 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, gfx::Size(1024, 768), 0));
243 UpdateTouchscreenDevices(screens);
244 std::vector<ui::KeyboardDevice> keyboards;
245 keyboards.push_back(
246 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL));
247 UpdateKeyboardDevices(keyboards);
248 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
249 // Toggle maximized mode on.
250 Shell::GetInstance()
251 ->maximize_mode_controller()
252 ->EnableMaximizeModeWindowManager(true);
253 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
254 // Toggle maximized mode off.
255 Shell::GetInstance()
256 ->maximize_mode_controller()
257 ->EnableMaximizeModeWindowManager(false);
258 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
261 // Tests that keyboard gets suppressed in maximized mode.
262 TEST_F(VirtualKeyboardControllerAutoTest, SuppressedInMaximizedMode) {
263 std::vector<ui::TouchscreenDevice> screens;
264 screens.push_back(ui::TouchscreenDevice(
265 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, gfx::Size(1024, 768), 0));
266 UpdateTouchscreenDevices(screens);
267 std::vector<ui::KeyboardDevice> keyboards;
268 keyboards.push_back(
269 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL));
270 keyboards.push_back(
271 ui::KeyboardDevice(2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL));
272 UpdateKeyboardDevices(keyboards);
273 // Toggle maximized mode on.
274 Shell::GetInstance()
275 ->maximize_mode_controller()
276 ->EnableMaximizeModeWindowManager(true);
277 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
278 ASSERT_TRUE(notified());
279 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
280 // Toggle show keyboard. Keyboard should be visible.
281 ResetObserver();
282 Shell::GetInstance()
283 ->virtual_keyboard_controller()
284 ->ToggleIgnoreExternalKeyboard();
285 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
286 ASSERT_TRUE(notified());
287 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
288 // Toggle show keyboard. Keyboard should be hidden.
289 ResetObserver();
290 Shell::GetInstance()
291 ->virtual_keyboard_controller()
292 ->ToggleIgnoreExternalKeyboard();
293 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
294 ASSERT_TRUE(notified());
295 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
296 // Remove external keyboard. Should be notified that the keyboard is not
297 // suppressed.
298 ResetObserver();
299 keyboards.pop_back();
300 UpdateKeyboardDevices(keyboards);
301 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
302 ASSERT_TRUE(notified());
303 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
304 // Toggle maximized mode oFF.
305 Shell::GetInstance()
306 ->maximize_mode_controller()
307 ->EnableMaximizeModeWindowManager(false);
308 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
311 class VirtualKeyboardControllerAlwaysEnabledTest
312 : public VirtualKeyboardControllerAutoTest {
313 public:
314 VirtualKeyboardControllerAlwaysEnabledTest()
315 : VirtualKeyboardControllerAutoTest() {}
316 ~VirtualKeyboardControllerAlwaysEnabledTest() override {}
318 void SetUp() override {
319 base::CommandLine::ForCurrentProcess()->AppendSwitch(
320 keyboard::switches::kEnableVirtualKeyboard);
321 VirtualKeyboardControllerAutoTest::SetUp();
324 private:
325 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAlwaysEnabledTest);
328 // Tests that the controller cannot suppress the keyboard if the virtual
329 // keyboard always enabled flag is active.
330 TEST_F(VirtualKeyboardControllerAlwaysEnabledTest, DoesNotSuppressKeyboard) {
331 std::vector<ui::TouchscreenDevice> screens;
332 screens.push_back(ui::TouchscreenDevice(
333 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, gfx::Size(1024, 768), 0));
334 UpdateTouchscreenDevices(screens);
335 std::vector<ui::KeyboardDevice> keyboards;
336 keyboards.push_back(
337 ui::KeyboardDevice(1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL));
338 UpdateKeyboardDevices(keyboards);
339 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
342 } // namespace test
343 } // namespace ash