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/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 "base/test/test_timeouts.h" | |
14 #include "chrome/browser/platform_util.h" | |
15 #include "content/public/common/content_switches.h" | |
16 #include "ui/base/material_design/material_design_controller.h" | |
17 #include "ui/base/test/material_design_controller_test_api.h" | |
18 #include "ui/base/test/user_interactive_test_case.h" | |
19 #include "ui/base/ui_base_switches.h" | |
20 #include "ui/compositor/compositor_switches.h" | |
21 #include "ui/views/widget/widget.h" | |
22 #include "ui/views/widget/widget_observer.h" | |
23 | |
24 namespace { | |
25 | |
26 // When present on the command line, runs the test indefinitely. | |
27 constexpr char kInteractiveSwitch[] = "interactive"; | |
28 | |
29 // Switch passed to TestBrowserDialog.Invoke indicating which registered dialog | |
30 // to invoke. | |
31 constexpr char kDialogSwitch[] = "dialog"; | |
32 | |
33 // Name of the test case generated by the TEST_BROWSER_DIALOG macro. | |
34 constexpr char kInvokeTestCase[] = "InvokeDefault"; | |
35 | |
36 std::vector<std::string>& GetCases() { | |
37 CR_DEFINE_STATIC_LOCAL(std::vector<std::string>, all_cases, ()); | |
38 return all_cases; | |
39 } | |
40 | |
41 // An automatic action for WidgetCloser to post to the RunLoop. | |
42 // TODO(tapted): Explore asynchronous Widget::Close() and DialogClientView:: | |
43 // {Accept,Cancel}Window() approaches to test other dialog lifetimes. | |
44 enum class DialogAction { | |
45 INTERACTIVE, // Run interactively. | |
46 CLOSE_NOW, // Call Widget::CloseNow(). | |
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(views::Widget* widget, DialogAction action) | |
53 : widget_(widget), weak_ptr_factory_(this) { | |
54 widget->AddObserver(this); | |
55 if (action == DialogAction::INTERACTIVE) | |
56 return; | |
57 | |
58 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
59 FROM_HERE, | |
60 base::Bind(&WidgetCloser::CloseNow, weak_ptr_factory_.GetWeakPtr())); | |
61 } | |
62 | |
63 // WidgetObserver: | |
64 void OnWidgetDestroyed(views::Widget* widget) override { | |
65 widget_->RemoveObserver(this); | |
66 widget_ = nullptr; | |
67 base::MessageLoop::current()->QuitNow(); | |
68 } | |
69 | |
70 private: | |
71 void CloseNow() { | |
72 if (widget_) | |
73 widget_->CloseNow(); | |
74 } | |
75 | |
76 views::Widget* widget_; | |
77 | |
78 base::WeakPtrFactory<WidgetCloser> weak_ptr_factory_; | |
79 | |
80 DISALLOW_COPY_AND_ASSIGN(WidgetCloser); | |
81 }; | |
82 | |
83 // Splits a --dialog= switch argument into |harness_name| and |test_case_name|. | |
84 void SplitDialogDescription(const std::string& argument, | |
85 std::string* harness_name, | |
86 std::string* test_case_name) { | |
Peter Kasting
2016/12/15 08:12:29
Nit: I don't know whether it's better or worse tha
tapted
2016/12/16 04:21:53
Yeah I considered this too - flip-flopped a bit. I
| |
87 std::string::size_type dot = argument.find('.'); | |
88 if (harness_name) | |
89 *harness_name = argument.substr(0, dot); | |
90 if (test_case_name && dot != std::string::npos) | |
91 *test_case_name = argument.substr(dot + 1); | |
92 } | |
93 | |
94 } // namespace | |
95 | |
96 // static | |
97 std::vector<std::string> TestDialogInterface::NameProvider() { | |
98 return {"Default"}; | |
99 } | |
100 | |
101 // static | |
102 int TestDialogInterface::Register(const char* harness, | |
103 NameProviderFunction name_provider) { | |
104 const std::string prefix = harness + std::string("."); | |
105 std::vector<std::string>& all_cases = GetCases(); | |
106 for (const std::string& name : name_provider()) | |
107 all_cases.push_back(prefix + name); | |
108 return 1; | |
109 } | |
110 | |
111 // static | |
112 void TestDialogInterface::TestBrowserDialogRun( | |
113 TestDialogInterface* harness, | |
114 const std::vector<std::string>& available_cases) { | |
115 const base::CommandLine& command_line = | |
116 *base::CommandLine::ForCurrentProcess(); | |
117 | |
118 #if defined(OS_MACOSX) | |
119 // The rest of this method assumes the child dialog is toolkit-views. So, for | |
120 // Mac, it will only work if --secondary-ui-md is passed. Without this, a | |
121 // Cocoa dialog will be created, which TestDialogInterface doesn't support. | |
122 // Force SecondaryUiMaterial() on Mac to get coverage on the bots. Leave it | |
123 // optional elsewhere so that the non-MD dialog can be invoked to compare. | |
124 ui::test::MaterialDesignControllerTestAPI md_test_api( | |
125 ui::MaterialDesignController::GetMode()); | |
126 md_test_api.SetSecondaryUiMaterial(true); | |
127 #endif | |
128 | |
129 int dialog_index = 0; | |
130 std::string dialog_name = command_line.GetSwitchValueASCII(kDialogSwitch); | |
131 if (!dialog_name.empty()) { | |
132 std::string case_name; | |
133 SplitDialogDescription(dialog_name, nullptr, &case_name); | |
134 auto it = | |
135 std::find(available_cases.begin(), available_cases.end(), case_name); | |
136 ASSERT_NE(it, available_cases.end()); | |
137 dialog_index = std::distance(available_cases.begin(), it); | |
138 } | |
139 | |
140 gfx::NativeView parent = | |
141 platform_util::GetViewForWindow(harness->DialogParent()); | |
142 views::Widget::Widgets widgets_before; | |
143 views::Widget::GetAllChildWidgets(parent, &widgets_before); | |
144 | |
145 harness->ShowDialog(dialog_index); | |
146 views::Widget::Widgets widgets_after; | |
147 views::Widget::GetAllChildWidgets(parent, &widgets_after); | |
148 | |
149 auto added = base::STLSetDifference<std::vector<views::Widget*>>( | |
150 widgets_after, widgets_before); | |
151 | |
152 // This can fail if no dialog was shown, if the dialog shown wasn't a toolkit- | |
153 // views dialog, or if more than one child dialog was shown. | |
154 ASSERT_EQ(1u, added.size()); | |
155 | |
156 const DialogAction action = command_line.HasSwitch(kInteractiveSwitch) | |
157 ? DialogAction::INTERACTIVE | |
158 : DialogAction::CLOSE_NOW; | |
159 | |
160 WidgetCloser closer(added[0], action); | |
161 ::test::RunTestInteractively(); | |
162 } | |
163 | |
164 // Adds a browser_test entry point into the dialog testing framework. Without a | |
165 // --dialog specified, just lists the available dialogs. | |
166 TEST(TestBrowserDialog, Invoke) { | |
167 const base::CommandLine& invoker = *base::CommandLine::ForCurrentProcess(); | |
168 const std::string dialog_name = invoker.GetSwitchValueASCII(kDialogSwitch); | |
169 | |
170 const std::vector<std::string>& all_cases = GetCases(); | |
171 ASSERT_FALSE(all_cases.empty()); | |
172 | |
173 if (dialog_name.empty()) { | |
174 std::ostringstream case_list; | |
175 for (const std::string& name : GetCases()) | |
176 case_list << "\t" << name << "\n"; | |
Peter Kasting
2016/12/15 08:12:29
Nit: Unless I overlooked something, you have no ne
tapted
2016/12/16 04:21:53
Done.
| |
177 VLOG(0) << "\nPass one of the following after --" << kDialogSwitch << "=\n" | |
178 << case_list.str(); | |
179 return; // Nothing provided (just list options). | |
Peter Kasting
2016/12/15 08:12:29
Nit: I found this comment more confusing than help
tapted
2016/12/16 04:21:53
Done (just added "and exits" to the function-level
| |
180 } | |
181 | |
182 auto it = std::find(all_cases.begin(), all_cases.end(), dialog_name); | |
183 ASSERT_NE(it, all_cases.end()) << "Dialog '" << dialog_name << "' not found."; | |
184 | |
185 std::string harness_name; | |
186 SplitDialogDescription(dialog_name, &harness_name, nullptr); | |
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 base::LaunchOptions options; | |
195 | |
196 // Disable timeouts and generate screen output if --interactive was specified. | |
197 if (command.HasSwitch(kInteractiveSwitch)) { | |
198 command.AppendSwitchASCII(switches::kUiTestActionMaxTimeout, | |
199 TestTimeouts::kNoTimeoutSwitchValue); | |
200 command.AppendSwitchASCII(switches::kTestLauncherTimeout, | |
201 TestTimeouts::kNoTimeoutSwitchValue); | |
202 command.AppendSwitch(switches::kEnablePixelOutputInTests); | |
203 } else { | |
204 options.wait = true; | |
tapted
2016/12/15 06:08:23
I realised it was pretty pointless removing the ti
| |
205 } | |
206 | |
207 base::LaunchProcess(command, options); | |
208 } | |
OLD | NEW |