Upstream version 9.38.198.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/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"
17
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   }
32
33   virtual void TearDown() {
34     [window_ close];
35     CocoaTest::TearDown();
36   }
37
38   // Returns a canonical snapshot of the window.
39   NSData* WindowContentsAsTIFF() {
40     [window_ display];
41
42     NSView* frameView = [window_ contentView];
43     while ([frameView superview]) {
44       frameView = [frameView superview];
45     }
46
47     // Inset to mask off left and right edges which vary in HighDPI.
48     NSRect bounds = NSInsetRect([frameView bounds], 4, 0);
49
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;
55
56     [frameView lockFocus];
57     base::scoped_nsobject<NSBitmapImageRep> bitmap(
58         [[NSBitmapImageRep alloc] initWithFocusedViewRect:bounds]);
59     [frameView unlockFocus];
60
61     return [bitmap TIFFRepresentation];
62   }
63
64   FramedBrowserWindow* window_;
65 };
66
67 // Baseline test that the window creates, displays, closes, and
68 // releases.
69 TEST_F(FramedBrowserWindowTest, ShowAndClose) {
70   [window_ display];
71 }
72
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).
78
79   [window_ setTitle:@""];
80   [window_ display];
81   NSData* emptyTitleData = WindowContentsAsTIFF();
82
83   [window_ setTitle:@"This is a title"];
84   [window_ display];
85   NSData* thisTitleData = WindowContentsAsTIFF();
86
87   // The default window with a title should look different from the
88   // window with an empty title.
89   EXPECT_FALSE([emptyTitleData isEqualToData:thisTitleData]);
90
91   [window_ setShouldHideTitle:YES];
92   [window_ setTitle:@""];
93   [window_ display];
94   [window_ setTitle:@"This is a title"];
95   [window_ display];
96   NSData* hiddenTitleData = WindowContentsAsTIFF();
97
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]);
102 }
103
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;
108
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];
121
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);
133
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];
144
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];
157
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);
169
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];
179 }