Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / framed_browser_window_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
18
19 class FramedBrowserWindowTest : public CocoaTest {
20  public:
21   virtual void SetUp() {
22     CocoaTest::SetUp();
23     // Create a window.
24     window_ = [[FramedBrowserWindow alloc]
25                initWithContentRect:NSMakeRect(0, 0, 800, 600)
26                        hasTabStrip:YES];
27     if (base::debug::BeingDebugged()) {
28       [window_ orderFront:nil];
29     } else {
30       [window_ orderBack:nil];
31     }
32   }
33
34   virtual void TearDown() {
35     [window_ close];
36     CocoaTest::TearDown();
37   }
38
39   // Returns a canonical snapshot of the window.
40   NSData* WindowContentsAsTIFF() {
41     [window_ display];
42
43     NSView* frameView = [window_ contentView];
44     while ([frameView superview]) {
45       frameView = [frameView superview];
46     }
47
48     // Inset to mask off left and right edges which vary in HighDPI.
49     NSRect bounds = NSInsetRect([frameView bounds], 4, 0);
50
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;
56
57     [frameView lockFocus];
58     base::scoped_nsobject<NSBitmapImageRep> bitmap(
59         [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
60     [frameView unlockFocus];
61
62     return [bitmap TIFFRepresentation];
63   }
64
65   FramedBrowserWindow* window_;
66 };
67
68 // Baseline test that the window creates, displays, closes, and
69 // releases.
70 TEST_F(FramedBrowserWindowTest, ShowAndClose) {
71   [window_ display];
72 }
73
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).
79
80   [window_ setTitle:@""];
81   [window_ display];
82   NSData* emptyTitleData = WindowContentsAsTIFF();
83
84   [window_ setTitle:@"This is a title"];
85   [window_ display];
86   NSData* thisTitleData = WindowContentsAsTIFF();
87
88   // The default window with a title should look different from the
89   // window with an empty title.
90   EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);
91
92   [window_ setShouldHideTitle:YES];
93   [window_ setTitle:@""];
94   [window_ display];
95   [window_ setTitle:@"This is a title"];
96   [window_ display];
97   NSData* hiddenTitleData = WindowContentsAsTIFF();
98
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]);
102
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]);
106 }
107
108 // Test to make sure that our window widgets are in the right place.
109 TEST_F(FramedBrowserWindowTest, WindowWidgetLocation) {
110   BOOL yes = YES;
111   BOOL no = NO;
112
113   // First without a tabstrip.
114   [window_ close];
115   window_ = [[FramedBrowserWindow alloc]
116              initWithContentRect:NSMakeRect(0, 0, 800, 600)
117                      hasTabStrip:NO];
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];
125
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);
137
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];
148
149   // Then with a tabstrip.
150   [window_ close];
151   window_ = [[FramedBrowserWindow alloc]
152              initWithContentRect:NSMakeRect(0, 0, 800, 600)
153                      hasTabStrip:YES];
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];
161
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);
173
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];
183 }