Home | History | Annotate | Download | only in extensions
      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/extensions/extension_install_prompt.h"
      9 #include "chrome/browser/extensions/tab_helper.h"
     10 #include "chrome/browser/extensions/webstore_inline_installer.h"
     11 #include "chrome/browser/extensions/webstore_inline_installer_factory.h"
     12 #include "chrome/browser/extensions/webstore_installer_test.h"
     13 #include "chrome/browser/extensions/webstore_standalone_installer.h"
     14 #include "chrome/browser/profiles/profile.h"
     15 #include "chrome/browser/ui/browser.h"
     16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     17 #include "chrome/common/chrome_switches.h"
     18 #include "chrome/test/base/in_process_browser_test.h"
     19 #include "chrome/test/base/test_switches.h"
     20 #include "chrome/test/base/ui_test_utils.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_view_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"
     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 
     53 void WebstoreInstallerTest::SetUpCommandLine(CommandLine* command_line) {
     54   // We start the test server now instead of in
     55   // SetUpInProcessBrowserTestFixture so that we can get its port number.
     56   ASSERT_TRUE(test_server()->Start());
     57 
     58   net::HostPortPair host_port = test_server()->host_port_pair();
     59   test_gallery_url_ = base::StringPrintf(
     60       "http://%s:%d/files/%s",
     61       webstore_domain_.c_str(), host_port.port(), test_data_path_.c_str());
     62   command_line->AppendSwitchASCII(
     63       switches::kAppsGalleryURL, test_gallery_url_);
     64 
     65   GURL crx_url = GenerateTestServerUrl(webstore_domain_, crx_filename_);
     66   CommandLine::ForCurrentProcess()->AppendSwitchASCII(
     67       switches::kAppsGalleryUpdateURL, crx_url.spec());
     68 
     69   // Allow tests to call window.gc(), so that we can check that callback
     70   // functions don't get collected prematurely.
     71   command_line->AppendSwitchASCII(switches::kJavaScriptFlags, "--expose-gc");
     72 }
     73 
     74 void WebstoreInstallerTest::SetUpInProcessBrowserTestFixture() {
     75   host_resolver()->AddRule(webstore_domain_, "127.0.0.1");
     76   host_resolver()->AddRule(verified_domain_, "127.0.0.1");
     77   host_resolver()->AddRule(unverified_domain_, "127.0.0.1");
     78 }
     79 
     80 GURL WebstoreInstallerTest::GenerateTestServerUrl(
     81     const std::string& domain,
     82     const std::string& page_filename) {
     83   GURL page_url = test_server()->GetURL(
     84       base::StringPrintf("files/%s/%s",
     85                          test_data_path_.c_str(),
     86                          page_filename.c_str()));
     87 
     88   GURL::Replacements replace_host;
     89   replace_host.SetHostStr(domain);
     90   return page_url.ReplaceComponents(replace_host);
     91 }
     92 
     93 void WebstoreInstallerTest::RunTest(const std::string& test_function_name) {
     94   bool result = false;
     95   std::string script = base::StringPrintf(
     96       "%s('%s')", test_function_name.c_str(),
     97       test_gallery_url_.c_str());
     98   ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
     99       browser()->tab_strip_model()->GetActiveWebContents(),
    100       script,
    101       &result));
    102   EXPECT_TRUE(result);
    103 }
    104 
    105 bool WebstoreInstallerTest::RunIndexedTest(
    106     const std::string& test_function_name,
    107     int i) {
    108   std::string result = "FAILED";
    109   std::string script = base::StringPrintf("%s('%s', %d)",
    110       test_function_name.c_str(), test_gallery_url_.c_str(), i);
    111   EXPECT_TRUE(content::ExecuteScriptAndExtractString(
    112       browser()->tab_strip_model()->GetActiveWebContents(),
    113       script,
    114       &result));
    115   EXPECT_TRUE(result != "FAILED");
    116   return result == "KEEPGOING";
    117 }
    118 
    119 void WebstoreInstallerTest::RunTestAsync(
    120     const std::string& test_function_name) {
    121   std::string script = base::StringPrintf(
    122       "%s('%s')", test_function_name.c_str(), test_gallery_url_.c_str());
    123   browser()->tab_strip_model()->GetActiveWebContents()->GetRenderViewHost()->
    124       ExecuteJavascriptInWebFrame(
    125           UTF8ToUTF16(std::string()),
    126           UTF8ToUTF16(script));
    127 }
    128