summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/infobar_container_controller_unittest.mm
blob: 98087486de697a850c71a8d78d016bd9aa2331d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "base/scoped_nsautorelease_pool.h"
#include "base/scoped_nsobject.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#import "chrome/browser/cocoa/infobar_container_controller.h"
#include "chrome/browser/cocoa/infobar_test_helper.h"
#import "chrome/browser/cocoa/view_resizer_pong.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

namespace {

class InfoBarContainerControllerTest : public CocoaTest {
  virtual void SetUp() {
    CocoaTest::SetUp();
    resizeDelegate_.reset([[ViewResizerPong alloc] init]);
    TabStripModel* model = browser_helper_.browser()->tabstrip_model();
    ViewResizerPong *viewResizer = resizeDelegate_.get();
    controller_ =
        [[InfoBarContainerController alloc] initWithTabStripModel:model
                                                   resizeDelegate:viewResizer];
    NSView* view = [controller_ view];
    [[test_window() contentView] addSubview:view];
  }

  virtual void TearDown() {
    [[controller_ view] removeFromSuperviewWithoutNeedingDisplay];
    [controller_ release];
    CocoaTest::TearDown();
  }

 public:
  BrowserTestHelper browser_helper_;
  scoped_nsobject<ViewResizerPong> resizeDelegate_;
  InfoBarContainerController* controller_;
};

TEST_VIEW(InfoBarContainerControllerTest, [controller_ view])

TEST_F(InfoBarContainerControllerTest, BWCPong) {
  // Call positionInfoBarsAndResize and check that |resizeDelegate_| got a
  // resize message.
  [resizeDelegate_ setHeight:-1];
  [controller_ positionInfoBarsAndRedraw];
  EXPECT_NE(-1, [resizeDelegate_ height]);
}

TEST_F(InfoBarContainerControllerTest, AddAndRemoveInfoBars) {
  NSView* view = [controller_ view];

  // Add three infobars, one of each type, and then remove them.
  // After each step check to make sure we have the correct number of
  // infobar subviews.
  MockAlertInfoBarDelegate alertDelegate;
  MockLinkInfoBarDelegate linkDelegate;
  MockConfirmInfoBarDelegate confirmDelegate;

  [controller_ addInfoBar:&alertDelegate animate:NO];
  EXPECT_EQ(1U, [[view subviews] count]);

  [controller_ addInfoBar:&linkDelegate animate:NO];
  EXPECT_EQ(2U, [[view subviews] count]);

  [controller_ addInfoBar:&confirmDelegate animate:NO];
  EXPECT_EQ(3U, [[view subviews] count]);

  // Just to mix things up, remove them in a different order.
  [controller_ closeInfoBarsForDelegate:&linkDelegate animate:NO];
  EXPECT_EQ(2U, [[view subviews] count]);

  [controller_ closeInfoBarsForDelegate:&confirmDelegate animate:NO];
  EXPECT_EQ(1U, [[view subviews] count]);

  [controller_ closeInfoBarsForDelegate:&alertDelegate animate:NO];
  EXPECT_EQ(0U, [[view subviews] count]);
}

TEST_F(InfoBarContainerControllerTest, RemoveAllInfoBars) {
  NSView* view = [controller_ view];

  // Add three infobars and then remove them all.
  MockAlertInfoBarDelegate alertDelegate;
  MockLinkInfoBarDelegate linkDelegate;
  MockConfirmInfoBarDelegate confirmDelegate;

  [controller_ addInfoBar:&alertDelegate animate:NO];
  [controller_ addInfoBar:&linkDelegate animate:NO];
  [controller_ addInfoBar:&confirmDelegate animate:NO];
  EXPECT_EQ(3U, [[view subviews] count]);

  [controller_ removeAllInfoBars];
  EXPECT_EQ(0U, [[view subviews] count]);
}
}  // namespace