| 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/infobars/infobar_tab_helper.h" |
| 10 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 11 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
| 11 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" |
| 12 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 13 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" |
| 13 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" | 14 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h" |
| 14 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" | 15 #include "chrome/browser/ui/cocoa/infobars/mock_confirm_infobar_delegate.h" |
| 15 #include "chrome/browser/ui/cocoa/infobars/mock_link_infobar_delegate.h" | 16 #include "chrome/browser/ui/cocoa/infobars/mock_link_infobar_delegate.h" |
| 16 #include "chrome/browser/ui/cocoa/run_loop_testing.h" | 17 #include "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 19 #import "content/browser/tab_contents/tab_contents.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 19 | 22 |
| 20 @interface InfoBarController (ExposedForTesting) | 23 @interface InfoBarController (ExposedForTesting) |
| 21 - (NSString*)labelString; | 24 - (NSString*)labelString; |
| 22 - (NSRect)labelFrame; | 25 - (NSRect)labelFrame; |
| 23 @end | 26 @end |
| 24 | 27 |
| 25 @implementation InfoBarController (ExposedForTesting) | 28 @implementation InfoBarController (ExposedForTesting) |
| 26 - (NSString*)labelString { | 29 - (NSString*)labelString { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 - (void)removeSelf { | 85 - (void)removeSelf { |
| 83 [self close]; | 86 [self close]; |
| 84 } | 87 } |
| 85 @end | 88 @end |
| 86 | 89 |
| 87 namespace { | 90 namespace { |
| 88 | 91 |
| 89 /////////////////////////////////////////////////////////////////////////// | 92 /////////////////////////////////////////////////////////////////////////// |
| 90 // Test fixtures | 93 // Test fixtures |
| 91 | 94 |
| 92 class LinkInfoBarControllerTest : public CocoaTest, | 95 class LinkInfoBarControllerTest : public CocoaProfileTest, |
| 93 public MockLinkInfoBarDelegate::Owner { | 96 public MockLinkInfoBarDelegate::Owner { |
| 94 public: | 97 public: |
| 95 virtual void SetUp() { | 98 virtual void SetUp() { |
| 96 CocoaTest::SetUp(); | 99 CocoaProfileTest::SetUp(); |
| 100 tab_contents_.reset(new TabContentsWrapper(new TabContents(profile(), NULL, |
| 101 MSG_ROUTING_NONE, NULL, NULL))); |
| 102 tab_contents_->infobar_tab_helper()->set_infobars_enabled(false); |
| 97 | 103 |
| 98 delegate_ = new MockLinkInfoBarDelegate(this); | 104 delegate_ = new MockLinkInfoBarDelegate(this); |
| 99 controller_.reset( | 105 controller_.reset([[TestLinkInfoBarController alloc] |
| 100 [[TestLinkInfoBarController alloc] initWithDelegate:delegate_ | 106 initWithDelegate:delegate_ |
| 101 owner:NULL]); | 107 owner:tab_contents_.get()]); |
| 102 container_.reset( | 108 container_.reset( |
| 103 [[InfoBarContainerTest alloc] initWithController:controller_]); | 109 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 104 [controller_ setContainerController:container_]; | 110 [controller_ setContainerController:container_]; |
| 105 [[test_window() contentView] addSubview:[controller_ view]]; | 111 [[test_window() contentView] addSubview:[controller_ view]]; |
| 106 closed_delegate_link_clicked_ = false; | 112 closed_delegate_link_clicked_ = false; |
| 107 } | 113 } |
| 108 | 114 |
| 109 virtual void TearDown() { | 115 virtual void TearDown() { |
| 110 if (delegate_) | 116 if (delegate_) |
| 111 delete delegate_; | 117 delete delegate_; |
| 112 CocoaTest::TearDown(); | 118 CocoaProfileTest::TearDown(); |
| 113 } | 119 } |
| 114 | 120 |
| 115 protected: | 121 protected: |
| 116 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | 122 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! |
| 117 bool delegate_closed() const { return delegate_ == NULL; } | 123 bool delegate_closed() const { return delegate_ == NULL; } |
| 118 | 124 |
| 119 MockLinkInfoBarDelegate* delegate_; // Owns itself. | 125 MockLinkInfoBarDelegate* delegate_; // Owns itself. |
| 120 scoped_nsobject<id> container_; | 126 scoped_nsobject<id> container_; |
| 121 scoped_nsobject<LinkInfoBarController> controller_; | 127 scoped_nsobject<LinkInfoBarController> controller_; |
| 122 bool closed_delegate_link_clicked_; | 128 bool closed_delegate_link_clicked_; |
| 123 | 129 |
| 124 private: | 130 private: |
| 125 virtual void OnInfoBarDelegateClosed() { | 131 virtual void OnInfoBarDelegateClosed() { |
| 126 closed_delegate_link_clicked_ = delegate_->link_clicked(); | 132 closed_delegate_link_clicked_ = delegate_->link_clicked(); |
| 127 delegate_ = NULL; | 133 delegate_ = NULL; |
| 128 } | 134 } |
| 135 |
| 136 scoped_ptr<TabContentsWrapper> tab_contents_; |
| 129 }; | 137 }; |
| 130 | 138 |
| 131 class ConfirmInfoBarControllerTest : public CocoaTest, | 139 class ConfirmInfoBarControllerTest : public CocoaProfileTest, |
| 132 public MockConfirmInfoBarDelegate::Owner { | 140 public MockConfirmInfoBarDelegate::Owner { |
| 133 public: | 141 public: |
| 134 virtual void SetUp() { | 142 virtual void SetUp() { |
| 135 CocoaTest::SetUp(); | 143 CocoaProfileTest::SetUp(); |
| 144 tab_contents_.reset(new TabContentsWrapper(new TabContents(profile(), NULL, |
| 145 MSG_ROUTING_NONE, NULL, NULL))); |
| 146 tab_contents_->infobar_tab_helper()->set_infobars_enabled(false); |
| 136 | 147 |
| 137 delegate_ = new MockConfirmInfoBarDelegate(this); | 148 delegate_ = new MockConfirmInfoBarDelegate(this); |
| 138 controller_.reset( | 149 controller_.reset([[TestConfirmInfoBarController alloc] |
| 139 [[TestConfirmInfoBarController alloc] initWithDelegate:delegate_ | 150 initWithDelegate:delegate_ |
| 140 owner:NULL]); | 151 owner:tab_contents_.get()]); |
| 141 container_.reset( | 152 container_.reset( |
| 142 [[InfoBarContainerTest alloc] initWithController:controller_]); | 153 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 143 [controller_ setContainerController:container_]; | 154 [controller_ setContainerController:container_]; |
| 144 [[test_window() contentView] addSubview:[controller_ view]]; | 155 [[test_window() contentView] addSubview:[controller_ view]]; |
| 145 closed_delegate_ok_clicked_ = false; | 156 closed_delegate_ok_clicked_ = false; |
| 146 closed_delegate_cancel_clicked_ = false; | 157 closed_delegate_cancel_clicked_ = false; |
| 147 closed_delegate_link_clicked_ = false; | 158 closed_delegate_link_clicked_ = false; |
| 148 } | 159 } |
| 149 | 160 |
| 150 virtual void TearDown() { | 161 virtual void TearDown() { |
| 151 if (delegate_) | 162 if (delegate_) |
| 152 delete delegate_; | 163 delete delegate_; |
| 153 CocoaTest::TearDown(); | 164 CocoaProfileTest::TearDown(); |
| 154 } | 165 } |
| 155 | 166 |
| 156 protected: | 167 protected: |
| 157 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | 168 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! |
| 158 bool delegate_closed() const { return delegate_ == NULL; } | 169 bool delegate_closed() const { return delegate_ == NULL; } |
| 159 | 170 |
| 160 MockConfirmInfoBarDelegate* delegate_; // Owns itself. | 171 MockConfirmInfoBarDelegate* delegate_; // Owns itself. |
| 161 scoped_nsobject<id> container_; | 172 scoped_nsobject<id> container_; |
| 162 scoped_nsobject<ConfirmInfoBarController> controller_; | 173 scoped_nsobject<ConfirmInfoBarController> controller_; |
| 163 bool closed_delegate_ok_clicked_; | 174 bool closed_delegate_ok_clicked_; |
| 164 bool closed_delegate_cancel_clicked_; | 175 bool closed_delegate_cancel_clicked_; |
| 165 bool closed_delegate_link_clicked_; | 176 bool closed_delegate_link_clicked_; |
| 166 | 177 |
| 167 private: | 178 private: |
| 168 virtual void OnInfoBarDelegateClosed() { | 179 virtual void OnInfoBarDelegateClosed() { |
| 169 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); | 180 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); |
| 170 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); | 181 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); |
| 171 closed_delegate_link_clicked_ = delegate_->link_clicked(); | 182 closed_delegate_link_clicked_ = delegate_->link_clicked(); |
| 172 delegate_ = NULL; | 183 delegate_ = NULL; |
| 173 } | 184 } |
| 185 |
| 186 scoped_ptr<TabContentsWrapper> tab_contents_; |
| 174 }; | 187 }; |
| 175 | 188 |
| 176 | 189 |
| 177 //////////////////////////////////////////////////////////////////////////// | 190 //////////////////////////////////////////////////////////////////////////// |
| 178 // Tests | 191 // Tests |
| 179 | 192 |
| 180 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]); | 193 TEST_VIEW(LinkInfoBarControllerTest, [controller_ view]); |
| 181 | 194 |
| 182 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) { | 195 TEST_F(LinkInfoBarControllerTest, ShowAndDismiss) { |
| 183 // Make sure someone looked at the message, link, and icon. | 196 // Make sure someone looked at the message, link, and icon. |
| 184 EXPECT_TRUE(delegate_->message_text_accessed()); | 197 EXPECT_TRUE(delegate_->message_text_accessed()); |
| 185 EXPECT_TRUE(delegate_->link_text_accessed()); | 198 EXPECT_TRUE(delegate_->link_text_accessed()); |
| 186 EXPECT_TRUE(delegate_->icon_accessed()); | 199 EXPECT_TRUE(delegate_->icon_accessed()); |
| 187 | 200 |
| 188 // Check that dismissing the infobar deletes the delegate. | 201 // Check that dismissing the infobar deletes the delegate. |
| 189 [controller_ dismiss:nil]; | 202 [controller_ removeSelf]; |
| 190 ASSERT_TRUE(delegate_closed()); | 203 ASSERT_TRUE(delegate_closed()); |
| 191 EXPECT_FALSE(closed_delegate_link_clicked_); | 204 EXPECT_FALSE(closed_delegate_link_clicked_); |
| 192 } | 205 } |
| 193 | 206 |
| 194 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) { | 207 TEST_F(LinkInfoBarControllerTest, ShowAndClickLink) { |
| 195 // Check that clicking on the link calls LinkClicked() on the | 208 // Check that clicking on the link calls LinkClicked() on the |
| 196 // delegate. It should also close the infobar. | 209 // delegate. It should also close the infobar. |
| 197 [controller_ linkClicked]; | 210 [controller_ linkClicked]; |
| 198 | 211 |
| 199 // Spin the runloop because the invocation for closing the infobar is done on | 212 // Spin the runloop because the invocation for closing the infobar is done on |
| (...skipping 26 matching lines...) Expand all Loading... |
| 226 // Make sure someone looked at the message, link, and icon. | 239 // Make sure someone looked at the message, link, and icon. |
| 227 EXPECT_TRUE(delegate_->message_text_accessed()); | 240 EXPECT_TRUE(delegate_->message_text_accessed()); |
| 228 EXPECT_TRUE(delegate_->link_text_accessed()); | 241 EXPECT_TRUE(delegate_->link_text_accessed()); |
| 229 EXPECT_TRUE(delegate_->icon_accessed()); | 242 EXPECT_TRUE(delegate_->icon_accessed()); |
| 230 | 243 |
| 231 // Check to make sure the infobar message was set properly. | 244 // Check to make sure the infobar message was set properly. |
| 232 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, | 245 EXPECT_EQ(MockConfirmInfoBarDelegate::kMessage, |
| 233 base::SysNSStringToUTF8([controller_.get() labelString])); | 246 base::SysNSStringToUTF8([controller_.get() labelString])); |
| 234 | 247 |
| 235 // Check that dismissing the infobar deletes the delegate. | 248 // Check that dismissing the infobar deletes the delegate. |
| 236 [controller_ dismiss:nil]; | 249 [controller_ removeSelf]; |
| 237 ASSERT_TRUE(delegate_closed()); | 250 ASSERT_TRUE(delegate_closed()); |
| 238 EXPECT_FALSE(closed_delegate_ok_clicked_); | 251 EXPECT_FALSE(closed_delegate_ok_clicked_); |
| 239 EXPECT_FALSE(closed_delegate_cancel_clicked_); | 252 EXPECT_FALSE(closed_delegate_cancel_clicked_); |
| 240 EXPECT_FALSE(closed_delegate_link_clicked_); | 253 EXPECT_FALSE(closed_delegate_link_clicked_); |
| 241 } | 254 } |
| 242 | 255 |
| 243 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { | 256 TEST_F(ConfirmInfoBarControllerTest, ShowAndClickOK) { |
| 244 // Check that clicking the OK button calls Accept() and then closes | 257 // Check that clicking the OK button calls Accept() and then closes |
| 245 // the infobar. | 258 // the infobar. |
| 246 [controller_ ok:nil]; | 259 [controller_ ok:nil]; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 const CGFloat width = 20; | 327 const CGFloat width = 20; |
| 315 NSRect newViewFrame = [[controller_ view] frame]; | 328 NSRect newViewFrame = [[controller_ view] frame]; |
| 316 newViewFrame.size.width += width; | 329 newViewFrame.size.width += width; |
| 317 [[controller_ view] setFrame:newViewFrame]; | 330 [[controller_ view] setFrame:newViewFrame]; |
| 318 | 331 |
| 319 NSRect newLabelFrame = [controller_ labelFrame]; | 332 NSRect newLabelFrame = [controller_ labelFrame]; |
| 320 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); | 333 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); |
| 321 } | 334 } |
| 322 | 335 |
| 323 } // namespace | 336 } // namespace |
| OLD | NEW |