Index: chrome/browser/extensions/extension_commands_global_registry_apitest.cc |
diff --git a/chrome/browser/extensions/extension_commands_global_registry_apitest.cc b/chrome/browser/extensions/extension_commands_global_registry_apitest.cc |
index b9b5f6d7c77b83a684ffc192f8a59295f93ba3b4..8ea48a19257e317729d99255123a26ca8ebbe347 100644 |
--- a/chrome/browser/extensions/extension_commands_global_registry_apitest.cc |
+++ b/chrome/browser/extensions/extension_commands_global_registry_apitest.cc |
@@ -17,6 +17,8 @@ |
#include "ui/events/keycodes/keyboard_code_conversion_x.h" |
#include "ui/gfx/x/x11_types.h" |
+#elif defined(OS_WIN) |
+#include "ui/events/keycodes/keyboard_code_conversion_win.h" |
#endif |
namespace extensions { |
@@ -66,6 +68,91 @@ void SendNativeKeyEventToXDisplay(ui::KeyboardCode key, |
} |
#endif // OS_LINUX |
+#if defined(OS_WIN) |
+bool FillKeyboardInput(ui::KeyboardCode key, INPUT* input, bool key_up) { |
+ memset(input, 0, sizeof(INPUT)); |
+ input->type = INPUT_KEYBOARD; |
+ input->ki.wVk = ui::WindowsKeyCodeForKeyboardCode(key); |
+ input->ki.dwFlags = key_up ? KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP : |
+ KEYEVENTF_EXTENDEDKEY; |
+ |
+ return true; |
+} |
+ |
+// This function is modified from |SendKeyPressImpl| of |
+// ui_controls_internal_win.cc. Use ui_test_utils::SendKeyPressSync when testing |
+// global media keys will fail, this is because we have register the single key |
+// using RegisterHotKey, so the keyboard hook of current thread will never be |
+// called when we only send the single key (For example: "MediaNextTrack" down |
+// -> "MediaNextTrack" up). The difference when testing global key combinations: |
+// the key event are sent one by one. For example, sending "Ctrl+Shift+1", we |
+// will send "Ctrl" down -> "Shift" down -> "1" down, (here the hot key is |
+// activated.), then 1 "up" (here InputDispatcher will receive an key up event, |
+// then the message loop in |SendKeyPressToWindowSync| could stop running |
+// normally) -> "Shift" up -> "Ctrl" up. |
+// |
+// Note: The above comment is only when I have digged out when investigate why |
+// using ui_test_utils::SendKeyPressSync to test global media keys will fail. |
+// I need some help about whether we can use this custom function? |
+// TODO(zhchbin): Clear up the comment. |
+bool SendNativeKeyEvent(ui::KeyboardCode key, |
+ bool control, |
+ bool shift, |
+ bool alt) { |
+ INPUT input[8] = { 0 }; // 8, assuming all the modifiers are activated. |
+ |
+ UINT i = 0; |
+ if (control) { |
+ if (!FillKeyboardInput(ui::VKEY_CONTROL, &input[i], false)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (shift) { |
+ if (!FillKeyboardInput(ui::VKEY_SHIFT, &input[i], false)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (alt) { |
+ if (!FillKeyboardInput(ui::VKEY_MENU, &input[i], false)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (!FillKeyboardInput(key, &input[i], false)) |
+ return false; |
+ i++; |
+ |
+ if (!FillKeyboardInput(key, &input[i], true)) |
+ return false; |
+ i++; |
+ |
+ if (alt) { |
+ if (!FillKeyboardInput(ui::VKEY_MENU, &input[i], true)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (shift) { |
+ if (!FillKeyboardInput(ui::VKEY_SHIFT, &input[i], true)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (control) { |
+ if (!FillKeyboardInput(ui::VKEY_CONTROL, &input[i], true)) |
+ return false; |
+ i++; |
+ } |
+ |
+ if (::SendInput(i, input, sizeof(INPUT)) != i) |
+ return false; |
+ |
+ return true; |
+} |
+#endif // OS_WIN |
+ |
#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS)) |
// The feature is only fully implemented on Windows and Linux, other platforms |
// coming. |
@@ -128,4 +215,34 @@ IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalCommand) { |
ASSERT_TRUE(catcher.GetNextResult()) << catcher.message(); |
} |
+#if defined(OS_WIN) |
+// The feature is only fully implemented on Windows, other platforms coming. |
+#define MAYBE_GlobalDuplicatedMediaKey GlobalDuplicatedMediaKey |
+#else |
+#define MAYBE_GlobalDuplicatedMediaKey DISABLED_GlobalDuplicatedMediaKey |
+#endif |
+ |
+IN_PROC_BROWSER_TEST_F(GlobalCommandsApiTest, MAYBE_GlobalDuplicatedMediaKey) { |
+ FeatureSwitch::ScopedOverride enable_global_commands( |
+ FeatureSwitch::global_commands(), true); |
+ |
+ ResultCatcher catcher; |
+ ASSERT_TRUE(RunExtensionTest("keybinding/global_media_keys_0")) << message_; |
+ ASSERT_TRUE(catcher.GetNextResult()); |
+ ASSERT_TRUE(RunExtensionTest("keybinding/global_media_keys_1")) << message_; |
+ ASSERT_TRUE(catcher.GetNextResult()); |
+ |
+ WindowController* controller = browser()->extension_window_controller(); |
+ controller->window()->Minimize(); |
+ |
+#if defined(OS_WIN) |
+ // Currently it works only on Windows. |
+ SendNativeKeyEvent(ui::VKEY_MEDIA_NEXT_TRACK, false, false, false); |
+ |
+ // We should get two success result. |
+ ASSERT_TRUE(catcher.GetNextResult()); |
+ ASSERT_TRUE(catcher.GetNextResult()); |
+#endif |
+} |
+ |
} // namespace extensions |