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