Home | History | Annotate | Download | only in cocoa
      1 // Copyright (c) 2011 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.
      4 
      5 #include <Carbon/Carbon.h>
      6 
      7 #include "base/debug/debugger.h"
      8 #include "base/memory/scoped_nsobject.h"
      9 #include "chrome/app/chrome_command_ids.h"
     10 #import "chrome/browser/ui/cocoa/chrome_event_processing_window.h"
     11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
     12 #import "chrome/browser/ui/cocoa/browser_frame_view.h"
     13 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
     14 #import "third_party/ocmock/OCMock/OCMock.h"
     15 
     16 namespace {
     17 
     18 NSEvent* KeyEvent(const NSUInteger flags, const NSUInteger keyCode) {
     19   return [NSEvent keyEventWithType:NSKeyDown
     20                           location:NSZeroPoint
     21                      modifierFlags:flags
     22                          timestamp:0.0
     23                       windowNumber:0
     24                            context:nil
     25                         characters:@""
     26        charactersIgnoringModifiers:@""
     27                          isARepeat:NO
     28                           keyCode:keyCode];
     29 }
     30 
     31 class ChromeEventProcessingWindowTest : public CocoaTest {
     32  public:
     33   virtual void SetUp() {
     34     CocoaTest::SetUp();
     35     // Create a window.
     36     const NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask |
     37         NSMiniaturizableWindowMask | NSResizableWindowMask;
     38     window_ = [[ChromeEventProcessingWindow alloc]
     39                initWithContentRect:NSMakeRect(0, 0, 800, 600)
     40                          styleMask:mask
     41                            backing:NSBackingStoreBuffered
     42                              defer:NO];
     43     if (base::debug::BeingDebugged()) {
     44       [window_ orderFront:nil];
     45     } else {
     46       [window_ orderBack:nil];
     47     }
     48   }
     49 
     50   virtual void TearDown() {
     51     [window_ close];
     52     CocoaTest::TearDown();
     53   }
     54 
     55   ChromeEventProcessingWindow* window_;
     56 };
     57 
     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   //
     63   // TODO(akalin): Figure out how to replace OCMOCK_ANY below with
     64   // @protocol(BrowserCommandExecutor) and have it work.
     65   BOOL yes = YES;
     66   [[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)]
     67     conformsToProtocol:OCMOCK_ANY];
     68   return delegate;
     69 }
     70 
     71 // Verify that the window intercepts a particular key event and
     72 // forwards it to [delegate executeCommand:].  Assume that other
     73 // CommandForKeyboardShortcut() will work the same for the rest.
     74 TEST_F(ChromeEventProcessingWindowTest,
     75        PerformKeyEquivalentForwardToExecuteCommand) {
     76   NSEvent* event = KeyEvent(NSCommandKeyMask, kVK_ANSI_1);
     77 
     78   id delegate = CreateBrowserWindowControllerMock();
     79   [[delegate expect] executeCommand:IDC_SELECT_TAB_0];
     80 
     81   [window_ setDelegate:delegate];
     82   [window_ performKeyEquivalent:event];
     83 
     84   // Don't wish to mock all the way down...
     85   [window_ setDelegate:nil];
     86   [delegate verify];
     87 }
     88 
     89 // Verify that an unhandled shortcut does not get forwarded via
     90 // -executeCommand:.
     91 // TODO(shess) Think of a way to test that it is sent to the
     92 // superclass.
     93 TEST_F(ChromeEventProcessingWindowTest, PerformKeyEquivalentNoForward) {
     94   NSEvent* event = KeyEvent(0, 0);
     95 
     96   id delegate = CreateBrowserWindowControllerMock();
     97 
     98   [window_ setDelegate:delegate];
     99   [window_ performKeyEquivalent:event];
    100 
    101   // Don't wish to mock all the way down...
    102   [window_ setDelegate:nil];
    103   [delegate verify];
    104 }
    105 
    106 }  // namespace
    107