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

Side by Side Diff: chrome/browser/ui/views/webshare/webshare_target_picker_view_unittest.cc

Issue 2679533002: Added unit test for WebShareTargetPickerView. (Closed)
Patch Set: Remove double-click on empty list logic + test. Created 3 years, 10 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
« no previous file with comments | « chrome/browser/ui/views/webshare/webshare_target_picker_view.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 view()->Cancel();
111
112 run_loop.Run();
113
114 EXPECT_EQ(base::nullopt, result());
115 }
116
117 // Table with 2 targets. Choose second target and share.
118 TEST_F(WebShareTargetPickerViewTest, ChooseItem) {
119 std::vector<std::pair<base::string16, GURL>> targets{
120 std::make_pair(base::ASCIIToUTF16("App One"),
121 GURL("https://appone.com/path/bits")),
122 std::make_pair(base::ASCIIToUTF16("App Two"),
123 GURL("https://apptwo.xyz"))};
124 CreateView(targets);
125 EXPECT_EQ(2, table()->RowCount());
126 EXPECT_EQ(base::ASCIIToUTF16("App One (https://appone.com/)"),
127 table()->model()->GetText(0, 0));
128 EXPECT_EQ(base::ASCIIToUTF16("App Two (https://apptwo.xyz/)"),
129 table()->model()->GetText(1, 0));
130 EXPECT_EQ(0, table()->FirstSelectedRow());
131 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
132
133 // Deselect and ensure OK button is disabled.
134 table()->Select(-1);
135 EXPECT_FALSE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
136
137 // Select the second app and ensure the OK button is enabled.
138 table()->Select(1);
139 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
140
141 base::RunLoop run_loop;
142 SetQuitClosure(run_loop.QuitClosure());
143
144 view()->Accept();
145
146 run_loop.Run();
147
148 EXPECT_EQ(base::Optional<std::string>("https://apptwo.xyz/"), result());
149 }
150
151 // Table with 1 target. Select using double-click.
152 TEST_F(WebShareTargetPickerViewTest, ChooseItemWithDoubleClick) {
153 std::vector<std::pair<base::string16, GURL>> targets{std::make_pair(
154 base::ASCIIToUTF16("App One"), GURL("https://appone.com/path/bits"))};
155 CreateView(targets);
156 EXPECT_EQ(1, table()->RowCount());
157 EXPECT_EQ(base::ASCIIToUTF16("App One (https://appone.com/)"),
158 table()->model()->GetText(0, 0));
159 EXPECT_EQ(0, table()->FirstSelectedRow());
160 EXPECT_TRUE(view()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
161
162 base::RunLoop run_loop;
163 SetQuitClosure(run_loop.QuitClosure());
164
165 view()->OnDoubleClick();
166
167 run_loop.Run();
168
169 EXPECT_EQ(base::Optional<std::string>("https://appone.com/path/bits"),
170 result());
171 }
172
173 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/webshare/webshare_target_picker_view.h ('k') | chrome/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698