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.
5 #include "base/command_line.h"
6 #include "base/strings/stringprintf.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/download/download_prefs.h"
9 #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"
21 #include "content/public/browser/notification_registrar.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/notification_types.h"
24 #include "content/public/browser/render_frame_host.h"
25 #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"
31 using content::WebContents
;
32 using extensions::Extension
;
33 using extensions::TabHelper
;
34 using extensions::WebstoreInlineInstaller
;
35 using extensions::WebstoreInlineInstallerFactory
;
36 using extensions::WebstoreStandaloneInstaller
;
38 WebstoreInstallerTest::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
) {
51 WebstoreInstallerTest::~WebstoreInstallerTest() {}
53 void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine
* command_line
) {
54 ExtensionBrowserTest::SetUpCommandLine(command_line
);
55 // 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());
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_
);
66 GURL crx_url
= GenerateTestServerUrl(webstore_domain_
, crx_filename_
);
67 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
68 switches::kAppsGalleryUpdateURL
, crx_url
.spec());
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");
75 void 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");
81 void WebstoreInstallerTest::SetUpOnMainThread() {
82 ExtensionBrowserTest::SetUpOnMainThread();
83 ASSERT_TRUE(download_directory_
.CreateUniqueTempDir());
84 DownloadPrefs
* download_prefs
= DownloadPrefs::FromBrowserContext(
85 browser()->profile());
86 download_prefs
->SetDownloadPath(download_directory_
.path());
89 GURL
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()));
97 GURL::Replacements replace_host
;
98 replace_host
.SetHostStr(domain
);
99 return page_url
.ReplaceComponents(replace_host
);
102 void WebstoreInstallerTest::RunTest(WebContents
* web_contents
,
103 const std::string
& test_function_name
) {
105 std::string script
= base::StringPrintf(
106 "%s('%s')", test_function_name
.c_str(),
107 test_gallery_url_
.c_str());
109 content::ExecuteScriptAndExtractBool(web_contents
, script
, &result
));
113 void WebstoreInstallerTest::RunTest(const std::string
& test_function_name
) {
114 RunTest(browser()->tab_strip_model()->GetActiveWebContents(),
118 bool WebstoreInstallerTest::RunIndexedTest(
119 const std::string
& test_function_name
,
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(),
128 EXPECT_TRUE(result
!= "FAILED");
129 return result
== "KEEPGOING";
132 void 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());
136 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
137 ExecuteJavaScriptWithUserGestureForTests(base::UTF8ToUTF16(script
));
140 void WebstoreInstallerTest::AutoAcceptInstall() {
141 install_auto_confirm_
.reset(); // Destroy any old override first.
142 install_auto_confirm_
.reset(new extensions::ScopedTestDialogAutoConfirm(
143 extensions::ScopedTestDialogAutoConfirm::ACCEPT
));
146 void WebstoreInstallerTest::AutoCancelInstall() {
147 install_auto_confirm_
.reset(); // Destroy any old override first.
148 install_auto_confirm_
.reset(new extensions::ScopedTestDialogAutoConfirm(
149 extensions::ScopedTestDialogAutoConfirm::CANCEL
));