OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Cocoa/Cocoa.h> | |
6 | |
7 #include "base/memory/scoped_nsobject.h" | |
8 #include "base/string_util.h" | |
9 #include "base/sys_string_conversions.h" | |
10 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" | |
11 #include "chrome/browser/api/infobars/infobar_service.h" | |
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" | |
13 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | |
14 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | |
15 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" | |
16 #include "chrome/browser/ui/cocoa/run_loop_testing.h" | |
17 #import "content/public/browser/web_contents.h" | |
18 #include "ipc/ipc_message.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 #include "testing/platform_test.h" | |
21 | |
22 using content::WebContents; | |
23 | |
24 @interface InfoBarController (ExposedForTesting) | |
25 - (NSString*)labelString; | |
26 - (NSRect)labelFrame; | |
27 @end | |
28 | |
29 @implementation InfoBarController (ExposedForTesting) | |
30 - (NSString*)labelString { | |
31 return [label_.get() string]; | |
32 } | |
33 - (NSRect)labelFrame { | |
34 return [label_.get() frame]; | |
35 } | |
36 @end | |
37 | |
38 | |
39 @interface InfoBarContainerTest : NSObject<InfoBarContainer> { | |
40 InfoBarController* controller_; | |
41 } | |
42 - (id)initWithController:(InfoBarController*)controller; | |
43 - (void)willRemoveController:(InfoBarController*)controller; | |
44 - (void)removeController:(InfoBarController*)controller; | |
45 @end | |
46 | |
47 @implementation InfoBarContainerTest | |
48 - (id)initWithController:(InfoBarController*)controller { | |
49 if ((self = [super init])) { | |
50 controller_ = controller; | |
51 } | |
52 return self; | |
53 } | |
54 | |
55 - (void)willRemoveController:(InfoBarController*)controller { | |
56 } | |
57 | |
58 - (void)removeController:(InfoBarController*)controller { | |
59 DCHECK(controller_ == controller); | |
60 controller_ = nil; | |
61 } | |
62 | |
63 - (BrowserWindowController*)browserWindowController { | |
64 return nil; | |
65 } | |
66 @end | |
67 | |
68 @interface TestConfirmInfoBarController : ConfirmInfoBarController | |
69 - (void)removeSelf; | |
70 @end | |
71 | |
72 @implementation TestConfirmInfoBarController | |
73 - (void)removeSelf { | |
74 [self close]; | |
75 } | |
76 @end | |
77 | |
78 namespace { | |
79 | |
80 class ConfirmInfoBarControllerTest : public CocoaProfileTest, | |
81 public MockConfirmInfoBarDelegate::Owner { | |
82 public: | |
83 virtual void SetUp() { | |
84 CocoaProfileTest::SetUp(); | |
85 web_contents_.reset( | |
86 WebContents::Create(WebContents::CreateParams(profile()))); | |
87 InfoBarService::CreateForWebContents(web_contents_.get()); | |
88 | |
89 InfoBarService* infobar_service = | |
90 InfoBarService::FromWebContents(web_contents_.get()); | |
91 delegate_ = new MockConfirmInfoBarDelegate(this); | |
92 controller_.reset([[TestConfirmInfoBarController alloc] | |
93 initWithDelegate:delegate_ owner:infobar_service]); | |
94 container_.reset( | |
95 [[InfoBarContainerTest alloc] initWithController:controller_]); | |
96 [controller_ setContainerController:container_]; | |
97 [[test_window() contentView] addSubview:[controller_ view]]; | |
98 closed_delegate_ok_clicked_ = false; | |
99 closed_delegate_cancel_clicked_ = false; | |
100 closed_delegate_link_clicked_ = false; | |
101 } | |
102 | |
103 virtual void TearDown() { | |
104 if (delegate_) | |
105 delete delegate_; | |
106 CocoaProfileTest::TearDown(); | |
107 } | |
108 | |
109 protected: | |
110 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | |
111 bool delegate_closed() const { return delegate_ == NULL; } | |
112 | |
113 MockConfirmInfoBarDelegate* delegate_; // Owns itself. | |
114 scoped_nsobject<id> container_; | |
115 scoped_nsobject<ConfirmInfoBarController> controller_; | |
116 bool closed_delegate_ok_clicked_; | |
117 bool closed_delegate_cancel_clicked_; | |
118 bool closed_delegate_link_clicked_; | |
119 | |
120 private: | |
121 virtual void OnInfoBarDelegateClosed() { | |
122 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); | |
123 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); | |
124 closed_delegate_link_clicked_ = delegate_->link_clicked(); | |
125 delegate_ = NULL; | |
126 } | |
127 | |
128 scoped_ptr<WebContents> web_contents_; | |
129 }; | |
130 | |
131 | |
132 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); | |
133 | |
134 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { | |
135 // Make sure someone looked at the message, link, and icon. | |
136 EXPECT_TRUE(delegate_->message_text_accessed()); | |
137 EXPECT_TRUE(delegate_->link_text_accessed()); | |
138 EXPECT_TRUE(delegate_->icon_accessed()); | |
139 | |
140 // Check to make sure the infobar message was set properly. | |
141 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, | |
142 base::SysNSStringToUTF8([controller_.get() labelString])); | |
143 | |
144 // Check that dismissing the infobar deletes the delegate. | |
145 [controller_ removeSelf]; | |
146 ASSERT_TRUE(delegate_closed()); | |
147 EXPECT_FALSE(closed_delegate_ok_clicked_); | |
148 EXPECT_FALSE(closed_delegate_cancel_clicked_); | |
149 EXPECT_FALSE(closed_delegate_link_clicked_); | |
150 } | |
151 | |
152 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { | |
153 // Check that clicking the OK button calls Accept() and then closes | |
154 // the infobar. | |
155 [controller_ ok:nil]; | |
156 ASSERT_TRUE(delegate_closed()); | |
157 EXPECT_TRUE(closed_delegate_ok_clicked_); | |
158 EXPECT_FALSE(closed_delegate_cancel_clicked_); | |
159 EXPECT_FALSE(closed_delegate_link_clicked_); | |
160 } | |
161 | |
162 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOKWithoutClosing) { | |
163 delegate_->set_dont_close_on_action(); | |
164 | |
165 // Check that clicking the OK button calls Accept() but does not close | |
166 // the infobar. | |
167 [controller_ ok:nil]; | |
168 ASSERT_FALSE(delegate_closed()); | |
169 EXPECT_TRUE(delegate_->ok_clicked()); | |
170 EXPECT_FALSE(delegate_->cancel_clicked()); | |
171 EXPECT_FALSE(delegate_->link_clicked()); | |
172 } | |
173 | |
174 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancel) { | |
175 // Check that clicking the cancel button calls Cancel() and closes | |
176 // the infobar. | |
177 [controller_ cancel:nil]; | |
178 ASSERT_TRUE(delegate_closed()); | |
179 EXPECT_FALSE(closed_delegate_ok_clicked_); | |
180 EXPECT_TRUE(closed_delegate_cancel_clicked_); | |
181 EXPECT_FALSE(closed_delegate_link_clicked_); | |
182 } | |
183 | |
184 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickCancelWithoutClosing) { | |
185 delegate_->set_dont_close_on_action(); | |
186 | |
187 // Check that clicking the cancel button calls Cancel() but does not close | |
188 // the infobar. | |
189 [controller_ cancel:nil]; | |
190 ASSERT_FALSE(delegate_closed()); | |
191 EXPECT_FALSE(delegate_->ok_clicked()); | |
192 EXPECT_TRUE(delegate_->cancel_clicked()); | |
193 EXPECT_FALSE(delegate_->link_clicked()); | |
194 } | |
195 | |
196 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLink) { | |
197 // Check that clicking on the link calls LinkClicked() on the | |
198 // delegate. It should also close the infobar. | |
199 [controller_ linkClicked]; | |
200 ASSERT_TRUE(delegate_closed()); | |
201 EXPECT_FALSE(closed_delegate_ok_clicked_); | |
202 EXPECT_FALSE(closed_delegate_cancel_clicked_); | |
203 EXPECT_TRUE(closed_delegate_link_clicked_); | |
204 } | |
205 | |
206 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickLinkWithoutClosing) { | |
207 delegate_->set_dont_close_on_action(); | |
208 | |
209 // Check that clicking on the link calls LinkClicked() on the | |
210 // delegate. It should not close the infobar. | |
211 [controller_ linkClicked]; | |
212 ASSERT_FALSE(delegate_closed()); | |
213 EXPECT_FALSE(delegate_->ok_clicked()); | |
214 EXPECT_FALSE(delegate_->cancel_clicked()); | |
215 EXPECT_TRUE(delegate_->link_clicked()); | |
216 } | |
217 | |
218 TEST_F(ConfirmInfoBarControllerTest, ResizeView) { | |
219 NSRect originalLabelFrame = [controller_ labelFrame]; | |
220 | |
221 // Expand the view by 20 pixels and make sure the label frame changes | |
222 // accordingly. | |
223 const CGFloat width = 20; | |
224 NSRect newViewFrame = [[controller_ view] frame]; | |
225 newViewFrame.size.width += width; | |
226 [[controller_ view] setFrame:newViewFrame]; | |
227 | |
228 NSRect newLabelFrame = [controller_ labelFrame]; | |
229 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); | |
230 } | |
231 | |
232 } // namespace | |
OLD | NEW |