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. 4 5 #include "base/memory/scoped_nsobject.h" 6 #include "base/memory/scoped_ptr.h" 7 #include "base/string_util.h" 8 #include "chrome/browser/bookmarks/bookmark_utils.h" 9 #include "chrome/browser/ui/cocoa/browser_test_helper.h" 10 #include "chrome/browser/ui/cocoa/browser_window_cocoa.h" 11 #include "chrome/browser/ui/cocoa/browser_window_controller.h" 12 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" 13 #include "content/common/notification_type.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 // A BrowserWindowCocoa that goes PONG when 17 // BOOKMARK_BAR_VISIBILITY_PREF_CHANGED is sent. This is so we can be 18 // sure we are observing it. 19 class BrowserWindowCocoaPong : public BrowserWindowCocoa { 20 public: 21 BrowserWindowCocoaPong(Browser* browser, 22 BrowserWindowController* controller) : 23 BrowserWindowCocoa(browser, controller, [controller window]) { 24 pong_ = false; 25 } 26 virtual ~BrowserWindowCocoaPong() { } 27 28 void Observe(NotificationType type, 29 const NotificationSource& source, 30 const NotificationDetails& details) { 31 if (type.value == NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED) 32 pong_ = true; 33 BrowserWindowCocoa::Observe(type, source, details); 34 } 35 36 bool pong_; 37 }; 38 39 // Main test class. 40 class BrowserWindowCocoaTest : public CocoaTest { 41 virtual void SetUp() { 42 CocoaTest::SetUp(); 43 Browser* browser = browser_helper_.browser(); 44 controller_ = [[BrowserWindowController alloc] initWithBrowser:browser 45 takeOwnership:NO]; 46 } 47 48 virtual void TearDown() { 49 [controller_ close]; 50 CocoaTest::TearDown(); 51 } 52 53 public: 54 BrowserTestHelper browser_helper_; 55 BrowserWindowController* controller_; 56 }; 57 58 59 TEST_F(BrowserWindowCocoaTest, TestNotification) { 60 BrowserWindowCocoaPong *bwc = 61 new BrowserWindowCocoaPong(browser_helper_.browser(), controller_); 62 63 EXPECT_FALSE(bwc->pong_); 64 bookmark_utils::ToggleWhenVisible(browser_helper_.profile()); 65 // Confirm we are listening 66 EXPECT_TRUE(bwc->pong_); 67 delete bwc; 68 // If this does NOT crash it confirms we stopped listening in the destructor. 69 bookmark_utils::ToggleWhenVisible(browser_helper_.profile()); 70 } 71 72 73 TEST_F(BrowserWindowCocoaTest, TestBookmarkBarVisible) { 74 BrowserWindowCocoaPong *bwc = new BrowserWindowCocoaPong( 75 browser_helper_.browser(), 76 controller_); 77 scoped_ptr<BrowserWindowCocoaPong> scoped_bwc(bwc); 78 79 bool before = bwc->IsBookmarkBarVisible(); 80 bookmark_utils::ToggleWhenVisible(browser_helper_.profile()); 81 EXPECT_NE(before, bwc->IsBookmarkBarVisible()); 82 83 bookmark_utils::ToggleWhenVisible(browser_helper_.profile()); 84 EXPECT_EQ(before, bwc->IsBookmarkBarVisible()); 85 } 86 87 @interface FakeController : NSWindowController { 88 BOOL fullscreen_; 89 } 90 @end 91 92 @implementation FakeController 93 - (void)setFullscreen:(BOOL)fullscreen { 94 fullscreen_ = fullscreen; 95 } 96 - (BOOL)isFullscreen { 97 return fullscreen_; 98 } 99 @end 100 101 TEST_F(BrowserWindowCocoaTest, TestFullscreen) { 102 // Wrap the FakeController in a scoped_nsobject instead of autoreleasing in 103 // windowWillClose: because we never actually open a window in this test (so 104 // windowWillClose: never gets called). 105 scoped_nsobject<FakeController> fake_controller( 106 [[FakeController alloc] init]); 107 BrowserWindowCocoaPong *bwc = new BrowserWindowCocoaPong( 108 browser_helper_.browser(), 109 (BrowserWindowController*)fake_controller.get()); 110 scoped_ptr<BrowserWindowCocoaPong> scoped_bwc(bwc); 111 112 EXPECT_FALSE(bwc->IsFullscreen()); 113 bwc->SetFullscreen(true); 114 EXPECT_TRUE(bwc->IsFullscreen()); 115 bwc->SetFullscreen(false); 116 EXPECT_FALSE(bwc->IsFullscreen()); 117 [fake_controller close]; 118 } 119 120 // TODO(???): test other methods of BrowserWindowCocoa 121