OLD | NEW |
---|---|
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" | |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 class MockTableViewObserver : public views::TableViewObserver { | 24 class MockTableViewObserver : public views::TableViewObserver { |
23 public: | 25 public: |
24 // views::TableViewObserver: | 26 // views::TableViewObserver: |
25 MOCK_METHOD0(OnSelectionChanged, void()); | 27 MOCK_METHOD0(OnSelectionChanged, void()); |
26 }; | 28 }; |
27 | 29 |
28 } // namespace | 30 } // namespace |
29 | 31 |
30 class ChooserContentViewTest : public testing::Test { | 32 class ChooserContentViewTest : public testing::Test { |
31 public: | 33 public: |
32 ChooserContentViewTest() {} | 34 ChooserContentViewTest() {} |
33 | 35 |
34 // testing::Test: | 36 // testing::Test: |
35 void SetUp() override { | 37 void SetUp() override { |
36 std::unique_ptr<MockChooserController> mock_chooser_controller( | 38 std::unique_ptr<MockChooserController> mock_chooser_controller( |
37 new MockChooserController(nullptr)); | 39 new MockChooserController(nullptr)); |
38 mock_chooser_controller_ = mock_chooser_controller.get(); | 40 mock_chooser_controller_ = mock_chooser_controller.get(); |
39 mock_table_view_observer_.reset(new MockTableViewObserver()); | 41 mock_table_view_observer_.reset(new MockTableViewObserver()); |
40 chooser_content_view_.reset(new ChooserContentView( | 42 chooser_content_view_.reset(new ChooserContentView( |
41 mock_table_view_observer_.get(), std::move(mock_chooser_controller))); | 43 mock_table_view_observer_.get(), std::move(mock_chooser_controller))); |
42 table_view_ = chooser_content_view_->table_view_for_test(); | 44 table_view_ = chooser_content_view_->table_view_for_test(); |
43 ASSERT_TRUE(table_view_); | 45 ASSERT_TRUE(table_view_); |
44 table_model_ = table_view_->model(); | 46 table_model_ = table_view_->model(); |
45 ASSERT_TRUE(table_model_); | 47 ASSERT_TRUE(table_model_); |
48 throbber_ = chooser_content_view_->throbber_for_test(); | |
49 ASSERT_TRUE(throbber_); | |
50 discovery_state_.reset(chooser_content_view_->CreateExtraView()); | |
51 ASSERT_TRUE(discovery_state_); | |
46 styled_label_.reset(chooser_content_view_->CreateFootnoteView()); | 52 styled_label_.reset(chooser_content_view_->CreateFootnoteView()); |
47 ASSERT_TRUE(styled_label_); | 53 ASSERT_TRUE(styled_label_); |
48 } | 54 } |
49 | 55 |
50 protected: | 56 protected: |
57 // |discovery_state_| needs to be valid when |chooser_content_view_| is | |
58 // released, since ChooserContentView's destructor needs to access it. | |
59 // So it is declared before |chooser_content_view_|. | |
60 std::unique_ptr<views::Link> discovery_state_; | |
51 std::unique_ptr<MockTableViewObserver> mock_table_view_observer_; | 61 std::unique_ptr<MockTableViewObserver> mock_table_view_observer_; |
52 std::unique_ptr<ChooserContentView> chooser_content_view_; | 62 std::unique_ptr<ChooserContentView> chooser_content_view_; |
53 MockChooserController* mock_chooser_controller_; | 63 MockChooserController* mock_chooser_controller_; |
54 views::TableView* table_view_; | 64 views::TableView* table_view_; |
55 ui::TableModel* table_model_; | 65 ui::TableModel* table_model_; |
66 views::Throbber* throbber_; | |
56 std::unique_ptr<views::StyledLabel> styled_label_; | 67 std::unique_ptr<views::StyledLabel> styled_label_; |
57 | 68 |
58 private: | 69 private: |
59 DISALLOW_COPY_AND_ASSIGN(ChooserContentViewTest); | 70 DISALLOW_COPY_AND_ASSIGN(ChooserContentViewTest); |
60 }; | 71 }; |
61 | 72 |
62 TEST_F(ChooserContentViewTest, InitialState) { | 73 TEST_F(ChooserContentViewTest, InitialState) { |
63 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); | 74 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); |
64 | 75 |
76 // |table_view_| is visible. | |
msw
2016/07/22 23:37:04
nit: comments that just repeat code aren't worthwh
juncai
2016/07/25 20:14:10
Done.
| |
77 EXPECT_TRUE(table_view_->visible()); | |
65 // Since "No devices found." needs to be displayed on the |table_view_|, | 78 // Since "No devices found." needs to be displayed on the |table_view_|, |
66 // the number of rows is 1. | 79 // the number of rows is 1. |
67 EXPECT_EQ(1, table_view_->RowCount()); | 80 EXPECT_EQ(1, table_view_->RowCount()); |
68 EXPECT_EQ( | 81 EXPECT_EQ( |
69 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), | 82 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), |
70 table_model_->GetText(0, 0)); | 83 table_model_->GetText(0, 0)); |
71 // |table_view_| should be disabled since there is no option shown. | 84 // |table_view_| should be disabled since there is no option shown. |
72 EXPECT_FALSE(table_view_->enabled()); | 85 EXPECT_FALSE(table_view_->enabled()); |
73 // No option selected. | 86 // No option selected. |
74 EXPECT_EQ(0, table_view_->SelectedRowCount()); | 87 EXPECT_EQ(0, table_view_->SelectedRowCount()); |
75 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); | 88 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); |
89 // |throbber_| is not visible. | |
90 EXPECT_FALSE(throbber_->visible()); | |
91 // |discovery_state_|'s text is an empty string. | |
92 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
Done.
| |
76 } | 93 } |
77 | 94 |
78 TEST_F(ChooserContentViewTest, AddOption) { | 95 TEST_F(ChooserContentViewTest, AddOption) { |
79 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); | 96 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(0); |
80 | 97 |
81 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a")); | 98 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a")); |
82 EXPECT_EQ(1, table_view_->RowCount()); | 99 EXPECT_EQ(1, table_view_->RowCount()); |
83 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0)); | 100 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0)); |
84 // |table_view_| should be enabled since there is an option. | 101 // |table_view_| should be enabled since there is an option. |
85 EXPECT_TRUE(table_view_->enabled()); | 102 EXPECT_TRUE(table_view_->enabled()); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 EXPECT_EQ( | 303 EXPECT_EQ( |
287 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), | 304 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), |
288 table_model_->GetText(0, 0)); | 305 table_model_->GetText(0, 0)); |
289 // |table_view_| should be disabled since all options are removed. | 306 // |table_view_| should be disabled since all options are removed. |
290 EXPECT_FALSE(table_view_->enabled()); | 307 EXPECT_FALSE(table_view_->enabled()); |
291 // No option selected. | 308 // No option selected. |
292 EXPECT_EQ(0, table_view_->SelectedRowCount()); | 309 EXPECT_EQ(0, table_view_->SelectedRowCount()); |
293 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); | 310 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); |
294 } | 311 } |
295 | 312 |
313 TEST_F(ChooserContentViewTest, AdapterOnAndOff) { | |
314 EXPECT_CALL(*mock_table_view_observer_, OnSelectionChanged()).Times(2); | |
315 | |
316 mock_chooser_controller_->OnAdapterPresenceChanged( | |
317 MockChooserController::AdapterPresence::ON); | |
318 // |table_view_| is visible. | |
319 EXPECT_TRUE(table_view_->visible()); | |
320 // Since "No devices found." needs to be displayed on the |table_view_|, | |
321 // the number of rows is 1. | |
322 EXPECT_EQ(1, table_view_->RowCount()); | |
323 EXPECT_EQ( | |
324 l10n_util::GetStringUTF16(IDS_DEVICE_CHOOSER_NO_DEVICES_FOUND_PROMPT), | |
325 table_model_->GetText(0, 0)); | |
326 // |table_view_| should be disabled since there is no option shown. | |
327 EXPECT_FALSE(table_view_->enabled()); | |
328 // No option selected. | |
329 EXPECT_EQ(0, table_view_->SelectedRowCount()); | |
330 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); | |
331 // |throbber_| is not visible. | |
332 EXPECT_FALSE(throbber_->visible()); | |
333 // |discovery_state_| is enabled and show a link. | |
334 EXPECT_TRUE(discovery_state_->enabled()); | |
335 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN), | |
336 discovery_state_->text()); | |
337 | |
338 // Add options. | |
339 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("a")); | |
340 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("b")); | |
341 mock_chooser_controller_->OptionAdded(base::ASCIIToUTF16("c")); | |
342 EXPECT_EQ(3, table_view_->RowCount()); | |
343 EXPECT_EQ(base::ASCIIToUTF16("a"), table_model_->GetText(0, 0)); | |
344 EXPECT_EQ(base::ASCIIToUTF16("b"), table_model_->GetText(1, 0)); | |
345 EXPECT_EQ(base::ASCIIToUTF16("c"), table_model_->GetText(2, 0)); | |
346 EXPECT_TRUE(table_view_->enabled()); | |
347 // No option selected. | |
348 EXPECT_EQ(0, table_view_->SelectedRowCount()); | |
349 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); | |
350 // Select option 1. | |
351 table_view_->Select(1); | |
352 EXPECT_EQ(1, table_view_->SelectedRowCount()); | |
353 EXPECT_EQ(1, table_view_->FirstSelectedRow()); | |
354 | |
355 mock_chooser_controller_->OnAdapterPresenceChanged( | |
356 MockChooserController::AdapterPresence::OFF); | |
357 // |table_view_| is visible. | |
358 EXPECT_TRUE(table_view_->visible()); | |
359 // Since "Adapter turned off." needs to be displayed on the |table_view_|, | |
360 // the number of rows is 1. | |
361 EXPECT_EQ(1, table_view_->RowCount()); | |
362 EXPECT_EQ(base::ASCIIToUTF16("Adapter turned off."), | |
363 table_model_->GetText(0, 0)); | |
364 // |table_view_| should be disabled since there is no option shown. | |
365 EXPECT_FALSE(table_view_->enabled()); | |
366 // No option selected. | |
367 EXPECT_EQ(0, table_view_->SelectedRowCount()); | |
368 EXPECT_EQ(-1, table_view_->FirstSelectedRow()); | |
369 // |throbber_| is not visible. | |
msw
2016/07/22 23:37:04
Can you test the throbber actually being visible i
juncai
2016/07/25 20:14:10
I tried testing the throbber in a unit test and it
msw
2016/07/26 00:18:56
Can you try constructing a base::TestSimpleTaskRun
juncai
2016/07/26 23:30:01
Thanks! Added base::TestSimpleTaskRunner and base:
| |
370 EXPECT_FALSE(throbber_->visible()); | |
371 // |discovery_state_| is disabled and show a label instead of a link. | |
msw
2016/07/22 23:37:04
nit: "and shows an empty string" and nix comment b
juncai
2016/07/25 20:14:10
Done.
| |
372 EXPECT_FALSE(discovery_state_->enabled()); | |
373 // |discovery_state_|'s text is an empty string. | |
374 EXPECT_EQ(base::string16(), discovery_state_->text()); | |
375 } | |
376 | |
377 TEST_F(ChooserContentViewTest, ClickRescanLink) { | |
378 EXPECT_CALL(*mock_chooser_controller_, RefreshOptions()).Times(1); | |
379 chooser_content_view_->LinkClicked(nullptr, 0); | |
380 } | |
381 | |
296 TEST_F(ChooserContentViewTest, ClickStyledLabelLink) { | 382 TEST_F(ChooserContentViewTest, ClickStyledLabelLink) { |
297 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1); | 383 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1); |
298 styled_label_->LinkClicked(nullptr, 0); | 384 styled_label_->LinkClicked(nullptr, 0); |
299 } | 385 } |
OLD | NEW |