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_utils.h"
23 #include "content/public/test/test_utils.h"
24 #include "extensions/browser/api/feedback_private/feedback_private_api.h"
25 #include "extensions/browser/app_window/app_window.h"
26 #include "extensions/browser/app_window/app_window_registry.h"
27 #include "extensions/browser/event_router.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/common/api/feedback_private.h"
30
31 using extensions::api::feedback_private::FeedbackFlow;
32
33 namespace {
34
StopMessageLoopCallback()35 void StopMessageLoopCallback() {
36 base::RunLoop::QuitCurrentWhenIdleDeprecated();
37 }
38
39 } // namespace
40
41 namespace extensions {
42
43 class FeedbackTest : public ExtensionBrowserTest {
44 public:
SetUp()45 void SetUp() override {
46 extensions::ComponentLoader::EnableBackgroundExtensionsForTesting();
47 InProcessBrowserTest::SetUp();
48 }
49
SetUpCommandLine(base::CommandLine * command_line)50 void SetUpCommandLine(base::CommandLine* command_line) override {
51 command_line->AppendSwitch(::switches::kEnableUserMediaScreenCapturing);
52 }
53
54 protected:
IsFeedbackAppAvailable()55 bool IsFeedbackAppAvailable() {
56 return extensions::EventRouter::Get(browser()->profile())
57 ->ExtensionHasEventListener(
58 extension_misc::kFeedbackExtensionId,
59 extensions::api::feedback_private::OnFeedbackRequested::kEventName);
60 }
61
StartFeedbackUI(FeedbackFlow flow,const std::string & extra_diagnostics,bool from_assistant=false,bool include_bluetooth_logs=false)62 void StartFeedbackUI(FeedbackFlow flow,
63 const std::string& extra_diagnostics,
64 bool from_assistant = false,
65 bool include_bluetooth_logs = false) {
66 base::Closure callback = base::Bind(&StopMessageLoopCallback);
67 extensions::FeedbackPrivateGetStringsFunction::set_test_callback(&callback);
68 InvokeFeedbackUI(flow, extra_diagnostics, from_assistant,
69 include_bluetooth_logs);
70 content::RunMessageLoop();
71 extensions::FeedbackPrivateGetStringsFunction::set_test_callback(NULL);
72 }
73
VerifyFeedbackAppLaunch()74 void VerifyFeedbackAppLaunch() {
75 AppWindow* window =
76 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
77 ASSERT_TRUE(window);
78 const Extension* feedback_app = window->GetExtension();
79 ASSERT_TRUE(feedback_app);
80 EXPECT_EQ(feedback_app->id(),
81 std::string(extension_misc::kFeedbackExtensionId));
82 }
83
84 private:
InvokeFeedbackUI(FeedbackFlow flow,const std::string & extra_diagnostics,bool from_assistant,bool include_bluetooth_logs)85 void InvokeFeedbackUI(FeedbackFlow flow,
86 const std::string& extra_diagnostics,
87 bool from_assistant,
88 bool include_bluetooth_logs) {
89 extensions::FeedbackPrivateAPI* api =
90 extensions::FeedbackPrivateAPI::GetFactoryInstance()->Get(
91 browser()->profile());
92 api->RequestFeedbackForFlow("Test description", "Test placeholder",
93 "Test tag", extra_diagnostics,
94 GURL("http://www.test.com"), flow,
95 from_assistant, include_bluetooth_logs);
96 }
97 };
98
99 class TestFeedbackUploaderDelegate
100 : public feedback::FeedbackUploaderChrome::Delegate {
101 public:
TestFeedbackUploaderDelegate(base::RunLoop * quit_on_dispatch)102 explicit TestFeedbackUploaderDelegate(base::RunLoop* quit_on_dispatch)
103 : quit_on_dispatch_(quit_on_dispatch) {}
104
OnStartDispatchingReport()105 void OnStartDispatchingReport() override { quit_on_dispatch_->Quit(); }
106
107 private:
108 base::RunLoop* quit_on_dispatch_;
109 };
110
IN_PROC_BROWSER_TEST_F(FeedbackTest,ShowFeedback)111 IN_PROC_BROWSER_TEST_F(FeedbackTest, ShowFeedback) {
112 WaitForExtensionViewsToLoad();
113
114 ASSERT_TRUE(IsFeedbackAppAvailable());
115 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_REGULAR, std::string());
116 VerifyFeedbackAppLaunch();
117 }
118
IN_PROC_BROWSER_TEST_F(FeedbackTest,ShowLoginFeedback)119 IN_PROC_BROWSER_TEST_F(FeedbackTest, ShowLoginFeedback) {
120 WaitForExtensionViewsToLoad();
121
122 ASSERT_TRUE(IsFeedbackAppAvailable());
123 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_LOGIN, std::string());
124 VerifyFeedbackAppLaunch();
125
126 AppWindow* const window =
127 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
128 ASSERT_TRUE(window);
129 content::WebContents* const content = window->web_contents();
130
131 bool bool_result = false;
132 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
133 content,
134 "domAutomationController.send("
135 "$('page-url').hidden && $('attach-file-container').hidden && "
136 "$('attach-file-note').hidden);",
137 &bool_result));
138 EXPECT_TRUE(bool_result);
139 }
140
141 // Tests that there's an option in the email drop down box with a value
142 // 'anonymous_user'.
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 == 'anonymous_user')"
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
IN_PROC_BROWSER_TEST_F(FeedbackTest,GetTargetTabUrl)286 IN_PROC_BROWSER_TEST_F(FeedbackTest, GetTargetTabUrl) {
287 const std::pair<std::string, std::string> test_cases[] = {
288 {"https://www.google.com/", "https://www.google.com/"},
289 {"about://version/", chrome::kChromeUIVersionURL},
290 {chrome::kChromeUIBookmarksURL, chrome::kChromeUIBookmarksURL},
291 };
292
293 for (const auto& test_case : test_cases) {
294 GURL expected_url = GURL(test_case.second);
295
296 ui_test_utils::NavigateToURL(browser(), GURL(test_case.first));
297
298 // Sanity check that we always have one tab in the browser.
299 ASSERT_EQ(browser()->tab_strip_model()->count(), 1);
300
301 ASSERT_EQ(expected_url,
302 browser()->tab_strip_model()->GetWebContentsAt(0)->GetURL());
303
304 ASSERT_EQ(expected_url,
305 chrome::GetTargetTabUrl(browser()->session_id(), 0));
306
307 // Open a DevTools window.
308 DevToolsWindow* devtools_window =
309 DevToolsWindowTesting::OpenDevToolsWindowSync(browser(), false);
310
311 // Verify the expected url returned from GetTargetTabUrl against a
312 // DevTools window.
313 ASSERT_EQ(expected_url, chrome::GetTargetTabUrl(
314 DevToolsWindowTesting::Get(devtools_window)
315 ->browser()
316 ->session_id(),
317 0));
318
319 DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window);
320 }
321 }
322
IN_PROC_BROWSER_TEST_F(FeedbackTest,SubmissionTest)323 IN_PROC_BROWSER_TEST_F(FeedbackTest, SubmissionTest) {
324 WaitForExtensionViewsToLoad();
325
326 ASSERT_TRUE(IsFeedbackAppAvailable());
327 StartFeedbackUI(FeedbackFlow::FEEDBACK_FLOW_GOOGLEINTERNAL, std::string());
328 VerifyFeedbackAppLaunch();
329
330 AppWindow* const window =
331 PlatformAppBrowserTest::GetFirstAppWindowForBrowser(browser());
332 ASSERT_TRUE(window);
333 content::WebContents* const content = window->web_contents();
334
335 // Set a delegate for the uploader which will be invoked when the report
336 // normally would have been uploaded. We have it setup to then quit the
337 // RunLoop which will then allow us to terminate.
338 base::RunLoop run_loop;
339 TestFeedbackUploaderDelegate delegate(&run_loop);
340 feedback::FeedbackUploaderFactoryChrome::GetInstance()
341 ->GetForBrowserContext(browser()->profile())
342 ->set_feedback_uploader_delegate(&delegate);
343
344 // Click the send button.
345 bool bool_result = false;
346 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
347 content,
348 "domAutomationController.send("
349 " ((function() {"
350 " if ($('send-report-button') != null) {"
351 " document.getElementById('send-report-button').click();"
352 " return true;"
353 " }"
354 " return false;"
355 " })()));",
356 &bool_result));
357 EXPECT_TRUE(bool_result);
358
359 // This will DCHECK if the JS private API call doesn't return a value, which
360 // is the main case we are concerned about.
361 run_loop.Run();
362 feedback::FeedbackUploaderFactoryChrome::GetInstance()
363 ->GetForBrowserContext(browser()->profile())
364 ->set_feedback_uploader_delegate(nullptr);
365 }
366
367 } // namespace extensions
368