infobar_container_controller_unittest.mm revision dc0f95d653279beabeb9817299e2902918ba123e
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 <Cocoa/Cocoa.h>
6
7#include "base/scoped_nsobject.h"
8#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
9#import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
10#include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h"
11#include "chrome/browser/ui/cocoa/infobars/mock_link_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
16namespace {
17
18class 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  scoped_nsobject<ViewResizerPong> resizeDelegate_;
37  InfoBarContainerController* controller_;
38};
39
40TEST_VIEW(InfoBarContainerControllerTest, [controller_ view])
41
42TEST_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
50TEST_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  MockLinkInfoBarDelegate linkDelegate, linkDelegate2;
57  MockConfirmInfoBarDelegate confirmDelegate;
58
59  [controller_ addInfoBar:&linkDelegate animate:NO];
60  EXPECT_EQ(1U, [[view subviews] count]);
61
62  [controller_ addInfoBar:&confirmDelegate animate:NO];
63  EXPECT_EQ(2U, [[view subviews] count]);
64
65  [controller_ addInfoBar:&linkDelegate2 animate:NO];
66  EXPECT_EQ(3U, [[view subviews] count]);
67
68  // Just to mix things up, remove them in a different order.
69  [controller_ closeInfoBarsForDelegate:&confirmDelegate animate:NO];
70  EXPECT_EQ(2U, [[view subviews] count]);
71
72  [controller_ closeInfoBarsForDelegate:&linkDelegate animate:NO];
73  EXPECT_EQ(1U, [[view subviews] count]);
74
75  [controller_ closeInfoBarsForDelegate:&linkDelegate2 animate:NO];
76  EXPECT_EQ(0U, [[view subviews] count]);
77}
78
79TEST_F(InfoBarContainerControllerTest, RemoveAllInfoBars) {
80  NSView* view = [controller_ view];
81
82  // Add three infobars and then remove them all.
83  MockLinkInfoBarDelegate linkDelegate;
84  MockConfirmInfoBarDelegate confirmDelegate, confirmDelegate2;
85
86  [controller_ addInfoBar:&linkDelegate animate:NO];
87  [controller_ addInfoBar:&confirmDelegate animate:NO];
88  [controller_ addInfoBar:&confirmDelegate2 animate:NO];
89  EXPECT_EQ(3U, [[view subviews] count]);
90
91  [controller_ removeAllInfoBars];
92  EXPECT_EQ(0U, [[view subviews] count]);
93}
94}  // namespace
95