1 // Copyright 2013 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 "base/bind.h"
6 #include "base/run_loop.h"
7 #include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/devtools/devtools_window_testing.h"
10 #include "chrome/browser/extensions/component_loader.h"
11 #include "chrome/browser/extensions/extension_browsertest.h"
12 #include "chrome/browser/feedback/feedback_dialog_utils.h"
13 #include "chrome/browser/feedback/feedback_uploader_chrome.h"
14 #include "chrome/browser/feedback/feedback_uploader_factory_chrome.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/common/extensions/extension_constants.h"
18 #include "chrome/common/webui_url_constants.h"
19 #include "chrome/test/base/in_process_browser_test.h"
20 #include "chrome/test/base/ui_test_utils.h"
21 #include "content/public/common/content_switches.h"
22 #include "content/public/test/browser_test.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "content/public/test/test_utils.h"
25 #include "extensions/browser/api/feedback_private/feedback_private_api.h"
26 #include "extensions/browser/app_window/app_window.h"
27 #include "extensions/browser/app_window/app_window_registry.h"
28 #include "extensions/browser/event_router.h"
29 #include "extensions/browser/extension_system.h"
30 #include "extensions/common/api/feedback_private.h"
31
32 using extensions::api::feedback_private::FeedbackFlow;
33
34 namespace {
35
StopMessageLoopCallback()36 void StopMessageLoopCallback() {
37 base::RunLoop::QuitCurrentWhenIdleDeprecated();
38 }
39
40 } // namespace
41
42 namespace extensions {
43
44 class FeedbackTest : public ExtensionBrowserTest {
45 public:
SetUp()46 void SetUp() override {
47 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
48 ExtensionBrowserTest::SetUp();
49 }
50
SetUpCommandLine(base::CommandLine * command_line)51 void SetUpCommandLine(base::CommandLine* command_line) override {
52 command_line->AppendSwitch(::switches::kEnableUserMediaScreenCapturing);
53 }
54
55 protected:
IsFeedbackAppAvailable()56 bool IsFeedbackAppAvailable() {
57 return extensions::EventRouter::Get(browser()->profile())
58 ->ExtensionHasEventListener(
59 extension_misc::kFeedbackExtensionId,
60 extensions::api::feedback_private::OnFeedbackRequested::kEventName);
61 }
62
StartFeedbackUI(FeedbackFlow flow,const std::string & extra_diagnostics,bool from_assistant=false,bool include_bluetooth_logs=false)63 void StartFeedbackUI(FeedbackFlow flow,
64 const std::string& extra_diagnostics,
65 bool from_assistant = false,
66 bool include_bluetooth_logs = false) {
67 base::Closure callback = base::Bind(&StopMessageLoopCallback);
68 extensions::FeedbackPrivateGetStringsFunction::set_test_callback(&callback);
69 InvokeFeedbackUI(flow, extra_diagnostics, from_assistant,
70 include_bluetooth_logs);
71 content::RunMessageLoop();
72 extensions::FeedbackPrivateGetStringsFunction::set_test_callback(NULL);
73 }
74
VerifyFeedbackAppLaunch()75 void VerifyFeedbackAppLaunch() {
76 AppWindow* window =
77 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
78 ASSERT_TRUE(window);
79 const Extension* feedback_app = window->GetExtension();
80 ASSERT_TRUE(feedback_app);
81 EXPECT_EQ(feedback_app->id(),
82 std::string(extension_misc::kFeedbackExtensionId));
83 }
84
85 private:
InvokeFeedbackUI(FeedbackFlow flow,const std::string & extra_diagnostics,bool from_assistant,bool include_bluetooth_logs)86 void InvokeFeedbackUI(FeedbackFlow flow,
87 const std::string& extra_diagnostics,
88 bool from_assistant,
89 bool include_bluetooth_logs) {
90 extensions::FeedbackPrivateAPI* api =
91 extensions::FeedbackPrivateAPI::GetFactoryInstance()->Get(
92 browser()->profile());
93 api->RequestFeedbackForFlow("Test description", "Test placeholder",
94 "Test tag", extra_diagnostics,
95 GURL("http://www.test.com"), flow,
96 from_assistant, include_bluetooth_logs);
97 }
98 };
99
100 class TestFeedbackUploaderDelegate
101 : public feedback::FeedbackUploaderChrome::Delegate {
102 public:
TestFeedbackUploaderDelegate(base::RunLoop * quit_on_dispatch)103 explicit TestFeedbackUploaderDelegate(base::RunLoop* quit_on_dispatch)
104 : quit_on_dispatch_(quit_on_dispatch) {}
105
OnStartDispatchingReport()106 void OnStartDispatchingReport() override { quit_on_dispatch_->Quit(); }
107
108 private:
109 base::RunLoop* quit_on_dispatch_;
110 };
111
IN_PROC_BROWSER_TEST_F(FeedbackTest,ShowFeedback)112 IN_PROC_BROWSER_TEST_F(FeedbackTest, ShowFeedback) {
113 WaitForExtensionViewsToLoad();
114
115 ASSERT_TRUE(IsFeedbackAppAvailable());
116 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_REGULAR, std::string());
117 VerifyFeedbackAppLaunch();
118 }
119
IN_PROC_BROWSER_TEST_F(FeedbackTest,ShowLoginFeedback)120 IN_PROC_BROWSER_TEST_F(FeedbackTest, ShowLoginFeedback) {
121 WaitForExtensionViewsToLoad();
122
123 ASSERT_TRUE(IsFeedbackAppAvailable());
124 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_LOGIN, std::string());
125 VerifyFeedbackAppLaunch();
126
127 AppWindow* const window =
128 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
129 ASSERT_TRUE(window);
130 content::WebContents* const content = window->web_contents();
131
132 bool bool_result = false;
133 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
134 content,
135 "domAutomationController.send("
136 "$('page-url').hidden && $('attach-file-container').hidden && "
137 "$('attach-file-note').hidden);",
138 &bool_result));
139 EXPECT_TRUE(bool_result);
140 }
141
142 // Tests that there's an option in the email drop down box with a value ''.
IN_PROC_BROWSER_TEST_F(FeedbackTest,AnonymousUser)143 IN_PROC_BROWSER_TEST_F(FeedbackTest, AnonymousUser) {
144 WaitForExtensionViewsToLoad();
145
146 ASSERT_TRUE(IsFeedbackAppAvailable());
147 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_REGULAR, std::string());
148 VerifyFeedbackAppLaunch();
149
150 AppWindow* const window =
151 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
152 ASSERT_TRUE(window);
153 content::WebContents* const content = window->web_contents();
154
155 bool bool_result = false;
156 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
157 content,
158 "domAutomationController.send("
159 " ((function() {"
160 " var options = $('user-email-drop-down').options;"
161 " for (var option in options) {"
162 " if (options[option].value == '')"
163 " return true;"
164 " }"
165 " return false;"
166 " })()));",
167 &bool_result));
168
169 EXPECT_TRUE(bool_result);
170 }
171
172 // Ensures that when extra diagnostics are provided with feedback, they are
173 // injected properly in the system information.
IN_PROC_BROWSER_TEST_F(FeedbackTest,ExtraDiagnostics)174 IN_PROC_BROWSER_TEST_F(FeedbackTest, ExtraDiagnostics) {
175 WaitForExtensionViewsToLoad();
176
177 ASSERT_TRUE(IsFeedbackAppAvailable());
178 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_REGULAR, "Some diagnostics");
179 VerifyFeedbackAppLaunch();
180
181 AppWindow* const window =
182 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
183 ASSERT_TRUE(window);
184 content::WebContents* const content = window->web_contents();
185
186 bool bool_result = false;
187 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
188 content,
189 "domAutomationController.send("
190 " ((function() {"
191 " var sysInfo = feedbackInfo.systemInformation;"
192 " for (var info in sysInfo) {"
193 " if (sysInfo[info].key == 'EXTRA_DIAGNOSTICS' &&"
194 " sysInfo[info].value == 'Some diagnostics') {"
195 " return true;"
196 " }"
197 " }"
198 " return false;"
199 " })()));",
200 &bool_result));
201
202 EXPECT_TRUE(bool_result);
203 }
204
205 // Ensures that when triggered from Assistant with Google account, Assistant
206 // checkbox are not hidden.
IN_PROC_BROWSER_TEST_F(FeedbackTest,ShowFeedbackFromAssistant)207 IN_PROC_BROWSER_TEST_F(FeedbackTest, ShowFeedbackFromAssistant) {
208 WaitForExtensionViewsToLoad();
209
210 ASSERT_TRUE(IsFeedbackAppAvailable());
211 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_GOOGLEINTERNAL, std::string(),
212 /*from_assistant*/ true);
213 VerifyFeedbackAppLaunch();
214
215 AppWindow* const window =
216 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
217 ASSERT_TRUE(window);
218 content::WebContents* const content = window->web_contents();
219
220 bool bool_result = false;
221 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
222 content,
223 "domAutomationController.send("
224 " ((function() {"
225 " if ($('assistant-checkbox-container') != null &&"
226 " $('assistant-checkbox-container').hidden == true) {"
227 " return false;"
228 " }"
229 " return true;"
230 " })()));",
231 &bool_result));
232 EXPECT_TRUE(bool_result);
233 }
234
235 #if defined(OS_CHROMEOS)
236 // Ensures that when triggered from a Google account and a Bluetooth related
237 // string is entered into the description, that we provide the option for
238 // uploading Bluetooth logs as well.
IN_PROC_BROWSER_TEST_F(FeedbackTest,ProvideBluetoothLogs)239 IN_PROC_BROWSER_TEST_F(FeedbackTest, ProvideBluetoothLogs) {
240 WaitForExtensionViewsToLoad();
241
242 ASSERT_TRUE(IsFeedbackAppAvailable());
243 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_GOOGLEINTERNAL, std::string(),
244 /*from_assistant*/ false, /*include_bluetooth_logs*/ true);
245 VerifyFeedbackAppLaunch();
246
247 AppWindow* const window =
248 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
249 ASSERT_TRUE(window);
250 content::WebContents* const content = window->web_contents();
251
252 // It shouldn't be visible until we put the Bluetooth text into the
253 // description.
254 bool bool_result = false;
255 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
256 content,
257 "domAutomationController.send("
258 " ((function() {"
259 " if ($('bluetooth-checkbox-container') != null &&"
260 " $('bluetooth-checkbox-container').hidden == true) {"
261 " return true;"
262 " }"
263 " return false;"
264 " })()));",
265 &bool_result));
266 EXPECT_TRUE(bool_result);
267 bool_result = false;
268 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
269 content,
270 "domAutomationController.send("
271 " ((function() {"
272 " var elem = document.getElementById('description-text');"
273 " elem.value = 'bluetooth';"
274 " elem.dispatchEvent(new Event('input', {}));"
275 " if ($('bluetooth-checkbox-container') != null &&"
276 " $('bluetooth-checkbox-container').hidden == false) {"
277 " return true;"
278 " }"
279 " return false;"
280 " })()));",
281 &bool_result));
282 EXPECT_TRUE(bool_result);
283 }
284 #endif // if defined(CHROME_OS)
285
286 // Disabled due to flake: https://crbug.com/1069870
IN_PROC_BROWSER_TEST_F(FeedbackTest,DISABLED_GetTargetTabUrl)287 IN_PROC_BROWSER_TEST_F(FeedbackTest, DISABLED_GetTargetTabUrl) {
288 const std::pair<std::string, std::string> test_cases[] = {
289 {"https://www.google.com/", "https://www.google.com/"},
290 {"chrome://version/", chrome::kChromeUIVersionURL},
291 {chrome::kChromeUIBookmarksURL, chrome::kChromeUIBookmarksURL},
292 };
293
294 for (const auto& test_case : test_cases) {
295 GURL expected_url = GURL(test_case.second);
296
297 ui_test_utils::NavigateToURL(browser(), GURL(test_case.first));
298
299 // Sanity check that we always have one tab in the browser.
300 ASSERT_EQ(browser()->tab_strip_model()->count(), 1);
301
302 ASSERT_EQ(expected_url, browser()
303 ->tab_strip_model()
304 ->GetWebContentsAt(0)
305 ->GetLastCommittedURL());
306
307 ASSERT_EQ(expected_url,
308 chrome::GetTargetTabUrl(browser()->session_id(), 0));
309
310 // Open a DevTools window.
311 DevToolsWindow* devtools_window =
312 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
313
314 // Verify the expected url returned from GetTargetTabUrl against a
315 // DevTools window.
316 ASSERT_EQ(expected_url, chrome::GetTargetTabUrl(
317 DevToolsWindowTesting::Get(devtools_window)
318 ->browser()
319 ->session_id(),
320 0));
321
322 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window);
323 }
324 }
325
IN_PROC_BROWSER_TEST_F(FeedbackTest,SubmissionTest)326 IN_PROC_BROWSER_TEST_F(FeedbackTest, SubmissionTest) {
327 WaitForExtensionViewsToLoad();
328
329 ASSERT_TRUE(IsFeedbackAppAvailable());
330 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_GOOGLEINTERNAL, std::string());
331 VerifyFeedbackAppLaunch();
332
333 AppWindow* const window =
334 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
335 ASSERT_TRUE(window);
336 content::WebContents* const content = window->web_contents();
337
338 // Set a delegate for the uploader which will be invoked when the report
339 // normally would have been uploaded. We have it setup to then quit the
340 // RunLoop which will then allow us to terminate.
341 base::RunLoop run_loop;
342 TestFeedbackUploaderDelegate delegate(&run_loop);
343 feedback::FeedbackUploaderFactoryChrome::GetInstance()
344 ->GetForBrowserContext(browser()->profile())
345 ->set_feedback_uploader_delegate(&delegate);
346
347 // Click the send button.
348 bool bool_result = false;
349 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
350 content,
351 "domAutomationController.send("
352 " ((function() {"
353 " if ($('send-report-button') != null) {"
354 " document.getElementById('send-report-button').click();"
355 " return true;"
356 " }"
357 " return false;"
358 " })()));",
359 &bool_result));
360 EXPECT_TRUE(bool_result);
361
362 // This will DCHECK if the JS private API call doesn't return a value, which
363 // is the main case we are concerned about.
364 run_loop.Run();
365 feedback::FeedbackUploaderFactoryChrome::GetInstance()
366 ->GetForBrowserContext(browser()->profile())
367 ->set_feedback_uploader_delegate(nullptr);
368 }
369
370 } // namespace extensions
371