1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <memory>
#include "ash/accessibility/sticky_keys/sticky_keys_controller.h"
#include "ash/accessibility/sticky_keys/sticky_keys_overlay.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/system/status_area_widget.h"
#include "ash/system/unified/unified_system_tray.h"
#include "base/run_loop.h"
#include "chrome/browser/ash/accessibility/accessibility_feature_browsertest.h"
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/automation_test_utils.h"
#include "chrome/browser/ash/accessibility/select_to_speak_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/test/browser_test.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/native_widget_types.h"
namespace ash {
class StickyKeysBrowserTest : public AccessibilityFeatureBrowserTest {
protected:
StickyKeysBrowserTest() = default;
StickyKeysBrowserTest(const StickyKeysBrowserTest&) = delete;
StickyKeysBrowserTest& operator=(const StickyKeysBrowserTest&) = delete;
~StickyKeysBrowserTest() override = default;
void SetUpOnMainThread() override {
aura::Window* root_window = Shell::Get()->GetPrimaryRootWindow();
generator_ = std::make_unique<ui::test::EventGenerator>(root_window);
Profile* profile = ProfileManager::GetActiveUserProfile();
AccessibilityFeatureBrowserTest::SetUpOnMainThread();
// Load Select to Speak so we have a Javascript context with access to the
// Automation API to inject AutomationTestUtils. Select to Speak doesn't do
// any work unless it is triggered, so this does not impact the test.
sts_test_utils::TurnOnSelectToSpeakForTest(profile);
utils_ = std::make_unique<AutomationTestUtils>(
extension_misc::kSelectToSpeakExtensionId);
utils_->SetUpTestSupport();
}
void SetStickyKeysEnabled(bool enabled) {
AccessibilityManager::Get()->EnableStickyKeys(enabled);
// Spin the message loop to ensure ash sees the change.
base::RunLoop().RunUntilIdle();
}
bool IsSystemTrayBubbleOpen() {
return Shell::Get()
->GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->unified_system_tray()
->IsBubbleShown();
}
void CloseSystemTrayBubble() {
Shell::Get()
->GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->unified_system_tray()
->CloseBubble();
}
void SendKeyPress(ui::KeyboardCode key) {
ui::test::EmulateFullKeyPressReleaseSequence(
generator_.get(), key,
/*control=*/false, /*shift=*/false, /*alt=*/false, /*command=*/false);
}
std::unique_ptr<ui::test::EventGenerator> generator_;
std::unique_ptr<AutomationTestUtils> utils_;
};
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenTrayMenu) {
SetStickyKeysEnabled(true);
// Open system tray bubble with shortcut.
SendKeyPress(ui::VKEY_MENU); // alt key.
SendKeyPress(ui::VKEY_SHIFT);
SendKeyPress(ui::VKEY_S);
EXPECT_TRUE(IsSystemTrayBubbleOpen());
// Hide system bubble.
CloseSystemTrayBubble();
EXPECT_FALSE(IsSystemTrayBubbleOpen());
// Pressing S again should not reopen the bubble.
SendKeyPress(ui::VKEY_S);
EXPECT_FALSE(IsSystemTrayBubbleOpen());
// With sticky keys disabled, we will fail to perform the shortcut.
SetStickyKeysEnabled(false);
SendKeyPress(ui::VKEY_MENU); // alt key.
SendKeyPress(ui::VKEY_SHIFT);
SendKeyPress(ui::VKEY_S);
EXPECT_FALSE(IsSystemTrayBubbleOpen());
}
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenNewTabs) {
// Lock the modifier key.
SetStickyKeysEnabled(true);
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_CONTROL);
// In the locked state, pressing 't' should open a new tab each time.
for (int tab_count = 1; tab_count < 5; ++tab_count) {
SendKeyPress(ui::VKEY_T);
utils_->WaitForNumTabsWithRegexName(tab_count, "/New Tab*/");
}
// Unlock the modifier key and shortcut should no longer activate.
// Instead, the omnibox is populated with the letter 't'.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
utils_->WaitForNodeWithClassNameAndValue("OmniboxViewViews", "t");
// Shortcut should not work after disabling sticky keys.
// Instead, another 't' is typed in the omnibox.
SetStickyKeysEnabled(false);
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
utils_->WaitForNodeWithClassNameAndValue("OmniboxViewViews", "tt");
}
// Flaky. https://crbug.com/331433886.
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, DISABLED_CtrlClickHomeButton) {
// Show home page button.
browser()->profile()->GetPrefs()->SetBoolean(prefs::kShowHomeButton, true);
TabStripModel* tab_strip_model = browser()->tab_strip_model();
int tab_count = 1;
EXPECT_EQ(tab_count, tab_strip_model->count());
// Test sticky keys with modified mouse click action.
SetStickyKeysEnabled(true);
SendKeyPress(ui::VKEY_CONTROL);
ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
EXPECT_EQ(++tab_count, tab_strip_model->count());
ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
EXPECT_EQ(tab_count, tab_strip_model->count());
// Test locked modifier key with mouse click.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_CONTROL);
for (; tab_count < 5; ++tab_count) {
EXPECT_EQ(tab_count, tab_strip_model->count());
ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
}
SendKeyPress(ui::VKEY_CONTROL);
ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
EXPECT_EQ(tab_count, tab_strip_model->count());
// Test disabling sticky keys prevent modified mouse click.
SetStickyKeysEnabled(false);
SendKeyPress(ui::VKEY_CONTROL);
ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
EXPECT_EQ(tab_count, tab_strip_model->count());
}
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, SearchLeftOmnibox) {
SetStickyKeysEnabled(true);
// Give omnibox focus by opening a new tab with ctrl+t.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
utils_->WaitForNumTabsWithRegexName(1, "/New Tab*/");
// Type 'foo'.
SendKeyPress(ui::VKEY_F);
SendKeyPress(ui::VKEY_O);
SendKeyPress(ui::VKEY_O);
utils_->WaitForNodeWithClassNameAndValue("OmniboxViewViews", "foo");
// Hit Home by sequencing Search (left Windows) and Left (arrow).
SendKeyPress(ui::VKEY_LWIN);
SendKeyPress(ui::VKEY_LEFT);
// Verify caret moved to the beginning by typing something else, this
// should appear before "foo" in the omnibox.
SendKeyPress(ui::VKEY_B);
SendKeyPress(ui::VKEY_A);
SendKeyPress(ui::VKEY_R);
utils_->WaitForNodeWithClassNameAndValue("OmniboxViewViews", "barfoo");
}
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OverlayShown) {
const ui::KeyboardCode modifier_keys[] = {ui::VKEY_CONTROL, ui::VKEY_SHIFT,
ui::VKEY_MENU, ui::VKEY_COMMAND};
// Overlay should not be visible if sticky keys is not enabled.
StickyKeysController* controller = Shell::Get()->sticky_keys_controller();
EXPECT_FALSE(controller->GetOverlayForTest());
for (auto key_code : modifier_keys) {
SendKeyPress(key_code);
EXPECT_FALSE(controller->GetOverlayForTest());
}
// Cycle through the modifier keys and make sure each gets shown.
SetStickyKeysEnabled(true);
StickyKeysOverlay* sticky_keys_overlay = controller->GetOverlayForTest();
for (auto key_code : modifier_keys) {
SendKeyPress(key_code);
EXPECT_TRUE(sticky_keys_overlay->is_visible());
SendKeyPress(key_code);
EXPECT_TRUE(sticky_keys_overlay->is_visible());
SendKeyPress(key_code);
EXPECT_FALSE(sticky_keys_overlay->is_visible());
}
// Disabling sticky keys should hide the overlay.
SendKeyPress(ui::VKEY_CONTROL);
EXPECT_TRUE(sticky_keys_overlay->is_visible());
SetStickyKeysEnabled(false);
EXPECT_FALSE(controller->GetOverlayForTest());
for (auto key_code : modifier_keys) {
SendKeyPress(key_code);
EXPECT_FALSE(controller->GetOverlayForTest());
}
}
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenIncognitoWindow) {
SetStickyKeysEnabled(true);
StickyKeysOverlay* overlay =
Shell::Get()->sticky_keys_controller()->GetOverlayForTest();
ASSERT_TRUE(overlay);
// Overlay is shown on first modifier key press.
EXPECT_FALSE(overlay->is_visible());
SendKeyPress(ui::VKEY_SHIFT);
EXPECT_TRUE(overlay->is_visible());
SendKeyPress(ui::VKEY_CONTROL);
EXPECT_TRUE(overlay->is_visible());
SendKeyPress(ui::VKEY_N);
EXPECT_FALSE(overlay->is_visible());
utils_->WaitForNumTabsWithRegexName(1, "/New Incognito Tab*/");
}
IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, CyclesWindows) {
SetStickyKeysEnabled(true);
// Ensure there is a normal browser window open with ctrl+t.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
int expected_tabs = 1;
utils_->WaitForNumTabsWithRegexName(expected_tabs, "/New Tab*/");
// Open an incognito browser.
SendKeyPress(ui::VKEY_SHIFT);
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_N);
utils_->WaitForNumTabsWithRegexName(1, "/New Incognito Tab*/");
// Ctrl+t opens another incognito tab because the incognito window is focused.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
utils_->WaitForNumTabsWithRegexName(2, "/New Incognito Tab*/");
// Cycle between windows.
SendKeyPress(ui::VKEY_MENU); // alt key.
SendKeyPress(ui::VKEY_TAB);
// Ctrl+t opens another non-incognito tab now, because the normal browser
// window is focused.
SendKeyPress(ui::VKEY_CONTROL);
SendKeyPress(ui::VKEY_T);
expected_tabs++;
utils_->WaitForNumTabsWithRegexName(expected_tabs, "/New Tab*/");
}
} // namespace ash
|