Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/extensions/chooser_dialog_view.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 10 #include "chrome/grit/generated_resources.h" | |
| 11 #include "components/chooser_controller/chooser_controller.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/gfx/range/range.h" | |
| 16 #include "ui/views/controls/table/table_view.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class MockChooserController : public ChooserController { | |
|
msw
2016/06/08 01:16:32
Can you share this between the dialog and bubble?
juncai
2016/06/09 01:59:53
Done.
| |
| 21 public: | |
| 22 MockChooserController() : ChooserController(nullptr) {} | |
| 23 ~MockChooserController() override {} | |
| 24 | |
| 25 // ChooserController: | |
| 26 size_t NumOptions() const override { return option_names_.size(); } | |
| 27 | |
| 28 // ChooserController: | |
| 29 const base::string16& GetOption(size_t index) const override { | |
| 30 return option_names_[index]; | |
| 31 } | |
| 32 | |
| 33 // ChooserController: | |
| 34 MOCK_METHOD1(Select, void(size_t index)); | |
| 35 | |
| 36 // ChooserController: | |
| 37 MOCK_METHOD0(Cancel, void()); | |
| 38 | |
| 39 // ChooserController: | |
| 40 MOCK_METHOD0(Close, void()); | |
| 41 | |
| 42 // ChooserController: | |
| 43 MOCK_CONST_METHOD0(OpenHelpCenterUrl, void()); | |
| 44 | |
| 45 void OptionAdded(const base::string16 option_name) { | |
| 46 option_names_.push_back(option_name); | |
| 47 if (observer()) | |
| 48 observer()->OnOptionAdded(option_names_.size() - 1); | |
| 49 } | |
| 50 | |
| 51 void OptionRemoved(const base::string16 option_name) { | |
| 52 for (auto it = option_names_.begin(); it != option_names_.end(); ++it) { | |
| 53 if (*it == option_name) { | |
| 54 size_t index = it - option_names_.begin(); | |
| 55 option_names_.erase(it); | |
| 56 if (observer()) | |
| 57 observer()->OnOptionRemoved(index); | |
| 58 return; | |
| 59 } | |
| 60 } | |
| 61 } | |
| 62 | |
| 63 private: | |
| 64 std::vector<base::string16> option_names_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(MockChooserController); | |
| 67 }; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 class ChooserDialogViewTest : public ExtensionBrowserTest { | |
| 72 public: | |
| 73 ChooserDialogViewTest() {} | |
| 74 ~ChooserDialogViewTest() override {} | |
| 75 | |
| 76 void SetUpOnMainThread() override { | |
| 77 content::WebContents* web_contents = | |
| 78 browser()->tab_strip_model()->GetWebContentsAt(0); | |
| 79 mock_chooser_controller_.reset(new MockChooserController()); | |
| 80 ASSERT_TRUE(mock_chooser_controller_); | |
|
msw
2016/06/08 01:16:32
nit: this seems unnecessary.
juncai
2016/06/09 01:59:53
Done.
| |
| 81 chooser_dialog_view_.reset( | |
| 82 new ChooserDialogView(web_contents, mock_chooser_controller_.get())); | |
| 83 ASSERT_TRUE(chooser_dialog_view_); | |
|
msw
2016/06/08 01:16:32
nit: this seems unnecessary.
juncai
2016/06/09 01:59:53
Done.
| |
| 84 table_view_ = chooser_dialog_view_->table_view_; | |
| 85 ASSERT_TRUE(table_view_); | |
| 86 table_model_ = chooser_dialog_view_->table_model_; | |
| 87 ASSERT_TRUE(table_model_); | |
| 88 } | |
| 89 | |
| 90 protected: | |
| 91 std::unique_ptr<MockChooserController> mock_chooser_controller_; | |
| 92 std::unique_ptr<ChooserDialogView> chooser_dialog_view_; | |
| 93 views::TableView* table_view_; | |
| 94 ui::TableModel* table_model_; | |
| 95 | |
| 96 private: | |
| 97 DISALLOW_COPY_AND_ASSIGN(ChooserDialogViewTest); | |
| 98 }; | |
| 99 | |
| 100 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, InitialState) { | |
| 101 // Since "No devices found." needs to be displayed on the |table_view_|, | |
| 102 // the number of rows is 1. | |
| 103 EXPECT_EQ(table_view_->RowCount(), 1); | |
| 104 EXPECT_EQ( | |
| 105 table_model_->GetText(0, 0), | |
| 106 l10n_util::GetStringUTF16(IDS_CHOOSER_BUBBLE_NO_DEVICES_FOUND_PROMPT)); | |
| 107 // |table_view_| should be disabled since there is no option shown. | |
| 108 EXPECT_FALSE(table_view_->enabled()); | |
| 109 // No option selected. | |
| 110 EXPECT_EQ(table_view_->SelectedRowCount(), 0); | |
| 111 EXPECT_EQ(table_view_->FirstSelectedRow(), -1); | |
| 112 } | |
| 113 | |
| 114 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Accept) { | |
| 115 EXPECT_CALL(*mock_chooser_controller_, Select(testing::_)).Times(1); | |
| 116 chooser_dialog_view_->Accept(); | |
| 117 } | |
| 118 | |
| 119 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Cancel) { | |
| 120 EXPECT_CALL(*mock_chooser_controller_, Cancel()).Times(1); | |
| 121 chooser_dialog_view_->Cancel(); | |
| 122 } | |
| 123 | |
| 124 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, Close) { | |
| 125 EXPECT_CALL(*mock_chooser_controller_, Close()).Times(1); | |
| 126 chooser_dialog_view_->Close(); | |
| 127 } | |
| 128 | |
| 129 IN_PROC_BROWSER_TEST_F(ChooserDialogViewTest, ClickStyledLabelLink) { | |
| 130 EXPECT_CALL(*mock_chooser_controller_, OpenHelpCenterUrl()).Times(1); | |
| 131 chooser_dialog_view_->StyledLabelLinkClicked(nullptr, gfx::Range(), 0); | |
| 132 } | |
| OLD | NEW |