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

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 MockLinkInfoBarDelegate* delegate_; // Owns itself.
88 scoped_nsobject<id> container_; 97 scoped_nsobject<id> container_;
89 scoped_nsobject<LinkInfoBarController> controller_; 98 scoped_nsobject<LinkInfoBarController> controller_;
99 bool closed_delegate_link_clicked_;
100
101 private:
102 virtual void OnInfoBarDelegateClosed() {
103 closed_delegate_link_clicked_ = delegate_->link_clicked();
104 delegate_ = NULL;
105 }
90 }; 106 };
91 107
92 class ConfirmInfoBarControllerTest : public CocoaTest { 108 class ConfirmInfoBarControllerTest : public CocoaTest,
109 public MockConfirmInfoBarDelegate::Owner {
93 public: 110 public:
94 virtual void SetUp() { 111 virtual void SetUp() {
95 CocoaTest::SetUp(); 112 CocoaTest::SetUp();
96 113
114 delegate_ = new MockConfirmInfoBarDelegate(this);
97 controller_.reset( 115 controller_.reset(
98 [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]); 116 [[ConfirmInfoBarController alloc] initWithDelegate:delegate_]);
99 container_.reset( 117 container_.reset(
100 [[InfoBarContainerTest alloc] initWithController:controller_]); 118 [[InfoBarContainerTest alloc] initWithController:controller_]);
101 [controller_ setContainerController:container_]; 119 [controller_ setContainerController:container_];
102 [[test_window() contentView] addSubview:[controller_ view]]; 120 [[test_window() contentView] addSubview:[controller_ view]];
121 closed_delegate_ok_clicked_ = false;
122 closed_delegate_cancel_clicked_ = false;
123 closed_delegate_link_clicked_ = false;
124 }
125
126 virtual void TearDown() {
127 if (delegate_)
128 delete delegate_;
129 CocoaTest::TearDown();
103 } 130 }
104 131
105 protected: 132 protected:
106 MockConfirmInfoBarDelegate delegate_; 133 MockConfirmInfoBarDelegate* delegate_; // Owns itself.
107 scoped_nsobject<id> container_; 134 scoped_nsobject<id> container_;
108 scoped_nsobject<ConfirmInfoBarController> controller_; 135 scoped_nsobject<ConfirmInfoBarController> controller_;
136 bool closed_delegate_ok_clicked_;
137 bool closed_delegate_cancel_clicked_;
138 bool closed_delegate_link_clicked_;
139
140 private:
141 virtual void OnInfoBarDelegateClosed() {
142 closed_delegate_ok_clicked_ = delegate_->ok_clicked();
143 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked();
144 closed_delegate_link_clicked_ = delegate_->link_clicked();
145 delegate_ = NULL;
146 }
109 }; 147 };
110 148
111 149
112 //////////////////////////////////////////////////////////////////////////// 150 ////////////////////////////////////////////////////////////////////////////
113 // Tests 151 // Tests
114 152
115 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]); 153 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]);
116 154
117 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) { 155 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) {
118 // Make sure someone looked at the message, link, and icon. 156 // Make sure someone looked at the message, link, and icon.
119 EXPECT_TRUE(delegate_.message_text_accessed()); 157 EXPECT_TRUE(delegate_->message_text_accessed());
120 EXPECT_TRUE(delegate_.link_text_accessed()); 158 EXPECT_TRUE(delegate_->link_text_accessed());
121 EXPECT_TRUE(delegate_.icon_accessed()); 159 EXPECT_TRUE(delegate_->icon_accessed());
122 160
123 // Check that dismissing the infobar calls InfoBarClosed() on the delegate. 161 // Check that dismissing the infobar deletes the delegate.
124 [controller_ dismiss:nil]; 162 [controller_ dismiss:nil];
125 EXPECT_FALSE(delegate_.link_clicked()); 163 ASSERT_TRUE(delegate_ == NULL);
126 EXPECT_TRUE(delegate_.closed()); 164 EXPECT_FALSE(closed_delegate_link_clicked_);
127 } 165 }
128 166
129 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) { 167 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) {
130 // Check that clicking on the link calls LinkClicked() on the 168 // Check that clicking on the link calls LinkClicked() on the
131 // delegate. It should also close the infobar. 169 // delegate. It should also close the infobar.
132 [controller_ linkClicked]; 170 [controller_ linkClicked];
133 EXPECT_TRUE(delegate_.link_clicked()); 171 ASSERT_TRUE(delegate_ == NULL);
Nico 2011/05/04 21:15:52 These would read better if there's a method on the
Peter Kasting 2011/05/04 22:54:56 Added "delegate_closed()".
134 EXPECT_TRUE(delegate_.closed()); 172 EXPECT_TRUE(closed_delegate_link_clicked_);
135 } 173 }
136 174
137 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { 175 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
138 delegate_.set_dont_close_on_action(); 176 delegate_->set_dont_close_on_action();
139 177
140 // Check that clicking on the link calls LinkClicked() on the 178 // Check that clicking on the link calls LinkClicked() on the
141 // delegate. It should not close the infobar. 179 // delegate. It should not close the infobar.
142 [controller_ linkClicked]; 180 [controller_ linkClicked];
143 EXPECT_TRUE(delegate_.link_clicked()); 181 ASSERT_TRUE(delegate_ != NULL);
144 EXPECT_FALSE(delegate_.closed()); 182 EXPECT_TRUE(delegate_->link_clicked());
145 } 183 }
146 184
147 TEST_F(LinkInfoBarControllerTest, DeallocController) { 185 TEST_F(LinkInfoBarControllerTest, DeallocController) {
148 // Test that dealloc'ing the controller does not send an 186 // Test that dealloc'ing the controller does not delete the delegate.
149 // InfoBarClosed() message to the delegate.
150 controller_.reset(nil); 187 controller_.reset(nil);
151 EXPECT_FALSE(delegate_.closed()); 188 ASSERT_TRUE(delegate_ != NULL);
152 } 189 }
153 190
154 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); 191 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]);
155 192
156 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { 193 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) {
157 // Make sure someone looked at the message, link, and icon. 194 // Make sure someone looked at the message, link, and icon.
158 EXPECT_TRUE(delegate_.message_text_accessed()); 195 EXPECT_TRUE(delegate_->message_text_accessed());
159 EXPECT_TRUE(delegate_.link_text_accessed()); 196 EXPECT_TRUE(delegate_->link_text_accessed());
160 EXPECT_TRUE(delegate_.icon_accessed()); 197 EXPECT_TRUE(delegate_->icon_accessed());
161 198
162 // Check to make sure the infobar message was set properly. 199 // Check to make sure the infobar message was set properly.
163 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, 200 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage,
164 base::SysNSStringToUTF8([controller_.get() labelString])); 201 base::SysNSStringToUTF8([controller_.get() labelString]));
165 202
166 // Check that dismissing the infobar calls InfoBarClosed() on the delegate. 203 // Check that dismissing the infobar deletes the delegate.
167 [controller_ dismiss:nil]; 204 [controller_ dismiss:nil];
168 EXPECT_FALSE(delegate_.ok_clicked()); 205 ASSERT_TRUE(delegate_ == NULL);
169 EXPECT_FALSE(delegate_.cancel_clicked()); 206 EXPECT_FALSE(closed_delegate_ok_clicked_);
170 EXPECT_FALSE(delegate_.link_clicked()); 207 EXPECT_FALSE(closed_delegate_cancel_clicked_);
171 EXPECT_TRUE(delegate_.closed()); 208 EXPECT_FALSE(closed_delegate_link_clicked_);
172 } 209 }
173 210
174 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { 211 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) {
175 // Check that clicking the OK button calls Accept() and then closes 212 // Check that clicking the OK button calls Accept() and then closes
176 // the infobar. 213 // the infobar.
177 [controller_ ok:nil]; 214 [controller_ ok:nil];
178 EXPECT_TRUE(delegate_.ok_clicked()); 215 ASSERT_TRUE(delegate_ == NULL);
179 EXPECT_FALSE(delegate_.cancel_clicked()); 216 EXPECT_TRUE(closed_delegate_ok_clicked_);
180 EXPECT_FALSE(delegate_.link_clicked()); 217 EXPECT_FALSE(closed_delegate_cancel_clicked_);
181 EXPECT_TRUE(delegate_.closed()); 218 EXPECT_FALSE(closed_delegate_link_clicked_);
182 } 219 }
183 220
184 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) { 221 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) {
185 delegate_.set_dont_close_on_action(); 222 delegate_->set_dont_close_on_action();
186 223
187 // Check that clicking the OK button calls Accept() but does not close 224 // Check that clicking the OK button calls Accept() but does not close
188 // the infobar. 225 // the infobar.
189 [controller_ ok:nil]; 226 [controller_ ok:nil];
190 EXPECT_TRUE(delegate_.ok_clicked()); 227 ASSERT_TRUE(delegate_ != NULL);
191 EXPECT_FALSE(delegate_.cancel_clicked()); 228 EXPECT_TRUE(delegate_->ok_clicked());
192 EXPECT_FALSE(delegate_.link_clicked()); 229 EXPECT_FALSE(delegate_->cancel_clicked());
193 EXPECT_FALSE(delegate_.closed()); 230 EXPECT_FALSE(delegate_->link_clicked());
194 } 231 }
195 232
196 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) { 233 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) {
197 // Check that clicking the cancel button calls Cancel() and closes 234 // Check that clicking the cancel button calls Cancel() and closes
198 // the infobar. 235 // the infobar.
199 [controller_ cancel:nil]; 236 [controller_ cancel:nil];
200 EXPECT_FALSE(delegate_.ok_clicked()); 237 ASSERT_TRUE(delegate_ == NULL);
201 EXPECT_TRUE(delegate_.cancel_clicked()); 238 EXPECT_FALSE(closed_delegate_ok_clicked_);
202 EXPECT_FALSE(delegate_.link_clicked()); 239 EXPECT_TRUE(closed_delegate_cancel_clicked_);
203 EXPECT_TRUE(delegate_.closed()); 240 EXPECT_FALSE(closed_delegate_link_clicked_);
204 } 241 }
205 242
206 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) { 243 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) {
207 delegate_.set_dont_close_on_action(); 244 delegate_->set_dont_close_on_action();
208 245
209 // Check that clicking the cancel button calls Cancel() but does not close 246 // Check that clicking the cancel button calls Cancel() but does not close
210 // the infobar. 247 // the infobar.
211 [controller_ cancel:nil]; 248 [controller_ cancel:nil];
212 EXPECT_FALSE(delegate_.ok_clicked()); 249 ASSERT_TRUE(delegate_ != NULL);
213 EXPECT_TRUE(delegate_.cancel_clicked()); 250 EXPECT_FALSE(delegate_->ok_clicked());
214 EXPECT_FALSE(delegate_.link_clicked()); 251 EXPECT_TRUE(delegate_->cancel_clicked());
215 EXPECT_FALSE(delegate_.closed()); 252 EXPECT_FALSE(delegate_->link_clicked());
216 } 253 }
217 254
218 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) { 255 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) {
219 // Check that clicking on the link calls LinkClicked() on the 256 // Check that clicking on the link calls LinkClicked() on the
220 // delegate. It should also close the infobar. 257 // delegate. It should also close the infobar.
221 [controller_ linkClicked]; 258 [controller_ linkClicked];
222 EXPECT_FALSE(delegate_.ok_clicked()); 259 ASSERT_TRUE(delegate_ == NULL);
223 EXPECT_FALSE(delegate_.cancel_clicked()); 260 EXPECT_FALSE(closed_delegate_ok_clicked_);
224 EXPECT_TRUE(delegate_.link_clicked()); 261 EXPECT_FALSE(closed_delegate_cancel_clicked_);
225 EXPECT_TRUE(delegate_.closed()); 262 EXPECT_TRUE(closed_delegate_link_clicked_);
226 } 263 }
227 264
228 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { 265 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) {
229 delegate_.set_dont_close_on_action(); 266 delegate_->set_dont_close_on_action();
230 267
231 // Check that clicking on the link calls LinkClicked() on the 268 // Check that clicking on the link calls LinkClicked() on the
232 // delegate. It should not close the infobar. 269 // delegate. It should not close the infobar.
233 [controller_ linkClicked]; 270 [controller_ linkClicked];
234 EXPECT_FALSE(delegate_.ok_clicked()); 271 ASSERT_TRUE(delegate_ != NULL);
235 EXPECT_FALSE(delegate_.cancel_clicked()); 272 EXPECT_FALSE(delegate_->ok_clicked());
236 EXPECT_TRUE(delegate_.link_clicked()); 273 EXPECT_FALSE(delegate_->cancel_clicked());
237 EXPECT_FALSE(delegate_.closed()); 274 EXPECT_TRUE(delegate_->link_clicked());
238 } 275 }
239 276
240 TEST_F(ConfirmInfoBarControllerTest, ResizeView) { 277 TEST_F(ConfirmInfoBarControllerTest, ResizeView) {
241 NSRect originalLabelFrame = [controller_ labelFrame]; 278 NSRect originalLabelFrame = [controller_ labelFrame];
242 279
243 // Expand the view by 20 pixels and make sure the label frame changes 280 // Expand the view by 20 pixels and make sure the label frame changes
244 // accordingly. 281 // accordingly.
245 const CGFloat width = 20; 282 const CGFloat width = 20;
246 NSRect newViewFrame = [[controller_ view] frame]; 283 NSRect newViewFrame = [[controller_ view] frame];
247 newViewFrame.size.width += width; 284 newViewFrame.size.width += width;
248 [[controller_ view] setFrame:newViewFrame]; 285 [[controller_ view] setFrame:newViewFrame];
249 286
250 NSRect newLabelFrame = [controller_ labelFrame]; 287 NSRect newLabelFrame = [controller_ labelFrame];
251 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); 288 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width);
252 } 289 }
253 290
254 } // namespace 291 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698