Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: chrome/browser/ui/views/chooser_content_view_unittest.cc

Issue 2029863002: Refactor ChooserBubbleUiViewDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updated test code Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "chrome/browser/ui/views/chooser_content_view.h"
6
7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "components/chooser_controller/chooser_controller.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/views/controls/styled_label.h"
15 #include "ui/views/controls/styled_label_listener.h"
16 #include "ui/views/controls/table/table_view.h"
17 #include "ui/views/controls/table/table_view_observer.h"
18
19 namespace {
20
21 class MockChooserController : public ChooserController {
22 public:
23 MockChooserController() : ChooserController(nullptr) {}
24 ~MockChooserController() override {}
25
26 // ChooserController:
27 size_t NumOptions() const override { return option_names_.size(); }
28
29 // ChooserController:
30 const base::string16& GetOption(size_t index) const override {
31 return option_names_[index];
32 }
33
34 // ChooserController:
35 void Select(size_t index) override {}
36 void Cancel() override {}
37 void Close() override {}
38 void OpenHelpCenterUrl() const override {}
39
40 void OptionAdded(const base::string16 option_name) {
41 option_names_.push_back(option_name);
42 if (observer())
43 observer()->OnOptionAdded(option_names_.size() - 1);
44 }
45
46 void OptionRemoved(const base::string16 option_name) {
47 for (auto it = option_names_.begin(); it != option_names_.end(); ++it) {
48 if (*it == option_name) {
49 size_t index = it - option_names_.begin();
50 option_names_.erase(it);
51 if (observer())
52 observer()->OnOptionRemoved(index);
53 return;
54 }
55 }
56 }
57
58 private:
59 std::vector<base::string16> option_names_;
60
61 DISALLOW_COPY_AND_ASSIGN(MockChooserController);
62 };
63
64 class MockTableViewObserver : public views::TableViewObserver {
65 public:
66 // views::TableViewObserver:
67 MOCK_METHOD0(OnSelectionChanged, void());
68 };
69
70 class MockStyledLabelListener : public views::StyledLabelListener {
71 public:
72 // views::StyledLabelListener:
73 MOCK_METHOD3(StyledLabelLinkClicked,
74 void(views::StyledLabel* label,
75 const gfx::Range& range,
76 int event_flags));
77 };
78
79 } // namespace
80
81 class ChooserContentViewTest : public testing::Test {
82 public:
83 ChooserContentViewTest() {}
84
85 // testing::Test:
86 void SetUp() override {
87 mock_chooser_controller_.reset(new MockChooserController());
88 ASSERT_TRUE(mock_chooser_controller_);
msw 2016/06/08 00:58:45 nit: this seems unnecessary.
juncai 2016/06/09 01:59:21 Done.
89 mock_table_view_observer_.reset(new MockTableViewObserver());
90 ASSERT_TRUE(mock_table_view_observer_);
msw 2016/06/08 00:58:45 nit: this seems unnecessary.
juncai 2016/06/09 01:59:21 Done.
91 chooser_content_view_.reset(new ChooserContentView(
92 mock_table_view_observer_.get(), mock_chooser_controller_.get()));
93 ASSERT_TRUE(chooser_content_view_);
msw 2016/06/08 00:58:45 nit: this seems unnecessary.
juncai 2016/06/09 01:59:21 Done.
94 table_view_.reset(chooser_content_view_->table_view());
95 ASSERT_TRUE(table_view_);
96 table_model_ = table_view_->model();
97 ASSERT_TRUE(table_model_);
98 mock_styled_label_listener_.reset(new MockStyledLabelListener());
99 ASSERT_TRUE(mock_styled_label_listener_);
msw 2016/06/08 00:58:45 nit: this seems unnecessary.
juncai 2016/06/09 01:59:21 Done.
100 styled_label_.reset(chooser_content_view_->CreateFootnoteView(
101 mock_styled_label_listener_.get()));
102 ASSERT_TRUE(styled_label_);
103 }
104
105 protected:
106 std::unique_ptr<MockChooserController> mock_chooser_controller_;
107 std::unique_ptr<MockTableViewObserver> mock_table_view_observer_;
108 std::unique_ptr<ChooserContentView> chooser_content_view_;
109 std::unique_ptr<MockStyledLabelListener> mock_styled_label_listener_;
110 std::unique_ptr<views::TableView> table_view_;
111 ui::TableModel* table_model_;
112 std::unique_ptr<views::StyledLabel> styled_label_;
113
114 private:
115 DISALLOW_COPY_AND_ASSIGN(ChooserContentViewTest);
116 };
117
118 TEST_F(ChooserContentViewTest, InitialState) {
119 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0);
120
121 // Since "No devices found." needs to be displayed on the |table_view_|,
122 // the number of rows is 1.
123 EXPECT_EQ(table_view_->RowCount(), 1);
msw 2016/06/08 00:58:45 nit: put expectation before the actual value here
juncai 2016/06/09 01:59:21 Done.
124 EXPECT_EQ(
125 table_model_->GetText(0, 0),
126 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT));
127 // |table_view_| should be disabled since there is no option shown.
128 EXPECT_FALSE(table_view_->enabled());
129 // No option selected.
130 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
131 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
132 }
133
134 TEST_F(ChooserContentViewTest, AddOption) {
135 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0);
136
137 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
138 EXPECT_EQ(table_view_->RowCount(), 1);
139 EXPECT_EQ(table_model_->GetText(0, 0), base::ASCIIToUTF16("a"));
140 // |table_view_| should be enabled since there is an option.
141 EXPECT_TRUE(table_view_->enabled());
142 // No option selected.
143 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
144 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
145
146 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
147 EXPECT_EQ(table_view_->RowCount(), 2);
148 EXPECT_EQ(table_model_->GetText(1, 0), base::ASCIIToUTF16("b"));
149 EXPECT_TRUE(table_view_->enabled());
150 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
151 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
152
153 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
154 EXPECT_EQ(table_view_->RowCount(), 3);
155 EXPECT_EQ(table_model_->GetText(2, 0), base::ASCIIToUTF16("c"));
156 EXPECT_TRUE(table_view_->enabled());
157 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
158 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
159 }
160
161 TEST_F(ChooserContentViewTest, RemoveOption) {
162 // Called from TableView::OnItemsRemoved().
163 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(3);
msw 2016/06/08 00:58:45 q: why does the selection change on removing optio
juncai 2016/06/09 01:59:21 https://cs.chromium.org/chromium/src/ui/views/cont
msw 2016/06/09 18:31:15 Okay, it might be nice if that were only called if
164
165 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
166 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
167 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
168
169 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("b"));
170 EXPECT_EQ(table_view_->RowCount(), 2);
171 EXPECT_EQ(table_model_->GetText(0, 0), base::ASCIIToUTF16("a"));
172 EXPECT_EQ(table_model_->GetText(1, 0), base::ASCIIToUTF16("c"));
173 EXPECT_TRUE(table_view_->enabled());
174 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
175 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
176
177 // Remove a non-existent option, the number of rows should not change.
178 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("non-existent"));
179 EXPECT_EQ(table_view_->RowCount(), 2);
180 EXPECT_EQ(table_model_->GetText(0, 0), base::ASCIIToUTF16("a"));
181 EXPECT_EQ(table_model_->GetText(1, 0), base::ASCIIToUTF16("c"));
182 EXPECT_TRUE(table_view_->enabled());
183 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
184 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
185
186 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("c"));
187 EXPECT_EQ(table_view_->RowCount(), 1);
188 EXPECT_EQ(table_model_->GetText(0, 0), base::ASCIIToUTF16("a"));
189 EXPECT_TRUE(table_view_->enabled());
190 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
191 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
192
193 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("a"));
194 // There is no option shown now. But since "No devices found."
195 // needs to be displayed on the |table_view_|, the number of rows is 1.
196 EXPECT_EQ(table_view_->RowCount(), 1);
197 EXPECT_EQ(
198 table_model_->GetText(0, 0),
199 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT));
200 // |table_view_| should be disabled since all options are removed.
201 EXPECT_FALSE(table_view_->enabled());
202 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
203 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
204 }
205
206 TEST_F(ChooserContentViewTest, AddAndRemoveOption) {
207 // Called from TableView::OnItemsRemoved().
208 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(3);
209
210 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
211 EXPECT_EQ(table_view_->RowCount(), 1);
212 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
213 EXPECT_EQ(table_view_->RowCount(), 2);
214 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("b"));
215 EXPECT_EQ(table_view_->RowCount(), 1);
216 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
217 EXPECT_EQ(table_view_->RowCount(), 2);
218 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("d"));
219 EXPECT_EQ(table_view_->RowCount(), 3);
220 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("d"));
221 EXPECT_EQ(table_view_->RowCount(), 2);
222 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("c"));
223 // There is no option shown now. But since "No devices found."
msw 2016/06/08 00:58:45 This isn't right... option "a" is still present.
juncai 2016/06/09 01:59:21 Done.
224 // needs to be displayed on the |table_view_|, the number of rows is 1.
225 EXPECT_EQ(table_view_->RowCount(), 1);
226 }
227
228 TEST_F(ChooserContentViewTest, SelectAndDeselectAnOption) {
229 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(4);
230
231 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
232 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
233 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
234
235 // Select option 0.
236 table_view_->Select(0);
237 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
238 EXPECT_EQ(table_view_->FirstSelectedRow(), 0);
239
240 // Disselect option 0.
msw 2016/06/08 00:58:45 nit: "Unselect" here and elsewhere.
juncai 2016/06/09 01:59:21 Done.
241 table_view_->Select(-1);
242 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
243 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
244
245 // Select option 1.
246 table_view_->Select(1);
247 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
248 EXPECT_EQ(table_view_->FirstSelectedRow(), 1);
249
250 // Disselect option 1.
251 table_view_->Select(-1);
252 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
253 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
254 }
255
256 TEST_F(ChooserContentViewTest, SelectAnOptionAndThenSelectAnotherOption) {
257 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(3);
258
259 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
260 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
261 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
262
263 // Select option 0.
264 table_view_->Select(0);
265 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
266 EXPECT_EQ(table_view_->FirstSelectedRow(), 0);
267
268 // Select option 1.
269 table_view_->Select(1);
270 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
271 EXPECT_EQ(table_view_->FirstSelectedRow(), 1);
272
273 // Select option 2.
274 table_view_->Select(2);
275 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
276 EXPECT_EQ(table_view_->FirstSelectedRow(), 2);
277 }
278
279 TEST_F(ChooserContentViewTest, SelectAnOptionAndRemoveAnotherOption) {
280 // Called one time from TableView::Select() and two times from
281 // TableView::OnItemsRemoved().
282 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(3);
283
284 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
285 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
286 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
287
288 // Select option 1.
289 table_view_->Select(1);
290 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
291 EXPECT_EQ(table_view_->FirstSelectedRow(), 1);
292
293 // Remove option 0, the list becomes: b c.
294 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("a"));
295 EXPECT_EQ(table_view_->RowCount(), 2);
296 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
297 // Since option 0 is removed, the original selected option 1 becomes
298 // the first option in the list.
299 EXPECT_EQ(table_view_->FirstSelectedRow(), 0);
300
301 // Remove option 1.
302 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("c"));
303 EXPECT_EQ(table_view_->RowCount(), 1);
304 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
305 EXPECT_EQ(table_view_->FirstSelectedRow(), 0);
306 }
307
308 TEST_F(ChooserContentViewTest, SelectAnOptionAndRemoveTheSelectedOption) {
309 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(2);
310
311 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
312 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
313 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
314
315 // Select option 1.
316 table_view_->Select(1);
317 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
318 EXPECT_EQ(table_view_->FirstSelectedRow(), 1);
319
320 // Remove option 1.
321 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("b"));
322 EXPECT_EQ(table_view_->RowCount(), 2);
323 // No option selected.
324 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
325 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
326 }
327
328 TEST_F(ChooserContentViewTest,
329 AddAnOptionAndSelectItAndRemoveTheSelectedOption) {
330 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(2);
331
332 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
333
334 // Select option 0.
335 table_view_->Select(0);
336 EXPECT_EQ(table_view_->SelectedRowCount(), 1);
337 EXPECT_EQ(table_view_->FirstSelectedRow(), 0);
338
339 // Remove option 0.
340 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("a"));
341 // There is no option shown now. But since "No devices found."
342 // needs to be displayed on the |table_view_|, the number of rows is 1.
343 EXPECT_EQ(table_view_->RowCount(), 1);
344 EXPECT_EQ(
345 table_model_->GetText(0, 0),
346 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT));
347 // |table_view_| should be disabled since all options are removed.
348 EXPECT_FALSE(table_view_->enabled());
349 // No option selected.
350 EXPECT_EQ(table_view_->SelectedRowCount(), 0);
351 EXPECT_EQ(table_view_->FirstSelectedRow(), -1);
352 }
353
354 TEST_F(ChooserContentViewTest, ClickStyledLabelLink) {
355 EXPECT_CALL(*mock_styled_label_listener_,
356 StyledLabelLinkClicked(styled_label_.get(), testing::_, 0))
357 .Times(1);
358 styled_label_->LinkClicked(nullptr, 0);
359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698