[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 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 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 5 | #include "chrome/browser/extensions/webstore_installer_test.h" |
| 6 | #include "base/bind.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 7 | #include "base/command_line.h" |
| 8 | #include "base/strings/stringprintf.h" |
| 9 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 10 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 11 | #include "chrome/browser/extensions/extension_install_prompt.h" |
| 12 | #include "chrome/browser/extensions/tab_helper.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 13 | #include "chrome/browser/extensions/webstore_standalone_installer.h" |
| 14 | #include "chrome/browser/profiles/profile.h" |
| 15 | #include "chrome/browser/ui/browser.h" |
| 16 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 17 | #include "chrome/common/chrome_switches.h" |
| 18 | #include "chrome/test/base/in_process_browser_test.h" |
| 19 | #include "chrome/test/base/test_switches.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 20 | #include "content/public/browser/notification_registrar.h" |
| 21 | #include "content/public/browser/notification_service.h" |
| 22 | #include "content/public/browser/notification_types.h" |
[email protected] | f13ab89 | 2014-03-12 06:48:52 | [diff] [blame] | 23 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
brettw | 90e9260 | 2015-10-10 00:12:40 | [diff] [blame] | 25 | #include "content/public/common/content_switches.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 26 | #include "content/public/test/browser_test_utils.h" |
| 27 | #include "net/base/host_port_pair.h" |
| 28 | #include "net/dns/mock_host_resolver.h" |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 29 | #include "net/test/embedded_test_server/embedded_test_server.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 30 | #include "url/gurl.h" |
| 31 | |
| 32 | using content::WebContents; |
| 33 | using extensions::Extension; |
| 34 | using extensions::TabHelper; |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 35 | using extensions::WebstoreStandaloneInstaller; |
| 36 | |
robertshield | 1fc1a4a | 2017-02-01 15:18:33 | [diff] [blame] | 37 | using net::test_server::HttpRequest; |
| 38 | |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 39 | WebstoreInstallerTest::WebstoreInstallerTest( |
| 40 | const std::string& webstore_domain, |
| 41 | const std::string& test_data_path, |
| 42 | const std::string& crx_filename, |
| 43 | const std::string& verified_domain, |
| 44 | const std::string& unverified_domain) |
| 45 | : webstore_domain_(webstore_domain), |
| 46 | test_data_path_(test_data_path), |
| 47 | crx_filename_(crx_filename), |
| 48 | verified_domain_(verified_domain), |
| 49 | unverified_domain_(unverified_domain) { |
| 50 | } |
| 51 | |
| 52 | WebstoreInstallerTest::~WebstoreInstallerTest() {} |
| 53 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 54 | void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine* command_line) { |
Devlin Cronin | 836f545d | 2018-05-09 00:25:05 | [diff] [blame] | 55 | extensions::ExtensionBrowserTest::SetUpCommandLine(command_line); |
robertshield | 1fc1a4a | 2017-02-01 15:18:33 | [diff] [blame] | 56 | |
| 57 | embedded_test_server()->RegisterRequestMonitor(base::Bind( |
| 58 | &WebstoreInstallerTest::ProcessServerRequest, base::Unretained(this))); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 59 | // We start the test server now instead of in |
| 60 | // SetUpInProcessBrowserTestFixture so that we can get its port number. |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 61 | ASSERT_TRUE(embedded_test_server()->Start()); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 62 | |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 63 | net::HostPortPair host_port = embedded_test_server()->host_port_pair(); |
| 64 | test_gallery_url_ = |
| 65 | base::StringPrintf("http://%s:%d/%s", webstore_domain_.c_str(), |
| 66 | host_port.port(), test_data_path_.c_str()); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 67 | command_line->AppendSwitchASCII( |
| 68 | switches::kAppsGalleryURL, test_gallery_url_); |
| 69 | |
| 70 | GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_); |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 71 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 72 | switches::kAppsGalleryUpdateURL, crx_url.spec()); |
| 73 | |
| 74 | // Allow tests to call window.gc(), so that we can check that callback |
| 75 | // functions don't get collected prematurely. |
| 76 | command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc"); |
| 77 | } |
| 78 | |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 79 | void WebstoreInstallerTest::SetUpOnMainThread() { |
Devlin Cronin | 836f545d | 2018-05-09 00:25:05 | [diff] [blame] | 80 | extensions::ExtensionBrowserTest::SetUpOnMainThread(); |
jam | 1a5b558 | 2017-05-01 16:50:10 | [diff] [blame] | 81 | |
| 82 | host_resolver()->AddRule(webstore_domain_, "127.0.0.1"); |
| 83 | host_resolver()->AddRule(verified_domain_, "127.0.0.1"); |
| 84 | host_resolver()->AddRule(unverified_domain_, "127.0.0.1"); |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 87 | GURL WebstoreInstallerTest::GenerateTestServerUrl( |
| 88 | const std::string& domain, |
| 89 | const std::string& page_filename) { |
svaldez | a01f7d9 | 2015-11-18 17:47:56 | [diff] [blame] | 90 | GURL page_url = embedded_test_server()->GetURL(base::StringPrintf( |
| 91 | "/%s/%s", test_data_path_.c_str(), page_filename.c_str())); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 92 | |
| 93 | GURL::Replacements replace_host; |
| 94 | replace_host.SetHostStr(domain); |
| 95 | return page_url.ReplaceComponents(replace_host); |
| 96 | } |
| 97 | |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame] | 98 | void WebstoreInstallerTest::RunTest(WebContents* web_contents, |
| 99 | const std::string& test_function_name) { |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 100 | bool result = false; |
| 101 | std::string script = base::StringPrintf( |
| 102 | "%s('%s')", test_function_name.c_str(), |
| 103 | test_gallery_url_.c_str()); |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame] | 104 | ASSERT_TRUE( |
| 105 | content::ExecuteScriptAndExtractBool(web_contents, script, &result)); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 106 | EXPECT_TRUE(result); |
| 107 | } |
| 108 | |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame] | 109 | void WebstoreInstallerTest::RunTest(const std::string& test_function_name) { |
| 110 | RunTest(browser()->tab_strip_model()->GetActiveWebContents(), |
| 111 | test_function_name); |
| 112 | } |
| 113 | |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 114 | bool WebstoreInstallerTest::RunIndexedTest( |
| 115 | const std::string& test_function_name, |
| 116 | int i) { |
| 117 | std::string result = "FAILED"; |
| 118 | std::string script = base::StringPrintf("%s('%s', %d)", |
| 119 | test_function_name.c_str(), test_gallery_url_.c_str(), i); |
| 120 | EXPECT_TRUE(content::ExecuteScriptAndExtractString( |
| 121 | browser()->tab_strip_model()->GetActiveWebContents(), |
| 122 | script, |
| 123 | &result)); |
| 124 | EXPECT_TRUE(result != "FAILED"); |
| 125 | return result == "KEEPGOING"; |
| 126 | } |
| 127 | |
| 128 | void WebstoreInstallerTest::RunTestAsync( |
| 129 | const std::string& test_function_name) { |
| 130 | std::string script = base::StringPrintf( |
| 131 | "%s('%s')", test_function_name.c_str(), test_gallery_url_.c_str()); |
[email protected] | f13ab89 | 2014-03-12 06:48:52 | [diff] [blame] | 132 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> |
jochen | 2e7d95b | 2015-05-04 15:28:50 | [diff] [blame] | 133 | ExecuteJavaScriptWithUserGestureForTests(base::UTF8ToUTF16(script)); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 134 | } |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 135 | |
robertshield | 1fc1a4a | 2017-02-01 15:18:33 | [diff] [blame] | 136 | void WebstoreInstallerTest::ProcessServerRequest(const HttpRequest& request) {} |
| 137 | |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 138 | void WebstoreInstallerTest::AutoAcceptInstall() { |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 139 | install_auto_confirm_.reset(); // Destroy any old override first. |
| 140 | install_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm( |
| 141 | extensions::ScopedTestDialogAutoConfirm::ACCEPT)); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void WebstoreInstallerTest::AutoCancelInstall() { |
rdevlin.cronin | 6620949 | 2015-06-10 20:44:05 | [diff] [blame] | 145 | install_auto_confirm_.reset(); // Destroy any old override first. |
| 146 | install_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm( |
| 147 | extensions::ScopedTestDialogAutoConfirm::CANCEL)); |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 148 | } |