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/unpacked_installer.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/browser.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/chrome_paths.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/notification_service.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/browser/render_view_host.h"
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/test/browser_test_utils.h"
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "content/public/test/test_utils.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/browser/extension_system.h"
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "extensions/common/extension_set.h"
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)using extensions::Extension;
308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : profile_(NULL) {}
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)Profile* ExtensionSettingsUIBrowserTest::GetProfile() {
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!profile_) {
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    profile_ = browser() ? browser()->profile() :
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                           ProfileManager::GetActiveUserProfile();
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return profile_;
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  WebUIBrowserTest::SetUpOnMainThread();
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  observer_.reset(new ExtensionTestNotificationObserver(browser()));
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::FilePath test_data_dir;
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ADD_FAILURE();
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::FilePath extensions_data_dir = test_data_dir.AppendASCII("extensions");
568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  InstallExtension(extensions_data_dir.AppendASCII("good.crx"));
578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class MockAutoConfirmExtensionInstallPrompt : public ExtensionInstallPrompt {
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  explicit MockAutoConfirmExtensionInstallPrompt(
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      content::WebContents* web_contents)
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : ExtensionInstallPrompt(web_contents) {}
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Proceed without confirmation prompt.
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual void ConfirmInstall(
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      Delegate* delegate,
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const Extension* extension,
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const ShowDialogCallback& show_dialog_callback) OVERRIDE {
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    delegate->InstallUIProceed();
718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const Extension* ExtensionSettingsUIBrowserTest::InstallExtension(
758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const base::FilePath& path) {
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Profile* profile = this->GetProfile();
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ExtensionService* service =
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extensions::ExtensionSystem::Get(profile)->extension_service();
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  service->set_show_extensions_prompts(false);
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t num_before = service->extensions()->size();
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  {
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    scoped_ptr<ExtensionInstallPrompt> install_ui;
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    install_ui.reset(new MockAutoConfirmExtensionInstallPrompt(
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        browser()->tab_strip_model()->GetActiveWebContents()));
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    base::FilePath crx_path = path;
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx"));
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (crx_path.empty())
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      return NULL;
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    scoped_refptr<extensions::CrxInstaller> installer(
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        extensions::CrxInstaller::Create(service, install_ui.Pass()));
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_expected_id(std::string());
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_is_gallery_install(false);
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_install_source(extensions::Manifest::INTERNAL);
96f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    installer->set_install_immediately(true);
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->set_off_store_install_allow_reason(
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        extensions::CrxInstaller::OffStoreInstallAllowedInTest);
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    observer_->Watch(
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        extensions::NOTIFICATION_CRX_INSTALLER_DONE,
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        content::Source<extensions::CrxInstaller>(installer.get()));
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    installer->InstallCrx(crx_path);
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    observer_->Wait();
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t num_after = service->extensions()->size();
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (num_before + 1 != num_after) {
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    VLOG(1) << "Num extensions before: " << base::IntToString(num_before)
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            << " num after: " << base::IntToString(num_after)
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            << " Installed extensions follow:";
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (extensions::ExtensionSet::const_iterator it =
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)             service->extensions()->begin();
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         it != service->extensions()->end(); ++it)
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      VLOG(1) << "  " << (*it)->id();
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    VLOG(1) << "Errors follow:";
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const std::vector<base::string16>* errors =
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        ExtensionErrorReporter::GetInstance()->GetErrors();
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    for (std::vector<base::string16>::const_iterator iter = errors->begin();
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)         iter != errors->end(); ++iter)
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      VLOG(1) << *iter;
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NULL;
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!observer_->WaitForExtensionViewsToLoad())
1318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NULL;
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return service->GetExtensionById(last_loaded_extension_id(), false);
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
134