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 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" 6 7 #import <Cocoa/Cocoa.h> 8 9 #include "base/mac/scoped_nsobject.h" 10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" 11 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" 12 #import "chrome/browser/ui/cocoa/view_resizer_pong.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/platform_test.h" 15 16 namespace { 17 18 class InfoBarContainerControllerTest : public CocoaTest { 19 virtual void SetUp() { 20 CocoaTest::SetUp(); 21 resizeDelegate_.reset([[ViewResizerPong alloc] init]); 22 ViewResizerPong *viewResizer = resizeDelegate_.get(); 23 controller_ = 24 [[InfoBarContainerController alloc] initWithResizeDelegate:viewResizer]; 25 NSView* view = [controller_ view]; 26 [[test_window() contentView] addSubview:view]; 27 } 28 29 virtual void TearDown() { 30 [[controller_ view] removeFromSuperviewWithoutNeedingDisplay]; 31 [controller_ release]; 32 CocoaTest::TearDown(); 33 } 34 35 public: 36 base::scoped_nsobject<ViewResizerPong> resizeDelegate_; 37 InfoBarContainerController* controller_; 38 }; 39 40 TEST_VIEW(InfoBarContainerControllerTest, [controller_ view]) 41 42 TEST_F(InfoBarContainerControllerTest, BWCPong) { 43 // Call positionInfoBarsAndResize and check that |resizeDelegate_| got a 44 // resize message. 45 [resizeDelegate_ setHeight:-1]; 46 [controller_ positionInfoBarsAndRedraw]; 47 EXPECT_NE(-1, [resizeDelegate_ height]); 48 } 49 50 TEST_F(InfoBarContainerControllerTest, AddAndRemoveInfoBars) { 51 NSView* view = [controller_ view]; 52 53 // Add three infobars and then remove them. 54 // After each step check to make sure we have the correct number of 55 // infobar subviews. 56 57 // This delegate deletes itself when they're told their infobars have closed. 58 InfoBarDelegate* confirmDelegate = new MockConfirmInfoBarDelegate(NULL); 59 60 [controller_ addInfoBar:confirmDelegate->CreateInfoBar(NULL) animate:NO]; 61 EXPECT_EQ(1U, [[view subviews] count]); 62 63 // Just to mix things up, remove them in a different order. 64 [controller_ closeInfoBarsForDelegate:confirmDelegate animate:NO]; 65 EXPECT_EQ(0U, [[view subviews] count]); 66 } 67 68 TEST_F(InfoBarContainerControllerTest, RemoveAllInfoBars) { 69 NSView* view = [controller_ view]; 70 71 // Add three infobars and then remove them all. 72 73 // removeAllInfobars does not close these, so we stack-allocate them so 74 // they'll get cleaned up. 75 MockConfirmInfoBarDelegate confirmDelegate(NULL); 76 MockConfirmInfoBarDelegate confirmDelegate2(NULL); 77 InfoBarDelegate* confirmDelegatePtr = &confirmDelegate; 78 InfoBarDelegate* confirmDelegate2Ptr = &confirmDelegate2; 79 80 [controller_ addInfoBar:confirmDelegatePtr->CreateInfoBar(NULL) animate:NO]; 81 [controller_ addInfoBar:confirmDelegate2Ptr->CreateInfoBar(NULL) animate:NO]; 82 EXPECT_EQ(2U, [[view subviews] count]); 83 84 [controller_ removeAllInfoBars]; 85 EXPECT_EQ(0U, [[view subviews] count]); 86 } 87 88 } // namespace 89