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 <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/api/infobars/confirm_infobar_delegate.h" | 10 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
11 #include "chrome/browser/api/infobars/infobar_service.h" | 11 #include "chrome/browser/api/infobars/infobar_service.h" |
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | 12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
13 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 13 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" |
14 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | 14 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" |
15 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" | 15 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" |
16 #include "chrome/browser/ui/cocoa/infobars/mock_link_infobar_delegate.h" | |
17 #include "chrome/browser/ui/cocoa/run_loop_testing.h" | 16 #include "chrome/browser/ui/cocoa/run_loop_testing.h" |
18 #import "content/public/browser/web_contents.h" | 17 #import "content/public/browser/web_contents.h" |
19 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
22 | 21 |
23 using content::WebContents; | 22 using content::WebContents; |
24 | 23 |
25 @interface InfoBarController (ExposedForTesting) | 24 @interface InfoBarController (ExposedForTesting) |
26 - (NSString*)labelString; | 25 - (NSString*)labelString; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 - (void)removeController:(InfoBarController*)controller { | 58 - (void)removeController:(InfoBarController*)controller { |
60 DCHECK(controller_ == controller); | 59 DCHECK(controller_ == controller); |
61 controller_ = nil; | 60 controller_ = nil; |
62 } | 61 } |
63 | 62 |
64 - (BrowserWindowController*)browserWindowController { | 63 - (BrowserWindowController*)browserWindowController { |
65 return nil; | 64 return nil; |
66 } | 65 } |
67 @end | 66 @end |
68 | 67 |
69 // Calls to removeSelf normally start an animation, which removes the infobar | |
70 // completely when finished. For testing purposes, we create a mock controller | |
71 // which calls close: immediately, rather than kicking off an animation. | |
72 @interface TestLinkInfoBarController : LinkInfoBarController | |
73 - (void)removeSelf; | |
74 @end | |
75 | |
76 @implementation TestLinkInfoBarController | |
77 - (void)removeSelf { | |
78 [self close]; | |
79 } | |
80 @end | |
81 | |
82 @interface TestConfirmInfoBarController : ConfirmInfoBarController | 68 @interface TestConfirmInfoBarController : ConfirmInfoBarController |
83 - (void)removeSelf; | 69 - (void)removeSelf; |
84 @end | 70 @end |
85 | 71 |
86 @implementation TestConfirmInfoBarController | 72 @implementation TestConfirmInfoBarController |
87 - (void)removeSelf { | 73 - (void)removeSelf { |
88 [self close]; | 74 [self close]; |
89 } | 75 } |
90 @end | 76 @end |
91 | 77 |
92 namespace { | 78 namespace { |
93 | 79 |
94 /////////////////////////////////////////////////////////////////////////// | |
95 // Test fixtures | |
96 | |
97 class LinkInfoBarControllerTest : public CocoaProfileTest, | |
98 public MockLinkInfoBarDelegate::Owner { | |
99 public: | |
100 virtual void SetUp() { | |
101 CocoaProfileTest::SetUp(); | |
102 web_contents_.reset( | |
103 WebContents::Create(WebContents::CreateParams(profile()))); | |
104 InfoBarService::CreateForWebContents(web_contents_.get()); | |
105 | |
106 InfoBarService* infobar_service = | |
107 InfoBarService::FromWebContents(web_contents_.get()); | |
108 delegate_ = new MockLinkInfoBarDelegate(this); | |
109 controller_.reset([[TestLinkInfoBarController alloc] | |
110 initWithDelegate:delegate_ owner:infobar_service]); | |
111 container_.reset( | |
112 [[InfoBarContainerTest alloc] initWithController:controller_]); | |
113 [controller_ setContainerController:container_]; | |
114 [[test_window() contentView] addSubview:[controller_ view]]; | |
115 closed_delegate_link_clicked_ = false; | |
116 } | |
117 | |
118 virtual void TearDown() { | |
119 if (delegate_) | |
120 delete delegate_; | |
121 CocoaProfileTest::TearDown(); | |
122 } | |
123 | |
124 protected: | |
125 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | |
126 bool delegate_closed() const { return delegate_ == NULL; } | |
127 | |
128 MockLinkInfoBarDelegate* delegate_; // Owns itself. | |
129 scoped_nsobject<id> container_; | |
130 scoped_nsobject<LinkInfoBarController> controller_; | |
131 bool closed_delegate_link_clicked_; | |
132 | |
133 private: | |
134 virtual void OnInfoBarDelegateClosed() { | |
135 closed_delegate_link_clicked_ = delegate_->link_clicked(); | |
136 delegate_ = NULL; | |
137 } | |
138 | |
139 scoped_ptr<WebContents> web_contents_; | |
140 }; | |
141 | |
142 class ConfirmInfoBarControllerTest : public CocoaProfileTest, | 80 class ConfirmInfoBarControllerTest : public CocoaProfileTest, |
143 public MockConfirmInfoBarDelegate::Owner { | 81 public MockConfirmInfoBarDelegate::Owner { |
144 public: | 82 public: |
145 virtual void SetUp() { | 83 virtual void SetUp() { |
146 CocoaProfileTest::SetUp(); | 84 CocoaProfileTest::SetUp(); |
147 web_contents_.reset( | 85 web_contents_.reset( |
148 WebContents::Create(WebContents::CreateParams(profile()))); | 86 WebContents::Create(WebContents::CreateParams(profile()))); |
149 InfoBarService::CreateForWebContents(web_contents_.get()); | 87 InfoBarService::CreateForWebContents(web_contents_.get()); |
150 | 88 |
151 InfoBarService* infobar_service = | 89 InfoBarService* infobar_service = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); | 122 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); |
185 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); | 123 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); |
186 closed_delegate_link_clicked_ = delegate_->link_clicked(); | 124 closed_delegate_link_clicked_ = delegate_->link_clicked(); |
187 delegate_ = NULL; | 125 delegate_ = NULL; |
188 } | 126 } |
189 | 127 |
190 scoped_ptr<WebContents> web_contents_; | 128 scoped_ptr<WebContents> web_contents_; |
191 }; | 129 }; |
192 | 130 |
193 | 131 |
194 //////////////////////////////////////////////////////////////////////////// | |
195 // Tests | |
196 | |
197 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]); | |
198 | |
199 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) { | |
200 // Make sure someone looked at the message, link, and icon. | |
201 EXPECT_TRUE(delegate_->message_text_accessed()); | |
202 EXPECT_TRUE(delegate_->link_text_accessed()); | |
203 EXPECT_TRUE(delegate_->icon_accessed()); | |
204 | |
205 // Check that dismissing the infobar deletes the delegate. | |
206 [controller_ removeSelf]; | |
207 ASSERT_TRUE(delegate_closed()); | |
208 EXPECT_FALSE(closed_delegate_link_clicked_); | |
209 } | |
210 | |
211 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) { | |
212 // Check that clicking on the link calls LinkClicked() on the | |
213 // delegate. It should also close the infobar. | |
214 [controller_ linkClicked]; | |
215 | |
216 // Spin the runloop because the invocation for closing the infobar is done on | |
217 // a 0-timer delayed selector. | |
218 chrome::testing::NSRunLoopRunAllPending(); | |
219 | |
220 ASSERT_TRUE(delegate_closed()); | |
221 EXPECT_TRUE(closed_delegate_link_clicked_); | |
222 } | |
223 | |
224 TEST_F(LinkInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { | |
225 delegate_->set_dont_close_on_action(); | |
226 | |
227 // Check that clicking on the link calls LinkClicked() on the | |
228 // delegate. It should not close the infobar. | |
229 [controller_ linkClicked]; | |
230 ASSERT_FALSE(delegate_closed()); | |
231 EXPECT_TRUE(delegate_->link_clicked()); | |
232 } | |
233 | |
234 TEST_F(LinkInfoBarControllerTest, DeallocController) { | |
235 // Test that dealloc'ing the controller does not delete the delegate. | |
236 controller_.reset(nil); | |
237 ASSERT_FALSE(delegate_closed()); | |
238 } | |
239 | |
240 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); | 132 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); |
241 | 133 |
242 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { | 134 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { |
243 // Make sure someone looked at the message, link, and icon. | 135 // Make sure someone looked at the message, link, and icon. |
244 EXPECT_TRUE(delegate_->message_text_accessed()); | 136 EXPECT_TRUE(delegate_->message_text_accessed()); |
245 EXPECT_TRUE(delegate_->link_text_accessed()); | 137 EXPECT_TRUE(delegate_->link_text_accessed()); |
246 EXPECT_TRUE(delegate_->icon_accessed()); | 138 EXPECT_TRUE(delegate_->icon_accessed()); |
247 | 139 |
248 // Check to make sure the infobar message was set properly. | 140 // Check to make sure the infobar message was set properly. |
249 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, | 141 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 const CGFloat width = 20; | 223 const CGFloat width = 20; |
332 NSRect newViewFrame = [[controller_ view] frame]; | 224 NSRect newViewFrame = [[controller_ view] frame]; |
333 newViewFrame.size.width += width; | 225 newViewFrame.size.width += width; |
334 [[controller_ view] setFrame:newViewFrame]; | 226 [[controller_ view] setFrame:newViewFrame]; |
335 | 227 |
336 NSRect newLabelFrame = [controller_ labelFrame]; | 228 NSRect newLabelFrame = [controller_ labelFrame]; |
337 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); | 229 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); |
338 } | 230 } |
339 | 231 |
340 } // namespace | 232 } // namespace |
OLD | NEW |