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.
5 #include "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h"
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_install_prompt_show_params.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/unpacked_installer.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "chrome/common/chrome_paths.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/render_view_host.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "content/public/test/test_utils.h"
27 #include "extensions/browser/api/management/management_api.h"
28 #include "extensions/browser/extension_registry.h"
29 #include "extensions/browser/extension_system.h"
30 #include "extensions/browser/test_extension_registry_observer.h"
31 #include "extensions/common/extension_set.h"
33 using extensions::Extension
;
34 using extensions::TestManagementPolicyProvider
;
36 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
38 policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
|
39 TestManagementPolicyProvider::MUST_REMAIN_ENABLED
|
40 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED
) {
41 CHECK(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
));
42 test_data_dir_
= test_data_dir_
.AppendASCII("extensions");
45 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
47 Profile
* ExtensionSettingsUIBrowserTest::GetProfile() {
49 profile_
= browser() ? browser()->profile() :
50 ProfileManager::GetActiveUserProfile();
55 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
56 WebUIBrowserTest::SetUpOnMainThread();
57 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
60 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
61 EXPECT_TRUE(InstallExtension(test_data_dir_
.AppendASCII("good.crx")));
64 void ExtensionSettingsUIBrowserTest::InstallErrorsExtension() {
65 EXPECT_TRUE(InstallUnpackedExtension(
66 test_data_dir_
.AppendASCII("error_console")
67 .AppendASCII("runtime_and_manifest_errors")));
70 void ExtensionSettingsUIBrowserTest::InstallSharedModule() {
71 base::FilePath shared_module_path
=
72 test_data_dir_
.AppendASCII("api_test").AppendASCII("shared_module");
73 EXPECT_TRUE(InstallUnpackedExtension(
74 shared_module_path
.AppendASCII("shared")));
75 EXPECT_TRUE(InstallUnpackedExtension(
76 shared_module_path
.AppendASCII("import_pass")));
79 void ExtensionSettingsUIBrowserTest::InstallPackagedApp() {
80 EXPECT_TRUE(InstallUnpackedExtension(
81 test_data_dir_
.AppendASCII("packaged_app")));
84 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() {
85 auto* extension_service
= extensions::ExtensionSystem::Get(GetProfile());
86 extension_service
->management_policy()->RegisterProvider(&policy_provider_
);
89 void ExtensionSettingsUIBrowserTest::SetAutoConfirmUninstall() {
90 extensions::ManagementUninstallFunctionBase::SetAutoConfirmForTest(true);
93 void ExtensionSettingsUIBrowserTest::EnableErrorConsole() {
94 error_console_override_
.reset(new extensions::FeatureSwitch::ScopedOverride(
95 extensions::FeatureSwitch::error_console(), true));
98 class MockAutoConfirmExtensionInstallPrompt
: public ExtensionInstallPrompt
{
100 explicit MockAutoConfirmExtensionInstallPrompt(
101 content::WebContents
* web_contents
)
102 : ExtensionInstallPrompt(web_contents
) {}
104 // Proceed without confirmation prompt.
105 void ConfirmInstall(Delegate
* delegate
,
106 const Extension
* extension
,
107 const ShowDialogCallback
& show_dialog_callback
) override
{
108 delegate
->InstallUIProceed();
112 const Extension
* ExtensionSettingsUIBrowserTest::InstallUnpackedExtension(
113 const base::FilePath
& path
) {
117 Profile
* profile
= GetProfile();
118 ExtensionService
* service
=
119 extensions::ExtensionSystem::Get(profile
)->extension_service();
120 service
->set_show_extensions_prompts(false);
121 extensions::ExtensionRegistry
* registry
=
122 extensions::ExtensionRegistry::Get(profile
);
123 extensions::TestExtensionRegistryObserver
observer(registry
);
124 extensions::UnpackedInstaller::Create(service
)->Load(path
);
125 base::FilePath extension_path
= base::MakeAbsoluteFilePath(path
);
126 const Extension
* extension
= nullptr;
128 extension
= observer
.WaitForExtensionLoaded();
129 } while (extension
->path() != extension_path
);
134 const Extension
* ExtensionSettingsUIBrowserTest::InstallExtension(
135 const base::FilePath
& path
) {
136 Profile
* profile
= GetProfile();
137 ExtensionService
* service
=
138 extensions::ExtensionSystem::Get(profile
)->extension_service();
139 extensions::ExtensionRegistry
* registry
=
140 extensions::ExtensionRegistry::Get(profile
);
141 service
->set_show_extensions_prompts(false);
142 size_t num_before
= registry
->enabled_extensions().size();
144 scoped_ptr
<ExtensionInstallPrompt
> install_ui
;
145 install_ui
.reset(new MockAutoConfirmExtensionInstallPrompt(
146 browser()->tab_strip_model()->GetActiveWebContents()));
148 base::FilePath crx_path
= path
;
149 DCHECK(crx_path
.Extension() == FILE_PATH_LITERAL(".crx"));
150 if (crx_path
.empty())
153 scoped_refptr
<extensions::CrxInstaller
> installer(
154 extensions::CrxInstaller::Create(service
, install_ui
.Pass()));
155 installer
->set_expected_id(std::string());
156 installer
->set_is_gallery_install(false);
157 installer
->set_install_source(extensions::Manifest::INTERNAL
);
158 installer
->set_install_immediately(true);
159 installer
->set_off_store_install_allow_reason(
160 extensions::CrxInstaller::OffStoreInstallAllowedInTest
);
163 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
164 content::Source
<extensions::CrxInstaller
>(installer
.get()));
166 installer
->InstallCrx(crx_path
);
171 size_t num_after
= registry
->enabled_extensions().size();
172 if (num_before
+ 1 != num_after
) {
173 VLOG(1) << "Num extensions before: " << base::IntToString(num_before
)
174 << " num after: " << base::IntToString(num_after
)
175 << " Installed extensions follow:";
177 for (const scoped_refptr
<const Extension
>& extension
:
178 registry
->enabled_extensions())
179 VLOG(1) << " " << extension
->id();
181 VLOG(1) << "Errors follow:";
182 const std::vector
<base::string16
>* errors
=
183 ExtensionErrorReporter::GetInstance()->GetErrors();
184 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
185 iter
!= errors
->end(); ++iter
)
191 if (!observer_
->WaitForExtensionViewsToLoad())
193 return service
->GetExtensionById(last_loaded_extension_id(), false);