18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/files/file_path.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/path_service.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/crx_installer.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_error_reporter.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_install_prompt.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_service.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/extension_system.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/extensions/unpacked_installer.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/chrome_paths.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/notification_service.h"
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/render_view_host.h"
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/test/browser_test_utils.h"
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/test/test_utils.h"
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)using extensions::Extension;
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : profile_(NULL) {}
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)Profile* ExtensionSettingsUIBrowserTest::GetProfile() {
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!profile_) {
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    profile_ =
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        browser() ? browser()->profile() : ProfileManager::GetDefaultProfile();
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return profile_;
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebUIBrowserTest::SetUpOnMainThread();
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  observer_.reset(new ExtensionTestNotificationObserver(browser()));
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::FilePath test_data_dir;
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ADD_FAILURE();
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::FilePath extensions_data_dir = test_data_dir.AppendASCII("extensions");
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  InstallExtension(extensions_data_dir.AppendASCII("good.crx"));
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  explicit MockAutoConfirmExtensionInstallPrompt(
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      content::WebContents* web_contents)
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : ExtensionInstallPrompt(web_contents) {}
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Proceed without confirmation prompt.
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void ConfirmInstall(
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      Delegate* delegate,
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const Extension* extension,
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    delegate->InstallUIProceed();
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const Extension* ExtensionSettingsUIBrowserTest::InstallExtension(
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::FilePath& path) {
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Profile* profile = this->GetProfile();
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ExtensionService* service = profile->GetExtensionService();
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  service->set_show_extensions_prompts(false);
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t num_before = service->extensions()->size();
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  {
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    scoped_ptr<ExtensionInstallPrompt> install_ui;
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    install_ui.reset(new MockAutoConfirmExtensionInstallPrompt(
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        browser()->tab_strip_model()->GetActiveWebContents()));
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    base::FilePath crx_path = path;
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx"));
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (crx_path.empty())
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      return NULL;
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    scoped_refptr<extensions::CrxInstaller> installer(
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        extensions::CrxInstaller::Create(service, install_ui.Pass()));
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_expected_id(std::string());
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_is_gallery_install(false);
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_install_source(extensions::Manifest::INTERNAL);
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_install_wait_for_idle(false);
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_off_store_install_allow_reason(
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        extensions::CrxInstaller::OffStoreInstallAllowedInTest);
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    observer_->Watch(
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        chrome::NOTIFICATION_CRX_INSTALLER_DONE,
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        content::Source<extensions::CrxInstaller>(installer.get()));
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->InstallCrx(crx_path);
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    observer_->Wait();
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t num_after = service->extensions()->size();
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (num_before + 1 != num_after) {
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            << " num after: " << base::IntToString(num_after)
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            << " Installed extensions follow:";
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (ExtensionSet::const_iterator it = service->extensions()->begin();
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         it != service->extensions()->end(); ++it)
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      VLOG(1) << "  " << (*it)->id();
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    VLOG(1) << "Errors follow:";
118d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)    const std::vector<base::string16>* errors =
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        ExtensionErrorReporter::GetInstance()->GetErrors();
120d57369da7c6519fef57db42085f7b42d4c8845c1Torne (Richard Coles)    for (std::vector<base::string16>::const_iterator iter = errors->begin();
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         iter != errors->end(); ++iter)
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      VLOG(1) << *iter;
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NULL;
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!observer_->WaitForExtensionViewsToLoad())
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NULL;
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return service->GetExtensionById(last_loaded_extension_id(), false);
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
131