summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/infobar_controller_unittest.mm
blob: c6c45c90d730673d3ae843a7898082ad0cc6715c (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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_nsobject.h"
#include "base/string_util.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#import "chrome/browser/cocoa/infobar_controller.h"
#include "chrome/browser/cocoa/infobar_test_helper.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

@interface InfoBarController (ExposedForTesting)
- (NSTextField*)label;
@end

@implementation InfoBarController (ExposedForTesting)
- (NSTextField*)label {
  return label_;
}
@end

namespace {

///////////////////////////////////////////////////////////////////////////
// Test fixtures

class AlertInfoBarControllerTest : public PlatformTest {
 public:
  virtual void SetUp() {
    PlatformTest::SetUp();

    controller_.reset(
        [[AlertInfoBarController alloc] initWithDelegate:&delegate_]);
    [helper_.contentView() addSubview:[controller_ view]];
  }

 protected:
  CocoaTestHelper helper_;
  MockAlertInfoBarDelegate delegate_;
  scoped_nsobject<AlertInfoBarController> controller_;
};

class LinkInfoBarControllerTest : public PlatformTest {
 public:
  virtual void SetUp() {
    PlatformTest::SetUp();

    controller_.reset(
        [[LinkInfoBarController alloc] initWithDelegate:&delegate_]);
    [helper_.contentView() addSubview:[controller_ view]];
  }

 protected:
  CocoaTestHelper helper_;
  MockLinkInfoBarDelegate delegate_;
  scoped_nsobject<LinkInfoBarController> controller_;
};

class ConfirmInfoBarControllerTest : public PlatformTest {
 public:
  virtual void SetUp() {
    PlatformTest::SetUp();

    controller_.reset(
        [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]);
    [helper_.contentView() addSubview:[controller_ view]];
  }

 protected:
  CocoaTestHelper helper_;
  MockConfirmInfoBarDelegate delegate_;
  scoped_nsobject<ConfirmInfoBarController> controller_;
};


////////////////////////////////////////////////////////////////////////////
// Tests

TEST_F(AlertInfoBarControllerTest, ShowAndDismiss) {
  // Make sure someone looked at the message and icon.
  EXPECT_TRUE(delegate_.message_text_accessed);
  EXPECT_TRUE(delegate_.icon_accessed);

  // Check to make sure the infobar message was set properly.
  EXPECT_EQ(std::wstring(kMockAlertInfoBarMessage),
            base::SysNSStringToWide([[controller_.get() label] stringValue]));

  // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
  [controller_ dismiss:nil];
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(AlertInfoBarControllerTest, DeallocController) {
  // Test that dealloc'ing the controller does not send an
  // InfoBarClosed() message to the delegate.
  controller_.reset(nil);
  EXPECT_FALSE(delegate_.closed);
}

TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) {
  // Make sure someone looked at the message, link, and icon.
  EXPECT_TRUE(delegate_.message_text_accessed);
  EXPECT_TRUE(delegate_.link_text_accessed);
  EXPECT_TRUE(delegate_.icon_accessed);

  // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
  [controller_ dismiss:nil];
  EXPECT_FALSE(delegate_.link_clicked);
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) {
  // Check that clicking on the link calls LinkClicked() on the
  // delegate.  It should also close the infobar.
  [controller_ linkClicked];
  EXPECT_TRUE(delegate_.link_clicked);
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
  delegate_.closes_on_action = false;

  // Check that clicking on the link calls LinkClicked() on the
  // delegate.  It should not close the infobar.
  [controller_ linkClicked];
  EXPECT_TRUE(delegate_.link_clicked);
  EXPECT_FALSE(delegate_.closed);
}

TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) {
  // Make sure someone looked at the message and icon.
  EXPECT_TRUE(delegate_.message_text_accessed);
  EXPECT_TRUE(delegate_.icon_accessed);

  // Check to make sure the infobar message was set properly.
  EXPECT_EQ(std::wstring(kMockConfirmInfoBarMessage),
            base::SysNSStringToWide([[controller_.get() label] stringValue]));

  // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
  [controller_ dismiss:nil];
  EXPECT_FALSE(delegate_.ok_clicked);
  EXPECT_FALSE(delegate_.cancel_clicked);
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) {
  // Check that clicking the OK button calls Accept() and then closes
  // the infobar.
  [controller_ ok:nil];
  EXPECT_TRUE(delegate_.ok_clicked);
  EXPECT_FALSE(delegate_.cancel_clicked);
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) {
  delegate_.closes_on_action = false;

  // Check that clicking the OK button calls Accept() but does not close
  // the infobar.
  [controller_ ok:nil];
  EXPECT_TRUE(delegate_.ok_clicked);
  EXPECT_FALSE(delegate_.cancel_clicked);
  EXPECT_FALSE(delegate_.closed);
}

TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) {
  // Check that clicking the cancel button calls Cancel() and closes
  // the infobar.
  [controller_ cancel:nil];
  EXPECT_FALSE(delegate_.ok_clicked);
  EXPECT_TRUE(delegate_.cancel_clicked);
  EXPECT_TRUE(delegate_.closed);
}

TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) {
  delegate_.closes_on_action = false;

  // Check that clicking the cancel button calls Cancel() but does not close
  // the infobar.
  [controller_ cancel:nil];
  EXPECT_FALSE(delegate_.ok_clicked);
  EXPECT_TRUE(delegate_.cancel_clicked);
  EXPECT_FALSE(delegate_.closed);
}

}  // namespace