[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 | |
| 5 | #include "base/command_line.h" |
| 6 | #include "base/strings/stringprintf.h" |
| 7 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 8 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 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" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 21 | #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] | f13ab89 | 2014-03-12 06:48:52 | [diff] [blame] | 24 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 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" |
| 29 | #include "url/gurl.h" |
| 30 | |
| 31 | using content::WebContents; |
| 32 | using extensions::Extension; |
| 33 | using extensions::TabHelper; |
| 34 | using extensions::WebstoreInlineInstaller; |
| 35 | using extensions::WebstoreInlineInstallerFactory; |
| 36 | using extensions::WebstoreStandaloneInstaller; |
| 37 | |
| 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) { |
| 49 | } |
| 50 | |
| 51 | WebstoreInstallerTest::~WebstoreInstallerTest() {} |
| 52 | |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 53 | void WebstoreInstallerTest::SetUpCommandLine(base::CommandLine* command_line) { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 54 | ExtensionBrowserTest::SetUpCommandLine(command_line); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 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()); |
| 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_); |
avi | 3ef9ec9e | 2014-12-22 22:50:17 | [diff] [blame] | 67 | base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 68 | 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 | |
| 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"); |
| 79 | } |
| 80 | |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 81 | void WebstoreInstallerTest::SetUpOnMainThread() { |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 82 | ExtensionBrowserTest::SetUpOnMainThread(); |
[email protected] | 987858a | 2014-05-10 05:55:32 | [diff] [blame] | 83 | 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] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 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())); |
| 96 | |
| 97 | GURL::Replacements replace_host; |
| 98 | replace_host.SetHostStr(domain); |
| 99 | return page_url.ReplaceComponents(replace_host); |
| 100 | } |
| 101 | |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame^] | 102 | void WebstoreInstallerTest::RunTest(WebContents* web_contents, |
| 103 | const std::string& test_function_name) { |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 104 | bool result = false; |
| 105 | std::string script = base::StringPrintf( |
| 106 | "%s('%s')", test_function_name.c_str(), |
| 107 | test_gallery_url_.c_str()); |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame^] | 108 | ASSERT_TRUE( |
| 109 | content::ExecuteScriptAndExtractBool(web_contents, script, &result)); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 110 | EXPECT_TRUE(result); |
| 111 | } |
| 112 | |
meacer | 92aa131 | 2015-02-17 22:36:13 | [diff] [blame^] | 113 | void WebstoreInstallerTest::RunTest(const std::string& test_function_name) { |
| 114 | RunTest(browser()->tab_strip_model()->GetActiveWebContents(), |
| 115 | test_function_name); |
| 116 | } |
| 117 | |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 118 | bool 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 | |
| 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()); |
[email protected] | f13ab89 | 2014-03-12 06:48:52 | [diff] [blame] | 136 | browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> |
zeeshanq | 3454e9c | 2014-09-04 21:30:28 | [diff] [blame] | 137 | ExecuteJavaScriptForTests(base::UTF8ToUTF16(script)); |
[email protected] | 3d6d67652 | 2013-10-14 20:44:55 | [diff] [blame] | 138 | } |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 139 | |
| 140 | void WebstoreInstallerTest::AutoAcceptInstall() { |
[email protected] | 2a74d6f6 | 2014-07-24 11:53:47 | [diff] [blame] | 141 | ExtensionInstallPrompt::g_auto_confirm_for_tests = |
| 142 | ExtensionInstallPrompt::ACCEPT; |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | void WebstoreInstallerTest::AutoCancelInstall() { |
[email protected] | 2a74d6f6 | 2014-07-24 11:53:47 | [diff] [blame] | 146 | ExtensionInstallPrompt::g_auto_confirm_for_tests = |
| 147 | ExtensionInstallPrompt::CANCEL; |
[email protected] | 9c7b309 | 2014-06-21 01:19:03 | [diff] [blame] | 148 | } |