| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "chrome/browser/global_keyboard_shortcuts_mac.h" | 5 #import "chrome/browser/global_keyboard_shortcuts_mac.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 | 8 |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "chrome/browser/extensions/extension_browsertest.h" | 10 #include "chrome/browser/extensions/extension_browsertest.h" |
| 11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
| 13 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 15 #import "ui/events/test/cocoa_test_event_utils.h" | 15 #import "ui/events/test/cocoa_test_event_utils.h" |
| 16 | 16 |
| 17 using cocoa_test_event_utils::SynthesizeKeyEvent; | 17 using cocoa_test_event_utils::SynthesizeKeyEvent; |
| 18 | 18 |
| 19 using GlobalKeyboardShortcutsTest = ExtensionBrowserTest; | 19 using GlobalKeyboardShortcutsTest = ExtensionBrowserTest; |
| 20 | 20 |
| 21 namespace { |
| 22 |
| 23 void ActivateAccelerator(NSWindow* window, NSEvent* ns_event) { |
| 24 if ([window performKeyEquivalent:ns_event]) |
| 25 return; |
| 26 |
| 27 // This is consistent with the way AppKit dispatches events when |
| 28 // -performKeyEquivalent: returns NO. See "The Path of Key Events" in the |
| 29 // Cocoa Event Architecture documentation. |
| 30 [window sendEvent:ns_event]; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 21 // Test that global keyboard shortcuts are handled by the native window. | 35 // Test that global keyboard shortcuts are handled by the native window. |
| 22 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, SwitchTabsMac) { | 36 IN_PROC_BROWSER_TEST_F(GlobalKeyboardShortcutsTest, SwitchTabsMac) { |
| 23 NSWindow* ns_window = browser()->window()->GetNativeWindow(); | 37 NSWindow* ns_window = browser()->window()->GetNativeWindow(); |
| 24 TabStripModel* tab_strip = browser()->tab_strip_model(); | 38 TabStripModel* tab_strip = browser()->tab_strip_model(); |
| 25 | 39 |
| 26 // Set up window with 2 tabs. | 40 // Set up window with 2 tabs. |
| 27 chrome::NewTab(browser()); | 41 chrome::NewTab(browser()); |
| 28 EXPECT_EQ(2, tab_strip->count()); | 42 EXPECT_EQ(2, tab_strip->count()); |
| 29 EXPECT_TRUE(tab_strip->IsTabSelected(1)); | 43 EXPECT_TRUE(tab_strip->IsTabSelected(1)); |
| 30 | 44 |
| 31 // Ctrl+Tab goes to the next tab, which loops back to the first tab. | 45 // Ctrl+Tab goes to the next tab, which loops back to the first tab. |
| 32 [ns_window performKeyEquivalent:SynthesizeKeyEvent( | 46 ActivateAccelerator( |
| 33 ns_window, true, ui::VKEY_TAB, NSControlKeyMask)]; | 47 ns_window, |
| 48 SynthesizeKeyEvent(ns_window, true, ui::VKEY_TAB, NSControlKeyMask)); |
| 34 EXPECT_TRUE(tab_strip->IsTabSelected(0)); | 49 EXPECT_TRUE(tab_strip->IsTabSelected(0)); |
| 35 | 50 |
| 36 // Cmd+2 goes to the second tab. | 51 // Cmd+2 goes to the second tab. |
| 37 [ns_window performKeyEquivalent:SynthesizeKeyEvent( | 52 ActivateAccelerator(ns_window, SynthesizeKeyEvent(ns_window, true, ui::VKEY_2, |
| 38 ns_window, true, ui::VKEY_2, NSCommandKeyMask)]; | 53 NSCommandKeyMask)); |
| 39 EXPECT_TRUE(tab_strip->IsTabSelected(1)); | 54 EXPECT_TRUE(tab_strip->IsTabSelected(1)); |
| 40 | 55 |
| 41 // Cmd+{ goes to the previous tab. | 56 // Cmd+{ goes to the previous tab. |
| 42 [ns_window performKeyEquivalent:SynthesizeKeyEvent( | 57 ActivateAccelerator(ns_window, |
| 43 ns_window, true, ui::VKEY_OEM_4, NSShiftKeyMask | NSCommandKeyMask)]; | 58 SynthesizeKeyEvent(ns_window, true, ui::VKEY_OEM_4, |
| 59 NSShiftKeyMask | NSCommandKeyMask)); |
| 44 EXPECT_TRUE(tab_strip->IsTabSelected(0)); | 60 EXPECT_TRUE(tab_strip->IsTabSelected(0)); |
| 45 } | 61 } |
| OLD | NEW |