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/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 "chrome/test/base/ui_test_utils.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_types.h"
25 #include "content/public/browser/render_frame_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "net/base/host_port_pair.h"
29 #include "net/dns/mock_host_resolver.h"
30 #include "url/gurl.h"
31
32 using content::WebContents;
33 using extensions::Extension;
34 using extensions::TabHelper;
35 using extensions::WebstoreInlineInstaller;
36 using extensions::WebstoreInlineInstallerFactory;
37 using extensions::WebstoreStandaloneInstaller;
38
WebstoreInstallerTest(const std::string & webstore_domain,const std::string & test_data_path,const std::string & crx_filename,const std::string & verified_domain,const std::string & unverified_domain)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
~WebstoreInstallerTest()52 WebstoreInstallerTest::~WebstoreInstallerTest() {}
53
SetUpCommandLine(CommandLine * command_line)54 void WebstoreInstallerTest::SetUpCommandLine(CommandLine* command_line) {
55 ExtensionBrowserTest::SetUpCommandLine(command_line);
56 // We start the test server now instead of in
57 // SetUpInProcessBrowserTestFixture so that we can get its port number.
58 ASSERT_TRUE(test_server()->Start());
59
60 net::HostPortPair host_port = test_server()->host_port_pair();
61 test_gallery_url_ = base::StringPrintf(
62 "http://%s:%d/files/%s",
63 webstore_domain_.c_str(), host_port.port(), test_data_path_.c_str());
64 command_line->AppendSwitchASCII(
65 switches::kAppsGalleryURL, test_gallery_url_);
66
67 GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_);
68 CommandLine::ForCurrentProcess()->AppendSwitchASCII(
69 switches::kAppsGalleryUpdateURL, crx_url.spec());
70
71 // Allow tests to call window.gc(), so that we can check that callback
72 // functions don't get collected prematurely.
73 command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
74 }
75
SetUpInProcessBrowserTestFixture()76 void WebstoreInstallerTest::SetUpInProcessBrowserTestFixture() {
77 host_resolver()->AddRule(webstore_domain_, "127.0.0.1");
78 host_resolver()->AddRule(verified_domain_, "127.0.0.1");
79 host_resolver()->AddRule(unverified_domain_, "127.0.0.1");
80 }
81
SetUpOnMainThread()82 void WebstoreInstallerTest::SetUpOnMainThread() {
83 ExtensionBrowserTest::SetUpOnMainThread();
84 ASSERT_TRUE(download_directory_.CreateUniqueTempDir());
85 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
86 browser()->profile());
87 download_prefs->SetDownloadPath(download_directory_.path());
88 }
89
GenerateTestServerUrl(const std::string & domain,const std::string & page_filename)90 GURL WebstoreInstallerTest::GenerateTestServerUrl(
91 const std::string& domain,
92 const std::string& page_filename) {
93 GURL page_url = test_server()->GetURL(
94 base::StringPrintf("files/%s/%s",
95 test_data_path_.c_str(),
96 page_filename.c_str()));
97
98 GURL::Replacements replace_host;
99 replace_host.SetHostStr(domain);
100 return page_url.ReplaceComponents(replace_host);
101 }
102
RunTest(const std::string & test_function_name)103 void WebstoreInstallerTest::RunTest(const std::string& test_function_name) {
104 bool result = false;
105 std::string script = base::StringPrintf(
106 "%s('%s')", test_function_name.c_str(),
107 test_gallery_url_.c_str());
108 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
109 browser()->tab_strip_model()->GetActiveWebContents(),
110 script,
111 &result));
112 EXPECT_TRUE(result);
113 }
114
RunIndexedTest(const std::string & test_function_name,int i)115 bool WebstoreInstallerTest::RunIndexedTest(
116 const std::string& test_function_name,
117 int i) {
118 std::string result = "FAILED";
119 std::string script = base::StringPrintf("%s('%s', %d)",
120 test_function_name.c_str(), test_gallery_url_.c_str(), i);
121 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
122 browser()->tab_strip_model()->GetActiveWebContents(),
123 script,
124 &result));
125 EXPECT_TRUE(result != "FAILED");
126 return result == "KEEPGOING";
127 }
128
RunTestAsync(const std::string & test_function_name)129 void WebstoreInstallerTest::RunTestAsync(
130 const std::string& test_function_name) {
131 std::string script = base::StringPrintf(
132 "%s('%s')", test_function_name.c_str(), test_gallery_url_.c_str());
133 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()->
134 ExecuteJavaScriptForTests(base::UTF8ToUTF16(script));
135 }
136
AutoAcceptInstall()137 void WebstoreInstallerTest::AutoAcceptInstall() {
138 ExtensionInstallPrompt::g_auto_confirm_for_tests =
139 ExtensionInstallPrompt::ACCEPT;
140 }
141
AutoCancelInstall()142 void WebstoreInstallerTest::AutoCancelInstall() {
143 ExtensionInstallPrompt::g_auto_confirm_for_tests =
144 ExtensionInstallPrompt::CANCEL;
145 }
146