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 |
| 26 #include "base/mac/scoped_cftyperef.h" |
| 27 #endif |
| 28 |
23 namespace extensions { | 29 namespace extensions { |
24 | 30 |
25 typedef ExtensionApiTest GlobalCommandsApiTest; | 31 typedef ExtensionApiTest GlobalCommandsApiTest; |
26 | 32 |
27 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) |
28 // Send a simulated key press and release event, where |control|, |shift| or | 34 // Send a simulated key press and release event, where |control|, |shift| or |
29 // |alt| indicates whether the key is struck with corresponding modifier. | 35 // |alt| indicates whether the key is struck with corresponding modifier. |
30 void SendNativeKeyEventToXDisplay(ui::KeyboardCode key, | 36 void SendNativeKeyEventToXDisplay(ui::KeyboardCode key, |
31 bool control, | 37 bool control, |
32 bool shift, | 38 bool shift, |
(...skipping 27 matching lines...) Expand all Loading... |
60 XTestFakeKeyEvent(display, *it, True, CurrentTime); | 66 XTestFakeKeyEvent(display, *it, True, CurrentTime); |
61 | 67 |
62 // Simulate the keys being released. | 68 // Simulate the keys being released. |
63 for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++) | 69 for (KeyCodes::iterator it = key_codes.begin(); it != key_codes.end(); it++) |
64 XTestFakeKeyEvent(display, *it, False, CurrentTime); | 70 XTestFakeKeyEvent(display, *it, False, CurrentTime); |
65 | 71 |
66 XFlush(display); | 72 XFlush(display); |
67 } | 73 } |
68 #endif // OS_LINUX | 74 #endif // OS_LINUX |
69 | 75 |
70 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) | 76 #if defined(OS_MACOSX) |
| 77 using base::ScopedCFTypeRef; |
| 78 |
| 79 void SendNativeCommandShift(int key_code) { |
| 80 CGEventSourceRef event_source = |
| 81 CGEventSourceCreate(kCGEventSourceStateHIDSystemState); |
| 82 CGEventTapLocation event_tap_location = kCGHIDEventTap; |
| 83 |
| 84 // Create the keyboard press events. |
| 85 ScopedCFTypeRef<CGEventRef> command_down(CGEventCreateKeyboardEvent( |
| 86 event_source, kVK_Command, true)); |
| 87 ScopedCFTypeRef<CGEventRef> shift_down(CGEventCreateKeyboardEvent( |
| 88 event_source, kVK_Shift, true)); |
| 89 ScopedCFTypeRef<CGEventRef> key_down(CGEventCreateKeyboardEvent( |
| 90 event_source, key_code, true)); |
| 91 CGEventSetFlags(key_down, kCGEventFlagMaskCommand | kCGEventFlagMaskShift); |
| 92 |
| 93 // Create the keyboard release events. |
| 94 ScopedCFTypeRef<CGEventRef> command_up(CGEventCreateKeyboardEvent( |
| 95 event_source, kVK_Command, false)); |
| 96 ScopedCFTypeRef<CGEventRef> shift_up(CGEventCreateKeyboardEvent( |
| 97 event_source, kVK_Shift, false)); |
| 98 ScopedCFTypeRef<CGEventRef> key_up(CGEventCreateKeyboardEvent( |
| 99 event_source, key_code, false)); |
| 100 CGEventSetFlags(key_up, kCGEventFlagMaskCommand | kCGEventFlagMaskShift); |
| 101 |
| 102 // Post all of the events. |
| 103 CGEventPost(event_tap_location, command_down); |
| 104 CGEventPost(event_tap_location, shift_down); |
| 105 CGEventPost(event_tap_location, key_down); |
| 106 CGEventPost(event_tap_location, key_up); |
| 107 CGEventPost(event_tap_location, shift_up); |
| 108 CGEventPost(event_tap_location, command_up); |
| 109 |
| 110 CFRelease(event_source); |
| 111 } |
| 112 #endif |
| 113 |
| 114 #if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) \ |
| 115 || defined(OS_MACOSX) |
71 // The feature is only fully implemented on Windows and Linux, other platforms | 116 // The feature is only fully implemented on Windows and Linux, other platforms |
72 // coming. | 117 // coming. |
73 #define MAYBE_GlobalCommand GlobalCommand | 118 #define MAYBE_GlobalCommand GlobalCommand |
74 #else | 119 #else |
75 #define MAYBE_GlobalCommand DISABLED_GlobalCommand | 120 #define MAYBE_GlobalCommand DISABLED_GlobalCommand |
76 #endif | 121 #endif |
77 | 122 |
78 // Test the basics of global commands and make sure they work when Chrome | 123 // 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 | 124 // 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 | 125 // global and that keys beyond Ctrl+Shift+[0..9] cannot be auto-assigned by an |
81 // extension. | 126 // extension. |
82 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) { | 127 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) { |
83 FeatureSwitch::ScopedOverride enable_global_commands( | 128 FeatureSwitch::ScopedOverride enable_global_commands( |
84 FeatureSwitch::global_commands(), true); | 129 FeatureSwitch::global_commands(), true); |
85 | 130 |
86 // Load the extension in the non-incognito browser. | 131 // Load the extension in the non-incognito browser. |
87 ResultCatcher catcher; | 132 ResultCatcher catcher; |
88 ASSERT_TRUE(RunExtensionTest("keybinding/global")) << message_; | 133 ASSERT_TRUE(RunExtensionTest("keybinding/global")) << message_; |
89 ASSERT_TRUE(catcher.GetNextResult()); | 134 ASSERT_TRUE(catcher.GetNextResult()); |
90 | 135 |
91 #if !defined(OS_LINUX) | 136 #if defined(OS_WIN) |
92 // Our infrastructure for sending keys expects a browser to send them to, but | 137 // 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. | 138 // 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 | 139 // 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 | 140 // 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 | 141 // shortcut really is global in nature and also that the non-global shortcut |
97 // is non-global. | 142 // is non-global. |
98 Browser* incognito_browser = CreateIncognitoBrowser(); | 143 Browser* incognito_browser = CreateIncognitoBrowser(); |
99 | 144 |
100 // Try to activate the non-global shortcut (Ctrl+Shift+1) and the | 145 // 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 | 146 // non-assignable shortcut (Ctrl+Shift+A) by sending the keystrokes to the |
102 // incognito browser. Both shortcuts should have no effect (extension is not | 147 // incognito browser. Both shortcuts should have no effect (extension is not |
103 // loaded there). | 148 // loaded there). |
104 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 149 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
105 incognito_browser, ui::VKEY_1, true, true, false, false)); | 150 incognito_browser, ui::VKEY_1, true, true, false, false)); |
106 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 151 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
107 incognito_browser, ui::VKEY_A, true, true, false, false)); | 152 incognito_browser, ui::VKEY_A, true, true, false, false)); |
108 | 153 |
109 // Activate the shortcut (Ctrl+Shift+9). This should have an effect. | 154 // Activate the shortcut (Ctrl+Shift+9). This should have an effect. |
110 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( | 155 ASSERT_TRUE(ui_test_utils::SendKeyPressSync( |
111 incognito_browser, ui::VKEY_9, true, true, false, false)); | 156 incognito_browser, ui::VKEY_9, true, true, false, false)); |
112 #else | 157 #elif defined(OS_LINUX) |
113 // Create an incognito browser to capture the focus. | 158 // Create an incognito browser to capture the focus. |
114 CreateIncognitoBrowser(); | 159 CreateIncognitoBrowser(); |
115 | 160 |
116 // On Linux, our infrastructure for sending keys just synthesize keyboard | 161 // On Linux, our infrastructure for sending keys just synthesize keyboard |
117 // event and send them directly to the specified window, without notifying the | 162 // 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 | 163 // 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 | 164 // 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. | 165 // is happening on X root window. So we simulate the keyboard input here. |
121 SendNativeKeyEventToXDisplay(ui::VKEY_1, true, true, false); | 166 SendNativeKeyEventToXDisplay(ui::VKEY_1, true, true, false); |
122 SendNativeKeyEventToXDisplay(ui::VKEY_A, true, true, false); | 167 SendNativeKeyEventToXDisplay(ui::VKEY_A, true, true, false); |
123 SendNativeKeyEventToXDisplay(ui::VKEY_9, true, true, false); | 168 SendNativeKeyEventToXDisplay(ui::VKEY_9, true, true, false); |
| 169 #elif defined(OS_MACOSX) |
| 170 // Create an incognito browser to capture the focus. |
| 171 CreateIncognitoBrowser(); |
| 172 |
| 173 // Send some native mac key events. |
| 174 SendNativeCommandShift(kVK_ANSI_1); |
| 175 SendNativeCommandShift(kVK_ANSI_A); |
| 176 SendNativeCommandShift(kVK_ANSI_9); |
124 #endif | 177 #endif |
125 | 178 |
126 // If this fails, it might be because the global shortcut failed to work, | 179 // 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 | 180 // but it might also be because the non-global shortcuts unexpectedly |
128 // worked. | 181 // worked. |
129 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); | 182 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
130 } | 183 } |
131 | 184 |
132 #if defined(OS_WIN) | 185 #if defined(OS_WIN) |
133 // The feature is only fully implemented on Windows, other platforms coming. | 186 // The feature is only fully implemented on Windows, other platforms coming. |
| 187 // TODO(smus): On mac, SendKeyPress must first support media keys. |
134 #define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey | 188 #define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey |
135 #else | 189 #else |
136 #define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey | 190 #define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey |
137 #endif | 191 #endif |
138 | 192 |
139 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) { | 193 IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) { |
140 FeatureSwitch::ScopedOverride enable_global_commands( | 194 FeatureSwitch::ScopedOverride enable_global_commands( |
141 FeatureSwitch::global_commands(), true); | 195 FeatureSwitch::global_commands(), true); |
142 | 196 |
143 ResultCatcher catcher; | 197 ResultCatcher catcher; |
(...skipping 12 matching lines...) Expand all Loading... |
156 false, | 210 false, |
157 false, | 211 false, |
158 false); | 212 false); |
159 | 213 |
160 // We should get two success result. | 214 // We should get two success result. |
161 ASSERT_TRUE(catcher.GetNextResult()); | 215 ASSERT_TRUE(catcher.GetNextResult()); |
162 ASSERT_TRUE(catcher.GetNextResult()); | 216 ASSERT_TRUE(catcher.GetNextResult()); |
163 } | 217 } |
164 | 218 |
165 } // namespace extensions | 219 } // namespace extensions |
OLD | NEW |