Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: chrome/browser/ui/cocoa/infobar_controller_unittest.mm

Issue 6377001: [Mac] Organize all infobar files into chrome/browser/ui/cocoa/infobars/.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h"
10 #include "chrome/browser/tab_contents/infobar_delegate.h"
11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12 #import "chrome/browser/ui/cocoa/infobar_container_controller.h"
13 #import "chrome/browser/ui/cocoa/infobar_controller.h"
14 #include "chrome/browser/ui/cocoa/infobar_test_helper.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "testing/platform_test.h"
17
18 @interface InfoBarController (ExposedForTesting)
19 - (NSString*)labelString;
20 - (NSRect)labelFrame;
21 @end
22
23 @implementation InfoBarController (ExposedForTesting)
24 - (NSString*)labelString {
25 return [label_.get() string];
26 }
27 - (NSRect)labelFrame {
28 return [label_.get() frame];
29 }
30 @end
31
32
33 // Calls to removeDelegate: normally start an animation, which removes the
34 // infobar completely when finished. For unittesting purposes, we create a mock
35 // container which calls close: immediately, rather than kicking off an
36 // animation.
37 @interface InfoBarContainerTest : NSObject <InfoBarContainer> {
38 InfoBarController* controller_;
39 }
40 - (id)initWithController:(InfoBarController*)controller;
41 - (void)removeDelegate:(InfoBarDelegate*)delegate;
42 - (void)removeController:(InfoBarController*)controller;
43 @end
44
45 @implementation InfoBarContainerTest
46 - (id)initWithController:(InfoBarController*)controller {
47 if ((self = [super init])) {
48 controller_ = controller;
49 }
50 return self;
51 }
52
53 - (void)removeDelegate:(InfoBarDelegate*)delegate {
54 [controller_ close];
55 }
56
57 - (void)removeController:(InfoBarController*)controller {
58 DCHECK(controller_ == controller);
59 controller_ = nil;
60 }
61 @end
62
63 namespace {
64
65 ///////////////////////////////////////////////////////////////////////////
66 // Test fixtures
67
68 class AlertInfoBarControllerTest : public CocoaTest {
69 public:
70 virtual void SetUp() {
71 CocoaTest::SetUp();
72
73 controller_.reset(
74 [[AlertInfoBarController alloc] initWithDelegate:&delegate_]);
75 container_.reset(
76 [[InfoBarContainerTest alloc] initWithController:controller_]);
77 [controller_ setContainerController:container_];
78 [[test_window() contentView] addSubview:[controller_ view]];
79 }
80
81 protected:
82 MockAlertInfoBarDelegate delegate_;
83 scoped_nsobject<id> container_;
84 scoped_nsobject<AlertInfoBarController> controller_;
85 };
86
87 class LinkInfoBarControllerTest : public CocoaTest {
88 public:
89 virtual void SetUp() {
90 CocoaTest::SetUp();
91
92 controller_.reset(
93 [[LinkInfoBarController alloc] initWithDelegate:&delegate_]);
94 container_.reset(
95 [[InfoBarContainerTest alloc] initWithController:controller_]);
96 [controller_ setContainerController:container_];
97 [[test_window() contentView] addSubview:[controller_ view]];
98 }
99
100 protected:
101 MockLinkInfoBarDelegate delegate_;
102 scoped_nsobject<id> container_;
103 scoped_nsobject<LinkInfoBarController> controller_;
104 };
105
106 class ConfirmInfoBarControllerTest : public CocoaTest {
107 public:
108 virtual void SetUp() {
109 CocoaTest::SetUp();
110
111 controller_.reset(
112 [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]);
113 container_.reset(
114 [[InfoBarContainerTest alloc] initWithController:controller_]);
115 [controller_ setContainerController:container_];
116 [[test_window() contentView] addSubview:[controller_ view]];
117 }
118
119 protected:
120 MockConfirmInfoBarDelegate delegate_;
121 scoped_nsobject<id> container_;
122 scoped_nsobject<ConfirmInfoBarController> controller_;
123 };
124
125
126 ////////////////////////////////////////////////////////////////////////////
127 // Tests
128
129 TEST_VIEW(AlertInfoBarControllerTest, [controller_ view]);
130
131 TEST_F(AlertInfoBarControllerTest, ShowAndDismiss) {
132 // Make sure someone looked at the message and icon.
133 EXPECT_TRUE(delegate_.message_text_accessed);
134 EXPECT_TRUE(delegate_.icon_accessed);
135
136 // Check to make sure the infobar message was set properly.
137 EXPECT_EQ(kMockAlertInfoBarMessage,
138 base::SysNSStringToUTF8([controller_.get() labelString]));
139
140 // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
141 [controller_ dismiss:nil];
142 EXPECT_TRUE(delegate_.closed);
143 }
144
145 TEST_F(AlertInfoBarControllerTest, DeallocController) {
146 // Test that dealloc'ing the controller does not send an
147 // InfoBarClosed() message to the delegate.
148 controller_.reset(nil);
149 EXPECT_FALSE(delegate_.closed);
150 }
151
152 TEST_F(AlertInfoBarControllerTest, ResizeView) {
153 NSRect originalLabelFrame = [controller_ labelFrame];
154
155 // Expand the view by 20 pixels and make sure the label frame changes
156 // accordingly.
157 const CGFloat width = 20;
158 NSRect newViewFrame = [[controller_ view] frame];
159 newViewFrame.size.width += width;
160 [[controller_ view] setFrame:newViewFrame];
161
162 NSRect newLabelFrame = [controller_ labelFrame];
163 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width);
164 }
165
166 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]);
167
168 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) {
169 // Make sure someone looked at the message, link, and icon.
170 EXPECT_TRUE(delegate_.message_text_accessed);
171 EXPECT_TRUE(delegate_.link_text_accessed);
172 EXPECT_TRUE(delegate_.icon_accessed);
173
174 // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
175 [controller_ dismiss:nil];
176 EXPECT_FALSE(delegate_.link_clicked);
177 EXPECT_TRUE(delegate_.closed);
178 }
179
180 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) {
181 // Check that clicking on the link calls LinkClicked() on the
182 // delegate. It should also close the infobar.
183 [controller_ linkClicked];
184 EXPECT_TRUE(delegate_.link_clicked);
185 EXPECT_TRUE(delegate_.closed);
186 }
187
188 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
189 delegate_.closes_on_action = false;
190
191 // Check that clicking on the link calls LinkClicked() on the
192 // delegate. It should not close the infobar.
193 [controller_ linkClicked];
194 EXPECT_TRUE(delegate_.link_clicked);
195 EXPECT_FALSE(delegate_.closed);
196 }
197
198 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]);
199
200 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) {
201 // Make sure someone looked at the message, link, and icon.
202 EXPECT_TRUE(delegate_.message_text_accessed);
203 EXPECT_TRUE(delegate_.link_text_accessed);
204 EXPECT_TRUE(delegate_.icon_accessed);
205
206 // Check to make sure the infobar message was set properly.
207 EXPECT_EQ(kMockConfirmInfoBarMessage,
208 base::SysNSStringToUTF8([controller_.get() labelString]));
209
210 // Check that dismissing the infobar calls InfoBarClosed() on the delegate.
211 [controller_ dismiss:nil];
212 EXPECT_FALSE(delegate_.ok_clicked);
213 EXPECT_FALSE(delegate_.cancel_clicked);
214 EXPECT_FALSE(delegate_.link_clicked);
215 EXPECT_TRUE(delegate_.closed);
216 }
217
218 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) {
219 // Check that clicking the OK button calls Accept() and then closes
220 // the infobar.
221 [controller_ ok:nil];
222 EXPECT_TRUE(delegate_.ok_clicked);
223 EXPECT_FALSE(delegate_.cancel_clicked);
224 EXPECT_FALSE(delegate_.link_clicked);
225 EXPECT_TRUE(delegate_.closed);
226 }
227
228 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) {
229 delegate_.closes_on_action = false;
230
231 // Check that clicking the OK button calls Accept() but does not close
232 // the infobar.
233 [controller_ ok:nil];
234 EXPECT_TRUE(delegate_.ok_clicked);
235 EXPECT_FALSE(delegate_.cancel_clicked);
236 EXPECT_FALSE(delegate_.link_clicked);
237 EXPECT_FALSE(delegate_.closed);
238 }
239
240 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) {
241 // Check that clicking the cancel button calls Cancel() and closes
242 // the infobar.
243 [controller_ cancel:nil];
244 EXPECT_FALSE(delegate_.ok_clicked);
245 EXPECT_TRUE(delegate_.cancel_clicked);
246 EXPECT_FALSE(delegate_.link_clicked);
247 EXPECT_TRUE(delegate_.closed);
248 }
249
250 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) {
251 delegate_.closes_on_action = false;
252
253 // Check that clicking the cancel button calls Cancel() but does not close
254 // the infobar.
255 [controller_ cancel:nil];
256 EXPECT_FALSE(delegate_.ok_clicked);
257 EXPECT_TRUE(delegate_.cancel_clicked);
258 EXPECT_FALSE(delegate_.link_clicked);
259 EXPECT_FALSE(delegate_.closed);
260 }
261
262 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) {
263 // Check that clicking on the link calls LinkClicked() on the
264 // delegate. It should also close the infobar.
265 [controller_ linkClicked];
266 EXPECT_FALSE(delegate_.ok_clicked);
267 EXPECT_FALSE(delegate_.cancel_clicked);
268 EXPECT_TRUE(delegate_.link_clicked);
269 EXPECT_TRUE(delegate_.closed);
270 }
271
272 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
273 delegate_.closes_on_action = false;
274
275 // Check that clicking on the link calls LinkClicked() on the
276 // delegate. It should not close the infobar.
277 [controller_ linkClicked];
278 EXPECT_FALSE(delegate_.ok_clicked);
279 EXPECT_FALSE(delegate_.cancel_clicked);
280 EXPECT_TRUE(delegate_.link_clicked);
281 EXPECT_FALSE(delegate_.closed);
282 }
283
284 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698