Make some Polymer elements accessible through chrome://resources.
[chromium-blink-merge.git] / ash / virtual_keyboard_controller_unittest.cc
blob7a6d67681cff2f4dae99307882b6d762c31645ae
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 AshTestBase::SetUp();
56 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
57 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
60 private:
61 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest);
64 TEST_F(VirtualKeyboardControllerTest, EnabledDuringMaximizeMode) {
65 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
66 // Toggle maximized mode on.
67 Shell::GetInstance()
68 ->maximize_mode_controller()
69 ->EnableMaximizeModeWindowManager(true);
70 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
71 // Toggle maximized mode off.
72 Shell::GetInstance()
73 ->maximize_mode_controller()
74 ->EnableMaximizeModeWindowManager(false);
75 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
78 // Mock event blocker that enables the internal keyboard when it's destructor
79 // is called.
80 class MockEventBlocker : public ScopedDisableInternalMouseAndKeyboard {
81 public:
82 MockEventBlocker() {}
83 ~MockEventBlocker() override {
84 std::vector<ui::KeyboardDevice> keyboards;
85 keyboards.push_back(ui::KeyboardDevice(
86 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
87 ui::DeviceHotplugEventObserver* manager =
88 ui::DeviceDataManager::GetInstance();
89 manager->OnKeyboardDevicesUpdated(keyboards);
92 private:
93 DISALLOW_COPY_AND_ASSIGN(MockEventBlocker);
96 // Tests that reenabling keyboard devices while shutting down does not
97 // cause the Virtual Keyboard Controller to crash. See crbug.com/446204.
98 TEST_F(VirtualKeyboardControllerTest, RestoreKeyboardDevices) {
99 // Toggle maximized mode on.
100 Shell::GetInstance()
101 ->maximize_mode_controller()
102 ->EnableMaximizeModeWindowManager(true);
103 scoped_ptr<ScopedDisableInternalMouseAndKeyboard>
104 blocker(new MockEventBlocker);
105 SetEventBlocker(blocker.Pass());
108 class VirtualKeyboardControllerAutoTest : public VirtualKeyboardControllerTest,
109 public VirtualKeyboardObserver {
110 public:
111 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
112 ~VirtualKeyboardControllerAutoTest() override {}
114 void SetUp() override {
115 base::CommandLine::ForCurrentProcess()->AppendSwitch(
116 keyboard::switches::kEnableAutoVirtualKeyboard);
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.
158 TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfInternalKeyboardPresent) {
159 std::vector<ui::TouchscreenDevice> screens;
160 screens.push_back(
161 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
162 "Touchscreen", gfx::Size(1024, 768), 0));
163 UpdateTouchscreenDevices(screens);
164 std::vector<ui::KeyboardDevice> keyboards;
165 keyboards.push_back(ui::KeyboardDevice(
166 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
167 UpdateKeyboardDevices(keyboards);
168 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
169 // Remove the internal keyboard. Virtual keyboard should now show.
170 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
171 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
172 // Replug in the internal keyboard. Virtual keyboard should hide.
173 UpdateKeyboardDevices(keyboards);
174 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
177 TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfNoTouchScreen) {
178 std::vector<ui::TouchscreenDevice> devices;
179 // Add a touchscreen. Keyboard should deploy.
180 devices.push_back(
181 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL,
182 "Touchscreen", gfx::Size(800, 600), 0));
183 UpdateTouchscreenDevices(devices);
184 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
185 // Remove touchscreen. Keyboard should hide.
186 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
187 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
190 TEST_F(VirtualKeyboardControllerAutoTest, SuppressedIfExternalKeyboardPresent) {
191 std::vector<ui::TouchscreenDevice> screens;
192 screens.push_back(
193 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
194 "Touchscreen", gfx::Size(1024, 768), 0));
195 UpdateTouchscreenDevices(screens);
196 std::vector<ui::KeyboardDevice> keyboards;
197 keyboards.push_back(ui::KeyboardDevice(
198 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
199 UpdateKeyboardDevices(keyboards);
200 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
201 ASSERT_TRUE(notified());
202 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
203 // Toggle show keyboard. Keyboard should be visible.
204 ResetObserver();
205 Shell::GetInstance()
206 ->virtual_keyboard_controller()
207 ->ToggleIgnoreExternalKeyboard();
208 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
209 ASSERT_TRUE(notified());
210 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
211 // Toggle show keyboard. Keyboard should be hidden.
212 ResetObserver();
213 Shell::GetInstance()
214 ->virtual_keyboard_controller()
215 ->ToggleIgnoreExternalKeyboard();
216 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
217 ASSERT_TRUE(notified());
218 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
219 // Remove external keyboard. Should be notified that the keyboard is not
220 // suppressed.
221 ResetObserver();
222 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
223 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
224 ASSERT_TRUE(notified());
225 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
228 // Tests handling multiple keyboards. Catches crbug.com/430252
229 TEST_F(VirtualKeyboardControllerAutoTest, HandleMultipleKeyboardsPresent) {
230 std::vector<ui::KeyboardDevice> keyboards;
231 keyboards.push_back(ui::KeyboardDevice(
232 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL, "keyboard"));
233 keyboards.push_back(ui::KeyboardDevice(
234 2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
235 keyboards.push_back(ui::KeyboardDevice(
236 3, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
237 UpdateKeyboardDevices(keyboards);
238 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
241 class VirtualKeyboardControllerAlwaysEnabledTest
242 : public VirtualKeyboardControllerAutoTest {
243 public:
244 VirtualKeyboardControllerAlwaysEnabledTest()
245 : VirtualKeyboardControllerAutoTest() {}
246 ~VirtualKeyboardControllerAlwaysEnabledTest() override {}
248 void SetUp() override {
249 base::CommandLine::ForCurrentProcess()->AppendSwitch(
250 keyboard::switches::kEnableVirtualKeyboard);
251 VirtualKeyboardControllerAutoTest::SetUp();
254 private:
255 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAlwaysEnabledTest);
258 // Tests that the controller cannot suppress the keyboard if the virtual
259 // keyboard always enabled flag is active.
260 TEST_F(VirtualKeyboardControllerAlwaysEnabledTest, DoesNotSuppressKeyboard) {
261 std::vector<ui::TouchscreenDevice> screens;
262 screens.push_back(
263 ui::TouchscreenDevice(1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
264 "Touchscreen", gfx::Size(1024, 768), 0));
265 UpdateTouchscreenDevices(screens);
266 std::vector<ui::KeyboardDevice> keyboards;
267 keyboards.push_back(ui::KeyboardDevice(
268 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL, "keyboard"));
269 UpdateKeyboardDevices(keyboards);
270 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
273 } // namespace test
274 } // namespace ash