a57006f97a981c4b153a24eb465d3b844637fd1c
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / accessibility / sticky_keys_browsertest.cc
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.
4
5 #include "ash/shell.h"
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"
23
24 namespace chromeos {
25
26 class StickyKeysBrowserTest : public InProcessBrowserTest {
27  protected:
28   StickyKeysBrowserTest() {}
29   virtual ~StickyKeysBrowserTest() {}
30
31   void EnableStickyKeys() {
32     AccessibilityManager::Get()->EnableStickyKeys(true);
33   }
34
35   void DisableStickyKeys() {
36     AccessibilityManager::Get()->EnableStickyKeys(false);
37   }
38
39   ash::SystemTray* GetSystemTray() {
40     return ash::Shell::GetInstance()->GetPrimarySystemTray();
41   }
42
43   void SendKeyPress(ui::KeyboardCode key) {
44     gfx::NativeWindow root_window =
45         ash::Shell::GetInstance()->GetPrimaryRootWindow();
46     ASSERT_TRUE(
47         ui_test_utils::SendKeyPressToWindowSync(root_window,
48                                                 key,
49                                                 false, // control
50                                                 false, // shift
51                                                 false, // alt
52                                                 false)); // command
53   }
54
55   content::NotificationRegistrar registrar_;
56
57   DISALLOW_COPY_AND_ASSIGN(StickyKeysBrowserTest);
58 };
59
60 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenTrayMenu) {
61   EnableStickyKeys();
62
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());
68
69   // Hide system bubble.
70   GetSystemTray()->CloseSystemBubble();
71   EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
72
73   // Pressing S again should not reopen the bubble.
74   SendKeyPress(ui::VKEY_S);
75   EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
76
77   // With sticky keys disabled, we will fail to perform the shortcut.
78   DisableStickyKeys();
79   SendKeyPress(ui::VKEY_MENU); // alt key.
80   SendKeyPress(ui::VKEY_SHIFT);
81   SendKeyPress(ui::VKEY_S);
82   EXPECT_FALSE(GetSystemTray()->HasSystemBubble());
83 }
84
85 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, OpenNewTabs) {
86   // Lock the modifier key.
87   EnableStickyKeys();
88   SendKeyPress(ui::VKEY_CONTROL);
89   SendKeyPress(ui::VKEY_CONTROL);
90
91   // In the locked state, pressing 't' should open a new tab each time.
92   TabStripModel* tab_strip_model = browser()->tab_strip_model();
93   int tab_count = 1;
94   for (; tab_count < 5; ++tab_count) {
95     EXPECT_EQ(tab_count, tab_strip_model->count());
96     SendKeyPress(ui::VKEY_T);
97   }
98
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());
103
104   // Shortcut should not work after disabling sticky keys.
105   DisableStickyKeys();
106   SendKeyPress(ui::VKEY_CONTROL);
107   SendKeyPress(ui::VKEY_CONTROL);
108   SendKeyPress(ui::VKEY_T);
109   EXPECT_EQ(tab_count, tab_strip_model->count());
110 }
111
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();
116   int tab_count = 1;
117   EXPECT_EQ(tab_count, tab_strip_model->count());
118
119   // Test sticky keys with modified mouse click action.
120   EnableStickyKeys();
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());
126
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);
133   }
134   SendKeyPress(ui::VKEY_CONTROL);
135   ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
136   EXPECT_EQ(tab_count, tab_strip_model->count());
137
138   // Test disabling sticky keys prevent modified mouse click.
139   DisableStickyKeys();
140   SendKeyPress(ui::VKEY_CONTROL);
141   ui_test_utils::ClickOnView(browser(), VIEW_ID_HOME_BUTTON);
142   EXPECT_EQ(tab_count, tab_strip_model->count());
143 }
144
145 IN_PROC_BROWSER_TEST_F(StickyKeysBrowserTest, SearchLeftOmnibox) {
146   EnableStickyKeys();
147
148   OmniboxView* omnibox =
149       browser()->window()->GetLocationBar()->GetOmniboxView();
150
151   // Give the omnibox focus.
152   omnibox->ShowURL();
153
154   // Type 'foo'.
155   SendKeyPress(ui::VKEY_F);
156   SendKeyPress(ui::VKEY_O);
157   SendKeyPress(ui::VKEY_O);
158
159   // Verify the location of the caret.
160   size_t start, end;
161   omnibox->GetSelectionBounds(&start, &end);
162   ASSERT_EQ(3U, start);
163   ASSERT_EQ(3U, end);
164
165   // Hit Home by sequencing Search (left Windows) and Left (arrow).
166   SendKeyPress(ui::VKEY_LWIN);
167   SendKeyPress(ui::VKEY_LEFT);
168
169   // Verify caret moved to the beginning.
170   omnibox->GetSelectionBounds(&start, &end);
171   ASSERT_EQ(0U, start);
172   ASSERT_EQ(0U, end);
173 }
174
175 }  // namespace chromeos