OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_apitest.h" | 5 #include "chrome/browser/extensions/extension_apitest.h" |
6 #include "chrome/browser/extensions/window_controller.h" | 6 #include "chrome/browser/extensions/window_controller.h" |
7 #include "chrome/browser/ui/browser_window.h" | 7 #include "chrome/browser/ui/browser_window.h" |
8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
9 #include "chrome/test/base/interactive_test_utils.h" | 9 #include "chrome/test/base/interactive_test_utils.h" |
10 #include "content/public/test/browser_test_utils.h" | 10 #include "content/public/test/browser_test_utils.h" |
11 #include "ui/base/base_window.h" | 11 #include "ui/base/base_window.h" |
12 #include "ui/base/test/ui_controls.h" | 12 #include "ui/base/test/ui_controls.h" |
13 | 13 |
14 #if defined(OS_LINUX) | 14 #if defined(OS_LINUX) |
15 #include <X11/Xlib.h> | 15 #include <X11/Xlib.h> |
16 #include <X11/extensions/XTest.h> | 16 #include <X11/extensions/XTest.h> |
17 #include <X11/keysym.h> | 17 #include <X11/keysym.h> |
18 | 18 |
19 #include "ui/events/keycodes/keyboard_code_conversion_x.h" | 19 #include "ui/events/keycodes/keyboard_code_conversion_x.h" |
20 #include "ui/gfx/x/x11_types.h" | 20 #include "ui/gfx/x/x11_types.h" |
21 #endif | 21 #endif |
22 | 22 |
23 #if defined(OS_MACOSX) | |
24 #include <Carbon/Carbon.h> | |
25 #endif | |
26 | |
23 namespace extensions { | 27 namespace extensions { |
24 | 28 |
25 typedef ExtensionApiTest GlobalCommandsApiTest; | 29 typedef ExtensionApiTest GlobalCommandsApiTest; |
26 | 30 |
27 #if defined(OS_LINUX) | 31 #if defined(OS_LINUX) |
28 // Send a simulated key press and release event, where |control|, |shift| or | 32 // Send a simulated key press and release event, where |control|, |shift| or |
29 // |alt| indicates whether the key is struck with corresponding modifier. | 33 // |alt| indicates whether the key is struck with corresponding modifier. |
30 void SendNativeKeyEventToXDisplay(ui::KeyboardCode key, | 34 void SendNativeKeyEventToXDisplay(ui::KeyboardCode key, |
31 bool control, | 35 bool control, |
32 bool shift, | 36 bool shift, |
(...skipping 27 matching lines...) Expand all Loading... | |
60 XTestFakeKeyEvent(display, *it, True, CurrentTime); | 64 XTestFakeKeyEvent(display, *it, True, CurrentTime); |
61 | 65 |
62 // Simulate the keys being released. | 66 // Simulate the keys being released. |
63 for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++) | 67 for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++) |
64 XTestFakeKeyEvent(display, *it, False, CurrentTime); | 68 XTestFakeKeyEvent(display, *it, False, CurrentTime); |
65 | 69 |
66 XFlush(display); | 70 XFlush(display); |
67 } | 71 } |
68 #endif // OS_LINUX | 72 #endif // OS_LINUX |
69 | 73 |
70 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) | 74 #if defined(OS_MACOSX) |
75 // From http://goo.gl/N5AwAJ: | |
76 const int kKeyCodeCommand = 55; | |
77 const int kKeyCodeShift = 56; | |
78 | |
79 void SendNativeKeyEventMac(ui::KeyboardCode key) { | |
80 CGEventSourceRef src = | |
81 CGEventSourceCreate(kCGEventSourceStateHIDSystemState); | |
82 | |
83 CGEventRef cmd_d = CGEventCreateKeyboardEvent(src, kKeyCodeCommand, true); | |
84 CGEventRef cmd_u = CGEventCreateKeyboardEvent(src, kKeyCodeCommand, false); | |
85 CGEventRef shift_d = CGEventCreateKeyboardEvent(src, kKeyCodeShift, true); | |
86 CGEventRef shift_u = CGEventCreateKeyboardEvent(src, kKeyCodeShift, false); | |
87 CGEventRef key_d = CGEventCreateKeyboardEvent(src, key, true); | |
88 CGEventRef key_u = CGEventCreateKeyboardEvent(src, key, false); | |
89 | |
90 CGEventSetFlags(key_d, kCGEventFlagMaskCommand | kCGEventFlagMaskShift); | |
91 CGEventSetFlags(key_u, kCGEventFlagMaskCommand | kCGEventFlagMaskShift); | |
92 | |
93 CGEventTapLocation loc = kCGHIDEventTap; // kCGSessionEventTap also works | |
94 CGEventPost(loc, cmd_d); | |
95 CGEventPost(loc, shift_d); | |
96 CGEventPost(loc, key_d); | |
97 CGEventPost(loc, key_u); | |
98 CGEventPost(loc, shift_u); | |
99 CGEventPost(loc, cmd_u); | |
100 | |
101 CFRelease(cmd_d); | |
102 CFRelease(cmd_u); | |
103 CFRelease(shift_d); | |
104 CFRelease(shift_u); | |
105 CFRelease(key_d); | |
106 CFRelease(key_u); | |
107 CFRelease(src); | |
108 } | |
109 #endif | |
110 | |
111 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(O S_MACOSX) | |
71 // The feature is only fully implemented on Windows and Linux, other platforms | 112 // The feature is only fully implemented on Windows and Linux, other platforms |
72 // coming. | 113 // coming. |
73 #define MAYBE_GlobalCommand GlobalCommand | 114 #define MAYBE_GlobalCommand GlobalCommand |
74 #else | 115 #else |
75 #define MAYBE_GlobalCommand DISABLED_GlobalCommand | 116 #define MAYBE_GlobalCommand DISABLED_GlobalCommand |
76 #endif | 117 #endif |
77 | 118 |
78 // Test the basics of global commands and make sure they work when Chrome | 119 // Test the basics of global commands and make sure they work when Chrome |
79 // doesn't have focus. Also test that non-global commands are not treated as | 120 // doesn't have focus. Also test that non-global commands are not treated as |
80 // global and that keys beyond Ctrl+Shift+[0..9] cannot be auto-assigned by an | 121 // global and that keys beyond Ctrl+Shift+[0..9] cannot be auto-assigned by an |
81 // extension. | 122 // extension. |
82 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) { | 123 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) { |
83 FeatureSwitch::ScopedOverride enable_global_commands( | 124 FeatureSwitch::ScopedOverride enable_global_commands( |
84 FeatureSwitch::global_commands(), true); | 125 FeatureSwitch::global_commands(), true); |
85 | 126 |
86 // Load the extension in the non-incognito browser. | 127 // Load the extension in the non-incognito browser. |
87 ResultCatcher catcher; | 128 ResultCatcher catcher; |
88 ASSERT_TRUE(RunExtensionTest("keybinding/global")) << message_; | 129 ASSERT_TRUE(RunExtensionTest("keybinding/global")) << message_; |
89 ASSERT_TRUE(catcher.GetNextResult()); | 130 ASSERT_TRUE(catcher.GetNextResult()); |
90 | 131 |
91 #if !defined(OS_LINUX) | 132 #if defined(OS_WIN) |
92 // Our infrastructure for sending keys expects a browser to send them to, but | 133 // Our infrastructure for sending keys expects a browser to send them to, but |
93 // to properly test global shortcuts you need to send them to another target. | 134 // to properly test global shortcuts you need to send them to another target. |
94 // So, create an incognito browser to use as a target to send the shortcuts | 135 // So, create an incognito browser to use as a target to send the shortcuts |
95 // to. It will ignore all of them and allow us test whether the global | 136 // to. It will ignore all of them and allow us test whether the global |
96 // shortcut really is global in nature and also that the non-global shortcut | 137 // shortcut really is global in nature and also that the non-global shortcut |
97 // is non-global. | 138 // is non-global. |
98 Browser* incognito_browser = CreateIncognitoBrowser(); | 139 Browser* incognito_browser = CreateIncognitoBrowser(); |
99 | 140 |
100 // Try to activate the non-global shortcut (Ctrl+Shift+1) and the | 141 // Try to activate the non-global shortcut (Ctrl+Shift+1) and the |
101 // non-assignable shortcut (Ctrl+Shift+A) by sending the keystrokes to the | 142 // non-assignable shortcut (Ctrl+Shift+A) by sending the keystrokes to the |
102 // incognito browser. Both shortcuts should have no effect (extension is not | 143 // incognito browser. Both shortcuts should have no effect (extension is not |
103 // loaded there). | 144 // loaded there). |
104 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 145 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
105 incognito_browser, ui::VKEY_1, true, true, false, false)); | 146 incognito_browser, ui::VKEY_1, true, true, false, false)); |
106 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 147 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
107 incognito_browser, ui::VKEY_A, true, true, false, false)); | 148 incognito_browser, ui::VKEY_A, true, true, false, false)); |
108 | 149 |
109 // Activate the shortcut (Ctrl+Shift+9). This should have an effect. | 150 // Activate the shortcut (Ctrl+Shift+9). This should have an effect. |
110 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 151 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
111 incognito_browser, ui::VKEY_9, true, true, false, false)); | 152 incognito_browser, ui::VKEY_9, true, true, false, false)); |
112 #else | 153 #elif defined(OS_LINUX) |
113 // Create an incognito browser to capture the focus. | 154 // Create an incognito browser to capture the focus. |
114 CreateIncognitoBrowser(); | 155 CreateIncognitoBrowser(); |
115 | 156 |
116 // On Linux, our infrastructure for sending keys just synthesize keyboard | 157 // On Linux, our infrastructure for sending keys just synthesize keyboard |
117 // event and send them directly to the specified window, without notifying the | 158 // event and send them directly to the specified window, without notifying the |
118 // X root window. It didn't work while testing global shortcut because the | 159 // X root window. It didn't work while testing global shortcut because the |
119 // stuff of global shortcut on Linux need to be notified when KeyPress event | 160 // stuff of global shortcut on Linux need to be notified when KeyPress event |
120 // is happening on X root window. So we simulate the keyboard input here. | 161 // is happening on X root window. So we simulate the keyboard input here. |
121 SendNativeKeyEventToXDisplay(ui::VKEY_1, true, true, false); | 162 SendNativeKeyEventToXDisplay(ui::VKEY_1, true, true, false); |
122 SendNativeKeyEventToXDisplay(ui::VKEY_A, true, true, false); | 163 SendNativeKeyEventToXDisplay(ui::VKEY_A, true, true, false); |
123 SendNativeKeyEventToXDisplay(ui::VKEY_9, true, true, false); | 164 SendNativeKeyEventToXDisplay(ui::VKEY_9, true, true, false); |
165 SendNativeKeyEventToXDisplay((ui::KeyboardCode)0x39, true, true, false); | |
Finnur
2013/12/09 17:29:08
wait... what is this for?
smus
2013/12/09 18:11:01
Done.
| |
166 #elif defined(OS_MACOSX) | |
167 // Create an incognito browser to capture the focus. | |
168 CreateIncognitoBrowser(); | |
169 | |
170 // Expect Command N. | |
171 SendNativeKeyEventMac((ui::KeyboardCode)25); | |
Finnur
2013/12/09 17:29:08
I'm confused... The other platforms send
Ctrl+Shi
smus
2013/12/09 18:11:01
I uploaded some stale version of the CL. Here's an
| |
124 #endif | 172 #endif |
125 | 173 |
126 // If this fails, it might be because the global shortcut failed to work, | 174 // If this fails, it might be because the global shortcut failed to work, |
127 // but it might also be because the non-global shortcuts unexpectedly | 175 // but it might also be because the non-global shortcuts unexpectedly |
128 // worked. | 176 // worked. |
129 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 177 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
130 } | 178 } |
131 | 179 |
132 #if defined(OS_WIN) | 180 #if defined(OS_WIN) |
133 // The feature is only fully implemented on Windows, other platforms coming. | 181 // The feature is only fully implemented on Windows, other platforms coming. |
182 // TODO(smus): On mac, SendKeyPress must first support media keys. | |
134 #define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey | 183 #define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey |
135 #else | 184 #else |
136 #define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey | 185 #define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey |
137 #endif | 186 #endif |
138 | 187 |
139 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) { | 188 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) { |
140 FeatureSwitch::ScopedOverride enable_global_commands( | 189 FeatureSwitch::ScopedOverride enable_global_commands( |
141 FeatureSwitch::global_commands(), true); | 190 FeatureSwitch::global_commands(), true); |
142 | 191 |
143 ResultCatcher catcher; | 192 ResultCatcher catcher; |
(...skipping 12 matching lines...) Expand all Loading... | |
156 false, | 205 false, |
157 false, | 206 false, |
158 false); | 207 false); |
159 | 208 |
160 // We should get two success result. | 209 // We should get two success result. |
161 ASSERT_TRUE(catcher.GetNextResult()); | 210 ASSERT_TRUE(catcher.GetNextResult()); |
162 ASSERT_TRUE(catcher.GetNextResult()); | 211 ASSERT_TRUE(catcher.GetNextResult()); |
163 } | 212 } |
164 | 213 |
165 } // namespace extensions | 214 } // namespace extensions |
OLD | NEW |