[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / browser_window_cocoa_unittest.mm
blob4bc8732fd7a11dc139ef3de2163a621928589ca6
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 "chrome/test/base/testing_profile.h"
17 #include "content/public/browser/notification_details.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #import "third_party/ocmock/OCMock/OCMock.h"
20 #import "third_party/ocmock/gtest_support.h"
22 // Main test class.
23 class BrowserWindowCocoaTest : public CocoaProfileTest {
24   virtual void SetUp() {
25     CocoaProfileTest::SetUp();
26     ASSERT_TRUE(browser());
28     controller_ = [[BrowserWindowController alloc] initWithBrowser:browser()
29                                                      takeOwnership:NO];
30   }
32   virtual void TearDown() {
33     [controller_ close];
34     CocoaProfileTest::TearDown();
35   }
37  public:
38   BrowserWindowController* controller_;
41 TEST_F(BrowserWindowCocoaTest, TestBookmarkBarVisible) {
42   scoped_ptr<BrowserWindowCocoa> bwc(
43       new BrowserWindowCocoa(browser(), controller_));
45   bool before = bwc->IsBookmarkBarVisible();
46   chrome::ToggleBookmarkBarWhenVisible(profile());
47   EXPECT_NE(before, bwc->IsBookmarkBarVisible());
49   chrome::ToggleBookmarkBarWhenVisible(profile());
50   EXPECT_EQ(before, bwc->IsBookmarkBarVisible());
53 @interface FakeController : NSWindowController {
54   enum { kNormal, kFullscreen, kPresentation } windowState_;
56 @end
58 @implementation FakeController
59 - (void)enterFullscreen {
60   windowState_ = kFullscreen;
62 - (void)exitFullscreen {
63   windowState_ = kNormal;
65 - (BOOL)isFullscreen {
66   return windowState_ != kNormal;
68 - (void)enterPresentationModeForURL:(const GURL&)url
69                          bubbleType:(FullscreenExitBubbleType)bubbleType {
70   windowState_ = kPresentation;
72 - (void)exitPresentationMode {
73   windowState_ = kNormal;
75 - (BOOL)inPresentationMode {
76   return windowState_ == kPresentation;
78 @end
80 TEST_F(BrowserWindowCocoaTest, TestFullscreen) {
81   // Wrap the FakeController in a scoped_nsobject instead of autoreleasing in
82   // windowWillClose: because we never actually open a window in this test (so
83   // windowWillClose: never gets called).
84   base::scoped_nsobject<FakeController> fake_controller(
85       [[FakeController alloc] init]);
86   scoped_ptr<BrowserWindowCocoa> bwc(new BrowserWindowCocoa(
87       browser(), static_cast<BrowserWindowController*>(fake_controller.get())));
89   EXPECT_FALSE(bwc->IsFullscreen());
90   bwc->EnterFullscreen(GURL(), FEB_TYPE_BROWSER_FULLSCREEN_EXIT_INSTRUCTION);
91   EXPECT_FALSE(bwc->IsFullscreenWithChrome());
92   EXPECT_TRUE(bwc->IsFullscreenWithoutChrome());
93   bwc->ExitFullscreen();
94   EXPECT_FALSE(bwc->IsFullscreen());
95   [fake_controller close];
98 TEST_F(BrowserWindowCocoaTest, TestFullscreenWithChrome) {
99   if (!chrome::mac::SupportsSystemFullscreen())
100     return;
101   // Wrap the FakeController in a scoped_nsobject instead of autoreleasing in
102   // windowWillClose: because we never actually open a window in this test (so
103   // windowWillClose: never gets called).
104   base::scoped_nsobject<FakeController> fake_controller(
105       [[FakeController alloc] init]);
106   scoped_ptr<BrowserWindowCocoa> bwc(new BrowserWindowCocoa(
107       browser(), static_cast<BrowserWindowController*>(fake_controller.get())));
109   EXPECT_FALSE(bwc->IsFullscreen());
110   bwc->EnterFullscreenWithChrome();
111   EXPECT_TRUE(bwc->IsFullscreenWithChrome());
112   EXPECT_FALSE(bwc->IsFullscreenWithoutChrome());
113   bwc->ExitFullscreen();
114   EXPECT_FALSE(bwc->IsFullscreen());
115   [fake_controller close];
118 // Tests that BrowserWindowCocoa::Close mimics the behavior of
119 // -[NSWindow performClose:].
120 class BrowserWindowCocoaCloseTest : public CocoaProfileTest {
121  public:
122   BrowserWindowCocoaCloseTest()
123       : controller_(
124             [OCMockObject mockForClass:[BrowserWindowController class]]),
125         window_([OCMockObject mockForClass:[NSWindow class]]) {
126     [[[controller_ stub] andReturn:nil] overlayWindow];
127   }
129   void CreateAndCloseBrowserWindow() {
130     BrowserWindowCocoa browser_window(browser(), controller_);
131     browser_window.Close();
132   }
134   id ValueYES() {
135     BOOL v = YES;
136     return OCMOCK_VALUE(v);
137   }
138   id ValueNO() {
139     BOOL v = NO;
140     return OCMOCK_VALUE(v);
141   }
143  protected:
144   id controller_;
145   id window_;
148 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYes) {
149   [[[window_ stub] andReturn:controller_] delegate];
150   [[[controller_ stub] andReturn:window_] window];
151   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
152   [[window_ expect] orderOut:nil];
153   [[window_ expect] close];
154   CreateAndCloseBrowserWindow();
155   EXPECT_OCMOCK_VERIFY(controller_);
156   EXPECT_OCMOCK_VERIFY(window_);
159 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNo) {
160   [[[window_ stub] andReturn:controller_] delegate];
161   [[[controller_ stub] andReturn:window_] window];
162   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
163   // Window should not be closed.
164   CreateAndCloseBrowserWindow();
165   EXPECT_OCMOCK_VERIFY(controller_);
166   EXPECT_OCMOCK_VERIFY(window_);
169 // NSWindow does not implement |-windowShouldClose:|, but subclasses can
170 // implement it, and |-performClose:| will invoke it if implemented.
171 @interface BrowserWindowCocoaCloseWindow : NSWindow
172 - (BOOL)windowShouldClose:(id)window;
173 @end
174 @implementation BrowserWindowCocoaCloseWindow
175 - (BOOL)windowShouldClose:(id)window {
176   return YES;
178 @end
180 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsYes) {
181   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
182   [[[window_ stub] andReturn:nil] delegate];
183   [[[controller_ stub] andReturn:window_] window];
184   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
185   [[window_ expect] orderOut:nil];
186   [[window_ expect] close];
187   CreateAndCloseBrowserWindow();
188   EXPECT_OCMOCK_VERIFY(controller_);
189   EXPECT_OCMOCK_VERIFY(window_);
192 TEST_F(BrowserWindowCocoaCloseTest, WindowRespondsNo) {
193   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
194   [[[window_ stub] andReturn:nil] delegate];
195   [[[controller_ stub] andReturn:window_] window];
196   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
197   // Window should not be closed.
198   CreateAndCloseBrowserWindow();
199   EXPECT_OCMOCK_VERIFY(controller_);
200   EXPECT_OCMOCK_VERIFY(window_);
203 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsYesWindowRespondsNo) {
204   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
205   [[[window_ stub] andReturn:controller_] delegate];
206   [[[controller_ stub] andReturn:window_] window];
207   [[[controller_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
208   [[[window_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
209   [[window_ expect] orderOut:nil];
210   [[window_ expect] close];
211   CreateAndCloseBrowserWindow();
212   EXPECT_OCMOCK_VERIFY(controller_);
213   EXPECT_OCMOCK_VERIFY(window_);
216 TEST_F(BrowserWindowCocoaCloseTest, DelegateRespondsNoWindowRespondsYes) {
217   window_ = [OCMockObject mockForClass:[BrowserWindowCocoaCloseWindow class]];
218   [[[window_ stub] andReturn:controller_] delegate];
219   [[[controller_ stub] andReturn:window_] window];
220   [[[controller_ stub] andReturnValue:ValueNO()] windowShouldClose:window_];
221   [[[window_ stub] andReturnValue:ValueYES()] windowShouldClose:window_];
222   // Window should not be closed.
223   CreateAndCloseBrowserWindow();
224   EXPECT_OCMOCK_VERIFY(controller_);
225   EXPECT_OCMOCK_VERIFY(window_);
228 TEST_F(BrowserWindowCocoaCloseTest, NoResponseFromDelegateNorWindow) {
229   [[[window_ stub] andReturn:nil] delegate];
230   [[[controller_ stub] andReturn:window_] window];
231   [[window_ expect] orderOut:nil];
232   [[window_ expect] close];
233   CreateAndCloseBrowserWindow();
234   EXPECT_OCMOCK_VERIFY(controller_);
235   EXPECT_OCMOCK_VERIFY(window_);
238 // TODO(???): test other methods of BrowserWindowCocoa