Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / framed_browser_window_unittest.mm
blob32322eec913e1b07e995026e5bf8bdd2cfc23139
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/scoped_nsobject.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #import "chrome/browser/ui/cocoa/framed_browser_window.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #import "testing/gtest_mac.h"
15 #include "testing/platform_test.h"
16 #import "third_party/ocmock/OCMock/OCMock.h"
18 class FramedBrowserWindowTest : public CocoaTest {
19  public:
20   virtual void SetUp() {
21     CocoaTest::SetUp();
22     // Create a window.
23     window_ = [[FramedBrowserWindow alloc]
24                initWithContentRect:NSMakeRect(0, 0, 800, 600)
25                        hasTabStrip:YES];
26     if (base::debug::BeingDebugged()) {
27       [window_ orderFront:nil];
28     } else {
29       [window_ orderBack:nil];
30     }
31   }
33   virtual void TearDown() {
34     [window_ close];
35     CocoaTest::TearDown();
36   }
38   // Returns a canonical snapshot of the window.
39   NSData* WindowContentsAsTIFF() {
40     [window_ display];
42     NSView* frameView = [window_ contentView];
43     while ([frameView superview]) {
44       frameView = [frameView superview];
45     }
47     // Inset to mask off left and right edges which vary in HighDPI.
48     NSRect bounds = NSInsetRect([frameView bounds], 4, 0);
50     // On 10.6, the grippy changes appearance slightly when painted the second
51     // time in a textured window. Since this test cares about the window title,
52     // cut off the bottom of the window.
53     bounds.size.height -= 40;
54     bounds.origin.y += 40;
56     [frameView lockFocus];
57     base::scoped_nsobject<NSBitmapImageRep> bitmap(
58         [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
59     [frameView unlockFocus];
61     return [bitmap TIFFRepresentation];
62   }
64   FramedBrowserWindow* window_;
67 // Baseline test that the window creates, displays, closes, and
68 // releases.
69 TEST_F(FramedBrowserWindowTest, ShowAndClose) {
70   [window_ display];
73 // Test that undocumented title-hiding API we're using does the job.
74 TEST_F(FramedBrowserWindowTest, DoesHideTitle) {
75   // The -display calls are not strictly necessary, but they do
76   // make it easier to see what's happening when debugging (without
77   // them the changes are never flushed to the screen).
79   [window_ setTitle:@""];
80   [window_ display];
81   NSData* emptyTitleData = WindowContentsAsTIFF();
83   [window_ setTitle:@"This is a title"];
84   [window_ display];
85   NSData* thisTitleData = WindowContentsAsTIFF();
87   // The default window with a title should look different from the
88   // window with an empty title.
89   EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);
91   [window_ setShouldHideTitle:YES];
92   [window_ setTitle:@""];
93   [window_ display];
94   [window_ setTitle:@"This is a title"];
95   [window_ display];
96   NSData* hiddenTitleData = WindowContentsAsTIFF();
98   // With our magic setting, the window with a title should look the
99   // same as the window with an empty title.
100   EXPECT_TRUE([window_ _isTitleHidden]);
101   EXPECT_TRUE([emptyTitleData isEqualToData:hiddenTitleData]);
104 // Test to make sure that our window widgets are in the right place.
105 TEST_F(FramedBrowserWindowTest, WindowWidgetLocation) {
106   BOOL yes = YES;
107   BOOL no = NO;
109   // First without a tabstrip.
110   [window_ close];
111   window_ = [[FramedBrowserWindow alloc]
112              initWithContentRect:NSMakeRect(0, 0, 800, 600)
113                      hasTabStrip:NO];
114   id controller = [OCMockObject mockForClass:[BrowserWindowController class]];
115   [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
116       isKindOfClass:[BrowserWindowController class]];
117   [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTabStrip];
118   [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTitleBar];
119   [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] isTabbedWindow];
120   [window_ setWindowController:controller];
122   NSView* closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
123   EXPECT_TRUE(closeBoxControl);
124   NSRect closeBoxFrame = [closeBoxControl frame];
125   NSRect windowBounds = [window_ frame];
126   windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
127   windowBounds.origin = NSZeroPoint;
128   EXPECT_EQ(NSMaxY(closeBoxFrame),
129             NSMaxY(windowBounds) -
130                 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
131   EXPECT_EQ(NSMinX(closeBoxFrame),
132             kFramedWindowButtonsWithoutTabStripOffsetFromLeft);
134   NSView* miniaturizeControl =
135       [window_ standardWindowButton:NSWindowMiniaturizeButton];
136   EXPECT_TRUE(miniaturizeControl);
137   NSRect miniaturizeFrame = [miniaturizeControl frame];
138   EXPECT_EQ(NSMaxY(miniaturizeFrame),
139             NSMaxY(windowBounds) -
140                 kFramedWindowButtonsWithoutTabStripOffsetFromTop);
141   EXPECT_EQ(NSMinX(miniaturizeFrame),
142             NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
143   [window_ setWindowController:nil];
145   // Then with a tabstrip.
146   [window_ close];
147   window_ = [[FramedBrowserWindow alloc]
148              initWithContentRect:NSMakeRect(0, 0, 800, 600)
149                      hasTabStrip:YES];
150   controller = [OCMockObject mockForClass:[BrowserWindowController class]];
151   [[[controller stub] andReturnValue:OCMOCK_VALUE(yes)]
152       isKindOfClass:[BrowserWindowController class]];
153   [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] hasTabStrip];
154   [[[controller expect] andReturnValue:OCMOCK_VALUE(no)] hasTitleBar];
155   [[[controller expect] andReturnValue:OCMOCK_VALUE(yes)] isTabbedWindow];
156   [window_ setWindowController:controller];
158   closeBoxControl = [window_ standardWindowButton:NSWindowCloseButton];
159   EXPECT_TRUE(closeBoxControl);
160   closeBoxFrame = [closeBoxControl frame];
161   windowBounds = [window_ frame];
162   windowBounds = [[window_ contentView] convertRect:windowBounds fromView:nil];
163   windowBounds.origin = NSZeroPoint;
164   EXPECT_EQ(NSMaxY(closeBoxFrame),
165             NSMaxY(windowBounds) -
166                 kFramedWindowButtonsWithTabStripOffsetFromTop);
167   EXPECT_EQ(NSMinX(closeBoxFrame),
168             kFramedWindowButtonsWithTabStripOffsetFromLeft);
170   miniaturizeControl = [window_ standardWindowButton:NSWindowMiniaturizeButton];
171   EXPECT_TRUE(miniaturizeControl);
172   miniaturizeFrame = [miniaturizeControl frame];
173   EXPECT_EQ(NSMaxY(miniaturizeFrame),
174             NSMaxY(windowBounds) -
175                 kFramedWindowButtonsWithTabStripOffsetFromTop);
176   EXPECT_EQ(NSMinX(miniaturizeFrame),
177             NSMaxX(closeBoxFrame) + [window_ windowButtonsInterButtonSpacing]);
178   [window_ setWindowController:nil];