blob: dc45cc1413f4feeeff2d162441b667bbc1714822 [file] [log] [blame]
bsheeaae09a2014-09-22 23:16:521// 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.
4
5#include "ash/virtual_keyboard_controller.h"
6
7#include "ash/shell.h"
rsadamc727b6a2014-10-31 18:59:338#include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_observer.h"
bsheeaae09a2014-09-22 23:16:529#include "ash/test/ash_test_base.h"
10#include "ash/wm/maximize_mode/maximize_mode_controller.h"
rsadam9172bc8a2014-10-29 23:37:3811#include "base/command_line.h"
dnicoara78a734702014-11-04 19:54:3812#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"
rsadam9172bc8a2014-10-29 23:37:3817#include "ui/keyboard/keyboard_export.h"
18#include "ui/keyboard/keyboard_switches.h"
bsheeaae09a2014-09-22 23:16:5219#include "ui/keyboard/keyboard_util.h"
20
21namespace ash {
22namespace test {
23
rsadam9172bc8a2014-10-29 23:37:3824class VirtualKeyboardControllerTest : public AshTestBase {
25 public:
26 VirtualKeyboardControllerTest() {}
27 virtual ~VirtualKeyboardControllerTest() {}
bsheeaae09a2014-09-22 23:16:5228
rsadam9172bc8a2014-10-29 23:37:3829 void UpdateTouchscreenDevices(
30 std::vector<ui::TouchscreenDevice> touchscreen_devices) {
31 ui::DeviceHotplugEventObserver* manager =
32 ui::DeviceDataManager::GetInstance();
33 manager->OnTouchscreenDevicesUpdated(touchscreen_devices);
34 }
35
36 void UpdateKeyboardDevices(std::vector<ui::KeyboardDevice> keyboard_devices) {
37 ui::DeviceHotplugEventObserver* manager =
38 ui::DeviceDataManager::GetInstance();
39 manager->OnKeyboardDevicesUpdated(keyboard_devices);
40 }
41
42 void SetUp() override {
43 AshTestBase::SetUp();
44 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
45 UpdateTouchscreenDevices(std::vector<ui::TouchscreenDevice>());
46 }
47
48 private:
49 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerTest);
50};
51
bsheeaae09a2014-09-22 23:16:5252TEST_F(VirtualKeyboardControllerTest, EnabledDuringMaximizeMode) {
53 ASSERT_FALSE(keyboard::IsKeyboardEnabled());
rsadam9172bc8a2014-10-29 23:37:3854 // Toggle maximized mode on.
55 Shell::GetInstance()
56 ->maximize_mode_controller()
57 ->EnableMaximizeModeWindowManager(true);
bsheeaae09a2014-09-22 23:16:5258 EXPECT_TRUE(keyboard::IsKeyboardEnabled());
rsadam9172bc8a2014-10-29 23:37:3859 // Toggle maximized mode off.
60 Shell::GetInstance()
61 ->maximize_mode_controller()
62 ->EnableMaximizeModeWindowManager(false);
63 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
64}
65
rsadamc727b6a2014-10-31 18:59:3366class VirtualKeyboardControllerAutoTest : public VirtualKeyboardControllerTest,
67 public VirtualKeyboardObserver {
rsadam9172bc8a2014-10-29 23:37:3868 public:
rsadamc727b6a2014-10-31 18:59:3369 VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {}
70 ~VirtualKeyboardControllerAutoTest() override {}
71
rsadam9172bc8a2014-10-29 23:37:3872 void SetUp() override {
73 CommandLine::ForCurrentProcess()->AppendSwitch(
74 keyboard::switches::kAutoVirtualKeyboard);
75 VirtualKeyboardControllerTest::SetUp();
rsadamc727b6a2014-10-31 18:59:3376 Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver(
77 this);
rsadam9172bc8a2014-10-29 23:37:3878 }
rsadamc727b6a2014-10-31 18:59:3379
80 void TearDown() override {
81 Shell::GetInstance()->system_tray_notifier()->RemoveVirtualKeyboardObserver(
82 this);
83 AshTestBase::TearDown();
84 }
85
86 void OnKeyboardSuppressionChanged(bool suppressed) override {
87 notified_ = true;
88 suppressed_ = suppressed;
89 }
90
91 void ResetObserver() {
92 suppressed_ = false;
93 notified_ = false;
94 }
95
96 bool IsVirtualKeyboardSuppressed() { return suppressed_; }
97
98 bool notified() { return notified_; }
99
100 private:
101 // Whether the observer method was called.
102 bool notified_;
103
104 // Whether the keeyboard is suppressed.
105 bool suppressed_;
106
107 DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardControllerAutoTest);
rsadam9172bc8a2014-10-29 23:37:38108};
109
110// Tests that the onscreen keyboard is disabled if an internal keyboard is
111// present.
112TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfInternalKeyboardPresent) {
113 std::vector<ui::TouchscreenDevice> screens;
114 screens.push_back(
115 ui::TouchscreenDevice(1,
116 ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
117 "Touchscreen",
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());
131}
132
133TEST_F(VirtualKeyboardControllerAutoTest, DisabledIfNoTouchScreen) {
134 std::vector<ui::TouchscreenDevice> devices;
135 // Add a touchscreen. Keyboard should deploy.
136 devices.push_back(
137 ui::TouchscreenDevice(1,
138 ui::InputDeviceType::INPUT_DEVICE_EXTERNAL,
139 "Touchscreen",
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>());
bsheeaae09a2014-09-22 23:16:52145 EXPECT_FALSE(keyboard::IsKeyboardEnabled());
146}
147
rsadamc727b6a2014-10-31 18:59:33148TEST_F(VirtualKeyboardControllerAutoTest, SuppressedIfExternalKeyboardPresent) {
149 std::vector<ui::TouchscreenDevice> screens;
150 screens.push_back(
151 ui::TouchscreenDevice(1,
152 ui::InputDeviceType::INPUT_DEVICE_INTERNAL,
153 "Touchscreen",
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.
164 ResetObserver();
165 Shell::GetInstance()
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.
172 ResetObserver();
173 Shell::GetInstance()
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
180 // suppressed.
181 ResetObserver();
182 UpdateKeyboardDevices(std::vector<ui::KeyboardDevice>());
183 ASSERT_TRUE(keyboard::IsKeyboardEnabled());
184 ASSERT_TRUE(notified());
185 ASSERT_FALSE(IsVirtualKeyboardSuppressed());
186}
187
rsadamf9719972014-11-05 02:57:59188// Tests handling multiple keyboards. Catches crbug.com/430252
189TEST_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());
199}
200
bsheeaae09a2014-09-22 23:16:52201} // namespace test
202} // namespace ash