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

Side by Side Diff: chrome/browser/ui/test/test_browser_dialog.cc

Issue 2532793002: Add a TestBrowserDialog helper class for testing browser dialogs in a consistent way. (Closed)
Patch Set: Add kEnablePixelOutputInTests Created 4 years 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
(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/test/test_browser_dialog.h"
6
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/process/launch.h"
10 #include "base/strings/string_split.h"
11 #include "base/test/launcher/test_launcher.h"
12 #include "base/test/test_switches.h"
13 #include "chrome/browser/platform_util.h"
14 #include "content/public/common/content_switches.h"
15 #include "ui/base/test/user_interactive_test_case.h"
16 #include "ui/base/ui_base_switches.h"
17 #include "ui/compositor/compositor_switches.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/widget/widget_observer.h"
20
21 using views::Widget;
Peter Kasting 2016/12/15 00:28:55 Nit: You already explicitly qualify the majority o
tapted 2016/12/15 06:08:22 Done.
22
23 namespace {
24
25 // When present on the command line, runs the test indefinitely.
26 const char kInteractiveSwitch[] = "interactive";
Peter Kasting 2016/12/15 00:28:55 Nit: Prefer constexpr to const for compile-time co
tapted 2016/12/15 06:08:22 Done.
27
28 // Switch passed to TestBrowserDialog.Invoke indicating which registered dialog
29 // to invoke.
30 const char kDialogSwitch[] = "dialog";
31
32 // Name of the test case generated by the TEST_BROWSER_DIALOG macro.
33 const char kInvokeTestCase[] = "InvokeDefault";
34
35 std::vector<std::string>& GetCases() {
36 CR_DEFINE_STATIC_LOCAL(std::vector<std::string>, all_cases, ());
37 return all_cases;
38 }
39
40 // An automatic action for WidgetCloser to post to the RunLoop.
41 enum class DialogAction {
42 INTERACTIVE, // Run interactively.
43 CLOSE_NOW, // Call Widget::CloseNow().
44 CLOSE, // Call Widget::Close() and empty the run loop.
Peter Kasting 2016/12/15 00:28:54 Nit: I feel like either you should implement these
tapted 2016/12/15 06:08:22 Done (added a TODO in the enum class comment)
45 ACCEPT, // Call DialogClientView::AcceptWindow().
46 CANCEL, // Call DialogClientView::CancelWindow().
47 };
48
49 // Helper to break out of the nested run loop that runs a test dialog.
50 class WidgetCloser : public views::WidgetObserver {
51 public:
52 WidgetCloser(Widget* widget, DialogAction action)
53 : widget_(widget), weak_ptr_factory_(this) {
54 widget->AddObserver(this);
55 auto action_function = &WidgetCloser::CloseNow;
56 switch (action) {
57 case DialogAction::INTERACTIVE:
58 return;
59 case DialogAction::CLOSE_NOW:
60 break;
61 default:
62 NOTIMPLEMENTED();
63 }
64 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE, base::Bind(action_function, weak_ptr_factory_.GetWeakPtr()));
66 }
67
68 // WidgetObserver:
69 void OnWidgetDestroyed(Widget* widget) override {
70 widget_->RemoveObserver(this);
71 widget_ = nullptr;
72 base::MessageLoop::current()->QuitNow();
73 }
74
75 private:
76 void CloseNow() {
77 if (widget_)
78 widget_->CloseNow();
79 }
80
81 Widget* widget_;
82
83 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_;
84
85 DISALLOW_COPY_AND_ASSIGN(WidgetCloser);
86 };
87
88 } // namespace
89
90 // static
91 std::vector<std::string> TestDialogInterface::NameProvider() {
92 return {"Default"};
93 }
94
95 // static
96 int TestDialogInterface::Register(const char* harness,
97 NameProviderFunction name_provider) {
98 const std::string separator = ".";
Peter Kasting 2016/12/15 00:28:54 Nit: constexpr
tapted 2016/12/15 06:08:22 Sadly std::basic_string invokes malloc for this un
99 std::vector<std::string> names = name_provider();
Peter Kasting 2016/12/15 00:28:55 Nit: Can inline below
tapted 2016/12/15 06:08:22 Done.
100 std::vector<std::string>& all_cases = GetCases();
101 for (const std::string& name : names)
102 all_cases.push_back(harness + separator + name);
103 return 1;
104 }
105
106 // static
107 void TestDialogInterface::TestBrowserDialogRun(
108 TestDialogInterface* harness,
109 const std::vector<std::string>& available_cases) {
110 const base::CommandLine& command_line =
111 *base::CommandLine::ForCurrentProcess();
112
113 #if defined(OS_MACOSX)
114 // The rest of this method assumes the child dialog is toolkit-views. So, for
115 // Mac, it will only work if --secondary-ui-md is passed. When called via the
116 // process spawned by TestBrowserDialog.Invoke (along with --dialog=), this
117 // will always be the case. However, when the test runner invokes
118 // FooTestHarness.InvokeDefault as part of the test suite it won't be. Skip
119 // the test in that case.
120 if (!command_line.HasSwitch(switches::kExtendMdToSecondaryUi))
Peter Kasting 2016/12/15 00:28:55 I'm worried this is going to break in the future.
tapted 2016/12/15 06:08:22 Since MD dialogs on Mac are only implemented using
Peter Kasting 2016/12/15 08:12:28 And to be clear, this is a problem because: * The
tapted 2016/12/16 04:21:53 Yes (I considered defining a DISABLED_ test to spa
121 return;
122 #endif
123
124 int dialog_index = 0;
125 std::string dialog_name = command_line.GetSwitchValueASCII(kDialogSwitch);
126 if (!dialog_name.empty()) {
127 std::vector<std::string> name_parts = base::SplitString(
128 dialog_name, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
Peter Kasting 2016/12/15 00:28:54 I think instead of using SplitString, you should f
tapted 2016/12/15 06:08:22 Done.
129 ASSERT_EQ(2u, name_parts.size());
130 const std::string& case_name = name_parts[1];
Peter Kasting 2016/12/15 00:28:54 Nit: There are two blocks in this file that want t
tapted 2016/12/15 06:08:22 Done.
131 auto it =
132 std::find(available_cases.begin(), available_cases.end(), case_name);
133 ASSERT_NE(it, available_cases.end());
134 dialog_index = std::distance(available_cases.begin(), it);
135 }
136
137 gfx::NativeView parent =
138 platform_util::GetViewForWindow(harness->DialogParent());
139 views::Widget::Widgets widgets_before;
140 views::Widget::GetAllChildWidgets(parent, &widgets_before);
141
142 harness->ShowDialog(dialog_index);
143 views::Widget::Widgets widgets_after;
144 views::Widget::GetAllChildWidgets(parent, &widgets_after);
145
146 EXPECT_GT(widgets_after.size(), widgets_before.size())
147 << "Dialog not shown, or not a toolkit-views Dialog.";
148 EXPECT_LE(widgets_after.size(), widgets_before.size() + 1)
149 << "More than one child dialog shown.";
Peter Kasting 2016/12/15 00:28:55 Nit: It doesn't seem like the error messages add m
tapted 2016/12/15 06:08:22 Done.
150
151 auto added = base::STLSetDifference<std::vector<views::Widget*>>(
152 widgets_after, widgets_before);
153 ASSERT_FALSE(added.empty());
154 DialogAction action = DialogAction::CLOSE_NOW;
155 if (command_line.HasSwitch(kInteractiveSwitch))
156 action = DialogAction::INTERACTIVE;
Peter Kasting 2016/12/15 00:28:55 Nit: Not shorter, but makes it clearer this is jus
tapted 2016/12/15 06:08:22 Done.
157
158 WidgetCloser closer(added[0], action);
159 ::test::RunTestInteractively();
160 }
161
162 // Adds a browser_test entry point into the dialog testing framework. Without a
163 // --dialog specified, just lists the available dialogs.
164 TEST(TestBrowserDialog, Invoke) {
165 const base::CommandLine& invoker = *base::CommandLine::ForCurrentProcess();
166 const std::string dialog_name = invoker.GetSwitchValueASCII(kDialogSwitch);
167
168 const std::vector<std::string>& all_cases = GetCases();
169 ASSERT_FALSE(all_cases.empty());
170
171 if (dialog_name.empty()) {
172 VLOG(0) << "\nPass one of the following after --" << kDialogSwitch << "=";
173 for (const std::string& name : GetCases())
174 std::cout << "\t" << name << "\n";
Peter Kasting 2016/12/15 00:28:54 Did you mean to VLOG() this? Or did you mean to c
tapted 2016/12/15 06:08:22 Ah, that's a better idea :). I wanted only the fi
175 }
176
177 if (dialog_name.empty())
178 return; // Nothing specified.
Peter Kasting 2016/12/15 00:28:55 Nit: Put this in the above conditional.
tapted 2016/12/15 06:08:22 Done.
179
180 auto it = std::find(all_cases.begin(), all_cases.end(), dialog_name);
181 ASSERT_NE(it, all_cases.end()) << "Dialog '" << dialog_name << "' not found.";
182
183 std::vector<std::string> name_parts = base::SplitString(
184 dialog_name, ".", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
185 ASSERT_EQ(2u, name_parts.size());
186 const std::string& harness_name = name_parts[0];
187
188 base::CommandLine command(invoker);
189
190 // Replace TestBrowserDialog.Invoke with |harness_name|.InvokeDefault.
191 command.AppendSwitchASCII(base::kGTestFilterFlag,
192 harness_name + "." + kInvokeTestCase);
193
194 // Enable MacViews and Harmony by default.
195 command.AppendSwitch(switches::kExtendMdToSecondaryUi);
Peter Kasting 2016/12/15 00:28:55 Same worry as earlier, but even more so. It seems
tapted 2016/12/15 06:08:22 Now removed with the change at the earlier comment
196
197 // Disable timeouts and generate screen output if --interactive was specified.
198 if (command.HasSwitch(kInteractiveSwitch)) {
199 const std::string disable = "10000000";
Peter Kasting 2016/12/15 00:28:55 This is an ugly way of disabling these timeouts.
tapted 2016/12/15 06:08:22 kAlmostInfiniteTimeoutMs is tricky to remove since
Peter Kasting 2016/12/15 08:12:28 Feel free to file those yaks as bugs...
tapted 2016/12/16 04:21:53 Filed http://crbug.com/674764 (which you've seen :
200 command.AppendSwitchASCII(switches::kUiTestActionMaxTimeout, disable);
201 command.AppendSwitchASCII(switches::kTestLauncherTimeout, disable);
202 command.AppendSwitch(switches::kEnablePixelOutputInTests);
203 }
204
205 base::LaunchOptions options;
206 options.wait = true;
207 base::LaunchProcess(command, options);
208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698