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 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 52 WebstoreInstallerTest::~WebstoreInstallerTest() {} 53 54 void WebstoreInstallerTest::SetUpCommandLine(CommandLine* 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()); 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_); 67 CommandLine::ForCurrentProcess()->AppendSwitchASCII( 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 81 void WebstoreInstallerTest::SetUpOnMainThread() { 82 InProcessBrowserTest::SetUpOnMainThread(); 83 ASSERT_TRUE(download_directory_.CreateUniqueTempDir()); 84 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( 85 browser()->profile()); 86 download_prefs->SetDownloadPath(download_directory_.path()); 87 } 88 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 102 void WebstoreInstallerTest::RunTest(const std::string& test_function_name) { 103 bool result = false; 104 std::string script = base::StringPrintf( 105 "%s('%s')", test_function_name.c_str(), 106 test_gallery_url_.c_str()); 107 ASSERT_TRUE(content::ExecuteScriptAndExtractBool( 108 browser()->tab_strip_model()->GetActiveWebContents(), 109 script, 110 &result)); 111 EXPECT_TRUE(result); 112 } 113 114 bool 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 128 void 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()); 132 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()-> 133 ExecuteJavaScript(base::UTF8ToUTF16(script)); 134 } 135