Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / browser_window_cocoa_unittest.mm
blob3ff2a87e7ab82e045b73aa449793fb8b8415642f
1 // Copyright (c) 2011 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 #include "base/mac/scoped_nsobject.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/string_util.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/fullscreen.h"
10 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
11 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/browser/notification_details.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #import "third_party/ocmock/OCMock/OCMock.h"
18 #import "third_party/ocmock/gtest_support.h"
20 // Main test class.
21 class BrowserWindowCocoaTest : public CocoaProfileTest {
22   void SetUp() override {
23     CocoaProfileTest::SetUp();
24     ASSERT_TRUE(browser());
26     controller_ = [[BrowserWindowController alloc] initWithBrowser:browser()
27                                                      takeOwnership:NO];
28   }
30   void TearDown() override {
31     [controller_ close];
32     CocoaProfileTest::TearDown();
33   }
35  public:
36   BrowserWindowController* controller_;
39 TEST_F(BrowserWindowCocoaTest, TestBookmarkBarVisible) {
40   scoped_ptr<BrowserWindowCocoa> bwc(
41       new BrowserWindowCocoa(browser(), controller_));
43   bool before = bwc->IsBookmarkBarVisible();
44   chrome::ToggleBookmarkBarWhenVisible(profile());
45   EXPECT_NE(before, bwc->IsBookmarkBarVisible());
47   chrome::ToggleBookmarkBarWhenVisible(profile());
48   EXPECT_EQ(before, bwc->IsBookmarkBarVisible());
51 // Test that IsMaximized() returns false when the browser window goes from
52 // maximized to minimized state - http://crbug/452976.
53 TEST_F(BrowserWindowCocoaTest, TestMinimizeState) {
54   scoped_ptr<BrowserWindowCocoa> bwc(
55       new BrowserWindowCocoa(browser(), controller_));
57   EXPECT_FALSE(bwc->IsMinimized());
58   bwc->Maximize();
59   EXPECT_TRUE(bwc->IsMaximized());
60   EXPECT_FALSE(bwc->IsMinimized());
61   bwc->Minimize();
62   EXPECT_FALSE(bwc->IsMaximized());
63   EXPECT_TRUE(bwc->IsMinimized());
64   bwc->Restore();
67 // Tests that BrowserWindowCocoa::Close mimics the behavior of
68 // -[NSWindow performClose:].
69 class BrowserWindowCocoaCloseTest : public CocoaProfileTest {
70  public:
71   BrowserWindowCocoaCloseTest()
72       : controller_(
73             [OCMockObject mockForClass:[BrowserWindowController class]]),
74         window_([OCMockObject mockForClass:[NSWindow class]]) {
75     [[[controller_ stub] andReturn:nil] overlayWindow];
76   }
78   void CreateAndCloseBrowserWindow() {
79     BrowserWindowCocoa browser_window(browser(), controller_);
80     browser_window.Close();
81   }
83   id ValueYES() {
84     BOOL v = YES;
85     return OCMOCK_VALUE(v);
86   }
87   id ValueNO() {
88     BOOL v = NO;
89     return OCMOCK_VALUE(v);
90   }
92  protected:
93   id controller_;
94   id window_;
97 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYes) {
98   [[[window_ stub] andReturn:controller_] delegate];
99   [[[controller_ stub] andReturn:window_] window];
100   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
101   [[window_ expect] orderOut:nil];
102   [[window_ expect] close];
103   CreateAndCloseBrowserWindow();
104   EXPECT_OCMOCK_VERIFY(controller_);
105   EXPECT_OCMOCK_VERIFY(window_);
108 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNo) {
109   [[[window_ stub] andReturn:controller_] delegate];
110   [[[controller_ stub] andReturn:window_] window];
111   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
112   // Window should not be closed.
113   CreateAndCloseBrowserWindow();
114   EXPECT_OCMOCK_VERIFY(controller_);
115   EXPECT_OCMOCK_VERIFY(window_);
118 // NSWindow does not implement |-windowShouldClose:|, but subclasses can
119 // implement it, and |-performClose:| will invoke it if implemented.
120 @interface BrowserWindowCocoaCloseWindow : NSWindow
121 - (BOOL)windowShouldClose:(id)window;
122 @end
123 @implementation BrowserWindowCocoaCloseWindow
124 - (BOOL)windowShouldClose:(id)window {
125   return YES;
127 @end
129 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsYes) {
130   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
131   [[[window_ stub] andReturn:nil] delegate];
132   [[[controller_ stub] andReturn:window_] window];
133   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
134   [[window_ expect] orderOut:nil];
135   [[window_ expect] close];
136   CreateAndCloseBrowserWindow();
137   EXPECT_OCMOCK_VERIFY(controller_);
138   EXPECT_OCMOCK_VERIFY(window_);
141 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsNo) {
142   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
143   [[[window_ stub] andReturn:nil] delegate];
144   [[[controller_ stub] andReturn:window_] window];
145   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
146   // Window should not be closed.
147   CreateAndCloseBrowserWindow();
148   EXPECT_OCMOCK_VERIFY(controller_);
149   EXPECT_OCMOCK_VERIFY(window_);
152 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYesWindowRespondsNo) {
153   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
154   [[[window_ stub] andReturn:controller_] delegate];
155   [[[controller_ stub] andReturn:window_] window];
156   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
157   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
158   [[window_ expect] orderOut:nil];
159   [[window_ expect] close];
160   CreateAndCloseBrowserWindow();
161   EXPECT_OCMOCK_VERIFY(controller_);
162   EXPECT_OCMOCK_VERIFY(window_);
165 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNoWindowRespondsYes) {
166   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
167   [[[window_ stub] andReturn:controller_] delegate];
168   [[[controller_ stub] andReturn:window_] window];
169   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
170   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
171   // Window should not be closed.
172   CreateAndCloseBrowserWindow();
173   EXPECT_OCMOCK_VERIFY(controller_);
174   EXPECT_OCMOCK_VERIFY(window_);
177 TEST_F(BrowserWindowCocoaCloseTest, NoResponseFromDelegateNorWindow) {
178   [[[window_ stub] andReturn:nil] delegate];
179   [[[controller_ stub] andReturn:window_] window];
180   [[window_ expect] orderOut:nil];
181   [[window_ expect] close];
182   CreateAndCloseBrowserWindow();
183   EXPECT_OCMOCK_VERIFY(controller_);
184   EXPECT_OCMOCK_VERIFY(window_);
187 // TODO(???): test other methods of BrowserWindowCocoa