OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/webshare/webshare_target_picker_view.h" |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/optional.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/ui/views/chrome_constrained_window_views_client.h" |
| 17 #include "chrome/test/base/browser_with_test_window_test.h" |
| 18 #include "components/constrained_window/constrained_window_views.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/views/controls/table/table_view.h" |
| 21 #include "ui/views/test/views_test_base.h" |
| 22 #include "ui/views/widget/widget.h" |
| 23 #include "ui/views/window/dialog_client_view.h" |
| 24 #include "ui/views/window/dialog_delegate.h" |
| 25 #include "url/gurl.h" |
| 26 |
| 27 namespace { |
| 28 |
| 29 class WebShareTargetPickerViewTest : public views::ViewsTestBase { |
| 30 public: |
| 31 WebShareTargetPickerViewTest() {} |
| 32 |
| 33 void SetUp() override { |
| 34 ViewsTestBase::SetUp(); |
| 35 |
| 36 SetConstrainedWindowViewsClient(CreateChromeConstrainedWindowViewsClient()); |
| 37 |
| 38 // Create the parent widget. |
| 39 views::Widget::InitParams params = |
| 40 CreateParams(views::Widget::InitParams::TYPE_WINDOW); |
| 41 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 42 |
| 43 parent_widget_.reset(new views::Widget()); |
| 44 parent_widget_->Init(params); |
| 45 parent_widget_->Show(); |
| 46 } |
| 47 |
| 48 void TearDown() override { |
| 49 if (view_) |
| 50 view_->GetWidget()->CloseNow(); |
| 51 parent_widget_->CloseNow(); |
| 52 quit_closure_ = base::Closure(); |
| 53 constrained_window::SetConstrainedWindowViewsClient(nullptr); |
| 54 |
| 55 ViewsTestBase::TearDown(); |
| 56 } |
| 57 |
| 58 protected: |
| 59 // Creates the WebShareTargetPickerView (available as view()). |
| 60 void CreateView(const std::vector<std::pair<base::string16, GURL>>& targets) { |
| 61 view_ = new WebShareTargetPickerView( |
| 62 targets, base::Bind(&WebShareTargetPickerViewTest::OnCallback, |
| 63 base::Unretained(this))); |
| 64 constrained_window::CreateBrowserModalDialogViews( |
| 65 view_, parent_widget_->GetNativeWindow()) |
| 66 ->Show(); |
| 67 } |
| 68 |
| 69 // Sets the closure that will be called when the dialog is closed. This is |
| 70 // used in tests to quit the RunLoop. |
| 71 void SetQuitClosure(base::Closure&& quit_closure) { |
| 72 quit_closure_ = std::move(quit_closure); |
| 73 } |
| 74 |
| 75 // The view under test. |
| 76 WebShareTargetPickerView* view() { return view_; } |
| 77 // The table inside the view (for inspection). |
| 78 views::TableView* table() { return view_->table_; } |
| 79 |
| 80 // The result that was returned to the dialog's callback. |
| 81 const base::Optional<std::string>& result() { return result_; } |
| 82 |
| 83 private: |
| 84 void OnCallback(base::Optional<std::string> result) { |
| 85 result_ = result; |
| 86 if (quit_closure_) |
| 87 quit_closure_.Run(); |
| 88 } |
| 89 |
| 90 std::unique_ptr<views::Widget> parent_widget_; |
| 91 WebShareTargetPickerView* view_ = nullptr; |
| 92 |
| 93 base::Optional<std::string> result_; |
| 94 |
| 95 base::Closure quit_closure_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(WebShareTargetPickerViewTest); |
| 98 }; |
| 99 |
| 100 // Table with 0 targets. Choose to cancel. |
| 101 TEST_F(WebShareTargetPickerViewTest, EmptyListCancel) { |
| 102 CreateView(std::vector<std::pair<base::string16, GURL>>()); |
| 103 EXPECT_EQ(0, table()->RowCount()); |
| 104 EXPECT_EQ(-1, table()->FirstSelectedRow()); // Nothing selected. |
| 105 EXPECT_FALSE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 106 |
| 107 base::RunLoop run_loop; |
| 108 SetQuitClosure(run_loop.QuitClosure()); |
| 109 |
| 110 // Double-click should have no effect. |
| 111 view()->OnDoubleClick(); |
| 112 EXPECT_TRUE(view()->visible()); |
| 113 |
| 114 view()->Cancel(); |
| 115 |
| 116 run_loop.Run(); |
| 117 |
| 118 EXPECT_EQ(base::nullopt, result()); |
| 119 } |
| 120 |
| 121 // Table with 2 targets. Choose second target and share. |
| 122 TEST_F(WebShareTargetPickerViewTest, ChooseItem) { |
| 123 std::vector<std::pair<base::string16, GURL>> targets{ |
| 124 std::make_pair(base::ASCIIToUTF16("App One"), |
| 125 GURL("https://appone.com/path/bits")), |
| 126 std::make_pair(base::ASCIIToUTF16("App Two"), |
| 127 GURL("https://apptwo.xyz"))}; |
| 128 CreateView(targets); |
| 129 EXPECT_EQ(2, table()->RowCount()); |
| 130 EXPECT_EQ(base::ASCIIToUTF16("App One (https://appone.com/)"), |
| 131 table()->model()->GetText(0, 0)); |
| 132 EXPECT_EQ(base::ASCIIToUTF16("App Two (https://apptwo.xyz/)"), |
| 133 table()->model()->GetText(1, 0)); |
| 134 EXPECT_EQ(0, table()->FirstSelectedRow()); |
| 135 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 136 |
| 137 // Deselect and ensure OK button is disabled. |
| 138 table()->Select(-1); |
| 139 EXPECT_FALSE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 140 |
| 141 // Select the second app and ensure the OK button is enabled. |
| 142 table()->Select(1); |
| 143 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 144 |
| 145 base::RunLoop run_loop; |
| 146 SetQuitClosure(run_loop.QuitClosure()); |
| 147 |
| 148 view()->Accept(); |
| 149 |
| 150 run_loop.Run(); |
| 151 |
| 152 EXPECT_EQ(base::Optional<std::string>("https://apptwo.xyz/"), result()); |
| 153 } |
| 154 |
| 155 // Table with 1 target. Select using double-click. |
| 156 TEST_F(WebShareTargetPickerViewTest, ChooseItemWithDoubleClick) { |
| 157 std::vector<std::pair<base::string16, GURL>> targets{std::make_pair( |
| 158 base::ASCIIToUTF16("App One"), GURL("https://appone.com/path/bits"))}; |
| 159 CreateView(targets); |
| 160 EXPECT_EQ(1, table()->RowCount()); |
| 161 EXPECT_EQ(base::ASCIIToUTF16("App One (https://appone.com/)"), |
| 162 table()->model()->GetText(0, 0)); |
| 163 EXPECT_EQ(0, table()->FirstSelectedRow()); |
| 164 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| 165 |
| 166 base::RunLoop run_loop; |
| 167 SetQuitClosure(run_loop.QuitClosure()); |
| 168 |
| 169 view()->OnDoubleClick(); |
| 170 |
| 171 run_loop.Run(); |
| 172 |
| 173 EXPECT_EQ(base::Optional<std::string>("https://appone.com/path/bits"), |
| 174 result()); |
| 175 } |
| 176 |
| 177 } // namespace |
OLD | NEW |