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 #import <Cocoa/Cocoa.h>
7 #include "base/debug/debugger.h"
8 #include "base/mac/mac_util.h"
9 #include "base/mac/scoped_nsobject.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #import "chrome/browser/ui/cocoa/framed_browser_window.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #import "testing/gtest_mac.h"
16 #include "testing/platform_test.h"
17 #import "third_party/ocmock/OCMock/OCMock.h"
19 class FramedBrowserWindowTest : public CocoaTest {
21 virtual void SetUp() {
24 window_ = [[FramedBrowserWindow alloc]
25 initWithContentRect:NSMakeRect(0, 0, 800, 600)
27 if (base::debug::BeingDebugged()) {
28 [window_ orderFront:nil];
30 [window_ orderBack:nil];
34 virtual void TearDown() {
36 CocoaTest::TearDown();
39 // Returns a canonical snapshot of the window.
40 NSData* WindowContentsAsTIFF() {
43 NSView* frameView = [window_ contentView];
44 while ([frameView superview]) {
45 frameView = [frameView superview];
48 // Inset to mask off left and right edges which vary in HighDPI.
49 NSRect bounds = NSInsetRect([frameView bounds], 4, 0);
51 // On 10.6, the grippy changes appearance slightly when painted the second
52 // time in a textured window. Since this test cares about the window title,
53 // cut off the bottom of the window.
54 bounds.size.height -= 40;
55 bounds.origin.y += 40;
57 [frameView lockFocus];
58 base::scoped_nsobject<NSBitmapImageRep> bitmap(
59 [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
60 [frameView unlockFocus];
62 return [bitmap TIFFRepresentation];
65 FramedBrowserWindow* window_;
68 // Baseline test that the window creates, displays, closes, and
70 TEST_F(FramedBrowserWindowTest, ShowAndClose) {
74 // Test that the title-hiding API we're using does the job.
75 TEST_F(FramedBrowserWindowTest, DoesHideTitle) {
76 // The -display calls are not strictly necessary, but they do
77 // make it easier to see what's happening when debugging (without
78 // them the changes are never flushed to the screen).
80 [window_ setTitle:@""];
82 NSData* emptyTitleData = WindowContentsAsTIFF();
84 [window_ setTitle:@"This is a title"];
86 NSData* thisTitleData = WindowContentsAsTIFF();
88 // The default window with a title should look different from the
89 // window with an empty title.
90 EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);
92 [window_ setShouldHideTitle:YES];
93 [window_ setTitle:@""];
95 [window_ setTitle:@"This is a title"];
97 NSData* hiddenTitleData = WindowContentsAsTIFF();
99 // With our magic setting, the window with a title should look the
100 // same as the window with an empty title.
101 EXPECT_TRUE([emptyTitleData isEqualToData:hiddenTitleData]);
103 // Test the secret API only on versions of the system in which it exists.
104 if (base::mac::IsOSMavericksOrEarlier())
105 EXPECT_TRUE([window_ _isTitleHidden]);
108 // Test to make sure that our window widgets are in the right place.
109 TEST_F(FramedBrowserWindowTest, WindowWidgetLocation) {
113 // First without a tabstrip.
115 window_ = [[FramedBrowserWindow alloc]
116 initWithContentRect:NSMakeRect(0, 0, 800, 600)
118 id controller = [OCMockObject mockForClass:[BrowserWindowController class]];
119 [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
120 isKindOfClass:[BrowserWindowController class]];
121 [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTabStrip];
122 [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTitleBar];
123 [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] isTabbedWindow];
124 [window_ setWindowController:controller];
126 NSView* closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
127 EXPECT_TRUE(closeBoxControl);
128 NSRect closeBoxFrame = [closeBoxControl frame];
129 NSRect windowBounds = [window_ frame];
130 windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
131 windowBounds.origin = NSZeroPoint;
132 EXPECT_EQ(NSMaxY(closeBoxFrame),
133 NSMaxY(windowBounds) -
134 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
135 EXPECT_EQ(NSMinX(closeBoxFrame),
136 kFramedWindowButtonsWithoutTabStripOffsetFromLeft);
138 NSView* miniaturizeControl =
139 [window_ standardWindowButton:NSWindowMiniaturizeButton];
140 EXPECT_TRUE(miniaturizeControl);
141 NSRect miniaturizeFrame = [miniaturizeControl frame];
142 EXPECT_EQ(NSMaxY(miniaturizeFrame),
143 NSMaxY(windowBounds) -
144 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
145 EXPECT_EQ(NSMinX(miniaturizeFrame),
146 NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
147 [window_ setWindowController:nil];
149 // Then with a tabstrip.
151 window_ = [[FramedBrowserWindow alloc]
152 initWithContentRect:NSMakeRect(0, 0, 800, 600)
154 controller = [OCMockObject mockForClass:[BrowserWindowController class]];
155 [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
156 isKindOfClass:[BrowserWindowController class]];
157 [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTabStrip];
158 [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTitleBar];
159 [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] isTabbedWindow];
160 [window_ setWindowController:controller];
162 closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
163 EXPECT_TRUE(closeBoxControl);
164 closeBoxFrame = [closeBoxControl frame];
165 windowBounds = [window_ frame];
166 windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
167 windowBounds.origin = NSZeroPoint;
168 EXPECT_EQ(NSMaxY(closeBoxFrame),
169 NSMaxY(windowBounds) -
170 kFramedWindowButtonsWithTabStripOffsetFromTop);
171 EXPECT_EQ(NSMinX(closeBoxFrame),
172 kFramedWindowButtonsWithTabStripOffsetFromLeft);
174 miniaturizeControl = [window_ standardWindowButton:NSWindowMiniaturizeButton];
175 EXPECT_TRUE(miniaturizeControl);
176 miniaturizeFrame = [miniaturizeControl frame];
177 EXPECT_EQ(NSMaxY(miniaturizeFrame),
178 NSMaxY(windowBounds) -
179 kFramedWindowButtonsWithTabStripOffsetFromTop);
180 EXPECT_EQ(NSMinX(miniaturizeFrame),
181 NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
182 [window_ setWindowController:nil];