OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #import <Cocoa/Cocoa.h> |
| 6 |
| 7 #include "base/scoped_nsobject.h" |
| 8 #include "chrome/app/chrome_dll_resource.h" |
| 9 #import "chrome/browser/cocoa/chrome_browser_window.h" |
| 10 #import "chrome/browser/cocoa/browser_window_controller.h" |
| 11 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "testing/platform_test.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 ChromeBrowserWindowTest : public PlatformTest { |
| 32 public: |
| 33 ChromeBrowserWindowTest() { |
| 34 // Create a window. |
| 35 const NSUInteger mask = NSTitledWindowMask | NSClosableWindowMask | |
| 36 NSMiniaturizableWindowMask | NSResizableWindowMask; |
| 37 window_.reset([[ChromeBrowserWindow alloc] |
| 38 initWithContentRect:NSMakeRect(0, 0, 800, 600) |
| 39 styleMask:mask |
| 40 backing:NSBackingStoreBuffered |
| 41 defer:NO]); |
| 42 if (DebugUtil::BeingDebugged()) { |
| 43 [window_ orderFront:nil]; |
| 44 } else { |
| 45 [window_ orderBack:nil]; |
| 46 } |
| 47 } |
| 48 |
| 49 // Returns a canonical snapshot of the window. |
| 50 NSData* WindowContentsAsTIFF() { |
| 51 NSRect frame([window_ frame]); |
| 52 frame.origin = [window_ convertScreenToBase:frame.origin]; |
| 53 |
| 54 NSData* pdfData = [window_ dataWithPDFInsideRect:frame]; |
| 55 |
| 56 // |pdfData| can differ for windows which look the same, so make it |
| 57 // canonical. |
| 58 NSImage* image = [[[NSImage alloc] initWithData:pdfData] autorelease]; |
| 59 return [image TIFFRepresentation]; |
| 60 } |
| 61 |
| 62 CocoaNoWindowTestHelper cocoa_helper_; |
| 63 scoped_nsobject<ChromeBrowserWindow> window_; |
| 64 }; |
| 65 |
| 66 // Baseline test that the window creates, displays, closes, and |
| 67 // releases. |
| 68 TEST_F(ChromeBrowserWindowTest, ShowAndClose) { |
| 69 [window_ display]; |
| 70 } |
| 71 |
| 72 // Verify that the window intercepts a particular key event and |
| 73 // forwards it to [delegate executeCommand:]. Assume that other |
| 74 // CommandForKeyboardShortcut() will work the same for the rest. |
| 75 TEST_F(ChromeBrowserWindowTest, PerformKeyEquivalentForwardToExecuteCommand) { |
| 76 NSEvent *event = KeyEvent(NSCommandKeyMask, kVK_ANSI_1); |
| 77 |
| 78 id delegate = [OCMockObject mockForClass:[BrowserWindowController class]]; |
| 79 // -stub to satisfy the DCHECK. |
| 80 BOOL yes = YES; |
| 81 [[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)] |
| 82 isKindOfClass:[BrowserWindowController class]]; |
| 83 [[delegate expect] executeCommand:IDC_SELECT_TAB_0]; |
| 84 |
| 85 [window_ setDelegate:delegate]; |
| 86 [window_ performKeyEquivalent:event]; |
| 87 |
| 88 // Don't wish to mock all the way down... |
| 89 [window_ setDelegate:nil]; |
| 90 [delegate verify]; |
| 91 } |
| 92 |
| 93 // Verify that an unhandled shortcut does not get forwarded via |
| 94 // -executeCommand:. |
| 95 // TODO(shess) Think of a way to test that it is sent to the |
| 96 // superclass. |
| 97 TEST_F(ChromeBrowserWindowTest, PerformKeyEquivalentNoForward) { |
| 98 NSEvent *event = KeyEvent(0, 0); |
| 99 |
| 100 id delegate = [OCMockObject mockForClass:[BrowserWindowController class]]; |
| 101 // -stub to satisfy the DCHECK. |
| 102 BOOL yes = YES; |
| 103 [[[delegate stub] andReturnValue:OCMOCK_VALUE(yes)] |
| 104 isKindOfClass:[BrowserWindowController class]]; |
| 105 |
| 106 [window_ setDelegate:delegate]; |
| 107 [window_ performKeyEquivalent:event]; |
| 108 |
| 109 // Don't wish to mock all the way down... |
| 110 [window_ setDelegate:nil]; |
| 111 [delegate verify]; |
| 112 } |
| 113 |
| 114 // Test that undocumented title-hiding API we're using does the job. |
| 115 TEST_F(ChromeBrowserWindowTest, DoesHideTitle) { |
| 116 // The -display calls are not strictly necessary, but they do |
| 117 // make it easier to see what's happening when debugging (without |
| 118 // them the changes are never flushed to the screen). |
| 119 |
| 120 [window_ setTitle:@""]; |
| 121 [window_ display]; |
| 122 NSData* emptyTitleData = WindowContentsAsTIFF(); |
| 123 |
| 124 [window_ setTitle:@"This is a title"]; |
| 125 [window_ display]; |
| 126 NSData* thisTitleData = WindowContentsAsTIFF(); |
| 127 |
| 128 // The default window with a title should look different from the |
| 129 // window with an emtpy title. |
| 130 EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]); |
| 131 |
| 132 [window_ setShouldHideTitle:YES]; |
| 133 [window_ setTitle:@""]; |
| 134 [window_ display]; |
| 135 [window_ setTitle:@"This is a title"]; |
| 136 [window_ display]; |
| 137 NSData* hiddenTitleData = WindowContentsAsTIFF(); |
| 138 |
| 139 // With our magic setting, the window with a title should look the |
| 140 // same as the window with an empty title. |
| 141 EXPECT_TRUE([window_ _isTitleHidden]); |
| 142 EXPECT_TRUE([emptyTitleData isEqualToData:hiddenTitleData]); |
| 143 } |
| 144 |
| 145 } // namespace |
OLD | NEW |