Home | History | Annotate | Download | only in extensions
      1 // Copyright 2014 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/bind.h"
      6 #include "base/run_loop.h"
      7 #include "chrome/browser/extensions/extension_service.h"
      8 #include "chrome/browser/extensions/webstore_installer_test.h"
      9 #include "chrome/browser/extensions/webstore_reinstaller.h"
     10 #include "chrome/browser/ui/browser.h"
     11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     12 #include "chrome/common/extensions/webstore_install_result.h"
     13 #include "extensions/browser/extension_registry.h"
     14 #include "extensions/common/extension.h"
     15 #include "extensions/common/extension_builder.h"
     16 #include "extensions/common/value_builder.h"
     17 
     18 namespace extensions {
     19 
     20 namespace {
     21 
     22 const char kWebstoreDomain[] = "cws.com";
     23 const char kAppDomain[] = "app.com";
     24 const char kNonAppDomain[] = "nonapp.com";
     25 const char kTestExtensionId[] = "ecglahbcnmdpdciemllbhojghbkagdje";
     26 const char kTestDataPath[] = "extensions/api_test/webstore_inline_install";
     27 const char kCrxFilename[] = "extension.crx";
     28 
     29 }  // namespace
     30 
     31 class WebstoreReinstallerBrowserTest : public WebstoreInstallerTest {
     32  public:
     33   WebstoreReinstallerBrowserTest()
     34       : WebstoreInstallerTest(
     35             kWebstoreDomain,
     36             kTestDataPath,
     37             kCrxFilename,
     38             kAppDomain,
     39             kNonAppDomain) {}
     40   virtual ~WebstoreReinstallerBrowserTest() {}
     41 
     42   void OnInstallCompletion(const base::Closure& quit_closure,
     43                            bool success,
     44                            const std::string& error,
     45                            webstore_install::Result result);
     46 
     47   bool last_install_result() const { return last_install_result_; }
     48 
     49  private:
     50   bool last_install_result_;
     51 };
     52 
     53 void WebstoreReinstallerBrowserTest::OnInstallCompletion(
     54     const base::Closure& quit_closure,
     55     bool success,
     56     const std::string& error,
     57     webstore_install::Result result) {
     58   last_install_result_ = success;
     59   quit_closure.Run();
     60 }
     61 
     62 IN_PROC_BROWSER_TEST_F(WebstoreReinstallerBrowserTest, TestWebstoreReinstall) {
     63   // Build an extension with the same id as our test extension and add it.
     64   const std::string kExtensionName("ReinstallerExtension");
     65   scoped_refptr<const Extension> extension =
     66       ExtensionBuilder().SetLocation(Manifest::INTERNAL)
     67                       .SetID(kTestExtensionId)
     68                       .SetManifest(
     69                           DictionaryBuilder().Set("name", kExtensionName)
     70                                              .Set("description", "Foo")
     71                                              .Set("manifest_version", 2)
     72                                              .Set("version", "1.0")
     73                                              .Build())
     74                       .Build();
     75   extension_service()->AddExtension(extension.get());
     76   ExtensionRegistry* registry = ExtensionRegistry::Get(profile());
     77   ASSERT_TRUE(registry->enabled_extensions().GetByID(kTestExtensionId));
     78 
     79   content::WebContents* active_web_contents =
     80       browser()->tab_strip_model()->GetActiveWebContents();
     81   ASSERT_TRUE(active_web_contents);
     82 
     83   // Start by canceling the repair prompt.
     84   AutoCancelInstall();
     85 
     86   // Create and run a WebstoreReinstaller.
     87   base::RunLoop run_loop;
     88   scoped_refptr<WebstoreReinstaller> reinstaller(
     89       new WebstoreReinstaller(
     90           active_web_contents,
     91           kTestExtensionId,
     92           base::Bind(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
     93                      base::Unretained(this),
     94                      run_loop.QuitClosure())));
     95   reinstaller->BeginReinstall();
     96   run_loop.Run();
     97 
     98   // We should have failed, and the old extension should still be present.
     99   EXPECT_FALSE(last_install_result());
    100   extension = registry->enabled_extensions().GetByID(kTestExtensionId);
    101   ASSERT_TRUE(extension.get());
    102   EXPECT_EQ(kExtensionName, extension->name());
    103 
    104   // Now accept the repair prompt.
    105   AutoAcceptInstall();
    106   base::RunLoop run_loop2;
    107   reinstaller =
    108       new WebstoreReinstaller(
    109           active_web_contents,
    110           kTestExtensionId,
    111           base::Bind(&WebstoreReinstallerBrowserTest::OnInstallCompletion,
    112                      base::Unretained(this),
    113                      run_loop2.QuitClosure()));
    114   reinstaller->BeginReinstall();
    115   run_loop2.Run();
    116 
    117   // The reinstall should have succeeded, and the extension should have been
    118   // "updated" (which in this case means that it should have been replaced with
    119   // the inline install test extension, since that's the id we used).
    120   EXPECT_TRUE(last_install_result());
    121   extension = registry->enabled_extensions().GetByID(kTestExtensionId);
    122   ASSERT_TRUE(extension.get());
    123   // The name should not match, since the extension changed.
    124   EXPECT_NE(kExtensionName, extension->name());
    125 }
    126 
    127 }  // namespace extensions
    128