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

Side by Side Diff: chrome/browser/ui/views/extensions/chooser_dialog_view_browsertest.cc

Issue 2171103002: Add test for WebBluetooth chooser spinner, status text and rescan button (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added test for WebBluetooth chooser spinner, status text and rescan button Created 4 years, 5 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/ui/views/extensions/chooser_dialog_view.h" 5 #include "chrome/browser/ui/views/extensions/chooser_dialog_view.h"
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chooser_controller/mock_chooser_controller.h" 9 #include "chrome/browser/chooser_controller/mock_chooser_controller.h"
9 #include "chrome/browser/extensions/extension_browsertest.h" 10 #include "chrome/browser/extensions/extension_browsertest.h"
10 #include "chrome/browser/platform_util.h" 11 #include "chrome/browser/platform_util.h"
11 #include "chrome/browser/ui/browser_window.h" 12 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/views/chooser_content_view.h" 13 #include "chrome/browser/ui/views/chooser_content_view.h"
13 #include "chrome/grit/generated_resources.h" 14 #include "chrome/grit/generated_resources.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/controls/button/label_button.h"
19 #include "ui/views/controls/link.h"
17 #include "ui/views/controls/styled_label.h" 20 #include "ui/views/controls/styled_label.h"
18 #include "ui/views/controls/table/table_view.h" 21 #include "ui/views/controls/table/table_view.h"
22 #include "ui/views/controls/throbber.h"
19 #include "ui/views/widget/widget.h" 23 #include "ui/views/widget/widget.h"
20 #include "ui/views/window/dialog_client_view.h" 24 #include "ui/views/window/dialog_client_view.h"
21 25
22 class ChooserDialogViewTest : public ExtensionBrowserTest { 26 class ChooserDialogViewTest : public ExtensionBrowserTest {
23 public: 27 public:
24 ChooserDialogViewTest() {} 28 ChooserDialogViewTest() {}
25 ~ChooserDialogViewTest() override {} 29 ~ChooserDialogViewTest() override {}
26 30
27 void SetUpOnMainThread() override { 31 void SetUpOnMainThread() override {
28 std::unique_ptr<MockChooserController> mock_chooser_controller( 32 std::unique_ptr<MockChooserController> mock_chooser_controller(
29 new MockChooserController(nullptr)); 33 new MockChooserController(nullptr));
30 mock_chooser_controller_ = mock_chooser_controller.get(); 34 mock_chooser_controller_ = mock_chooser_controller.get();
31 std::unique_ptr<ChooserDialogView> chooser_dialog_view( 35 std::unique_ptr<ChooserDialogView> chooser_dialog_view(
32 new ChooserDialogView(std::move(mock_chooser_controller))); 36 new ChooserDialogView(std::move(mock_chooser_controller)));
33 chooser_dialog_view_ = chooser_dialog_view.get(); 37 chooser_dialog_view_ = chooser_dialog_view.get();
34 table_view_ = chooser_dialog_view_->chooser_content_view_for_test() 38 table_view_ = chooser_dialog_view_->chooser_content_view_for_test()
35 ->table_view_for_test(); 39 ->table_view_for_test();
36 ASSERT_TRUE(table_view_); 40 ASSERT_TRUE(table_view_);
37 table_model_ = table_view_->model(); 41 table_model_ = table_view_->model();
38 ASSERT_TRUE(table_model_); 42 ASSERT_TRUE(table_model_);
39 views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget( 43 views::Widget* modal_dialog = views::DialogDelegate::CreateDialogWidget(
40 chooser_dialog_view.release(), nullptr, 44 chooser_dialog_view.release(), nullptr,
41 platform_util::GetViewForWindow( 45 platform_util::GetViewForWindow(
42 browser()->window()->GetNativeWindow())); 46 browser()->window()->GetNativeWindow()));
43 modal_dialog->Show(); 47 modal_dialog->Show();
48 throbber_ = chooser_dialog_view_->chooser_content_view_for_test()
49 ->throbber_for_test();
50 ASSERT_TRUE(throbber_);
51 discovery_state_ = chooser_dialog_view_->chooser_content_view_for_test()
52 ->discovery_state_for_test();
53 ASSERT_TRUE(discovery_state_);
54 ok_button_ = chooser_dialog_view_->GetDialogClientView()->ok_button();
55 ASSERT_TRUE(ok_button_);
56 cancel_button_ =
57 chooser_dialog_view_->GetDialogClientView()->cancel_button();
58 ASSERT_TRUE(cancel_button_);
44 styled_label_ = chooser_dialog_view_->chooser_content_view_for_test() 59 styled_label_ = chooser_dialog_view_->chooser_content_view_for_test()
45 ->styled_label_for_test(); 60 ->styled_label_for_test();
46 ASSERT_TRUE(styled_label_); 61 ASSERT_TRUE(styled_label_);
47 } 62 }
48 63
49 protected: 64 protected:
50 MockChooserController* mock_chooser_controller_; 65 MockChooserController* mock_chooser_controller_;
51 ChooserDialogView* chooser_dialog_view_; 66 ChooserDialogView* chooser_dialog_view_;
52 views::TableView* table_view_; 67 views::TableView* table_view_;
53 ui::TableModel* table_model_; 68 ui::TableModel* table_model_;
69 views::Throbber* throbber_;
70 views::Link* discovery_state_;
71 views::LabelButton* ok_button_;
72 views::LabelButton* cancel_button_;
54 views::StyledLabel* styled_label_; 73 views::StyledLabel* styled_label_;
55 74
56 private: 75 private:
57 DISALLOW_COPY_AND_ASSIGN(ChooserDialogViewTest); 76 DISALLOW_COPY_AND_ASSIGN(ChooserDialogViewTest);
58 }; 77 };
59 78
60 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, InitialState) { 79 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, InitialState) {
msw 2016/07/22 23:37:05 It seems like some of these could be unit tests; t
juncai 2016/07/25 20:14:10 Removed some redundant tests that are already in u
80 // |table_view_| is visible.
msw 2016/07/22 23:37:05 ditto nit here and elsewhere
juncai 2016/07/25 20:14:10 Removed this line.
81 EXPECT_TRUE(table_view_->visible());
61 // Since "No devices found." needs to be displayed on the |table_view_|, 82 // Since "No devices found." needs to be displayed on the |table_view_|,
62 // the number of rows is 1. 83 // the number of rows is 1.
63 EXPECT_EQ(1, table_view_->RowCount()); 84 EXPECT_EQ(1, table_view_->RowCount());
64 EXPECT_EQ( 85 EXPECT_EQ(
65 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), 86 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
66 table_model_->GetText(0, 0)); 87 table_model_->GetText(0, 0));
67 // |table_view_| should be disabled since there is no option shown. 88 // |table_view_| should be disabled since there is no option shown.
68 EXPECT_FALSE(table_view_->enabled()); 89 EXPECT_FALSE(table_view_->enabled());
69 // No option selected. 90 // No option selected.
70 EXPECT_EQ(0, table_view_->SelectedRowCount()); 91 EXPECT_EQ(0, table_view_->SelectedRowCount());
71 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); 92 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
93 // |throbber_| is not visible.
94 EXPECT_FALSE(throbber_->visible());
95 // OK button is disabled since there is no option.
96 EXPECT_FALSE(ok_button_->enabled());
97 // Cancel button is always enabled.
98 EXPECT_TRUE(cancel_button_->enabled());
99 // Check the text on buttons.
100 EXPECT_EQ(
101 l10n_util::GetStringUTF16(IDS_USB_DEVICE_CHOOSER_CONNECT_BUTTON_TEXT),
102 ok_button_->GetText());
103 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_CANCEL_BUTTON_TEXT),
104 cancel_button_->GetText());
105 // |discovery_state_|'s text is an empty string.
106 EXPECT_EQ(base::string16(), discovery_state_->text());
msw 2016/07/22 23:37:04 nit: EXPECT_TRUE(discovery_state_->text().empty())
juncai 2016/07/25 20:14:10 Removed this line.
107 }
108
109 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, SelectAndDeselectAnOption) {
110 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
111 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
112 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
113 // OK button is disabled since no option is selected.
114 EXPECT_FALSE(ok_button_->enabled());
115 EXPECT_TRUE(cancel_button_->enabled());
116
117 // Select option 0.
118 table_view_->Select(0);
119 // OK button is enabled since an option is selected.
120 EXPECT_TRUE(ok_button_->enabled());
121 EXPECT_TRUE(cancel_button_->enabled());
122
123 // Unselect option 0.
124 table_view_->Select(-1);
125 // OK button is disabled since no option is selected.
126 EXPECT_FALSE(ok_button_->enabled());
127 EXPECT_TRUE(cancel_button_->enabled());
128
129 // Select option 1.
130 table_view_->Select(1);
131 // OK button is enabled since an option is selected.
132 EXPECT_TRUE(ok_button_->enabled());
133 EXPECT_TRUE(cancel_button_->enabled());
134
135 // Unselect option 1.
136 table_view_->Select(-1);
137 // OK button is disabled since no option is selected.
138 EXPECT_FALSE(ok_button_->enabled());
139 EXPECT_TRUE(cancel_button_->enabled());
140 }
141
142 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest,
143 SelectAnOptionAndThenSelectAnotherOption) {
144 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
145 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
146 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
147 EXPECT_FALSE(ok_button_->enabled());
148 EXPECT_TRUE(cancel_button_->enabled());
149
150 // Select option 0.
151 table_view_->Select(0);
152 EXPECT_TRUE(ok_button_->enabled());
153 EXPECT_TRUE(cancel_button_->enabled());
154
155 // Select option 1.
156 table_view_->Select(1);
157 // Both OK and Cancel buttons are still enabled.
158 EXPECT_TRUE(ok_button_->enabled());
159 EXPECT_TRUE(cancel_button_->enabled());
160
161 // Select option 2.
162 table_view_->Select(2);
163 // Both OK and Cancel buttons are still enabled.
164 EXPECT_TRUE(ok_button_->enabled());
165 EXPECT_TRUE(cancel_button_->enabled());
166 }
167
168 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest,
169 SelectAnOptionAndRemoveAnotherOption) {
170 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
171 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
172 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
173 EXPECT_FALSE(ok_button_->enabled());
174 EXPECT_TRUE(cancel_button_->enabled());
175
176 // Select option 1.
177 table_view_->Select(1);
178 EXPECT_TRUE(ok_button_->enabled());
179 EXPECT_TRUE(cancel_button_->enabled());
180
181 // Remove option 0, the list becomes: b c.
182 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("a"));
183 // Both OK and Cancel buttons are still enabled.
184 EXPECT_TRUE(ok_button_->enabled());
185 EXPECT_TRUE(cancel_button_->enabled());
186
187 // Remove option 1.
188 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("c"));
189 // Both OK and Cancel buttons are still enabled.
190 EXPECT_TRUE(ok_button_->enabled());
191 EXPECT_TRUE(cancel_button_->enabled());
192 }
193
194 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest,
195 SelectAnOptionAndRemoveTheSelectedOption) {
196 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
197 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
198 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
199 EXPECT_FALSE(ok_button_->enabled());
200 EXPECT_TRUE(cancel_button_->enabled());
201
202 // Select option 1.
203 table_view_->Select(1);
204 EXPECT_TRUE(ok_button_->enabled());
205 EXPECT_TRUE(cancel_button_->enabled());
206
207 // Remove option 1.
208 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("b"));
209 // OK button is disabled since the selected option is removed.
210 EXPECT_FALSE(ok_button_->enabled());
211 EXPECT_TRUE(cancel_button_->enabled());
212 }
213
214 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest,
215 AddAnOptionAndSelectItAndRemoveTheSelectedOption) {
msw 2016/07/22 23:37:05 How is this usefully different from SelectAnOption
juncai 2016/07/25 20:14:10 In this case, since there is only one option added
msw 2016/07/26 00:18:56 I suppose it's okay.
216 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
217 EXPECT_FALSE(ok_button_->enabled());
218 EXPECT_TRUE(cancel_button_->enabled());
219
220 // Select option 0.
221 table_view_->Select(0);
222 EXPECT_TRUE(ok_button_->enabled());
223 EXPECT_TRUE(cancel_button_->enabled());
224
225 // Remove option 0.
226 mock_chooser_controller_->OptionRemoved(base::ASCIIToUTF16("a"));
227 // There is no option shown now and the OK button is disabled.
228 EXPECT_FALSE(ok_button_->enabled());
229 EXPECT_TRUE(cancel_button_->enabled());
72 } 230 }
73 231
74 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Accept) { 232 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Accept) {
75 EXPECT_CALL(*mock_chooser_controller_, Select(testing::_)).Times(1); 233 EXPECT_CALL(*mock_chooser_controller_, Select(testing::_)).Times(1);
76 chooser_dialog_view_->Accept(); 234 chooser_dialog_view_->Accept();
77 } 235 }
78 236
79 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Cancel) { 237 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Cancel) {
80 EXPECT_CALL(*mock_chooser_controller_, Cancel()).Times(1); 238 EXPECT_CALL(*mock_chooser_controller_, Cancel()).Times(1);
81 chooser_dialog_view_->Cancel(); 239 chooser_dialog_view_->Cancel();
82 } 240 }
83 241
84 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Close) { 242 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Close) {
85 EXPECT_CALL(*mock_chooser_controller_, Close()).Times(1); 243 EXPECT_CALL(*mock_chooser_controller_, Close()).Times(1);
86 chooser_dialog_view_->Close(); 244 chooser_dialog_view_->Close();
87 } 245 }
88 246
89 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, ClickStyledLabelLink) { 247 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, ClickStyledLabelLink) {
90 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1); 248 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1);
91 styled_label_->LinkClicked(nullptr, 0); 249 styled_label_->LinkClicked(nullptr, 0);
92 } 250 }
251
252 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, AdapterOnAndOffAndOn) {
253 mock_chooser_controller_->OnAdapterPresenceChanged(
254 MockChooserController::AdapterPresence::ON);
255 EXPECT_FALSE(ok_button_->enabled());
256 EXPECT_TRUE(cancel_button_->enabled());
257
258 // Add options.
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 EXPECT_FALSE(ok_button_->enabled());
263 EXPECT_TRUE(cancel_button_->enabled());
264
265 // Select option 1.
266 table_view_->Select(1);
267 EXPECT_TRUE(ok_button_->enabled());
268 EXPECT_TRUE(cancel_button_->enabled());
269
270 mock_chooser_controller_->OnAdapterPresenceChanged(
271 MockChooserController::AdapterPresence::OFF);
272 // Since the adapter is turned off, the previously selected option
msw 2016/07/22 23:37:04 nit: check the option count here and after the ada
juncai 2016/07/25 20:14:10 Done.
273 // becomes invalid, the OK button is disabled.
274 EXPECT_FALSE(ok_button_->enabled());
275 EXPECT_TRUE(cancel_button_->enabled());
276
277 mock_chooser_controller_->OnAdapterPresenceChanged(
278 MockChooserController::AdapterPresence::ON);
279 EXPECT_FALSE(ok_button_->enabled());
280 EXPECT_TRUE(cancel_button_->enabled());
281 }
282
283 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, DiscoveringAndIdle) {
284 // Add options.
285 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
286 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
287 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
288 // |table_view_| is visible.
289 EXPECT_TRUE(table_view_->visible());
290 EXPECT_EQ(3, table_view_->RowCount());
291 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0));
msw 2016/07/22 23:37:04 nit: Try to avoid adding tangential checks here an
juncai 2016/07/25 20:14:10 Done.
292 EXPECT_EQ(base::ASCIIToUTF16("b"), table_model_->GetText(1, 0));
293 EXPECT_EQ(base::ASCIIToUTF16("c"), table_model_->GetText(2, 0));
294 EXPECT_TRUE(table_view_->enabled());
295 // No option selected.
296 EXPECT_EQ(0, table_view_->SelectedRowCount());
297 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
298 // Select option 1.
299 table_view_->Select(1);
300 EXPECT_EQ(1, table_view_->SelectedRowCount());
301 EXPECT_EQ(1, table_view_->FirstSelectedRow());
302 // |throbber_| is not visible.
303 EXPECT_FALSE(throbber_->visible());
304 // Both OK and Cancel buttons are enabled.
305 EXPECT_TRUE(ok_button_->enabled());
306 EXPECT_TRUE(cancel_button_->enabled());
307 // |discovery_state_|'s text is an empty string.
308 EXPECT_EQ(base::string16(), discovery_state_->text());
309
310 mock_chooser_controller_->OnDiscoveryStateChanged(
311 MockChooserController::DiscoveryState::DISCOVERING);
312 // |table_view_| is not visible.
313 EXPECT_FALSE(table_view_->visible());
314 // |throbber_| is visible.
315 EXPECT_TRUE(throbber_->visible());
316 // |discovery_state_| is disabled and show a label instead of a link.
msw 2016/07/22 23:37:04 nit: shows
juncai 2016/07/25 20:14:10 Done.
317 EXPECT_FALSE(discovery_state_->enabled());
318 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
319 discovery_state_->text());
320 // OK button is disabled since the chooser is refreshing options.
321 EXPECT_FALSE(ok_button_->enabled());
322 EXPECT_TRUE(cancel_button_->enabled());
323
324 mock_chooser_controller_->OnDiscoveryStateChanged(
325 MockChooserController::DiscoveryState::IDLE);
326 // |table_view_| is visible.
327 EXPECT_TRUE(table_view_->visible());
328 // Since "No devices found." needs to be displayed on the |table_view_|,
329 // the number of rows is 1.
330 EXPECT_EQ(1, table_view_->RowCount());
331 EXPECT_EQ(
332 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
333 table_model_->GetText(0, 0));
334 // |table_view_| should be disabled since there is no option shown.
335 EXPECT_FALSE(table_view_->enabled());
336 // No option selected.
337 EXPECT_EQ(0, table_view_->SelectedRowCount());
338 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
339 // |throbber_| is not visible.
340 EXPECT_FALSE(throbber_->visible());
341 // |discovery_state_| is enabled and show a link.
msw 2016/07/22 23:37:05 nit: shows
juncai 2016/07/25 20:14:10 Done.
342 EXPECT_TRUE(discovery_state_->enabled());
343 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
344 discovery_state_->text());
345 // OK button is disabled since the chooser refreshed options.
346 EXPECT_FALSE(ok_button_->enabled());
347 EXPECT_TRUE(cancel_button_->enabled());
348 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698