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