Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / browser_window_cocoa_unittest.mm
bloba2e6acc747d89e3ba81e1007612d0472f60532e7
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/mac_util.h"
6 #include "base/mac/scoped_nsobject.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/fullscreen.h"
11 #include "chrome/browser/ui/bookmarks/bookmark_utils.h"
12 #import "chrome/browser/ui/cocoa/browser_window_cocoa.h"
13 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
14 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
15 #include "chrome/common/pref_names.h"
16 #include "content/public/browser/notification_details.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #import "third_party/ocmock/OCMock/OCMock.h"
19 #import "third_party/ocmock/gtest_support.h"
21 // Main test class.
22 class BrowserWindowCocoaTest : public CocoaProfileTest {
23   virtual void SetUp() {
24     CocoaProfileTest::SetUp();
25     ASSERT_TRUE(browser());
27     controller_ = [[BrowserWindowController alloc] initWithBrowser:browser()
28                                                      takeOwnership:NO];
29   }
31   virtual void TearDown() {
32     [controller_ close];
33     CocoaProfileTest::TearDown();
34   }
36  public:
37   BrowserWindowController* controller_;
40 TEST_F(BrowserWindowCocoaTest, TestBookmarkBarVisible) {
41   scoped_ptr<BrowserWindowCocoa> bwc(
42       new BrowserWindowCocoa(browser(), controller_));
44   bool before = bwc->IsBookmarkBarVisible();
45   chrome::ToggleBookmarkBarWhenVisible(profile());
46   EXPECT_NE(before, bwc->IsBookmarkBarVisible());
48   chrome::ToggleBookmarkBarWhenVisible(profile());
49   EXPECT_EQ(before, bwc->IsBookmarkBarVisible());
52 @interface FakeController : NSWindowController {
53   enum { kNormal, kFullscreen, kPresentation } windowState_;
55 @end
57 @implementation FakeController
58 - (void)enterFullscreen {
59   windowState_ = kFullscreen;
61 - (void)exitFullscreen {
62   windowState_ = kNormal;
64 - (BOOL)isFullscreen {
65   return windowState_ != kNormal;
67 - (void)enterPresentationModeForURL:(const GURL&)url
68                          bubbleType:(FullscreenExitBubbleType)bubbleType {
69   windowState_ = kPresentation;
71 - (void)exitPresentationMode {
72   windowState_ = kNormal;
74 - (BOOL)inPresentationMode {
75   return windowState_ == kPresentation;
77 @end
79 TEST_F(BrowserWindowCocoaTest, TestFullscreen) {
80   // Wrap the FakeController in a scoped_nsobject instead of autoreleasing in
81   // windowWillClose: because we never actually open a window in this test (so
82   // windowWillClose: never gets called).
83   base::scoped_nsobject<FakeController> fake_controller(
84       [[FakeController alloc] init]);
85   scoped_ptr<BrowserWindowCocoa> bwc(new BrowserWindowCocoa(
86       browser(), static_cast<BrowserWindowController*>(fake_controller.get())));
88   EXPECT_FALSE(bwc->IsFullscreen());
89   bwc->EnterFullscreen(GURL(), FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION);
90   EXPECT_FALSE(bwc->IsFullscreenWithChrome());
91   EXPECT_TRUE(bwc->IsFullscreenWithoutChrome());
92   bwc->ExitFullscreen();
93   EXPECT_FALSE(bwc->IsFullscreen());
94   [fake_controller close];
97 TEST_F(BrowserWindowCocoaTest, TestFullscreenWithChrome) {
98   if (!chrome::mac::SupportsSystemFullscreen())
99     return;
100   // Wrap the FakeController in a scoped_nsobject instead of autoreleasing in
101   // windowWillClose: because we never actually open a window in this test (so
102   // windowWillClose: never gets called).
103   base::scoped_nsobject<FakeController> fake_controller(
104       [[FakeController alloc] init]);
105   scoped_ptr<BrowserWindowCocoa> bwc(new BrowserWindowCocoa(
106       browser(), static_cast<BrowserWindowController*>(fake_controller.get())));
108   EXPECT_FALSE(bwc->IsFullscreen());
109   bwc->EnterFullscreenWithChrome();
110   EXPECT_TRUE(bwc->IsFullscreenWithChrome());
111   EXPECT_FALSE(bwc->IsFullscreenWithoutChrome());
112   bwc->ExitFullscreen();
113   EXPECT_FALSE(bwc->IsFullscreen());
114   [fake_controller close];
117 // Tests that BrowserWindowCocoa::Close mimics the behavior of
118 // -[NSWindow performClose:].
119 class BrowserWindowCocoaCloseTest : public CocoaProfileTest {
120  public:
121   BrowserWindowCocoaCloseTest()
122       : controller_(
123             [OCMockObject mockForClass:[BrowserWindowController class]]),
124         window_([OCMockObject mockForClass:[NSWindow class]]) {
125     [[[controller_ stub] andReturn:nil] overlayWindow];
126   }
128   void CreateAndCloseBrowserWindow() {
129     BrowserWindowCocoa browser_window(browser(), controller_);
130     browser_window.Close();
131   }
133   id ValueYES() {
134     BOOL v = YES;
135     return OCMOCK_VALUE(v);
136   }
137   id ValueNO() {
138     BOOL v = NO;
139     return OCMOCK_VALUE(v);
140   }
142  protected:
143   id controller_;
144   id window_;
147 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYes) {
148   [[[window_ stub] andReturn:controller_] delegate];
149   [[[controller_ stub] andReturn:window_] window];
150   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
151   [[window_ expect] orderOut:nil];
152   [[window_ expect] close];
153   CreateAndCloseBrowserWindow();
154   EXPECT_OCMOCK_VERIFY(controller_);
155   EXPECT_OCMOCK_VERIFY(window_);
158 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNo) {
159   [[[window_ stub] andReturn:controller_] delegate];
160   [[[controller_ stub] andReturn:window_] window];
161   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
162   // Window should not be closed.
163   CreateAndCloseBrowserWindow();
164   EXPECT_OCMOCK_VERIFY(controller_);
165   EXPECT_OCMOCK_VERIFY(window_);
168 // NSWindow does not implement |-windowShouldClose:|, but subclasses can
169 // implement it, and |-performClose:| will invoke it if implemented.
170 @interface BrowserWindowCocoaCloseWindow : NSWindow
171 - (BOOL)windowShouldClose:(id)window;
172 @end
173 @implementation BrowserWindowCocoaCloseWindow
174 - (BOOL)windowShouldClose:(id)window {
175   return YES;
177 @end
179 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsYes) {
180   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
181   [[[window_ stub] andReturn:nil] delegate];
182   [[[controller_ stub] andReturn:window_] window];
183   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
184   [[window_ expect] orderOut:nil];
185   [[window_ expect] close];
186   CreateAndCloseBrowserWindow();
187   EXPECT_OCMOCK_VERIFY(controller_);
188   EXPECT_OCMOCK_VERIFY(window_);
191 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsNo) {
192   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
193   [[[window_ stub] andReturn:nil] delegate];
194   [[[controller_ stub] andReturn:window_] window];
195   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
196   // Window should not be closed.
197   CreateAndCloseBrowserWindow();
198   EXPECT_OCMOCK_VERIFY(controller_);
199   EXPECT_OCMOCK_VERIFY(window_);
202 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYesWindowRespondsNo) {
203   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
204   [[[window_ stub] andReturn:controller_] delegate];
205   [[[controller_ stub] andReturn:window_] window];
206   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
207   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
208   [[window_ expect] orderOut:nil];
209   [[window_ expect] close];
210   CreateAndCloseBrowserWindow();
211   EXPECT_OCMOCK_VERIFY(controller_);
212   EXPECT_OCMOCK_VERIFY(window_);
215 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNoWindowRespondsYes) {
216   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
217   [[[window_ stub] andReturn:controller_] delegate];
218   [[[controller_ stub] andReturn:window_] window];
219   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
220   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
221   // Window should not be closed.
222   CreateAndCloseBrowserWindow();
223   EXPECT_OCMOCK_VERIFY(controller_);
224   EXPECT_OCMOCK_VERIFY(window_);
227 TEST_F(BrowserWindowCocoaCloseTest, NoResponseFromDelegateNorWindow) {
228   [[[window_ stub] andReturn:nil] delegate];
229   [[[controller_ stub] andReturn:window_] window];
230   [[window_ expect] orderOut:nil];
231   [[window_ expect] close];
232   CreateAndCloseBrowserWindow();
233   EXPECT_OCMOCK_VERIFY(controller_);
234   EXPECT_OCMOCK_VERIFY(window_);
237 // TODO(???): test other methods of BrowserWindowCocoa