OLD | NEW |
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 "chrome/browser/ui/cocoa/infobars/confirm_infobar_controller.h" | 5 #import "chrome/browser/ui/cocoa/infobars/confirm_infobar_controller.h" |
6 | 6 |
7 #include "base/mac/scoped_nsobject.h" | 7 #include "base/mac/scoped_nsobject.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "chrome/browser/infobars/confirm_infobar_delegate.h" | 10 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 | 82 |
83 namespace { | 83 namespace { |
84 | 84 |
85 class ConfirmInfoBarControllerTest : public CocoaProfileTest, | 85 class ConfirmInfoBarControllerTest : public CocoaProfileTest, |
86 public MockConfirmInfoBarDelegate::Owner { | 86 public MockConfirmInfoBarDelegate::Owner { |
87 public: | 87 public: |
88 virtual void SetUp() OVERRIDE { | 88 virtual void SetUp() OVERRIDE { |
89 CocoaProfileTest::SetUp(); | 89 CocoaProfileTest::SetUp(); |
90 web_contents_.reset( | 90 web_contents_.reset( |
91 WebContents::Create(WebContents::CreateParams(profile()))); | 91 WebContents::Create(WebContents::CreateParams(profile()))); |
92 InfoBarService::CreateForWebContents(web_contents_.get()); | 92 InfoBarService::CreateForWebContents(web_contents_.get()); |
| 93 InfoBarService* infobar_service = |
| 94 InfoBarService::FromWebContents(web_contents_.get()); |
93 | 95 |
94 InfoBarService* infobar_service = | 96 scoped_ptr<InfoBarDelegate> delegate( |
95 InfoBarService::FromWebContents(web_contents_.get()); | 97 new MockConfirmInfoBarDelegate(this)); |
96 delegate_ = new MockConfirmInfoBarDelegate(this); | 98 infobar_ = new InfoBarCocoa(delegate.Pass()); |
97 infobar_.reset(new InfoBarCocoa(infobar_service, delegate_)); | 99 infobar_->SetOwner(infobar_service); |
98 | 100 |
99 controller_.reset([[TestConfirmInfoBarController alloc] | 101 controller_.reset([[TestConfirmInfoBarController alloc] |
100 initWithInfoBar:infobar_.get()]); | 102 initWithInfoBar:infobar_]); |
101 infobar_->set_controller(controller_); | 103 infobar_->set_controller(controller_); |
102 | 104 |
103 container_.reset( | 105 container_.reset( |
104 [[InfoBarContainerTest alloc] initWithController:controller_]); | 106 [[InfoBarContainerTest alloc] initWithController:controller_]); |
105 [controller_ setContainerController:container_]; | 107 [controller_ setContainerController:container_]; |
106 [[test_window() contentView] addSubview:[controller_ view]]; | 108 [[test_window() contentView] addSubview:[controller_ view]]; |
107 closed_delegate_ok_clicked_ = false; | 109 closed_delegate_ok_clicked_ = false; |
108 closed_delegate_cancel_clicked_ = false; | 110 closed_delegate_cancel_clicked_ = false; |
109 closed_delegate_link_clicked_ = false; | 111 closed_delegate_link_clicked_ = false; |
| 112 delegate_closed_ = false; |
110 } | 113 } |
111 | 114 |
112 virtual void TearDown() OVERRIDE { | 115 virtual void TearDown() OVERRIDE { |
113 [controller_ removeSelf]; | 116 [controller_ removeSelf]; |
114 if (delegate_) | |
115 delete delegate_; | |
116 CocoaProfileTest::TearDown(); | 117 CocoaProfileTest::TearDown(); |
117 } | 118 } |
118 | 119 |
119 protected: | 120 protected: |
120 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | 121 // True if delegate is closed. |
121 bool delegate_closed() const { return delegate_ == NULL; } | 122 bool delegate_closed() const { return delegate_closed_; } |
122 | 123 |
123 MockConfirmInfoBarDelegate* delegate_; // Owns itself. | 124 MockConfirmInfoBarDelegate* delegate() const { |
| 125 return static_cast<MockConfirmInfoBarDelegate*>(infobar_->delegate()); |
| 126 } |
| 127 |
124 base::scoped_nsobject<id> container_; | 128 base::scoped_nsobject<id> container_; |
125 base::scoped_nsobject<ConfirmInfoBarController> controller_; | 129 base::scoped_nsobject<ConfirmInfoBarController> controller_; |
126 bool closed_delegate_ok_clicked_; | 130 bool closed_delegate_ok_clicked_; |
127 bool closed_delegate_cancel_clicked_; | 131 bool closed_delegate_cancel_clicked_; |
128 bool closed_delegate_link_clicked_; | 132 bool closed_delegate_link_clicked_; |
129 | 133 |
130 private: | 134 private: |
131 virtual void OnInfoBarDelegateClosed() OVERRIDE { | 135 virtual void OnInfoBarDelegateClosed() OVERRIDE { |
132 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); | 136 closed_delegate_ok_clicked_ = delegate()->ok_clicked(); |
133 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); | 137 closed_delegate_cancel_clicked_ = delegate()->cancel_clicked(); |
134 closed_delegate_link_clicked_ = delegate_->link_clicked(); | 138 closed_delegate_link_clicked_ = delegate()->link_clicked(); |
135 delegate_ = NULL; | 139 delegate_closed_ = true; |
| 140 controller_.reset(); |
136 } | 141 } |
137 | 142 |
138 scoped_ptr<WebContents> web_contents_; | 143 scoped_ptr<WebContents> web_contents_; |
139 scoped_ptr<InfoBarCocoa> infobar_; | 144 InfoBarCocoa* infobar_; // weak, will delete itself. |
| 145 bool delegate_closed_; |
140 }; | 146 }; |
141 | 147 |
142 | 148 |
143 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); | 149 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); |
144 | 150 |
145 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { | 151 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { |
146 // Make sure someone looked at the message, link, and icon. | 152 // Make sure someone looked at the message, link, and icon. |
147 EXPECT_TRUE(delegate_->message_text_accessed()); | 153 EXPECT_TRUE(delegate()->message_text_accessed()); |
148 EXPECT_TRUE(delegate_->link_text_accessed()); | 154 EXPECT_TRUE(delegate()->link_text_accessed()); |
149 EXPECT_TRUE(delegate_->icon_accessed()); | 155 EXPECT_TRUE(delegate()->icon_accessed()); |
150 | 156 |
151 // Check to make sure the infobar message was set properly. | 157 // Check to make sure the infobar message was set properly. |
152 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, | 158 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, |
153 base::SysNSStringToUTF8([controller_.get() labelString])); | 159 base::SysNSStringToUTF8([controller_.get() labelString])); |
154 | 160 |
155 // Check that dismissing the infobar deletes the delegate. | 161 // Check that dismissing the infobar deletes the delegate. |
156 [controller_ removeSelf]; | 162 [controller_ removeSelf]; |
157 ASSERT_TRUE(delegate_closed()); | 163 ASSERT_TRUE(delegate_closed()); |
158 EXPECT_FALSE(closed_delegate_ok_clicked_); | 164 EXPECT_FALSE(closed_delegate_ok_clicked_); |
159 EXPECT_FALSE(closed_delegate_cancel_clicked_); | 165 EXPECT_FALSE(closed_delegate_cancel_clicked_); |
160 EXPECT_FALSE(closed_delegate_link_clicked_); | 166 EXPECT_FALSE(closed_delegate_link_clicked_); |
161 } | 167 } |
162 | 168 |
163 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { | 169 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { |
164 // Check that clicking the OK button calls Accept() and then closes | 170 // Check that clicking the OK button calls Accept() and then closes |
165 // the infobar. | 171 // the infobar. |
166 [controller_ ok:nil]; | 172 [controller_ ok:nil]; |
167 ASSERT_TRUE(delegate_closed()); | 173 ASSERT_TRUE(delegate_closed()); |
168 EXPECT_TRUE(closed_delegate_ok_clicked_); | 174 EXPECT_TRUE(closed_delegate_ok_clicked_); |
169 EXPECT_FALSE(closed_delegate_cancel_clicked_); | 175 EXPECT_FALSE(closed_delegate_cancel_clicked_); |
170 EXPECT_FALSE(closed_delegate_link_clicked_); | 176 EXPECT_FALSE(closed_delegate_link_clicked_); |
171 } | 177 } |
172 | 178 |
173 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) { | 179 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) { |
174 delegate_->set_dont_close_on_action(); | 180 delegate()->set_dont_close_on_action(); |
175 | 181 |
176 // Check that clicking the OK button calls Accept() but does not close | 182 // Check that clicking the OK button calls Accept() but does not close |
177 // the infobar. | 183 // the infobar. |
178 [controller_ ok:nil]; | 184 [controller_ ok:nil]; |
179 ASSERT_FALSE(delegate_closed()); | 185 ASSERT_FALSE(delegate_closed()); |
180 EXPECT_TRUE(delegate_->ok_clicked()); | 186 EXPECT_TRUE(delegate()->ok_clicked()); |
181 EXPECT_FALSE(delegate_->cancel_clicked()); | 187 EXPECT_FALSE(delegate()->cancel_clicked()); |
182 EXPECT_FALSE(delegate_->link_clicked()); | 188 EXPECT_FALSE(delegate()->link_clicked()); |
183 } | 189 } |
184 | 190 |
185 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) { | 191 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) { |
186 // Check that clicking the cancel button calls Cancel() and closes | 192 // Check that clicking the cancel button calls Cancel() and closes |
187 // the infobar. | 193 // the infobar. |
188 [controller_ cancel:nil]; | 194 [controller_ cancel:nil]; |
189 ASSERT_TRUE(delegate_closed()); | 195 ASSERT_TRUE(delegate_closed()); |
190 EXPECT_FALSE(closed_delegate_ok_clicked_); | 196 EXPECT_FALSE(closed_delegate_ok_clicked_); |
191 EXPECT_TRUE(closed_delegate_cancel_clicked_); | 197 EXPECT_TRUE(closed_delegate_cancel_clicked_); |
192 EXPECT_FALSE(closed_delegate_link_clicked_); | 198 EXPECT_FALSE(closed_delegate_link_clicked_); |
193 } | 199 } |
194 | 200 |
195 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) { | 201 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) { |
196 delegate_->set_dont_close_on_action(); | 202 delegate()->set_dont_close_on_action(); |
197 | 203 |
198 // Check that clicking the cancel button calls Cancel() but does not close | 204 // Check that clicking the cancel button calls Cancel() but does not close |
199 // the infobar. | 205 // the infobar. |
200 [controller_ cancel:nil]; | 206 [controller_ cancel:nil]; |
201 ASSERT_FALSE(delegate_closed()); | 207 ASSERT_FALSE(delegate_closed()); |
202 EXPECT_FALSE(delegate_->ok_clicked()); | 208 EXPECT_FALSE(delegate()->ok_clicked()); |
203 EXPECT_TRUE(delegate_->cancel_clicked()); | 209 EXPECT_TRUE(delegate()->cancel_clicked()); |
204 EXPECT_FALSE(delegate_->link_clicked()); | 210 EXPECT_FALSE(delegate()->link_clicked()); |
205 } | 211 } |
206 | 212 |
207 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) { | 213 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) { |
208 // Check that clicking on the link calls LinkClicked() on the | 214 // Check that clicking on the link calls LinkClicked() on the |
209 // delegate. It should also close the infobar. | 215 // delegate. It should also close the infobar. |
210 [controller_ linkClicked]; | 216 [controller_ linkClicked]; |
211 ASSERT_TRUE(delegate_closed()); | 217 ASSERT_TRUE(delegate_closed()); |
212 EXPECT_FALSE(closed_delegate_ok_clicked_); | 218 EXPECT_FALSE(closed_delegate_ok_clicked_); |
213 EXPECT_FALSE(closed_delegate_cancel_clicked_); | 219 EXPECT_FALSE(closed_delegate_cancel_clicked_); |
214 EXPECT_TRUE(closed_delegate_link_clicked_); | 220 EXPECT_TRUE(closed_delegate_link_clicked_); |
215 } | 221 } |
216 | 222 |
217 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { | 223 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { |
218 delegate_->set_dont_close_on_action(); | 224 delegate()->set_dont_close_on_action(); |
219 | 225 |
220 // Check that clicking on the link calls LinkClicked() on the | 226 // Check that clicking on the link calls LinkClicked() on the |
221 // delegate. It should not close the infobar. | 227 // delegate. It should not close the infobar. |
222 [controller_ linkClicked]; | 228 [controller_ linkClicked]; |
223 ASSERT_FALSE(delegate_closed()); | 229 ASSERT_FALSE(delegate_closed()); |
224 EXPECT_FALSE(delegate_->ok_clicked()); | 230 EXPECT_FALSE(delegate()->ok_clicked()); |
225 EXPECT_FALSE(delegate_->cancel_clicked()); | 231 EXPECT_FALSE(delegate()->cancel_clicked()); |
226 EXPECT_TRUE(delegate_->link_clicked()); | 232 EXPECT_TRUE(delegate()->link_clicked()); |
227 } | 233 } |
228 | 234 |
229 TEST_F(ConfirmInfoBarControllerTest, ResizeView) { | 235 TEST_F(ConfirmInfoBarControllerTest, ResizeView) { |
230 NSRect originalLabelFrame = [controller_ labelFrame]; | 236 NSRect originalLabelFrame = [controller_ labelFrame]; |
231 | 237 |
232 // Expand the view by 20 pixels and make sure the label frame changes | 238 // Expand the view by 20 pixels and make sure the label frame changes |
233 // accordingly. | 239 // accordingly. |
234 const CGFloat width = 20; | 240 const CGFloat width = 20; |
235 NSRect newViewFrame = [[controller_ view] frame]; | 241 NSRect newViewFrame = [[controller_ view] frame]; |
236 newViewFrame.size.width += width; | 242 newViewFrame.size.width += width; |
237 [[controller_ view] setFrame:newViewFrame]; | 243 [[controller_ view] setFrame:newViewFrame]; |
238 | 244 |
239 NSRect newLabelFrame = [controller_ labelFrame]; | 245 NSRect newLabelFrame = [controller_ labelFrame]; |
240 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); | 246 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); |
241 } | 247 } |
242 | 248 |
243 } // namespace | 249 } // namespace |
OLD | NEW |