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"
8 #include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_observer.h"
9 #include "ash/test/ash_test_base.h"
10 #include "ash/wm/maximize_mode/maximize_mode_controller.h"
11 #include "base/command_line.h"
12 #include "ui/events/devices/device_data_manager.h"
13 #include "ui/events/devices/device_hotplug_event_observer.h"
14 #include "ui/events/devices/input_device.h"
15 #include "ui/events/devices/keyboard_device.h"
16 #include "ui/events/devices/touchscreen_device.h"
17 #include "ui/keyboard/keyboard_export.h"
18 #include "ui/keyboard/keyboard_switches.h"
19 #include "ui/keyboard/keyboard_util.h"
24 class VirtualKeyboardControllerTest
: public AshTestBase
{
26 VirtualKeyboardControllerTest() {}
27 virtual ~VirtualKeyboardControllerTest() {}
29 void UpdateTouchscreenDevices(
30 std::vector
<ui::TouchscreenDevice
> touchscreen_devices
) {
31 ui::DeviceHotplugEventObserver
* manager
=
32 ui::DeviceDataManager::GetInstance();
33 manager
->OnTouchscreenDevicesUpdated(touchscreen_devices
);
36 void UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
> keyboard_devices
) {
37 ui::DeviceHotplugEventObserver
* manager
=
38 ui::DeviceDataManager::GetInstance();
39 manager
->OnKeyboardDevicesUpdated(keyboard_devices
);
42 void SetUp() override
{
44 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
45 UpdateTouchscreenDevices(std::vector
<ui::TouchscreenDevice
>());
49 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest
);
52 TEST_F(VirtualKeyboardControllerTest
, EnabledDuringMaximizeMode
) {
53 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
54 // Toggle maximized mode on.
56 ->maximize_mode_controller()
57 ->EnableMaximizeModeWindowManager(true);
58 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
59 // Toggle maximized mode off.
61 ->maximize_mode_controller()
62 ->EnableMaximizeModeWindowManager(false);
63 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
66 class VirtualKeyboardControllerAutoTest
: public VirtualKeyboardControllerTest
,
67 public VirtualKeyboardObserver
{
69 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
70 ~VirtualKeyboardControllerAutoTest() override
{}
72 void SetUp() override
{
73 CommandLine::ForCurrentProcess()->AppendSwitch(
74 keyboard::switches::kAutoVirtualKeyboard
);
75 VirtualKeyboardControllerTest::SetUp();
76 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver(
80 void TearDown() override
{
81 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver(
83 AshTestBase::TearDown();
86 void OnKeyboardSuppressionChanged(bool suppressed
) override
{
88 suppressed_
= suppressed
;
91 void ResetObserver() {
96 bool IsVirtualKeyboardSuppressed() { return suppressed_
; }
98 bool notified() { return notified_
; }
101 // Whether the observer method was called.
104 // Whether the keeyboard is suppressed.
107 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAutoTest
);
110 // Tests that the onscreen keyboard is disabled if an internal keyboard is
112 TEST_F(VirtualKeyboardControllerAutoTest
, DisabledIfInternalKeyboardPresent
) {
113 std::vector
<ui::TouchscreenDevice
> screens
;
115 ui::TouchscreenDevice(1,
116 ui::InputDeviceType::INPUT_DEVICE_INTERNAL
,
118 gfx::Size(1024, 768)));
119 UpdateTouchscreenDevices(screens
);
120 std::vector
<ui::KeyboardDevice
> keyboards
;
121 keyboards
.push_back(ui::KeyboardDevice(
122 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "keyboard"));
123 UpdateKeyboardDevices(keyboards
);
124 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
125 // Remove the internal keyboard. Virtual keyboard should now show.
126 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
127 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
128 // Replug in the internal keyboard. Virtual keyboard should hide.
129 UpdateKeyboardDevices(keyboards
);
130 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
133 TEST_F(VirtualKeyboardControllerAutoTest
, DisabledIfNoTouchScreen
) {
134 std::vector
<ui::TouchscreenDevice
> devices
;
135 // Add a touchscreen. Keyboard should deploy.
137 ui::TouchscreenDevice(1,
138 ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
,
140 gfx::Size(800, 600)));
141 UpdateTouchscreenDevices(devices
);
142 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
143 // Remove touchscreen. Keyboard should hide.
144 UpdateTouchscreenDevices(std::vector
<ui::TouchscreenDevice
>());
145 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
148 TEST_F(VirtualKeyboardControllerAutoTest
, SuppressedIfExternalKeyboardPresent
) {
149 std::vector
<ui::TouchscreenDevice
> screens
;
151 ui::TouchscreenDevice(1,
152 ui::InputDeviceType::INPUT_DEVICE_INTERNAL
,
154 gfx::Size(1024, 768)));
155 UpdateTouchscreenDevices(screens
);
156 std::vector
<ui::KeyboardDevice
> keyboards
;
157 keyboards
.push_back(ui::KeyboardDevice(
158 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
159 UpdateKeyboardDevices(keyboards
);
160 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
161 ASSERT_TRUE(notified());
162 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
163 // Toggle show keyboard. Keyboard should be visible.
166 ->virtual_keyboard_controller()
167 ->ToggleIgnoreExternalKeyboard();
168 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
169 ASSERT_TRUE(notified());
170 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
171 // Toggle show keyboard. Keyboard should be hidden.
174 ->virtual_keyboard_controller()
175 ->ToggleIgnoreExternalKeyboard();
176 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
177 ASSERT_TRUE(notified());
178 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
179 // Remove external keyboard. Should be notified that the keyboard is not
182 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
183 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
184 ASSERT_TRUE(notified());
185 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
188 // Tests handling multiple keyboards. Catches crbug.com/430252
189 TEST_F(VirtualKeyboardControllerAutoTest
, HandleMultipleKeyboardsPresent
) {
190 std::vector
<ui::KeyboardDevice
> keyboards
;
191 keyboards
.push_back(ui::KeyboardDevice(
192 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "keyboard"));
193 keyboards
.push_back(ui::KeyboardDevice(
194 2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
195 keyboards
.push_back(ui::KeyboardDevice(
196 3, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
197 UpdateKeyboardDevices(keyboards
);
198 ASSERT_FALSE(keyboard::IsKeyboardEnabled());