| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/scoped_nsobject.h" | 5 #include "base/scoped_nsobject.h" |
| 6 #include "base/scoped_nsautorelease_pool.h" | 6 #include "base/scoped_nsautorelease_pool.h" |
| 7 #include "base/scoped_ptr.h" | 7 #include "base/scoped_ptr.h" |
| 8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/browser_window.h" |
| 9 #include "chrome/browser/cocoa/browser_test_helper.h" | 10 #include "chrome/browser/cocoa/browser_test_helper.h" |
| 10 #include "chrome/browser/cocoa/browser_window_controller.h" | 11 #include "chrome/browser/cocoa/browser_window_controller.h" |
| 11 #include "chrome/browser/cocoa/cocoa_test_helper.h" | 12 #include "chrome/browser/cocoa/cocoa_test_helper.h" |
| 12 #include "chrome/browser/cocoa/find_bar_bridge.h" | 13 #include "chrome/browser/cocoa/find_bar_bridge.h" |
| 13 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/common/pref_service.h" | 15 #include "chrome/common/pref_service.h" |
| 15 #include "chrome/test/testing_browser_process.h" | 16 #include "chrome/test/testing_browser_process.h" |
| 16 #include "chrome/test/testing_profile.h" | 17 #include "chrome/test/testing_profile.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 | 18 |
| 19 @interface BrowserWindowController (JustForTesting) | 19 @interface BrowserWindowController (JustForTesting) |
| 20 // Already defined in BWC. | 20 // Already defined in BWC. |
| 21 - (void)saveWindowPositionToPrefs:(PrefService*)prefs; | 21 - (void)saveWindowPositionToPrefs:(PrefService*)prefs; |
| 22 - (void)layoutSubviews; | 22 - (void)layoutSubviews; |
| 23 @end | 23 @end |
| 24 | 24 |
| 25 @interface BrowserWindowController (ExposedForTesting) | 25 @interface BrowserWindowController (ExposedForTesting) |
| 26 // Implementations are below. | 26 // Implementations are below. |
| 27 - (NSView*)infoBarContainerView; | 27 - (NSView*)infoBarContainerView; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 50 | 50 |
| 51 - (BOOL)bookmarkBarVisible { | 51 - (BOOL)bookmarkBarVisible { |
| 52 return [bookmarkBarController_ isVisible]; | 52 return [bookmarkBarController_ isVisible]; |
| 53 } | 53 } |
| 54 | 54 |
| 55 - (NSView*)extensionShelfView { | 55 - (NSView*)extensionShelfView { |
| 56 return [extensionShelfController_ view]; | 56 return [extensionShelfController_ view]; |
| 57 } | 57 } |
| 58 @end | 58 @end |
| 59 | 59 |
| 60 class BrowserWindowControllerTest : public testing::Test { | 60 class BrowserWindowControllerTest : public CocoaTest { |
| 61 public: |
| 61 virtual void SetUp() { | 62 virtual void SetUp() { |
| 62 controller_.reset([[BrowserWindowController alloc] | 63 CocoaTest::SetUp(); |
| 63 initWithBrowser:browser_helper_.browser() | 64 Browser* browser = browser_helper_.browser(); |
| 64 takeOwnership:NO]); | 65 controller_ = [[BrowserWindowController alloc] initWithBrowser:browser |
| 66 takeOwnership:NO]; |
| 67 } |
| 68 |
| 69 virtual void TearDown() { |
| 70 [controller_ close]; |
| 71 CocoaTest::TearDown(); |
| 65 } | 72 } |
| 66 | 73 |
| 67 public: | 74 public: |
| 68 // Order is very important here. We want the controller deleted | |
| 69 // before the pool, and want the pool deleted before | |
| 70 // BrowserTestHelper. | |
| 71 CocoaTestHelper cocoa_helper_; | |
| 72 BrowserTestHelper browser_helper_; | 75 BrowserTestHelper browser_helper_; |
| 73 base::ScopedNSAutoreleasePool pool_; | 76 BrowserWindowController* controller_; |
| 74 scoped_nsobject<BrowserWindowController> controller_; | |
| 75 }; | 77 }; |
| 76 | 78 |
| 77 TEST_F(BrowserWindowControllerTest, TestSaveWindowPosition) { | 79 TEST_F(BrowserWindowControllerTest, TestSaveWindowPosition) { |
| 78 PrefService* prefs = browser_helper_.profile()->GetPrefs(); | 80 PrefService* prefs = browser_helper_.profile()->GetPrefs(); |
| 79 ASSERT_TRUE(prefs != NULL); | 81 ASSERT_TRUE(prefs != NULL); |
| 80 | 82 |
| 81 // Check to make sure there is no existing pref for window placement. | 83 // Check to make sure there is no existing pref for window placement. |
| 82 ASSERT_TRUE(prefs->GetDictionary(prefs::kBrowserWindowPlacement) == NULL); | 84 ASSERT_TRUE(prefs->GetDictionary(prefs::kBrowserWindowPlacement) == NULL); |
| 83 | 85 |
| 84 // Ask the window to save its position, then check that a preference | 86 // Ask the window to save its position, then check that a preference |
| 85 // exists. We're technically passing in a pointer to the user prefs | 87 // exists. We're technically passing in a pointer to the user prefs |
| 86 // and not the local state prefs, but a PrefService* is a | 88 // and not the local state prefs, but a PrefService* is a |
| 87 // PrefService*, and this is a unittest. | 89 // PrefService*, and this is a unittest. |
| 88 [controller_ saveWindowPositionToPrefs:prefs]; | 90 [controller_ saveWindowPositionToPrefs:prefs]; |
| 89 EXPECT_TRUE(prefs->GetDictionary(prefs::kBrowserWindowPlacement) != NULL); | 91 EXPECT_TRUE(prefs->GetDictionary(prefs::kBrowserWindowPlacement) != NULL); |
| 90 } | 92 } |
| 91 | 93 |
| 92 @interface BrowserWindowControllerFakeFullscreen : BrowserWindowController { | 94 TEST_F(BrowserWindowControllerTest, TestFullScreenWindow) { |
| 93 @private | 95 // Confirm the fullscreen command doesn't return nil. |
| 94 // We release the window ourselves, so we don't have to rely on the unittest | 96 // See BrowserWindowFullScreenControllerTest for more fullscreen tests. |
| 95 // doing it for us. | |
| 96 scoped_nsobject<NSWindow> fullscreenWindow_; | |
| 97 } | |
| 98 @end | |
| 99 @implementation BrowserWindowControllerFakeFullscreen | |
| 100 // Override fullscreenWindow to return a dummy window. This isn't needed to | |
| 101 // pass the test, but because the dummy window is only 100x100, it prevents the | |
| 102 // real fullscreen window from flashing up and taking over the whole screen.. | |
| 103 // We have to return an actual window because layoutSubviews: looks at the | |
| 104 // window's frame. | |
| 105 - (NSWindow*)fullscreenWindow { | |
| 106 if (fullscreenWindow_.get()) | |
| 107 return fullscreenWindow_.get(); | |
| 108 | |
| 109 fullscreenWindow_.reset( | |
| 110 [[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,400,400) | |
| 111 styleMask:NSBorderlessWindowMask | |
| 112 backing:NSBackingStoreBuffered | |
| 113 defer:NO]); | |
| 114 [fullscreenWindow_ setReleasedWhenClosed:NO]; | |
| 115 return fullscreenWindow_.get(); | |
| 116 } | |
| 117 @end | |
| 118 | |
| 119 TEST_F(BrowserWindowControllerTest, TestFullscreen) { | |
| 120 // Note use of "controller", not "controller_" | |
| 121 scoped_nsobject<BrowserWindowController> controller; | |
| 122 controller.reset([[BrowserWindowControllerFakeFullscreen alloc] | |
| 123 initWithBrowser:browser_helper_.browser() | |
| 124 takeOwnership:NO]); | |
| 125 EXPECT_FALSE([controller isFullscreen]); | |
| 126 [controller setFullscreen:YES]; | |
| 127 EXPECT_TRUE([controller isFullscreen]); | |
| 128 [controller setFullscreen:NO]; | |
| 129 EXPECT_FALSE([controller isFullscreen]); | |
| 130 | |
| 131 // Confirm the real fullscreen command doesn't return nil | |
| 132 EXPECT_TRUE([controller_ fullscreenWindow]); | 97 EXPECT_TRUE([controller_ fullscreenWindow]); |
| 133 } | 98 } |
| 134 | 99 |
| 135 TEST_F(BrowserWindowControllerTest, TestActivate) { | |
| 136 // Note use of "controller", not "controller_" | |
| 137 scoped_nsobject<BrowserWindowController> controller; | |
| 138 controller.reset([[BrowserWindowControllerFakeFullscreen alloc] | |
| 139 initWithBrowser:browser_helper_.browser() | |
| 140 takeOwnership:NO]); | |
| 141 EXPECT_FALSE([controller isFullscreen]); | |
| 142 | |
| 143 [controller activate]; | |
| 144 NSWindow* frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; | |
| 145 EXPECT_EQ(frontmostWindow, [controller window]); | |
| 146 | |
| 147 [controller setFullscreen:YES]; | |
| 148 [controller activate]; | |
| 149 frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; | |
| 150 EXPECT_EQ(frontmostWindow, [controller fullscreenWindow]); | |
| 151 | |
| 152 // We have to cleanup after ourselves by unfullscreening. | |
| 153 [controller setFullscreen:NO]; | |
| 154 } | |
| 155 | |
| 156 TEST_F(BrowserWindowControllerTest, TestNormal) { | 100 TEST_F(BrowserWindowControllerTest, TestNormal) { |
| 157 // Force the bookmark bar to be shown. | 101 // Force the bookmark bar to be shown. |
| 158 browser_helper_.profile()->GetPrefs()-> | 102 browser_helper_.profile()->GetPrefs()-> |
| 159 SetBoolean(prefs::kShowBookmarkBar, true); | 103 SetBoolean(prefs::kShowBookmarkBar, true); |
| 160 | 104 |
| 161 // Make sure a normal BrowserWindowController is, uh, normal. | 105 // Make sure a normal BrowserWindowController is, uh, normal. |
| 162 EXPECT_TRUE([controller_ isNormalWindow]); | 106 EXPECT_TRUE([controller_ isNormalWindow]); |
| 163 EXPECT_TRUE([controller_ isBookmarkBarVisible]); | 107 EXPECT_TRUE([controller_ isBookmarkBarVisible]); |
| 164 | 108 |
| 165 // And make sure a controller for a pop-up window is not normal. | 109 // And make sure a controller for a pop-up window is not normal. |
| 166 scoped_ptr<Browser> popup_browser(Browser::CreateForPopup( | 110 // popup_browser will be owned by its window. |
| 167 browser_helper_.profile())); | 111 Browser *popup_browser(Browser::CreateForPopup(browser_helper_.profile())); |
| 168 controller_.reset([[BrowserWindowController alloc] | 112 NSWindow *cocoaWindow = popup_browser->window()->GetNativeHandle(); |
| 169 initWithBrowser:popup_browser.get() | 113 BrowserWindowController* controller = |
| 170 takeOwnership:NO]); | 114 static_cast<BrowserWindowController*>([cocoaWindow windowController]); |
| 171 EXPECT_FALSE([controller_ isNormalWindow]); | 115 ASSERT_TRUE([controller isKindOfClass:[BrowserWindowController class]]); |
| 172 EXPECT_FALSE([controller_ isBookmarkBarVisible]); | 116 EXPECT_FALSE([controller isNormalWindow]); |
| 173 | 117 EXPECT_FALSE([controller isBookmarkBarVisible]); |
| 174 // The created BrowserWindowController gets autoreleased, so make | 118 [controller close]; |
| 175 // sure we don't also release it. | |
| 176 // (Confirmed with valgrind). | |
| 177 controller_.release(); | |
| 178 } | 119 } |
| 179 | 120 |
| 180 @interface GTMTheme (BrowserThemeProviderInitialization) | 121 @interface GTMTheme (BrowserThemeProviderInitialization) |
| 181 + (GTMTheme *)themeWithBrowserThemeProvider:(BrowserThemeProvider *)provider | 122 + (GTMTheme *)themeWithBrowserThemeProvider:(BrowserThemeProvider *)provider |
| 182 isOffTheRecord:(BOOL)isOffTheRecord; | 123 isOffTheRecord:(BOOL)isOffTheRecord; |
| 183 @end | 124 @end |
| 184 | 125 |
| 185 TEST_F(BrowserWindowControllerTest, TestTheme) { | 126 TEST_F(BrowserWindowControllerTest, TestTheme) { |
| 186 [controller_ userChangedTheme]; | 127 [controller_ userChangedTheme]; |
| 187 } | 128 } |
| (...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 defaultFrame:screenFrame]; | 404 defaultFrame:screenFrame]; |
| 464 // Use the actual width of the window frame, since it's subject to rounding. | 405 // Use the actual width of the window frame, since it's subject to rounding. |
| 465 EXPECT_EQ([window frame].size.width, zoomFrame.size.width); | 406 EXPECT_EQ([window frame].size.width, zoomFrame.size.width); |
| 466 EXPECT_EQ(screenFrame.size.height, zoomFrame.size.height); | 407 EXPECT_EQ(screenFrame.size.height, zoomFrame.size.height); |
| 467 EXPECT_EQ(testFrame.origin.x, zoomFrame.origin.x); | 408 EXPECT_EQ(testFrame.origin.x, zoomFrame.origin.x); |
| 468 EXPECT_EQ(screenFrame.origin.y, zoomFrame.origin.y); | 409 EXPECT_EQ(screenFrame.origin.y, zoomFrame.origin.y); |
| 469 } | 410 } |
| 470 | 411 |
| 471 TEST_F(BrowserWindowControllerTest, TestFindBarOnTop) { | 412 TEST_F(BrowserWindowControllerTest, TestFindBarOnTop) { |
| 472 FindBarBridge bridge; | 413 FindBarBridge bridge; |
| 473 [controller_.get() addFindBar:bridge.find_bar_cocoa_controller()]; | 414 [controller_ addFindBar:bridge.find_bar_cocoa_controller()]; |
| 474 | 415 |
| 475 // Test that the Z-order of the find bar is on top of everything. | 416 // Test that the Z-order of the find bar is on top of everything. |
| 476 NSArray* subviews = [[[controller_.get() window] contentView] subviews]; | 417 NSArray* subviews = [[[controller_ window] contentView] subviews]; |
| 477 NSUInteger findBar_index = | 418 NSUInteger findBar_index = |
| 478 [subviews indexOfObject:[controller_.get() findBarView]]; | 419 [subviews indexOfObject:[controller_ findBarView]]; |
| 479 EXPECT_NE(NSNotFound, findBar_index); | 420 EXPECT_NE(NSNotFound, findBar_index); |
| 480 NSUInteger toolbar_index = | 421 NSUInteger toolbar_index = |
| 481 [subviews indexOfObject:[controller_.get() toolbarView]]; | 422 [subviews indexOfObject:[controller_ toolbarView]]; |
| 482 EXPECT_NE(NSNotFound, toolbar_index); | 423 EXPECT_NE(NSNotFound, toolbar_index); |
| 483 NSUInteger bookmark_index = | 424 NSUInteger bookmark_index = |
| 484 [subviews indexOfObject:[controller_.get() bookmarkView]]; | 425 [subviews indexOfObject:[controller_ bookmarkView]]; |
| 485 EXPECT_NE(NSNotFound, bookmark_index); | 426 EXPECT_NE(NSNotFound, bookmark_index); |
| 486 | 427 |
| 487 EXPECT_GT(findBar_index, toolbar_index); | 428 EXPECT_GT(findBar_index, toolbar_index); |
| 488 EXPECT_GT(findBar_index, bookmark_index); | 429 EXPECT_GT(findBar_index, bookmark_index); |
| 489 } | 430 } |
| 490 | 431 |
| 432 @interface BrowserWindowControllerFakeFullscreen : BrowserWindowController { |
| 433 @private |
| 434 // We release the window ourselves, so we don't have to rely on the unittest |
| 435 // doing it for us. |
| 436 scoped_nsobject<NSWindow> fullscreenWindow_; |
| 437 } |
| 438 @end |
| 439 |
| 440 class BrowserWindowFullScreenControllerTest : public CocoaTest { |
| 441 public: |
| 442 virtual void SetUp() { |
| 443 CocoaTest::SetUp(); |
| 444 Browser* browser = browser_helper_.browser(); |
| 445 controller_ = |
| 446 [[BrowserWindowControllerFakeFullscreen alloc] initWithBrowser:browser |
| 447 takeOwnership:NO]; |
| 448 } |
| 449 |
| 450 virtual void TearDown() { |
| 451 [controller_ close]; |
| 452 CocoaTest::TearDown(); |
| 453 } |
| 454 |
| 455 public: |
| 456 BrowserTestHelper browser_helper_; |
| 457 BrowserWindowController* controller_; |
| 458 }; |
| 459 |
| 460 TEST_F(BrowserWindowFullScreenControllerTest, TestFullscreen) { |
| 461 EXPECT_FALSE([controller_ isFullscreen]); |
| 462 [controller_ setFullscreen:YES]; |
| 463 EXPECT_TRUE([controller_ isFullscreen]); |
| 464 [controller_ setFullscreen:NO]; |
| 465 EXPECT_FALSE([controller_ isFullscreen]); |
| 466 } |
| 467 |
| 468 TEST_F(BrowserWindowFullScreenControllerTest, TestActivate) { |
| 469 EXPECT_FALSE([controller_ isFullscreen]); |
| 470 |
| 471 [controller_ activate]; |
| 472 NSWindow* frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; |
| 473 EXPECT_EQ(frontmostWindow, [controller_ window]); |
| 474 |
| 475 [controller_ setFullscreen:YES]; |
| 476 [controller_ activate]; |
| 477 frontmostWindow = [[NSApp orderedWindows] objectAtIndex:0]; |
| 478 EXPECT_EQ(frontmostWindow, [controller_ fullscreenWindow]); |
| 479 |
| 480 // We have to cleanup after ourselves by unfullscreening. |
| 481 [controller_ setFullscreen:NO]; |
| 482 } |
| 483 |
| 484 @implementation BrowserWindowControllerFakeFullscreen |
| 485 // Override fullscreenWindow to return a dummy window. This isn't needed to |
| 486 // pass the test, but because the dummy window is only 100x100, it prevents the |
| 487 // real fullscreen window from flashing up and taking over the whole screen.. |
| 488 // We have to return an actual window because layoutSubviews: looks at the |
| 489 // window's frame. |
| 490 - (NSWindow*)fullscreenWindow { |
| 491 if (fullscreenWindow_.get()) |
| 492 return fullscreenWindow_.get(); |
| 493 |
| 494 fullscreenWindow_.reset( |
| 495 [[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,400,400) |
| 496 styleMask:NSBorderlessWindowMask |
| 497 backing:NSBackingStoreBuffered |
| 498 defer:NO]); |
| 499 return fullscreenWindow_.get(); |
| 500 } |
| 501 @end |
| 491 | 502 |
| 492 /* TODO(???): test other methods of BrowserWindowController */ | 503 /* TODO(???): test other methods of BrowserWindowController */ |
| OLD | NEW |