blob: 1a5f2369d432c961e4f1090a2c42f3713e720e99 [file] [log] [blame]
[email protected]3d6d676522013-10-14 20:44:551// 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 Marchandf1349f52019-01-25 03:16:415#include "chrome/browser/extensions/webstore_installer_test.h"
6#include "base/bind.h"
[email protected]3d6d676522013-10-14 20:44:557#include "base/command_line.h"
8#include "base/strings/stringprintf.h"
9#include "base/strings/utf_string_conversions.h"
[email protected]987858a2014-05-10 05:55:3210#include "chrome/browser/download/download_prefs.h"
[email protected]3d6d676522013-10-14 20:44:5511#include "chrome/browser/extensions/extension_install_prompt.h"
12#include "chrome/browser/extensions/tab_helper.h"
[email protected]3d6d676522013-10-14 20:44:5513#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]3d6d676522013-10-14 20:44:5520#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]f13ab892014-03-12 06:48:5223#include "content/public/browser/render_frame_host.h"
[email protected]3d6d676522013-10-14 20:44:5524#include "content/public/browser/web_contents.h"
brettw90e92602015-10-10 00:12:4025#include "content/public/common/content_switches.h"
[email protected]3d6d676522013-10-14 20:44:5526#include "content/public/test/browser_test_utils.h"
27#include "net/base/host_port_pair.h"
28#include "net/dns/mock_host_resolver.h"
svaldeza01f7d92015-11-18 17:47:5629#include "net/test/embedded_test_server/embedded_test_server.h"
[email protected]3d6d676522013-10-14 20:44:5530#include "url/gurl.h"
31
32using content::WebContents;
33using extensions::Extension;
34using extensions::TabHelper;
[email protected]3d6d676522013-10-14 20:44:5535using extensions::WebstoreStandaloneInstaller;
36
robertshield1fc1a4a2017-02-01 15:18:3337using net::test_server::HttpRequest;
38
[email protected]3d6d676522013-10-14 20:44:5539WebstoreInstallerTest::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
52WebstoreInstallerTest::~WebstoreInstallerTest() {}
53
avi3ef9ec9e2014-12-22 22:50:1754void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine* command_line) {
Devlin Cronin836f545d2018-05-09 00:25:0555 extensions::ExtensionBrowserTest::SetUpCommandLine(command_line);
robertshield1fc1a4a2017-02-01 15:18:3356
57 embedded_test_server()->RegisterRequestMonitor(base::Bind(
58 &WebstoreInstallerTest::ProcessServerRequest, base::Unretained(this)));
[email protected]3d6d676522013-10-14 20:44:5559 // We start the test server now instead of in
60 // SetUpInProcessBrowserTestFixture so that we can get its port number.
svaldeza01f7d92015-11-18 17:47:5661 ASSERT_TRUE(embedded_test_server()->Start());
[email protected]3d6d676522013-10-14 20:44:5562
svaldeza01f7d92015-11-18 17:47:5663 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]3d6d676522013-10-14 20:44:5567 command_line->AppendSwitchASCII(
68 switches::kAppsGalleryURL, test_gallery_url_);
69
70 GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_);
avi3ef9ec9e2014-12-22 22:50:1771 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]3d6d676522013-10-14 20:44:5572 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]987858a2014-05-10 05:55:3279void WebstoreInstallerTest::SetUpOnMainThread() {
Devlin Cronin836f545d2018-05-09 00:25:0580 extensions::ExtensionBrowserTest::SetUpOnMainThread();
jam1a5b5582017-05-01 16:50:1081
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]987858a2014-05-10 05:55:3285}
86
[email protected]3d6d676522013-10-14 20:44:5587GURL WebstoreInstallerTest::GenerateTestServerUrl(
88 const std::string& domain,
89 const std::string& page_filename) {
svaldeza01f7d92015-11-18 17:47:5690 GURL page_url = embedded_test_server()->GetURL(base::StringPrintf(
91 "/%s/%s", test_data_path_.c_str(), page_filename.c_str()));
[email protected]3d6d676522013-10-14 20:44:5592
93 GURL::Replacements replace_host;
94 replace_host.SetHostStr(domain);
95 return page_url.ReplaceComponents(replace_host);
96}
97
meacer92aa1312015-02-17 22:36:1398void WebstoreInstallerTest::RunTest(WebContents* web_contents,
99 const std::string& test_function_name) {
[email protected]3d6d676522013-10-14 20:44:55100 bool result = false;
101 std::string script = base::StringPrintf(
102 "%s('%s')", test_function_name.c_str(),
103 test_gallery_url_.c_str());
meacer92aa1312015-02-17 22:36:13104 ASSERT_TRUE(
105 content::ExecuteScriptAndExtractBool(web_contents, script, &result));
[email protected]3d6d676522013-10-14 20:44:55106 EXPECT_TRUE(result);
107}
108
meacer92aa1312015-02-17 22:36:13109void WebstoreInstallerTest::RunTest(const std::string& test_function_name) {
110 RunTest(browser()->tab_strip_model()->GetActiveWebContents(),
111 test_function_name);
112}
113
[email protected]3d6d676522013-10-14 20:44:55114bool 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
128void 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]f13ab892014-03-12 06:48:52132 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
jochen2e7d95b2015-05-04 15:28:50133 ExecuteJavaScriptWithUserGestureForTests(base::UTF8ToUTF16(script));
[email protected]3d6d676522013-10-14 20:44:55134}
[email protected]9c7b3092014-06-21 01:19:03135
robertshield1fc1a4a2017-02-01 15:18:33136void WebstoreInstallerTest::ProcessServerRequest(const HttpRequest& request) {}
137
[email protected]9c7b3092014-06-21 01:19:03138void WebstoreInstallerTest::AutoAcceptInstall() {
rdevlin.cronin66209492015-06-10 20:44:05139 install_auto_confirm_.reset(); // Destroy any old override first.
140 install_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm(
141 extensions::ScopedTestDialogAutoConfirm::ACCEPT));
[email protected]9c7b3092014-06-21 01:19:03142}
143
144void WebstoreInstallerTest::AutoCancelInstall() {
rdevlin.cronin66209492015-06-10 20:44:05145 install_auto_confirm_.reset(); // Destroy any old override first.
146 install_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm(
147 extensions::ScopedTestDialogAutoConfirm::CANCEL));
[email protected]9c7b3092014-06-21 01:19:03148}