OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/memory/scoped_nsobject.h" | 6 #include "base/memory/scoped_nsobject.h" |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" | 10 #include "chrome/browser/ui/cocoa/cocoa_test_helper.h" |
11 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h" | 11 #import "chrome/browser/ui/cocoa/confirm_bubble_controller.h" |
12 #import "chrome/browser/ui/cocoa/confirm_bubble_view.h" | 12 #import "chrome/browser/ui/cocoa/confirm_bubble_view.h" |
13 #include "chrome/browser/ui/confirm_bubble_model.h" | 13 #include "chrome/browser/ui/confirm_bubble_model.h" |
14 #include "grit/theme_resources.h" | 14 #include "grit/theme_resources.h" |
15 #import "testing/gtest_mac.h" | 15 #import "testing/gtest_mac.h" |
16 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
17 #import "ui/gfx/point.h" | 17 #import "ui/gfx/point.h" |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // The model object used in this test. This model implements all methods and | 21 // The model object used in this test. This model implements all methods and |
22 // updates its status when ConfirmBubbleController calls its methods. | 22 // updates its status when ConfirmBubbleController calls its methods. |
23 class TestConfirmBubbleModel : public ConfirmBubbleModel { | 23 class TestConfirmBubbleModel : public ConfirmBubbleModel { |
24 public: | 24 public: |
| 25 TestConfirmBubbleModel(bool* model_deleted, |
| 26 bool* accept_clicked, |
| 27 bool* cancel_clicked, |
| 28 bool* link_clicked); |
25 TestConfirmBubbleModel(); | 29 TestConfirmBubbleModel(); |
26 virtual ~TestConfirmBubbleModel() OVERRIDE; | 30 virtual ~TestConfirmBubbleModel() OVERRIDE; |
27 virtual string16 GetTitle() const OVERRIDE; | 31 virtual string16 GetTitle() const OVERRIDE; |
28 virtual string16 GetMessageText() const OVERRIDE; | 32 virtual string16 GetMessageText() const OVERRIDE; |
29 virtual gfx::Image* GetIcon() const OVERRIDE; | 33 virtual gfx::Image* GetIcon() const OVERRIDE; |
30 virtual int GetButtons() const OVERRIDE; | 34 virtual int GetButtons() const OVERRIDE; |
31 virtual string16 GetButtonLabel(BubbleButton button) const OVERRIDE; | 35 virtual string16 GetButtonLabel(BubbleButton button) const OVERRIDE; |
32 virtual void Accept() OVERRIDE; | 36 virtual void Accept() OVERRIDE; |
33 virtual void Cancel() OVERRIDE; | 37 virtual void Cancel() OVERRIDE; |
34 virtual string16 GetLinkText() const OVERRIDE; | 38 virtual string16 GetLinkText() const OVERRIDE; |
35 virtual void LinkClicked() OVERRIDE; | 39 virtual void LinkClicked() OVERRIDE; |
36 | 40 |
37 bool accept_clicked() const { return accept_clicked_; } | |
38 bool cancel_clicked() const { return cancel_clicked_; } | |
39 bool link_clicked() const { return link_clicked_; } | |
40 | |
41 private: | 41 private: |
42 bool accept_clicked_; | 42 bool* model_deleted_; |
43 bool cancel_clicked_; | 43 bool* accept_clicked_; |
44 bool link_clicked_; | 44 bool* cancel_clicked_; |
| 45 bool* link_clicked_; |
45 }; | 46 }; |
46 | 47 |
47 TestConfirmBubbleModel::TestConfirmBubbleModel() : | 48 TestConfirmBubbleModel::TestConfirmBubbleModel(bool* model_deleted, |
48 accept_clicked_(false), | 49 bool* accept_clicked, |
49 cancel_clicked_(false), | 50 bool* cancel_clicked, |
50 link_clicked_(false) { | 51 bool* link_clicked) |
| 52 : model_deleted_(model_deleted), |
| 53 accept_clicked_(accept_clicked), |
| 54 cancel_clicked_(cancel_clicked), |
| 55 link_clicked_(link_clicked) { |
51 } | 56 } |
52 | 57 |
53 TestConfirmBubbleModel::~TestConfirmBubbleModel() { | 58 TestConfirmBubbleModel::~TestConfirmBubbleModel() { |
| 59 *model_deleted_ = true; |
54 } | 60 } |
55 | 61 |
56 string16 TestConfirmBubbleModel::GetTitle() const { | 62 string16 TestConfirmBubbleModel::GetTitle() const { |
57 return ASCIIToUTF16("Test"); | 63 return ASCIIToUTF16("Test"); |
58 } | 64 } |
59 | 65 |
60 string16 TestConfirmBubbleModel::GetMessageText() const { | 66 string16 TestConfirmBubbleModel::GetMessageText() const { |
61 return ASCIIToUTF16("Test Message"); | 67 return ASCIIToUTF16("Test Message"); |
62 } | 68 } |
63 | 69 |
64 gfx::Image* TestConfirmBubbleModel::GetIcon() const { | 70 gfx::Image* TestConfirmBubbleModel::GetIcon() const { |
65 return &ResourceBundle::GetSharedInstance().GetImageNamed( | 71 return &ResourceBundle::GetSharedInstance().GetImageNamed( |
66 IDR_PRODUCT_LOGO_16); | 72 IDR_PRODUCT_LOGO_16); |
67 } | 73 } |
68 | 74 |
69 int TestConfirmBubbleModel::GetButtons() const { | 75 int TestConfirmBubbleModel::GetButtons() const { |
70 return BUTTON_OK | BUTTON_CANCEL; | 76 return BUTTON_OK | BUTTON_CANCEL; |
71 } | 77 } |
72 | 78 |
73 string16 TestConfirmBubbleModel::GetButtonLabel(BubbleButton button) const { | 79 string16 TestConfirmBubbleModel::GetButtonLabel(BubbleButton button) const { |
74 return button == BUTTON_OK ? ASCIIToUTF16("OK") : ASCIIToUTF16("Cancel"); | 80 return button == BUTTON_OK ? ASCIIToUTF16("OK") : ASCIIToUTF16("Cancel"); |
75 } | 81 } |
76 | 82 |
77 void TestConfirmBubbleModel::Accept() { | 83 void TestConfirmBubbleModel::Accept() { |
78 accept_clicked_ = true; | 84 *accept_clicked_ = true; |
79 } | 85 } |
80 | 86 |
81 void TestConfirmBubbleModel::Cancel() { | 87 void TestConfirmBubbleModel::Cancel() { |
82 cancel_clicked_ = true; | 88 *cancel_clicked_ = true; |
83 } | 89 } |
84 | 90 |
85 string16 TestConfirmBubbleModel::GetLinkText() const { | 91 string16 TestConfirmBubbleModel::GetLinkText() const { |
86 return ASCIIToUTF16("Link"); | 92 return ASCIIToUTF16("Link"); |
87 } | 93 } |
88 | 94 |
89 void TestConfirmBubbleModel::LinkClicked() { | 95 void TestConfirmBubbleModel::LinkClicked() { |
90 link_clicked_ = true; | 96 *link_clicked_ = true; |
91 } | 97 } |
92 | 98 |
93 } // namespace | 99 } // namespace |
94 | 100 |
95 class ConfirmBubbleControllerTest : public CocoaTest { | 101 class ConfirmBubbleControllerTest : public CocoaTest { |
96 public: | 102 public: |
97 ConfirmBubbleControllerTest() { | 103 ConfirmBubbleControllerTest() |
| 104 : model_deleted_(false), |
| 105 accept_clicked_(false), |
| 106 cancel_clicked_(false), |
| 107 link_clicked_(false) { |
98 NSView* view = [test_window() contentView]; | 108 NSView* view = [test_window() contentView]; |
99 model_.reset(new TestConfirmBubbleModel); | 109 // This model is owned by the controller created below. |
| 110 model_ = new TestConfirmBubbleModel(&model_deleted_, |
| 111 &accept_clicked_, |
| 112 &cancel_clicked_, |
| 113 &link_clicked_); |
100 gfx::Point origin(0, 0); | 114 gfx::Point origin(0, 0); |
101 controller_ = | 115 controller_ = |
102 [[ConfirmBubbleController alloc] initWithParent:view | 116 [[ConfirmBubbleController alloc] initWithParent:view |
103 origin:origin.ToCGPoint() | 117 origin:origin.ToCGPoint() |
104 model:model_.get()]; | 118 model:model_]; |
105 [view addSubview:[controller_ view] | 119 [view addSubview:[controller_ view] |
106 positioned:NSWindowAbove | 120 positioned:NSWindowAbove |
107 relativeTo:nil]; | 121 relativeTo:nil]; |
108 } | 122 } |
109 | 123 |
110 ConfirmBubbleView* GetBubbleView() const { | 124 ConfirmBubbleView* GetBubbleView() const { |
111 return (ConfirmBubbleView*)[controller_ view]; | 125 return (ConfirmBubbleView*)[controller_ view]; |
112 } | 126 } |
113 | 127 |
114 TestConfirmBubbleModel* model() const { | 128 TestConfirmBubbleModel* model() const { return model_; } |
115 return model_.get(); | 129 bool model_deleted() const { return model_deleted_; } |
116 } | 130 bool accept_clicked() const { return accept_clicked_; } |
| 131 bool cancel_clicked() const { return cancel_clicked_; } |
| 132 bool link_clicked() const { return link_clicked_; } |
117 | 133 |
118 private: | 134 private: |
119 ConfirmBubbleController* controller_; // weak; owns self | 135 ConfirmBubbleController* controller_; // weak; owns self |
120 scoped_ptr<TestConfirmBubbleModel> model_; | 136 TestConfirmBubbleModel* model_; // weak |
| 137 bool model_deleted_; |
| 138 bool accept_clicked_; |
| 139 bool cancel_clicked_; |
| 140 bool link_clicked_; |
121 }; | 141 }; |
122 | 142 |
123 // Verify clicking a button or a link removes the ConfirmBubbleView object and | 143 // Verify clicking a button or a link removes the ConfirmBubbleView object and |
124 // calls an appropriate model method. | 144 // calls an appropriate model method. |
125 TEST_F(ConfirmBubbleControllerTest, ClickOk) { | 145 TEST_F(ConfirmBubbleControllerTest, ClickOk) { |
126 NSView* view = [test_window() contentView]; | 146 NSView* view = [test_window() contentView]; |
127 ConfirmBubbleView* bubble_view = GetBubbleView(); | 147 ConfirmBubbleView* bubble_view = GetBubbleView(); |
128 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 148 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
129 EXPECT_TRUE(contains_bubble_view); | 149 EXPECT_TRUE(contains_bubble_view); |
130 | 150 |
131 // Click its OK button and verify this view has been removed from the test | 151 // Click its OK button and verify this view has been removed from the test |
132 // window. Also verify TestConfirmBubbleModel::Accept() has been called. | 152 // window. Also verify TestConfirmBubbleModel::Accept() has been called. |
133 [bubble_view clickOk]; | 153 [bubble_view clickOk]; |
134 | 154 |
135 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 155 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
136 EXPECT_FALSE(contains_bubble_view); | 156 EXPECT_FALSE(contains_bubble_view); |
137 EXPECT_TRUE(model()->accept_clicked()); | 157 EXPECT_TRUE(accept_clicked()); |
138 EXPECT_FALSE(model()->cancel_clicked()); | 158 EXPECT_FALSE(cancel_clicked()); |
139 EXPECT_FALSE(model()->link_clicked()); | 159 EXPECT_FALSE(link_clicked()); |
140 } | 160 } |
141 | 161 |
142 TEST_F(ConfirmBubbleControllerTest, ClickCancel) { | 162 TEST_F(ConfirmBubbleControllerTest, ClickCancel) { |
143 NSView* view = [test_window() contentView]; | 163 NSView* view = [test_window() contentView]; |
144 ConfirmBubbleView* bubble_view = GetBubbleView(); | 164 ConfirmBubbleView* bubble_view = GetBubbleView(); |
145 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 165 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
146 EXPECT_TRUE(contains_bubble_view); | 166 EXPECT_TRUE(contains_bubble_view); |
147 | 167 |
148 // Click its cancel button and verify this view has been removed from the test | 168 // Click its cancel button and verify this view has been removed from the test |
149 // window. Also verify TestConfirmBubbleModel::Cancel() has been called. | 169 // window. Also verify TestConfirmBubbleModel::Cancel() has been called. |
150 [bubble_view clickCancel]; | 170 [bubble_view clickCancel]; |
151 | 171 |
152 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 172 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
153 EXPECT_FALSE(contains_bubble_view); | 173 EXPECT_FALSE(contains_bubble_view); |
154 EXPECT_FALSE(model()->accept_clicked()); | 174 EXPECT_FALSE(accept_clicked()); |
155 EXPECT_TRUE(model()->cancel_clicked()); | 175 EXPECT_TRUE(cancel_clicked()); |
156 EXPECT_FALSE(model()->link_clicked()); | 176 EXPECT_FALSE(link_clicked()); |
157 } | 177 } |
158 | 178 |
159 TEST_F(ConfirmBubbleControllerTest, ClickLink) { | 179 TEST_F(ConfirmBubbleControllerTest, ClickLink) { |
160 NSView* view = [test_window() contentView]; | 180 NSView* view = [test_window() contentView]; |
161 ConfirmBubbleView* bubble_view = GetBubbleView(); | 181 ConfirmBubbleView* bubble_view = GetBubbleView(); |
162 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 182 bool contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
163 EXPECT_TRUE(contains_bubble_view); | 183 EXPECT_TRUE(contains_bubble_view); |
164 | 184 |
165 // Click its link and verify this view has been removed from the test window. | 185 // Click its link and verify this view has been removed from the test window. |
166 // Also verify TestConfirmBubbleModel::LinkClicked() has been called. | 186 // Also verify TestConfirmBubbleModel::LinkClicked() has been called. |
167 [bubble_view clickLink]; | 187 [bubble_view clickLink]; |
168 | 188 |
169 contains_bubble_view = [[view subviews] containsObject:bubble_view]; | 189 contains_bubble_view = [[view subviews] containsObject:bubble_view]; |
170 EXPECT_FALSE(contains_bubble_view); | 190 EXPECT_FALSE(contains_bubble_view); |
171 EXPECT_FALSE(model()->accept_clicked()); | 191 EXPECT_FALSE(accept_clicked()); |
172 EXPECT_FALSE(model()->cancel_clicked()); | 192 EXPECT_FALSE(cancel_clicked()); |
173 EXPECT_TRUE(model()->link_clicked()); | 193 EXPECT_TRUE(link_clicked()); |
174 } | 194 } |
OLD | NEW |