1 // Copyright (c) 2012 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.
5 #include <Carbon/Carbon.h>
7 #include "base/debug/debugger.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
10 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #import "third_party/ocmock/OCMock/OCMock.h"
13 #include "third_party/ocmock/gtest_support.h"
14 #import "third_party/ocmock/ocmock_extensions.h"
18 NSEvent* KeyEvent(const NSUInteger flags, const NSUInteger keyCode) {
19 return [NSEvent keyEventWithType:NSKeyDown
26 charactersIgnoringModifiers:@""
31 class ChromeEventProcessingWindowTest : public CocoaTest {
33 void SetUp() override {
36 const NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask |
37 NSMiniaturizableWindowMask | NSResizableWindowMask;
38 window_ = [[ChromeEventProcessingWindow alloc]
39 initWithContentRect:NSMakeRect(0, 0, 800, 600)
41 backing:NSBackingStoreBuffered
43 if (base::debug::BeingDebugged()) {
44 [window_ orderFront:nil];
46 [window_ orderBack:nil];
50 void TearDown() override {
52 CocoaTest::TearDown();
55 ChromeEventProcessingWindow* window_;
58 id CreateBrowserWindowControllerMock() {
59 id delegate = [OCMockObject mockForClass:[BrowserWindowController class]];
60 // Make conformsToProtocol return YES for @protocol(BrowserCommandExecutor)
61 // to satisfy the DCHECK() in handleExtraKeyboardShortcut.
62 [[[delegate stub] andReturnBool:YES]
63 conformsToProtocol:[OCMArg conformsToProtocol:
64 @protocol(BrowserCommandExecutor)]];
68 // Verify that the window intercepts a particular key event and
69 // forwards it to [delegate executeCommand:]. Assume that other
70 // CommandForKeyboardShortcut() will work the same for the rest.
71 TEST_F(ChromeEventProcessingWindowTest,
72 PerformKeyEquivalentForwardToExecuteCommand) {
73 NSEvent* event = KeyEvent(NSCommandKeyMask, kVK_ANSI_1);
75 id delegate = CreateBrowserWindowControllerMock();
76 [[delegate expect] executeCommand:IDC_SELECT_TAB_0];
78 [window_ setDelegate:delegate];
79 [window_ performKeyEquivalent:event];
81 // Don't wish to mock all the way down...
82 [window_ setDelegate:nil];
83 EXPECT_OCMOCK_VERIFY(delegate);
86 // Verify that an unhandled shortcut does not get forwarded via
88 // TODO(shess) Think of a way to test that it is sent to the
90 TEST_F(ChromeEventProcessingWindowTest, PerformKeyEquivalentNoForward) {
91 NSEvent* event = KeyEvent(0, 0);
93 id delegate = CreateBrowserWindowControllerMock();
95 [window_ setDelegate:delegate];
96 [window_ performKeyEquivalent:event];
98 // Don't wish to mock all the way down...
99 [window_ setDelegate:nil];
100 EXPECT_OCMOCK_VERIFY(delegate);