blob: c6d48bc24d679e0f162ef093c7d4cbbe93dd1e99 [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
5#include "base/command_line.h"
6#include "base/strings/stringprintf.h"
7#include "base/strings/utf_string_conversions.h"
[email protected]987858a2014-05-10 05:55:328#include "chrome/browser/download/download_prefs.h"
[email protected]3d6d676522013-10-14 20:44:559#include "chrome/browser/extensions/extension_install_prompt.h"
10#include "chrome/browser/extensions/tab_helper.h"
11#include "chrome/browser/extensions/webstore_inline_installer.h"
12#include "chrome/browser/extensions/webstore_inline_installer_factory.h"
13#include "chrome/browser/extensions/webstore_installer_test.h"
14#include "chrome/browser/extensions/webstore_standalone_installer.h"
15#include "chrome/browser/profiles/profile.h"
16#include "chrome/browser/ui/browser.h"
17#include "chrome/browser/ui/tabs/tab_strip_model.h"
18#include "chrome/common/chrome_switches.h"
19#include "chrome/test/base/in_process_browser_test.h"
20#include "chrome/test/base/test_switches.h"
[email protected]3d6d676522013-10-14 20:44:5521#include "content/public/browser/notification_registrar.h"
22#include "content/public/browser/notification_service.h"
23#include "content/public/browser/notification_types.h"
[email protected]f13ab892014-03-12 06:48:5224#include "content/public/browser/render_frame_host.h"
[email protected]3d6d676522013-10-14 20:44:5525#include "content/public/browser/web_contents.h"
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"
29#include "url/gurl.h"
30
31using content::WebContents;
32using extensions::Extension;
33using extensions::TabHelper;
34using extensions::WebstoreInlineInstaller;
35using extensions::WebstoreInlineInstallerFactory;
36using extensions::WebstoreStandaloneInstaller;
37
38WebstoreInstallerTest::WebstoreInstallerTest(
39 const std::string& webstore_domain,
40 const std::string& test_data_path,
41 const std::string& crx_filename,
42 const std::string& verified_domain,
43 const std::string& unverified_domain)
44 : webstore_domain_(webstore_domain),
45 test_data_path_(test_data_path),
46 crx_filename_(crx_filename),
47 verified_domain_(verified_domain),
48 unverified_domain_(unverified_domain) {
49}
50
51WebstoreInstallerTest::~WebstoreInstallerTest() {}
52
avi3ef9ec9e2014-12-22 22:50:1753void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine* command_line) {
[email protected]9c7b3092014-06-21 01:19:0354 ExtensionBrowserTest::SetUpCommandLine(command_line);
[email protected]3d6d676522013-10-14 20:44:5555 // We start the test server now instead of in
56 // SetUpInProcessBrowserTestFixture so that we can get its port number.
57 ASSERT_TRUE(test_server()->Start());
58
59 net::HostPortPair host_port = test_server()->host_port_pair();
60 test_gallery_url_ = base::StringPrintf(
61 "http://%s:%d/files/%s",
62 webstore_domain_.c_str(), host_port.port(), test_data_path_.c_str());
63 command_line->AppendSwitchASCII(
64 switches::kAppsGalleryURL, test_gallery_url_);
65
66 GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_);
avi3ef9ec9e2014-12-22 22:50:1767 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
[email protected]3d6d676522013-10-14 20:44:5568 switches::kAppsGalleryUpdateURL, crx_url.spec());
69
70 // Allow tests to call window.gc(), so that we can check that callback
71 // functions don't get collected prematurely.
72 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
73}
74
75void WebstoreInstallerTest::SetUpInProcessBrowserTestFixture() {
76 host_resolver()->AddRule(webstore_domain_, "127.0.0.1");
77 host_resolver()->AddRule(verified_domain_, "127.0.0.1");
78 host_resolver()->AddRule(unverified_domain_, "127.0.0.1");
79}
80
[email protected]987858a2014-05-10 05:55:3281void WebstoreInstallerTest::SetUpOnMainThread() {
[email protected]9c7b3092014-06-21 01:19:0382 ExtensionBrowserTest::SetUpOnMainThread();
[email protected]987858a2014-05-10 05:55:3283 ASSERT_TRUE(download_directory_.CreateUniqueTempDir());
84 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
85 browser()->profile());
86 download_prefs->SetDownloadPath(download_directory_.path());
87}
88
[email protected]3d6d676522013-10-14 20:44:5589GURL WebstoreInstallerTest::GenerateTestServerUrl(
90 const std::string& domain,
91 const std::string& page_filename) {
92 GURL page_url = test_server()->GetURL(
93 base::StringPrintf("files/%s/%s",
94 test_data_path_.c_str(),
95 page_filename.c_str()));
96
97 GURL::Replacements replace_host;
98 replace_host.SetHostStr(domain);
99 return page_url.ReplaceComponents(replace_host);
100}
101
meacer92aa1312015-02-17 22:36:13102void WebstoreInstallerTest::RunTest(WebContents* web_contents,
103 const std::string& test_function_name) {
[email protected]3d6d676522013-10-14 20:44:55104 bool result = false;
105 std::string script = base::StringPrintf(
106 "%s('%s')", test_function_name.c_str(),
107 test_gallery_url_.c_str());
meacer92aa1312015-02-17 22:36:13108 ASSERT_TRUE(
109 content::ExecuteScriptAndExtractBool(web_contents, script, &result));
[email protected]3d6d676522013-10-14 20:44:55110 EXPECT_TRUE(result);
111}
112
meacer92aa1312015-02-17 22:36:13113void WebstoreInstallerTest::RunTest(const std::string& test_function_name) {
114 RunTest(browser()->tab_strip_model()->GetActiveWebContents(),
115 test_function_name);
116}
117
[email protected]3d6d676522013-10-14 20:44:55118bool WebstoreInstallerTest::RunIndexedTest(
119 const std::string& test_function_name,
120 int i) {
121 std::string result = "FAILED";
122 std::string script = base::StringPrintf("%s('%s', %d)",
123 test_function_name.c_str(), test_gallery_url_.c_str(), i);
124 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
125 browser()->tab_strip_model()->GetActiveWebContents(),
126 script,
127 &result));
128 EXPECT_TRUE(result != "FAILED");
129 return result == "KEEPGOING";
130}
131
132void WebstoreInstallerTest::RunTestAsync(
133 const std::string& test_function_name) {
134 std::string script = base::StringPrintf(
135 "%s('%s')", test_function_name.c_str(), test_gallery_url_.c_str());
[email protected]f13ab892014-03-12 06:48:52136 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
zeeshanq3454e9c2014-09-04 21:30:28137 ExecuteJavaScriptForTests(base::UTF8ToUTF16(script));
[email protected]3d6d676522013-10-14 20:44:55138}
[email protected]9c7b3092014-06-21 01:19:03139
140void WebstoreInstallerTest::AutoAcceptInstall() {
[email protected]2a74d6f62014-07-24 11:53:47141 ExtensionInstallPrompt::g_auto_confirm_for_tests =
142 ExtensionInstallPrompt::ACCEPT;
[email protected]9c7b3092014-06-21 01:19:03143}
144
145void WebstoreInstallerTest::AutoCancelInstall() {
[email protected]2a74d6f62014-07-24 11:53:47146 ExtensionInstallPrompt::g_auto_confirm_for_tests =
147 ExtensionInstallPrompt::CANCEL;
[email protected]9c7b3092014-06-21 01:19:03148}