bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ash/virtual_keyboard_controller.h" |
| 6 | |
| 7 | #include "ash/shell.h" |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 8 | #include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_observer.h" |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 9 | #include "ash/test/ash_test_base.h" |
| 10 | #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 11 | #include "base/command_line.h" |
dnicoara | 78a73470 | 2014-11-04 19:54:38 | [diff] [blame] | 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" |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 17 | #include "ui/keyboard/keyboard_export.h" |
| 18 | #include "ui/keyboard/keyboard_switches.h" |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 19 | #include "ui/keyboard/keyboard_util.h" |
| 20 | |
| 21 | namespace ash { |
| 22 | namespace test { |
| 23 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 24 | class VirtualKeyboardControllerTest : public AshTestBase { |
| 25 | public: |
| 26 | VirtualKeyboardControllerTest() {} |
| 27 | virtual ~VirtualKeyboardControllerTest() {} |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 28 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 29 | 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 | |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 52 | TEST_F(VirtualKeyboardControllerTest, EnabledDuringMaximizeMode) { |
| 53 | ASSERT_FALSE(keyboard::IsKeyboardEnabled()); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 54 | // Toggle maximized mode on. |
| 55 | Shell::GetInstance() |
| 56 | ->maximize_mode_controller() |
| 57 | ->EnableMaximizeModeWindowManager(true); |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 58 | EXPECT_TRUE(keyboard::IsKeyboardEnabled()); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 59 | // Toggle maximized mode off. |
| 60 | Shell::GetInstance() |
| 61 | ->maximize_mode_controller() |
| 62 | ->EnableMaximizeModeWindowManager(false); |
| 63 | EXPECT_FALSE(keyboard::IsKeyboardEnabled()); |
| 64 | } |
| 65 | |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 66 | class VirtualKeyboardControllerAutoTest : public VirtualKeyboardControllerTest, |
| 67 | public VirtualKeyboardObserver { |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 68 | public: |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 69 | VirtualKeyboardControllerAutoTest() : notified_(false), suppressed_(false) {} |
| 70 | ~VirtualKeyboardControllerAutoTest() override {} |
| 71 | |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 72 | void SetUp() override { |
| 73 | CommandLine::ForCurrentProcess()->AppendSwitch( |
| 74 | keyboard::switches::kAutoVirtualKeyboard); |
| 75 | VirtualKeyboardControllerTest::SetUp(); |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 76 | Shell::GetInstance()->system_tray_notifier()->AddVirtualKeyboardObserver( |
| 77 | this); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 78 | } |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 79 | |
| 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); |
rsadam | 9172bc8a | 2014-10-29 23:37:38 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | // Tests that the onscreen keyboard is disabled if an internal keyboard is |
| 111 | // present. |
| 112 | TEST_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 | |
| 133 | TEST_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>()); |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 145 | EXPECT_FALSE(keyboard::IsKeyboardEnabled()); |
| 146 | } |
| 147 | |
rsadam | c727b6a | 2014-10-31 18:59:33 | [diff] [blame] | 148 | TEST_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 | |
rsadam | f971997 | 2014-11-05 02:57:59 | [diff] [blame] | 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()); |
| 199 | } |
| 200 | |
bshe | eaae09a | 2014-09-22 23:16:52 | [diff] [blame] | 201 | } // namespace test |
| 202 | } // namespace ash |