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

Side by Side Diff: chrome/browser/ui/views/chooser_content_view_unittest.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: address comments Created 4 years, 4 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/chooser_content_view.h" 5 #include "chrome/browser/ui/views/chooser_content_view.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chooser_controller/mock_chooser_controller.h" 11 #include "chrome/browser/chooser_controller/mock_chooser_controller.h"
12 #include "chrome/grit/generated_resources.h" 12 #include "chrome/grit/generated_resources.h"
13 #include "testing/gmock/include/gmock/gmock.h" 13 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/views/controls/link.h"
16 #include "ui/views/controls/styled_label.h" 17 #include "ui/views/controls/styled_label.h"
17 #include "ui/views/controls/table/table_view.h" 18 #include "ui/views/controls/table/table_view.h"
18 #include "ui/views/controls/table/table_view_observer.h" 19 #include "ui/views/controls/table/table_view_observer.h"
20 #include "ui/views/controls/throbber.h"
21 #include "ui/views/test/views_test_base.h"
19 22
20 namespace { 23 namespace {
21 24
22 class MockTableViewObserver : public views::TableViewObserver { 25 class MockTableViewObserver : public views::TableViewObserver {
23 public: 26 public:
24 // views::TableViewObserver: 27 // views::TableViewObserver:
25 MOCK_METHOD0(OnSelectionChanged, void()); 28 MOCK_METHOD0(OnSelectionChanged, void());
26 }; 29 };
27 30
28 } // namespace 31 } // namespace
29 32
30 class ChooserContentViewTest : public testing::Test { 33 class ChooserContentViewTest : public views::ViewsTestBase {
31 public: 34 public:
32 ChooserContentViewTest() {} 35 ChooserContentViewTest() {}
33 36
34 // testing::Test: 37 // views::ViewsTestBase:
35 void SetUp() override { 38 void SetUp() override {
39 views::ViewsTestBase::SetUp();
36 std::unique_ptr<MockChooserController> mock_chooser_controller( 40 std::unique_ptr<MockChooserController> mock_chooser_controller(
37 new MockChooserController(nullptr)); 41 new MockChooserController(nullptr));
38 mock_chooser_controller_ = mock_chooser_controller.get(); 42 mock_chooser_controller_ = mock_chooser_controller.get();
39 mock_table_view_observer_.reset(new MockTableViewObserver()); 43 mock_table_view_observer_.reset(new MockTableViewObserver());
40 chooser_content_view_.reset(new ChooserContentView( 44 chooser_content_view_.reset(new ChooserContentView(
41 mock_table_view_observer_.get(), std::move(mock_chooser_controller))); 45 mock_table_view_observer_.get(), std::move(mock_chooser_controller)));
42 table_view_ = chooser_content_view_->table_view_for_test(); 46 table_view_ = chooser_content_view_->table_view_for_test();
43 ASSERT_TRUE(table_view_); 47 ASSERT_TRUE(table_view_);
44 table_model_ = table_view_->model(); 48 table_model_ = table_view_->model();
45 ASSERT_TRUE(table_model_); 49 ASSERT_TRUE(table_model_);
50 throbber_ = chooser_content_view_->throbber_for_test();
51 ASSERT_TRUE(throbber_);
52 discovery_state_.reset(chooser_content_view_->CreateExtraView());
53 ASSERT_TRUE(discovery_state_);
46 styled_label_.reset(chooser_content_view_->CreateFootnoteView()); 54 styled_label_.reset(chooser_content_view_->CreateFootnoteView());
47 ASSERT_TRUE(styled_label_); 55 ASSERT_TRUE(styled_label_);
48 } 56 }
49 57
50 protected: 58 protected:
59 // |discovery_state_| needs to be valid when |chooser_content_view_| is
60 // released, since ChooserContentView's destructor needs to access it.
61 // So it is declared before |chooser_content_view_|.
62 std::unique_ptr<views::Link> discovery_state_;
51 std::unique_ptr<MockTableViewObserver> mock_table_view_observer_; 63 std::unique_ptr<MockTableViewObserver> mock_table_view_observer_;
52 std::unique_ptr<ChooserContentView> chooser_content_view_; 64 std::unique_ptr<ChooserContentView> chooser_content_view_;
53 MockChooserController* mock_chooser_controller_; 65 MockChooserController* mock_chooser_controller_;
54 views::TableView* table_view_; 66 views::TableView* table_view_;
55 ui::TableModel* table_model_; 67 ui::TableModel* table_model_;
68 views::Throbber* throbber_;
56 std::unique_ptr<views::StyledLabel> styled_label_; 69 std::unique_ptr<views::StyledLabel> styled_label_;
57 70
58 private: 71 private:
59 DISALLOW_COPY_AND_ASSIGN(ChooserContentViewTest); 72 DISALLOW_COPY_AND_ASSIGN(ChooserContentViewTest);
60 }; 73 };
61 74
62 TEST_F(ChooserContentViewTest, InitialState) { 75 TEST_F(ChooserContentViewTest, InitialState) {
63 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); 76 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0);
64 77
78 EXPECT_TRUE(table_view_->visible());
65 // Since "No devices found." needs to be displayed on the |table_view_|, 79 // Since "No devices found." needs to be displayed on the |table_view_|,
66 // the number of rows is 1. 80 // the number of rows is 1.
67 EXPECT_EQ(1, table_view_->RowCount()); 81 EXPECT_EQ(1, table_view_->RowCount());
68 EXPECT_EQ( 82 EXPECT_EQ(
69 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), 83 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
70 table_model_->GetText(0, 0)); 84 table_model_->GetText(0, 0));
71 // |table_view_| should be disabled since there is no option shown. 85 // |table_view_| should be disabled since there is no option shown.
72 EXPECT_FALSE(table_view_->enabled()); 86 EXPECT_FALSE(table_view_->enabled());
73 // No option selected. 87 // No option selected.
74 EXPECT_EQ(0, table_view_->SelectedRowCount()); 88 EXPECT_EQ(0, table_view_->SelectedRowCount());
75 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); 89 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
90 EXPECT_FALSE(throbber_->visible());
91 EXPECT_TRUE(discovery_state_->text().empty());
76 } 92 }
77 93
78 TEST_F(ChooserContentViewTest, AddOption) { 94 TEST_F(ChooserContentViewTest, AddOption) {
79 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); 95 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0);
80 96
81 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a")); 97 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
82 EXPECT_EQ(1, table_view_->RowCount()); 98 EXPECT_EQ(1, table_view_->RowCount());
83 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0)); 99 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0));
84 // |table_view_| should be enabled since there is an option. 100 // |table_view_| should be enabled since there is an option.
85 EXPECT_TRUE(table_view_->enabled()); 101 EXPECT_TRUE(table_view_->enabled());
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 EXPECT_EQ( 302 EXPECT_EQ(
287 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), 303 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
288 table_model_->GetText(0, 0)); 304 table_model_->GetText(0, 0));
289 // |table_view_| should be disabled since all options are removed. 305 // |table_view_| should be disabled since all options are removed.
290 EXPECT_FALSE(table_view_->enabled()); 306 EXPECT_FALSE(table_view_->enabled());
291 // No option selected. 307 // No option selected.
292 EXPECT_EQ(0, table_view_->SelectedRowCount()); 308 EXPECT_EQ(0, table_view_->SelectedRowCount());
293 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); 309 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
294 } 310 }
295 311
312 TEST_F(ChooserContentViewTest, AdapterOnAndOffAndOn) {
313 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(2);
314
315 mock_chooser_controller_->OnAdapterPresenceChanged(
316 content::BluetoothChooser::AdapterPresence::POWERED_ON);
317 EXPECT_TRUE(table_view_->visible());
318 // Since "No devices found." needs to be displayed on the |table_view_|,
319 // the number of rows is 1.
320 EXPECT_EQ(1, table_view_->RowCount());
321 EXPECT_EQ(
322 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
323 table_model_->GetText(0, 0));
324 // |table_view_| should be disabled since there is no option shown.
325 EXPECT_FALSE(table_view_->enabled());
326 // No option selected.
327 EXPECT_EQ(0, table_view_->SelectedRowCount());
328 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
329 EXPECT_FALSE(throbber_->visible());
330 EXPECT_TRUE(discovery_state_->enabled());
331 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
332 discovery_state_->text());
333
334 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
335 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
336 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
337 table_view_->Select(1);
338
339 mock_chooser_controller_->OnAdapterPresenceChanged(
340 content::BluetoothChooser::AdapterPresence::POWERED_OFF);
341 EXPECT_EQ(0u, mock_chooser_controller_->NumOptions());
342 EXPECT_TRUE(table_view_->visible());
343 // Since "Bluetooth turned off." needs to be displayed on the |table_view_|,
344 // the number of rows is 1.
345 EXPECT_EQ(1, table_view_->RowCount());
346 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_ADAPTER_OFF),
347 table_model_->GetText(0, 0));
348 // |table_view_| should be disabled since there is no option shown.
349 EXPECT_FALSE(table_view_->enabled());
350 // No option selected.
351 EXPECT_EQ(0, table_view_->SelectedRowCount());
352 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
353 EXPECT_FALSE(throbber_->visible());
354 EXPECT_FALSE(discovery_state_->enabled());
355 EXPECT_TRUE(discovery_state_->text().empty());
356
357 mock_chooser_controller_->OnAdapterPresenceChanged(
358 content::BluetoothChooser::AdapterPresence::POWERED_ON);
359 EXPECT_EQ(0u, mock_chooser_controller_->NumOptions());
360 EXPECT_TRUE(table_view_->visible());
361 EXPECT_EQ(1, table_view_->RowCount());
362 EXPECT_EQ(
363 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
364 table_model_->GetText(0, 0));
365 EXPECT_FALSE(table_view_->enabled());
366 EXPECT_EQ(0, table_view_->SelectedRowCount());
367 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
368 EXPECT_FALSE(throbber_->visible());
369 EXPECT_TRUE(discovery_state_->enabled());
370 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
371 discovery_state_->text());
372 }
373
374 TEST_F(ChooserContentViewTest, DiscoveringAndIdle) {
375 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(2);
376
377 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a"));
378 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b"));
379 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c"));
380 table_view_->Select(1);
381
382 mock_chooser_controller_->OnDiscoveryStateChanged(
383 content::BluetoothChooser::DiscoveryState::DISCOVERING);
384 EXPECT_FALSE(table_view_->visible());
385 EXPECT_TRUE(throbber_->visible());
386 // |discovery_state_| is disabled and shows a label instead of a link.
387 EXPECT_FALSE(discovery_state_->enabled());
388 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING),
389 discovery_state_->text());
390
391 mock_chooser_controller_->OnDiscoveryStateChanged(
392 content::BluetoothChooser::DiscoveryState::IDLE);
393 EXPECT_TRUE(table_view_->visible());
394 // Since "No devices found." needs to be displayed on the |table_view_|,
395 // the number of rows is 1.
396 EXPECT_EQ(1, table_view_->RowCount());
397 EXPECT_EQ(
398 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT),
399 table_model_->GetText(0, 0));
400 // |table_view_| should be disabled since there is no option shown.
401 EXPECT_FALSE(table_view_->enabled());
402 // No option selected.
403 EXPECT_EQ(0, table_view_->SelectedRowCount());
404 EXPECT_EQ(-1, table_view_->FirstSelectedRow());
405 EXPECT_FALSE(throbber_->visible());
406 // |discovery_state_| is enabled and shows a link.
407 EXPECT_TRUE(discovery_state_->enabled());
408 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN),
409 discovery_state_->text());
410 }
411
412 TEST_F(ChooserContentViewTest, ClickRescanLink) {
413 EXPECT_CALL(*mock_chooser_controller_, RefreshOptions()).Times(1);
414 chooser_content_view_->LinkClicked(nullptr, 0);
415 }
416
296 TEST_F(ChooserContentViewTest, ClickStyledLabelLink) { 417 TEST_F(ChooserContentViewTest, ClickStyledLabelLink) {
297 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1); 418 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1);
298 styled_label_->LinkClicked(nullptr, 0); 419 styled_label_->LinkClicked(nullptr, 0);
299 } 420 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/chooser_content_view.cc ('k') | chrome/browser/ui/views/extensions/chooser_dialog_view.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698