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

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

Issue 6926001: Replace the virtual InfoBarDelegate::InfoBarClosed() function with a non-virtual one. This is a ... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 7 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
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #include "base/memory/scoped_nsobject.h" 7 #include "base/memory/scoped_nsobject.h"
8 #include "base/string_util.h" 8 #include "base/string_util.h"
9 #include "base/sys_string_conversions.h" 9 #include "base/sys_string_conversions.h"
10 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" 10 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 DCHECK(controller_ == controller); 63 DCHECK(controller_ == controller);
64 controller_ = nil; 64 controller_ = nil;
65 } 65 }
66 @end 66 @end
67 67
68 namespace { 68 namespace {
69 69
70 /////////////////////////////////////////////////////////////////////////// 70 ///////////////////////////////////////////////////////////////////////////
71 // Test fixtures 71 // Test fixtures
72 72
73 class LinkInfoBarControllerTest : public CocoaTest { 73 class LinkInfoBarControllerTest : public CocoaTest,
74 public MockLinkInfoBarDelegate::Owner {
74 public: 75 public:
75 virtual void SetUp() { 76 virtual void SetUp() {
76 CocoaTest::SetUp(); 77 CocoaTest::SetUp();
77 78
79 delegate_ = new MockLinkInfoBarDelegate(this);
78 controller_.reset( 80 controller_.reset(
79 [[LinkInfoBarController alloc] initWithDelegate:&delegate_]); 81 [[LinkInfoBarController alloc] initWithDelegate:delegate_]);
80 container_.reset( 82 container_.reset(
81 [[InfoBarContainerTest alloc] initWithController:controller_]); 83 [[InfoBarContainerTest alloc] initWithController:controller_]);
82 [controller_ setContainerController:container_]; 84 [controller_ setContainerController:container_];
83 [[test_window() contentView] addSubview:[controller_ view]]; 85 [[test_window() contentView] addSubview:[controller_ view]];
86 closed_delegate_link_clicked_ = false;
87 }
88
89 virtual void TearDown() {
90 if (delegate_)
91 delete delegate_;
92 CocoaTest::TearDown();
84 } 93 }
85 94
86 protected: 95 protected:
87 MockLinkInfoBarDelegate delegate_; 96 // Hopefully-obvious: If this returns true, you must not deref |delegate_|!
97 bool delegate_closed() const { return delegate_ == NULL; }
98
99 MockLinkInfoBarDelegate* delegate_; // Owns itself.
88 scoped_nsobject<id> container_; 100 scoped_nsobject<id> container_;
89 scoped_nsobject<LinkInfoBarController> controller_; 101 scoped_nsobject<LinkInfoBarController> controller_;
102 bool closed_delegate_link_clicked_;
103
104 private:
105 virtual void OnInfoBarDelegateClosed() {
106 closed_delegate_link_clicked_ = delegate_->link_clicked();
107 delegate_ = NULL;
108 }
90 }; 109 };
91 110
92 class ConfirmInfoBarControllerTest : public CocoaTest { 111 class ConfirmInfoBarControllerTest : public CocoaTest,
112 public MockConfirmInfoBarDelegate::Owner {
93 public: 113 public:
94 virtual void SetUp() { 114 virtual void SetUp() {
95 CocoaTest::SetUp(); 115 CocoaTest::SetUp();
96 116
117 delegate_ = new MockConfirmInfoBarDelegate(this);
97 controller_.reset( 118 controller_.reset(
98 [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]); 119 [[ConfirmInfoBarController alloc] initWithDelegate:delegate_]);
99 container_.reset( 120 container_.reset(
100 [[InfoBarContainerTest alloc] initWithController:controller_]); 121 [[InfoBarContainerTest alloc] initWithController:controller_]);
101 [controller_ setContainerController:container_]; 122 [controller_ setContainerController:container_];
102 [[test_window() contentView] addSubview:[controller_ view]]; 123 [[test_window() contentView] addSubview:[controller_ view]];
124 closed_delegate_ok_clicked_ = false;
125 closed_delegate_cancel_clicked_ = false;
126 closed_delegate_link_clicked_ = false;
127 }
128
129 virtual void TearDown() {
130 if (delegate_)
131 delete delegate_;
132 CocoaTest::TearDown();
103 } 133 }
104 134
105 protected: 135 protected:
106 MockConfirmInfoBarDelegate delegate_; 136 // Hopefully-obvious: If this returns true, you must not deref |delegate_|!
137 bool delegate_closed() const { return delegate_ == NULL; }
138
139 MockConfirmInfoBarDelegate* delegate_; // Owns itself.
107 scoped_nsobject<id> container_; 140 scoped_nsobject<id> container_;
108 scoped_nsobject<ConfirmInfoBarController> controller_; 141 scoped_nsobject<ConfirmInfoBarController> controller_;
142 bool closed_delegate_ok_clicked_;
143 bool closed_delegate_cancel_clicked_;
144 bool closed_delegate_link_clicked_;
145
146 private:
147 virtual void OnInfoBarDelegateClosed() {
148 closed_delegate_ok_clicked_ = delegate_->ok_clicked();
149 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked();
150 closed_delegate_link_clicked_ = delegate_->link_clicked();
151 delegate_ = NULL;
152 }
109 }; 153 };
110 154
111 155
112 //////////////////////////////////////////////////////////////////////////// 156 ////////////////////////////////////////////////////////////////////////////
113 // Tests 157 // Tests
114 158
115 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]); 159 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]);
116 160
117 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) { 161 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) {
118 // Make sure someone looked at the message, link, and icon. 162 // Make sure someone looked at the message, link, and icon.
119 EXPECT_TRUE(delegate_.message_text_accessed()); 163 EXPECT_TRUE(delegate_->message_text_accessed());
120 EXPECT_TRUE(delegate_.link_text_accessed()); 164 EXPECT_TRUE(delegate_->link_text_accessed());
121 EXPECT_TRUE(delegate_.icon_accessed()); 165 EXPECT_TRUE(delegate_->icon_accessed());
122 166
123 // Check that dismissing the infobar calls InfoBarClosed() on the delegate. 167 // Check that dismissing the infobar deletes the delegate.
124 [controller_ dismiss:nil]; 168 [controller_ dismiss:nil];
125 EXPECT_FALSE(delegate_.link_clicked()); 169 ASSERT_TRUE(delegate_closed());
126 EXPECT_TRUE(delegate_.closed()); 170 EXPECT_FALSE(closed_delegate_link_clicked_);
127 } 171 }
128 172
129 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) { 173 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) {
130 // Check that clicking on the link calls LinkClicked() on the 174 // Check that clicking on the link calls LinkClicked() on the
131 // delegate. It should also close the infobar. 175 // delegate. It should also close the infobar.
132 [controller_ linkClicked]; 176 [controller_ linkClicked];
133 EXPECT_TRUE(delegate_.link_clicked()); 177 ASSERT_TRUE(delegate_closed());
134 EXPECT_TRUE(delegate_.closed()); 178 EXPECT_TRUE(closed_delegate_link_clicked_);
135 } 179 }
136 180
137 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { 181 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
138 delegate_.set_dont_close_on_action(); 182 delegate_->set_dont_close_on_action();
139 183
140 // Check that clicking on the link calls LinkClicked() on the 184 // Check that clicking on the link calls LinkClicked() on the
141 // delegate. It should not close the infobar. 185 // delegate. It should not close the infobar.
142 [controller_ linkClicked]; 186 [controller_ linkClicked];
143 EXPECT_TRUE(delegate_.link_clicked()); 187 ASSERT_FALSE(delegate_closed());
144 EXPECT_FALSE(delegate_.closed()); 188 EXPECT_TRUE(delegate_->link_clicked());
145 } 189 }
146 190
147 TEST_F(LinkInfoBarControllerTest, DeallocController) { 191 TEST_F(LinkInfoBarControllerTest, DeallocController) {
148 // Test that dealloc'ing the controller does not send an 192 // Test that dealloc'ing the controller does not delete the delegate.
149 // InfoBarClosed() message to the delegate.
150 controller_.reset(nil); 193 controller_.reset(nil);
151 EXPECT_FALSE(delegate_.closed()); 194 ASSERT_FALSE(delegate_closed());
152 } 195 }
153 196
154 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); 197 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]);
155 198
156 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { 199 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) {
157 // Make sure someone looked at the message, link, and icon. 200 // Make sure someone looked at the message, link, and icon.
158 EXPECT_TRUE(delegate_.message_text_accessed()); 201 EXPECT_TRUE(delegate_->message_text_accessed());
159 EXPECT_TRUE(delegate_.link_text_accessed()); 202 EXPECT_TRUE(delegate_->link_text_accessed());
160 EXPECT_TRUE(delegate_.icon_accessed()); 203 EXPECT_TRUE(delegate_->icon_accessed());
161 204
162 // Check to make sure the infobar message was set properly. 205 // Check to make sure the infobar message was set properly.
163 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, 206 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage,
164 base::SysNSStringToUTF8([controller_.get() labelString])); 207 base::SysNSStringToUTF8([controller_.get() labelString]));
165 208
166 // Check that dismissing the infobar calls InfoBarClosed() on the delegate. 209 // Check that dismissing the infobar deletes the delegate.
167 [controller_ dismiss:nil]; 210 [controller_ dismiss:nil];
168 EXPECT_FALSE(delegate_.ok_clicked()); 211 ASSERT_TRUE(delegate_closed());
169 EXPECT_FALSE(delegate_.cancel_clicked()); 212 EXPECT_FALSE(closed_delegate_ok_clicked_);
170 EXPECT_FALSE(delegate_.link_clicked()); 213 EXPECT_FALSE(closed_delegate_cancel_clicked_);
171 EXPECT_TRUE(delegate_.closed()); 214 EXPECT_FALSE(closed_delegate_link_clicked_);
172 } 215 }
173 216
174 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { 217 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) {
175 // Check that clicking the OK button calls Accept() and then closes 218 // Check that clicking the OK button calls Accept() and then closes
176 // the infobar. 219 // the infobar.
177 [controller_ ok:nil]; 220 [controller_ ok:nil];
178 EXPECT_TRUE(delegate_.ok_clicked()); 221 ASSERT_TRUE(delegate_closed());
179 EXPECT_FALSE(delegate_.cancel_clicked()); 222 EXPECT_TRUE(closed_delegate_ok_clicked_);
180 EXPECT_FALSE(delegate_.link_clicked()); 223 EXPECT_FALSE(closed_delegate_cancel_clicked_);
181 EXPECT_TRUE(delegate_.closed()); 224 EXPECT_FALSE(closed_delegate_link_clicked_);
182 } 225 }
183 226
184 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) { 227 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) {
185 delegate_.set_dont_close_on_action(); 228 delegate_->set_dont_close_on_action();
186 229
187 // Check that clicking the OK button calls Accept() but does not close 230 // Check that clicking the OK button calls Accept() but does not close
188 // the infobar. 231 // the infobar.
189 [controller_ ok:nil]; 232 [controller_ ok:nil];
190 EXPECT_TRUE(delegate_.ok_clicked()); 233 ASSERT_FALSE(delegate_closed());
191 EXPECT_FALSE(delegate_.cancel_clicked()); 234 EXPECT_TRUE(delegate_->ok_clicked());
192 EXPECT_FALSE(delegate_.link_clicked()); 235 EXPECT_FALSE(delegate_->cancel_clicked());
193 EXPECT_FALSE(delegate_.closed()); 236 EXPECT_FALSE(delegate_->link_clicked());
194 } 237 }
195 238
196 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) { 239 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) {
197 // Check that clicking the cancel button calls Cancel() and closes 240 // Check that clicking the cancel button calls Cancel() and closes
198 // the infobar. 241 // the infobar.
199 [controller_ cancel:nil]; 242 [controller_ cancel:nil];
200 EXPECT_FALSE(delegate_.ok_clicked()); 243 ASSERT_TRUE(delegate_closed());
201 EXPECT_TRUE(delegate_.cancel_clicked()); 244 EXPECT_FALSE(closed_delegate_ok_clicked_);
202 EXPECT_FALSE(delegate_.link_clicked()); 245 EXPECT_TRUE(closed_delegate_cancel_clicked_);
203 EXPECT_TRUE(delegate_.closed()); 246 EXPECT_FALSE(closed_delegate_link_clicked_);
204 } 247 }
205 248
206 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) { 249 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) {
207 delegate_.set_dont_close_on_action(); 250 delegate_->set_dont_close_on_action();
208 251
209 // Check that clicking the cancel button calls Cancel() but does not close 252 // Check that clicking the cancel button calls Cancel() but does not close
210 // the infobar. 253 // the infobar.
211 [controller_ cancel:nil]; 254 [controller_ cancel:nil];
212 EXPECT_FALSE(delegate_.ok_clicked()); 255 ASSERT_FALSE(delegate_closed());
213 EXPECT_TRUE(delegate_.cancel_clicked()); 256 EXPECT_FALSE(delegate_->ok_clicked());
214 EXPECT_FALSE(delegate_.link_clicked()); 257 EXPECT_TRUE(delegate_->cancel_clicked());
215 EXPECT_FALSE(delegate_.closed()); 258 EXPECT_FALSE(delegate_->link_clicked());
216 } 259 }
217 260
218 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) { 261 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) {
219 // Check that clicking on the link calls LinkClicked() on the 262 // Check that clicking on the link calls LinkClicked() on the
220 // delegate. It should also close the infobar. 263 // delegate. It should also close the infobar.
221 [controller_ linkClicked]; 264 [controller_ linkClicked];
222 EXPECT_FALSE(delegate_.ok_clicked()); 265 ASSERT_TRUE(delegate_closed());
223 EXPECT_FALSE(delegate_.cancel_clicked()); 266 EXPECT_FALSE(closed_delegate_ok_clicked_);
224 EXPECT_TRUE(delegate_.link_clicked()); 267 EXPECT_FALSE(closed_delegate_cancel_clicked_);
225 EXPECT_TRUE(delegate_.closed()); 268 EXPECT_TRUE(closed_delegate_link_clicked_);
226 } 269 }
227 270
228 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { 271 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
229 delegate_.set_dont_close_on_action(); 272 delegate_->set_dont_close_on_action();
230 273
231 // Check that clicking on the link calls LinkClicked() on the 274 // Check that clicking on the link calls LinkClicked() on the
232 // delegate. It should not close the infobar. 275 // delegate. It should not close the infobar.
233 [controller_ linkClicked]; 276 [controller_ linkClicked];
234 EXPECT_FALSE(delegate_.ok_clicked()); 277 ASSERT_FALSE(delegate_closed());
235 EXPECT_FALSE(delegate_.cancel_clicked()); 278 EXPECT_FALSE(delegate_->ok_clicked());
236 EXPECT_TRUE(delegate_.link_clicked()); 279 EXPECT_FALSE(delegate_->cancel_clicked());
237 EXPECT_FALSE(delegate_.closed()); 280 EXPECT_TRUE(delegate_->link_clicked());
238 } 281 }
239 282
240 TEST_F(ConfirmInfoBarControllerTest, ResizeView) { 283 TEST_F(ConfirmInfoBarControllerTest, ResizeView) {
241 NSRect originalLabelFrame = [controller_ labelFrame]; 284 NSRect originalLabelFrame = [controller_ labelFrame];
242 285
243 // Expand the view by 20 pixels and make sure the label frame changes 286 // Expand the view by 20 pixels and make sure the label frame changes
244 // accordingly. 287 // accordingly.
245 const CGFloat width = 20; 288 const CGFloat width = 20;
246 NSRect newViewFrame = [[controller_ view] frame]; 289 NSRect newViewFrame = [[controller_ view] frame];
247 newViewFrame.size.width += width; 290 newViewFrame.size.width += width;
248 [[controller_ view] setFrame:newViewFrame]; 291 [[controller_ view] setFrame:newViewFrame];
249 292
250 NSRect newLabelFrame = [controller_ labelFrame]; 293 NSRect newLabelFrame = [controller_ labelFrame];
251 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); 294 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width);
252 } 295 }
253 296
254 } // namespace 297 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698