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 "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h"
      6 
      7 #include "base/files/file_path.h"
      8 #include "base/path_service.h"
      9 #include "base/strings/string_number_conversions.h"
     10 #include "chrome/browser/chrome_notification_types.h"
     11 #include "chrome/browser/extensions/crx_installer.h"
     12 #include "chrome/browser/extensions/extension_error_reporter.h"
     13 #include "chrome/browser/extensions/extension_install_prompt.h"
     14 #include "chrome/browser/extensions/extension_service.h"
     15 #include "chrome/browser/extensions/unpacked_installer.h"
     16 #include "chrome/browser/profiles/profile.h"
     17 #include "chrome/browser/profiles/profile_manager.h"
     18 #include "chrome/browser/ui/browser.h"
     19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
     20 #include "chrome/common/chrome_paths.h"
     21 #include "content/public/browser/notification_registrar.h"
     22 #include "content/public/browser/notification_service.h"
     23 #include "content/public/browser/render_view_host.h"
     24 #include "content/public/test/browser_test_utils.h"
     25 #include "content/public/test/test_utils.h"
     26 #include "extensions/browser/extension_system.h"
     27 #include "extensions/common/extension_set.h"
     28 
     29 using extensions::Extension;
     30 
     31 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
     32     : profile_(NULL) {}
     33 
     34 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
     35 
     36 Profile* ExtensionSettingsUIBrowserTest::GetProfile() {
     37   if (!profile_) {
     38     profile_ = browser() ? browser()->profile() :
     39                            ProfileManager::GetActiveUserProfile();
     40   }
     41   return profile_;
     42 }
     43 
     44 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
     45   WebUIBrowserTest::SetUpOnMainThread();
     46   observer_.reset(new ExtensionTestNotificationObserver(browser()));
     47 }
     48 
     49 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
     50   base::FilePath test_data_dir;
     51   if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
     52     ADD_FAILURE();
     53     return;
     54   }
     55   base::FilePath extensions_data_dir = test_data_dir.AppendASCII("extensions");
     56   InstallExtension(extensions_data_dir.AppendASCII("good.crx"));
     57 }
     58 
     59 class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
     60  public:
     61   explicit MockAutoConfirmExtensionInstallPrompt(
     62       content::WebContents* web_contents)
     63     : ExtensionInstallPrompt(web_contents) {}
     64 
     65   // Proceed without confirmation prompt.
     66   virtual void ConfirmInstall(
     67       Delegate* delegate,
     68       const Extension* extension,
     69       const ShowDialogCallback& show_dialog_callback) OVERRIDE {
     70     delegate->InstallUIProceed();
     71   }
     72 };
     73 
     74 const Extension* ExtensionSettingsUIBrowserTest::InstallExtension(
     75     const base::FilePath& path) {
     76   Profile* profile = this->GetProfile();
     77   ExtensionService* service =
     78       extensions::ExtensionSystem::Get(profile)->extension_service();
     79   service->set_show_extensions_prompts(false);
     80   size_t num_before = service->extensions()->size();
     81   {
     82     scoped_ptr<ExtensionInstallPrompt> install_ui;
     83     install_ui.reset(new MockAutoConfirmExtensionInstallPrompt(
     84         browser()->tab_strip_model()->GetActiveWebContents()));
     85 
     86     base::FilePath crx_path = path;
     87     DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx"));
     88     if (crx_path.empty())
     89       return NULL;
     90 
     91     scoped_refptr<extensions::CrxInstaller> installer(
     92         extensions::CrxInstaller::Create(service, install_ui.Pass()));
     93     installer->set_expected_id(std::string());
     94     installer->set_is_gallery_install(false);
     95     installer->set_install_source(extensions::Manifest::INTERNAL);
     96     installer->set_install_immediately(true);
     97     installer->set_off_store_install_allow_reason(
     98         extensions::CrxInstaller::OffStoreInstallAllowedInTest);
     99 
    100     observer_->Watch(
    101         extensions::NOTIFICATION_CRX_INSTALLER_DONE,
    102         content::Source<extensions::CrxInstaller>(installer.get()));
    103 
    104     installer->InstallCrx(crx_path);
    105 
    106     observer_->Wait();
    107   }
    108 
    109   size_t num_after = service->extensions()->size();
    110   if (num_before + 1 != num_after) {
    111     VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
    112             << " num after: " << base::IntToString(num_after)
    113             << " Installed extensions follow:";
    114 
    115     for (extensions::ExtensionSet::const_iterator it =
    116              service->extensions()->begin();
    117          it != service->extensions()->end(); ++it)
    118       VLOG(1) << "  " << (*it)->id();
    119 
    120     VLOG(1) << "Errors follow:";
    121     const std::vector<base::string16>* errors =
    122         ExtensionErrorReporter::GetInstance()->GetErrors();
    123     for (std::vector<base::string16>::const_iterator iter = errors->begin();
    124          iter != errors->end(); ++iter)
    125       VLOG(1) << *iter;
    126 
    127     return NULL;
    128   }
    129 
    130   if (!observer_->WaitForExtensionViewsToLoad())
    131     return NULL;
    132   return service->GetExtensionById(last_loaded_extension_id(), false);
    133 }
    134