OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/scoped_nsobject.h" | 7 #include "base/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 #import "chrome/browser/cocoa/cocoa_test_helper.h" | 10 #import "chrome/browser/cocoa/cocoa_test_helper.h" |
| 11 #import "chrome/browser/cocoa/infobar_container_controller.h" |
11 #import "chrome/browser/cocoa/infobar_controller.h" | 12 #import "chrome/browser/cocoa/infobar_controller.h" |
12 #include "chrome/browser/cocoa/infobar_test_helper.h" | 13 #include "chrome/browser/cocoa/infobar_test_helper.h" |
13 #include "chrome/browser/tab_contents/infobar_delegate.h" | 14 #include "chrome/browser/tab_contents/infobar_delegate.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "testing/platform_test.h" | 16 #include "testing/platform_test.h" |
16 | 17 |
17 @interface InfoBarController (ExposedForTesting) | 18 @interface InfoBarController (ExposedForTesting) |
18 - (NSTextField*)label; | 19 - (NSTextField*)label; |
19 @end | 20 @end |
20 | 21 |
21 @implementation InfoBarController (ExposedForTesting) | 22 @implementation InfoBarController (ExposedForTesting) |
22 - (NSTextField*)label { | 23 - (NSTextField*)label { |
23 return label_; | 24 return label_; |
24 } | 25 } |
25 @end | 26 @end |
26 | 27 |
| 28 |
| 29 // Calls to removeDelegate: normally start an animation, which removes the |
| 30 // infobar completely when finished. For unittesting purposes, we create a mock |
| 31 // container which calls close: immediately, rather than kicking off an |
| 32 // animation. |
| 33 @interface InfoBarContainerTest : NSObject <InfoBarContainer> { |
| 34 InfoBarController* controller_; |
| 35 } |
| 36 - (id)initWithController:(InfoBarController*)controller; |
| 37 - (void)removeDelegate:(InfoBarDelegate*)delegate; |
| 38 - (void)removeController:(InfoBarController*)controller; |
| 39 @end |
| 40 |
| 41 @implementation InfoBarContainerTest |
| 42 - (id)initWithController:(InfoBarController*)controller { |
| 43 if ((self = [super init])) { |
| 44 controller_ = controller; |
| 45 } |
| 46 return self; |
| 47 } |
| 48 |
| 49 - (void)removeDelegate:(InfoBarDelegate*)delegate { |
| 50 [controller_ close]; |
| 51 } |
| 52 |
| 53 - (void)removeController:(InfoBarController*)controller { |
| 54 DCHECK(controller_ == controller); |
| 55 controller_ = nil; |
| 56 } |
| 57 @end |
| 58 |
27 namespace { | 59 namespace { |
28 | 60 |
29 /////////////////////////////////////////////////////////////////////////// | 61 /////////////////////////////////////////////////////////////////////////// |
30 // Test fixtures | 62 // Test fixtures |
31 | 63 |
32 class AlertInfoBarControllerTest : public PlatformTest { | 64 class AlertInfoBarControllerTest : public PlatformTest { |
33 public: | 65 public: |
34 virtual void SetUp() { | 66 virtual void SetUp() { |
35 PlatformTest::SetUp(); | 67 PlatformTest::SetUp(); |
36 | 68 |
37 controller_.reset( | 69 controller_.reset( |
38 [[AlertInfoBarController alloc] initWithDelegate:&delegate_]); | 70 [[AlertInfoBarController alloc] initWithDelegate:&delegate_]); |
| 71 container_.reset( |
| 72 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 73 [controller_ setContainerController:container_]; |
39 [helper_.contentView() addSubview:[controller_ view]]; | 74 [helper_.contentView() addSubview:[controller_ view]]; |
40 } | 75 } |
41 | 76 |
42 protected: | 77 protected: |
43 CocoaTestHelper helper_; | 78 CocoaTestHelper helper_; |
44 MockAlertInfoBarDelegate delegate_; | 79 MockAlertInfoBarDelegate delegate_; |
| 80 scoped_nsobject<id> container_; |
45 scoped_nsobject<AlertInfoBarController> controller_; | 81 scoped_nsobject<AlertInfoBarController> controller_; |
46 }; | 82 }; |
47 | 83 |
48 class LinkInfoBarControllerTest : public PlatformTest { | 84 class LinkInfoBarControllerTest : public PlatformTest { |
49 public: | 85 public: |
50 virtual void SetUp() { | 86 virtual void SetUp() { |
51 PlatformTest::SetUp(); | 87 PlatformTest::SetUp(); |
52 | 88 |
53 controller_.reset( | 89 controller_.reset( |
54 [[LinkInfoBarController alloc] initWithDelegate:&delegate_]); | 90 [[LinkInfoBarController alloc] initWithDelegate:&delegate_]); |
| 91 container_.reset( |
| 92 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 93 [controller_ setContainerController:container_]; |
55 [helper_.contentView() addSubview:[controller_ view]]; | 94 [helper_.contentView() addSubview:[controller_ view]]; |
56 } | 95 } |
57 | 96 |
58 protected: | 97 protected: |
59 CocoaTestHelper helper_; | 98 CocoaTestHelper helper_; |
60 MockLinkInfoBarDelegate delegate_; | 99 MockLinkInfoBarDelegate delegate_; |
| 100 scoped_nsobject<id> container_; |
61 scoped_nsobject<LinkInfoBarController> controller_; | 101 scoped_nsobject<LinkInfoBarController> controller_; |
62 }; | 102 }; |
63 | 103 |
64 class ConfirmInfoBarControllerTest : public PlatformTest { | 104 class ConfirmInfoBarControllerTest : public PlatformTest { |
65 public: | 105 public: |
66 virtual void SetUp() { | 106 virtual void SetUp() { |
67 PlatformTest::SetUp(); | 107 PlatformTest::SetUp(); |
68 | 108 |
69 controller_.reset( | 109 controller_.reset( |
70 [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]); | 110 [[ConfirmInfoBarController alloc] initWithDelegate:&delegate_]); |
| 111 container_.reset( |
| 112 [[InfoBarContainerTest alloc] initWithController:controller_]); |
| 113 [controller_ setContainerController:container_]; |
71 [helper_.contentView() addSubview:[controller_ view]]; | 114 [helper_.contentView() addSubview:[controller_ view]]; |
72 } | 115 } |
73 | 116 |
74 protected: | 117 protected: |
75 CocoaTestHelper helper_; | 118 CocoaTestHelper helper_; |
76 MockConfirmInfoBarDelegate delegate_; | 119 MockConfirmInfoBarDelegate delegate_; |
| 120 scoped_nsobject<id> container_; |
77 scoped_nsobject<ConfirmInfoBarController> controller_; | 121 scoped_nsobject<ConfirmInfoBarController> controller_; |
78 }; | 122 }; |
79 | 123 |
80 | 124 |
81 //////////////////////////////////////////////////////////////////////////// | 125 //////////////////////////////////////////////////////////////////////////// |
82 // Tests | 126 // Tests |
83 | 127 |
84 TEST_F(AlertInfoBarControllerTest, ShowAndDismiss) { | 128 TEST_F(AlertInfoBarControllerTest, ShowAndDismiss) { |
85 // Make sure someone looked at the message and icon. | 129 // Make sure someone looked at the message and icon. |
86 EXPECT_TRUE(delegate_.message_text_accessed); | 130 EXPECT_TRUE(delegate_.message_text_accessed); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 226 |
183 // Check that clicking the cancel button calls Cancel() but does not close | 227 // Check that clicking the cancel button calls Cancel() but does not close |
184 // the infobar. | 228 // the infobar. |
185 [controller_ cancel:nil]; | 229 [controller_ cancel:nil]; |
186 EXPECT_FALSE(delegate_.ok_clicked); | 230 EXPECT_FALSE(delegate_.ok_clicked); |
187 EXPECT_TRUE(delegate_.cancel_clicked); | 231 EXPECT_TRUE(delegate_.cancel_clicked); |
188 EXPECT_FALSE(delegate_.closed); | 232 EXPECT_FALSE(delegate_.closed); |
189 } | 233 } |
190 | 234 |
191 } // namespace | 235 } // namespace |
OLD | NEW |