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
{
43 base::CommandLine::ForCurrentProcess()->AppendSwitch(
44 keyboard::switches::kDisableSmartVirtualKeyboard
);
46 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
47 UpdateTouchscreenDevices(std::vector
<ui::TouchscreenDevice
>());
51 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest
);
54 TEST_F(VirtualKeyboardControllerTest
, EnabledDuringMaximizeMode
) {
55 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
56 // Toggle maximized mode on.
58 ->maximize_mode_controller()
59 ->EnableMaximizeModeWindowManager(true);
60 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
61 // Toggle maximized mode off.
63 ->maximize_mode_controller()
64 ->EnableMaximizeModeWindowManager(false);
65 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
68 class VirtualKeyboardControllerAutoTest
: public VirtualKeyboardControllerTest
,
69 public VirtualKeyboardObserver
{
71 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
72 ~VirtualKeyboardControllerAutoTest() override
{}
74 void SetUp() override
{
76 // Set the current list of devices to empty so that they don't interfere
78 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
79 UpdateTouchscreenDevices(std::vector
<ui::TouchscreenDevice
>());
80 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver(
84 void TearDown() override
{
85 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver(
87 AshTestBase::TearDown();
90 void OnKeyboardSuppressionChanged(bool suppressed
) override
{
92 suppressed_
= suppressed
;
95 void ResetObserver() {
100 bool IsVirtualKeyboardSuppressed() { return suppressed_
; }
102 bool notified() { return notified_
; }
105 // Whether the observer method was called.
108 // Whether the keeyboard is suppressed.
111 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAutoTest
);
114 // Tests that the onscreen keyboard is disabled if an internal keyboard is
116 TEST_F(VirtualKeyboardControllerAutoTest
, DisabledIfInternalKeyboardPresent
) {
117 std::vector
<ui::TouchscreenDevice
> screens
;
119 ui::TouchscreenDevice(1,
120 ui::InputDeviceType::INPUT_DEVICE_INTERNAL
,
122 gfx::Size(1024, 768)));
123 UpdateTouchscreenDevices(screens
);
124 std::vector
<ui::KeyboardDevice
> keyboards
;
125 keyboards
.push_back(ui::KeyboardDevice(
126 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "keyboard"));
127 UpdateKeyboardDevices(keyboards
);
128 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
129 // Remove the internal keyboard. Virtual keyboard should now show.
130 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
131 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
132 // Replug in the internal keyboard. Virtual keyboard should hide.
133 UpdateKeyboardDevices(keyboards
);
134 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
137 TEST_F(VirtualKeyboardControllerAutoTest
, DisabledIfNoTouchScreen
) {
138 std::vector
<ui::TouchscreenDevice
> devices
;
139 // Add a touchscreen. Keyboard should deploy.
141 ui::TouchscreenDevice(1,
142 ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
,
144 gfx::Size(800, 600)));
145 UpdateTouchscreenDevices(devices
);
146 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
147 // Remove touchscreen. Keyboard should hide.
148 UpdateTouchscreenDevices(std::vector
<ui::TouchscreenDevice
>());
149 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
152 TEST_F(VirtualKeyboardControllerAutoTest
, SuppressedIfExternalKeyboardPresent
) {
153 std::vector
<ui::TouchscreenDevice
> screens
;
155 ui::TouchscreenDevice(1,
156 ui::InputDeviceType::INPUT_DEVICE_INTERNAL
,
158 gfx::Size(1024, 768)));
159 UpdateTouchscreenDevices(screens
);
160 std::vector
<ui::KeyboardDevice
> keyboards
;
161 keyboards
.push_back(ui::KeyboardDevice(
162 1, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
163 UpdateKeyboardDevices(keyboards
);
164 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
165 ASSERT_TRUE(notified());
166 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
167 // Toggle show keyboard. Keyboard should be visible.
170 ->virtual_keyboard_controller()
171 ->ToggleIgnoreExternalKeyboard();
172 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
173 ASSERT_TRUE(notified());
174 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
175 // Toggle show keyboard. Keyboard should be hidden.
178 ->virtual_keyboard_controller()
179 ->ToggleIgnoreExternalKeyboard();
180 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
181 ASSERT_TRUE(notified());
182 ASSERT_TRUE(IsVirtualKeyboardSuppressed());
183 // Remove external keyboard. Should be notified that the keyboard is not
186 UpdateKeyboardDevices(std::vector
<ui::KeyboardDevice
>());
187 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
188 ASSERT_TRUE(notified());
189 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
192 // Tests handling multiple keyboards. Catches crbug.com/430252
193 TEST_F(VirtualKeyboardControllerAutoTest
, HandleMultipleKeyboardsPresent
) {
194 std::vector
<ui::KeyboardDevice
> keyboards
;
195 keyboards
.push_back(ui::KeyboardDevice(
196 1, ui::InputDeviceType::INPUT_DEVICE_INTERNAL
, "keyboard"));
197 keyboards
.push_back(ui::KeyboardDevice(
198 2, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
199 keyboards
.push_back(ui::KeyboardDevice(
200 3, ui::InputDeviceType::INPUT_DEVICE_EXTERNAL
, "keyboard"));
201 UpdateKeyboardDevices(keyboards
);
202 ASSERT_FALSE(keyboard::IsKeyboardEnabled());