Chromium Code Reviews| 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" |
| 11 #include "chrome/browser/infobars/infobar_service.h" | 11 #include "chrome/browser/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_cocoa.h" | |
| 13 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h" | 14 #import "chrome/browser/ui/cocoa/infobars/infobar_container_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/run_loop_testing.h" | 16 #include "chrome/browser/ui/cocoa/run_loop_testing.h" |
| 16 #import "content/public/browser/web_contents.h" | 17 #import "content/public/browser/web_contents.h" |
| 17 #include "ipc/ipc_message.h" | 18 #include "ipc/ipc_message.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/platform_test.h" | 20 #include "testing/platform_test.h" |
| 20 | 21 |
| 21 using content::WebContents; | 22 using content::WebContents; |
| 22 | 23 |
| 23 @interface InfoBarController (ExposedForTesting) | 24 @interface InfoBarController (ExposedForTesting) |
| 24 - (NSString*)labelString; | 25 - (NSString*)labelString; |
| 25 - (NSRect)labelFrame; | 26 - (NSRect)labelFrame; |
| 26 @end | 27 @end |
| 27 | 28 |
| 28 @implementation InfoBarController (ExposedForTesting) | 29 @implementation InfoBarController (ExposedForTesting) |
| 29 - (NSString*)labelString { | 30 - (NSString*)labelString { |
| 30 return [label_.get() string]; | 31 return [label_.get() string]; |
| 31 } | 32 } |
| 32 - (NSRect)labelFrame { | 33 - (NSRect)labelFrame { |
| 33 return [label_.get() frame]; | 34 return [label_.get() frame]; |
| 34 } | 35 } |
| 35 @end | 36 @end |
| 36 | 37 |
| 37 | 38 |
| 38 @interface InfoBarContainerTest : NSObject<InfoBarContainer> { | 39 @interface InfoBarContainerTest : NSObject<InfoBarContainerControllerBase> { |
| 39 InfoBarController* controller_; | 40 InfoBarController* controller_; |
| 40 } | 41 } |
| 42 | |
| 41 - (id)initWithController:(InfoBarController*)controller; | 43 - (id)initWithController:(InfoBarController*)controller; |
| 42 - (void)willRemoveController:(InfoBarController*)controller; | 44 |
| 43 - (void)removeController:(InfoBarController*)controller; | |
| 44 @end | 45 @end |
| 45 | 46 |
| 46 @implementation InfoBarContainerTest | 47 @implementation InfoBarContainerTest |
| 48 | |
| 47 - (id)initWithController:(InfoBarController*)controller { | 49 - (id)initWithController:(InfoBarController*)controller { |
| 48 if ((self = [super init])) { | 50 if ((self = [super init])) { |
| 49 controller_ = controller; | 51 controller_ = controller; |
| 50 } | 52 } |
| 51 return self; | 53 return self; |
| 52 } | 54 } |
| 53 | 55 |
| 54 - (void)willRemoveController:(InfoBarController*)controller { | |
| 55 } | |
| 56 | |
| 57 - (void)removeController:(InfoBarController*)controller { | |
| 58 DCHECK(controller_ == controller); | |
| 59 controller_ = nil; | |
| 60 } | |
| 61 | |
| 62 - (BrowserWindowController*)browserWindowController { | 56 - (BrowserWindowController*)browserWindowController { |
| 63 return nil; | 57 return nil; |
| 64 } | 58 } |
| 59 | |
|
Robert Sesek
2013/08/26 14:06:56
nit: double blank line
sail
2013/08/26 18:32:23
Done.
| |
| 60 | |
| 61 - (BOOL)shouldSuppressTopInfoBarTip { | |
| 62 return NO; | |
| 63 } | |
| 64 | |
| 65 - (CGFloat)infobarArrowX { | |
| 66 return 0; | |
| 67 } | |
| 68 | |
| 65 @end | 69 @end |
| 66 | 70 |
| 67 @interface TestConfirmInfoBarController : ConfirmInfoBarController | 71 @interface TestConfirmInfoBarController : ConfirmInfoBarController |
| 68 - (void)removeSelf; | 72 - (void)removeSelf; |
| 69 @end | 73 @end |
| 70 | 74 |
| 71 @implementation TestConfirmInfoBarController | 75 @implementation TestConfirmInfoBarController |
| 72 - (void)removeSelf { | 76 - (void)removeSelf { |
| 73 [self close]; | 77 [self infobarWillClose]; |
| 78 if ([self infobar]) | |
| 79 [self infobar]->CloseSoon(); | |
| 74 } | 80 } |
| 75 @end | 81 @end |
| 76 | 82 |
| 77 namespace { | 83 namespace { |
| 78 | 84 |
| 79 class ConfirmInfoBarControllerTest : public CocoaProfileTest, | 85 class ConfirmInfoBarControllerTest : public CocoaProfileTest, |
| 80 public MockConfirmInfoBarDelegate::Owner { | 86 public MockConfirmInfoBarDelegate::Owner { |
| 81 public: | 87 public: |
| 82 virtual void SetUp() { | 88 virtual void SetUp() { |
| 83 CocoaProfileTest::SetUp(); | 89 CocoaProfileTest::SetUp(); |
| 84 web_contents_.reset( | 90 web_contents_.reset( |
| 85 WebContents::Create(WebContents::CreateParams(profile()))); | 91 WebContents::Create(WebContents::CreateParams(profile()))); |
| 86 InfoBarService::CreateForWebContents(web_contents_.get()); | 92 InfoBarService::CreateForWebContents(web_contents_.get()); |
| 87 | 93 |
| 88 InfoBarService* infobar_service = | 94 InfoBarService* infobar_service = |
| 89 InfoBarService::FromWebContents(web_contents_.get()); | 95 InfoBarService::FromWebContents(web_contents_.get()); |
| 90 delegate_ = new MockConfirmInfoBarDelegate(this); | 96 delegate_ = new MockConfirmInfoBarDelegate(this); |
| 97 infobar_.reset(new InfoBarCocoa(infobar_service, delegate_)); | |
| 98 | |
| 91 controller_.reset([[TestConfirmInfoBarController alloc] | 99 controller_.reset([[TestConfirmInfoBarController alloc] |
| 92 initWithDelegate:delegate_ owner:infobar_service]); | 100 initWithInfoBar:infobar_.get()]); |
| 101 infobar_->set_controller(controller_); | |
| 102 | |
| 93 container_.reset( | 103 container_.reset( |
| 94 [[InfoBarContainerTest alloc] initWithController:controller_]); | 104 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 95 [controller_ setContainerController:container_]; | 105 [controller_ setContainerController:container_]; |
| 96 [[test_window() contentView] addSubview:[controller_ view]]; | 106 [[test_window() contentView] addSubview:[controller_ view]]; |
| 97 closed_delegate_ok_clicked_ = false; | 107 closed_delegate_ok_clicked_ = false; |
| 98 closed_delegate_cancel_clicked_ = false; | 108 closed_delegate_cancel_clicked_ = false; |
| 99 closed_delegate_link_clicked_ = false; | 109 closed_delegate_link_clicked_ = false; |
| 100 } | 110 } |
| 101 | 111 |
| 102 virtual void TearDown() { | 112 virtual void TearDown() { |
| 113 [controller_ removeSelf]; | |
| 103 if (delegate_) | 114 if (delegate_) |
| 104 delete delegate_; | 115 delete delegate_; |
| 105 CocoaProfileTest::TearDown(); | 116 CocoaProfileTest::TearDown(); |
| 106 } | 117 } |
| 107 | 118 |
| 108 protected: | 119 protected: |
| 109 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! | 120 // Hopefully-obvious: If this returns true, you must not deref |delegate_|! |
| 110 bool delegate_closed() const { return delegate_ == NULL; } | 121 bool delegate_closed() const { return delegate_ == NULL; } |
| 111 | 122 |
| 112 MockConfirmInfoBarDelegate* delegate_; // Owns itself. | 123 MockConfirmInfoBarDelegate* delegate_; // Owns itself. |
| 113 base::scoped_nsobject<id> container_; | 124 base::scoped_nsobject<id> container_; |
| 114 base::scoped_nsobject<ConfirmInfoBarController> controller_; | 125 base::scoped_nsobject<ConfirmInfoBarController> controller_; |
| 115 bool closed_delegate_ok_clicked_; | 126 bool closed_delegate_ok_clicked_; |
| 116 bool closed_delegate_cancel_clicked_; | 127 bool closed_delegate_cancel_clicked_; |
| 117 bool closed_delegate_link_clicked_; | 128 bool closed_delegate_link_clicked_; |
| 118 | 129 |
| 119 private: | 130 private: |
| 120 virtual void OnInfoBarDelegateClosed() OVERRIDE { | 131 virtual void OnInfoBarDelegateClosed() OVERRIDE { |
| 121 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); | 132 closed_delegate_ok_clicked_ = delegate_->ok_clicked(); |
| 122 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); | 133 closed_delegate_cancel_clicked_ = delegate_->cancel_clicked(); |
| 123 closed_delegate_link_clicked_ = delegate_->link_clicked(); | 134 closed_delegate_link_clicked_ = delegate_->link_clicked(); |
| 124 delegate_ = NULL; | 135 delegate_ = NULL; |
| 125 } | 136 } |
| 126 | 137 |
| 127 scoped_ptr<WebContents> web_contents_; | 138 scoped_ptr<WebContents> web_contents_; |
| 139 scoped_ptr<InfoBarCocoa> infobar_; | |
| 128 }; | 140 }; |
| 129 | 141 |
| 130 | 142 |
| 131 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); | 143 TEST_VIEW(ConfirmInfoBarControllerTest, [controller_ view]); |
| 132 | 144 |
| 133 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { | 145 TEST_F(ConfirmInfoBarControllerTest, ShowAndDismiss) { |
| 134 // Make sure someone looked at the message, link, and icon. | 146 // Make sure someone looked at the message, link, and icon. |
| 135 EXPECT_TRUE(delegate_->message_text_accessed()); | 147 EXPECT_TRUE(delegate_->message_text_accessed()); |
| 136 EXPECT_TRUE(delegate_->link_text_accessed()); | 148 EXPECT_TRUE(delegate_->link_text_accessed()); |
| 137 EXPECT_TRUE(delegate_->icon_accessed()); | 149 EXPECT_TRUE(delegate_->icon_accessed()); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 const CGFloat width = 20; | 234 const CGFloat width = 20; |
| 223 NSRect newViewFrame = [[controller_ view] frame]; | 235 NSRect newViewFrame = [[controller_ view] frame]; |
| 224 newViewFrame.size.width += width; | 236 newViewFrame.size.width += width; |
| 225 [[controller_ view] setFrame:newViewFrame]; | 237 [[controller_ view] setFrame:newViewFrame]; |
| 226 | 238 |
| 227 NSRect newLabelFrame = [controller_ labelFrame]; | 239 NSRect newLabelFrame = [controller_ labelFrame]; |
| 228 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); | 240 EXPECT_EQ(NSWidth(newLabelFrame), NSWidth(originalLabelFrame) + width); |
| 229 } | 241 } |
| 230 | 242 |
| 231 } // namespace | 243 } // namespace |
| OLD | NEW |