1 // Copyright 2013 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.
6 #include "ash/system/tray/system_tray.h"
7 #include "base/command_line.h"
8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/chromeos/accessibility/accessibility_manager.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/location_bar/location_bar.h"
14 #include "chrome/browser/ui/omnibox/omnibox_view.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/browser/ui/view_ids.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/interactive_test_utils.h"
20 #include "ui/aura/window_event_dispatcher.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
22 #include "ui/gfx/native_widget_types.h"
26 class StickyKeysBrowserTest : public InProcessBrowserTest {
28 StickyKeysBrowserTest() {}
29 virtual ~StickyKeysBrowserTest() {}
31 void EnableStickyKeys() {
32 AccessibilityManager::Get()->EnableStickyKeys(true);
35 void DisableStickyKeys() {
36 AccessibilityManager::Get()->EnableStickyKeys(false);
39 ash::SystemTray* GetSystemTray() {
40 return ash::Shell::GetInstance()->GetPrimarySystemTray();
43 void SendKeyPress(ui::KeyboardCode key) {
44 gfx::NativeWindow root_window =
45 ash::Shell::GetInstance()->GetPrimaryRootWindow();
47 ui_test_utils::SendKeyPressToWindowSync(root_window,
55 content::NotificationRegistrar registrar_;
57 DISALLOW_COPY_AND_ASSIGN(StickyKeysBrowserTest);
60 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenTrayMenu) {
63 // Open system tray bubble with shortcut.
64 SendKeyPress(ui::VKEY_MENU); // alt key.
65 SendKeyPress(ui::VKEY_SHIFT);
66 SendKeyPress(ui::VKEY_S);
67 EXPECT_TRUE(GetSystemTray()->HasSystemBubble());
69 // Hide system bubble.
70 GetSystemTray()->CloseSystemBubble();
71 EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
73 // Pressing S again should not reopen the bubble.
74 SendKeyPress(ui::VKEY_S);
75 EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
77 // With sticky keys disabled, we will fail to perform the shortcut.
79 SendKeyPress(ui::VKEY_MENU); // alt key.
80 SendKeyPress(ui::VKEY_SHIFT);
81 SendKeyPress(ui::VKEY_S);
82 EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
85 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenNewTabs) {
86 // Lock the modifier key.
88 SendKeyPress(ui::VKEY_CONTROL);
89 SendKeyPress(ui::VKEY_CONTROL);
91 // In the locked state, pressing 't' should open a new tab each time.
92 TabStripModel* tab_strip_model = browser()->tab_strip_model();
94 for (; tab_count < 5; ++tab_count) {
95 EXPECT_EQ(tab_count, tab_strip_model->count());
96 SendKeyPress(ui::VKEY_T);
99 // Unlock the modifier key and shortcut should no longer activate.
100 SendKeyPress(ui::VKEY_CONTROL);
101 SendKeyPress(ui::VKEY_T);
102 EXPECT_EQ(tab_count, tab_strip_model->count());
104 // Shortcut should not work after disabling sticky keys.
106 SendKeyPress(ui::VKEY_CONTROL);
107 SendKeyPress(ui::VKEY_CONTROL);
108 SendKeyPress(ui::VKEY_T);
109 EXPECT_EQ(tab_count, tab_strip_model->count());
112 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, CtrlClickHomeButton) {
113 // Show home page button.
114 browser()->profile()->GetPrefs()->SetBoolean(prefs::kShowHomeButton, true);
115 TabStripModel* tab_strip_model = browser()->tab_strip_model();
117 EXPECT_EQ(tab_count, tab_strip_model->count());
119 // Test sticky keys with modified mouse click action.
121 SendKeyPress(ui::VKEY_CONTROL);
122 ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
123 EXPECT_EQ(++tab_count, tab_strip_model->count());
124 ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
125 EXPECT_EQ(tab_count, tab_strip_model->count());
127 // Test locked modifier key with mouse click.
128 SendKeyPress(ui::VKEY_CONTROL);
129 SendKeyPress(ui::VKEY_CONTROL);
130 for (; tab_count < 5; ++tab_count) {
131 EXPECT_EQ(tab_count, tab_strip_model->count());
132 ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
134 SendKeyPress(ui::VKEY_CONTROL);
135 ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
136 EXPECT_EQ(tab_count, tab_strip_model->count());
138 // Test disabling sticky keys prevent modified mouse click.
140 SendKeyPress(ui::VKEY_CONTROL);
141 ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
142 EXPECT_EQ(tab_count, tab_strip_model->count());
145 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, SearchLeftOmnibox) {
148 OmniboxView* omnibox =
149 browser()->window()->GetLocationBar()->GetOmniboxView();
151 // Give the omnibox focus.
155 SendKeyPress(ui::VKEY_F);
156 SendKeyPress(ui::VKEY_O);
157 SendKeyPress(ui::VKEY_O);
159 // Verify the location of the caret.
161 omnibox->GetSelectionBounds(&start, &end);
162 ASSERT_EQ(3U, start);
165 // Hit Home by sequencing Search (left Windows) and Left (arrow).
166 SendKeyPress(ui::VKEY_LWIN);
167 SendKeyPress(ui::VKEY_LEFT);
169 // Verify caret moved to the beginning.
170 omnibox->GetSelectionBounds(&start, &end);
171 ASSERT_EQ(0U, start);
175 } // namespace chromeos