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/browser_window.h"
21 #include "chrome/browser/ui/tabs/tab_strip_model.h"
22 #include "chrome/browser/ui/web_contents_sizer.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "content/public/test/test_utils.h"
29 #include "extensions/browser/extension_registry.h"
30 #include "extensions/browser/extension_system.h"
31 #include "extensions/browser/test_extension_registry_observer.h"
32 #include "extensions/common/extension_set.h"
34 using extensions::Extension
;
35 using extensions::TestManagementPolicyProvider
;
37 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
39 policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
|
40 TestManagementPolicyProvider::MUST_REMAIN_ENABLED
|
41 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED
) {
42 CHECK(PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
));
43 test_data_dir_
= test_data_dir_
.AppendASCII("extensions");
46 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
48 Profile
* ExtensionSettingsUIBrowserTest::GetProfile() {
50 profile_
= browser() ? browser()->profile() :
51 ProfileManager::GetActiveUserProfile();
56 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
57 WebUIBrowserTest::SetUpOnMainThread();
58 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
61 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
62 EXPECT_TRUE(InstallExtension(test_data_dir_
.AppendASCII("good.crx")));
65 void ExtensionSettingsUIBrowserTest::InstallErrorsExtension() {
66 EXPECT_TRUE(InstallUnpackedExtension(
67 test_data_dir_
.AppendASCII("error_console")
68 .AppendASCII("runtime_and_manifest_errors")));
71 void ExtensionSettingsUIBrowserTest::InstallSharedModule() {
72 base::FilePath shared_module_path
=
73 test_data_dir_
.AppendASCII("api_test").AppendASCII("shared_module");
74 EXPECT_TRUE(InstallUnpackedExtension(
75 shared_module_path
.AppendASCII("shared")));
76 EXPECT_TRUE(InstallUnpackedExtension(
77 shared_module_path
.AppendASCII("import_pass")));
80 void ExtensionSettingsUIBrowserTest::InstallPackagedApp() {
81 EXPECT_TRUE(InstallUnpackedExtension(
82 test_data_dir_
.AppendASCII("packaged_app")));
85 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() {
86 auto* extension_service
= extensions::ExtensionSystem::Get(GetProfile());
87 extension_service
->management_policy()->RegisterProvider(&policy_provider_
);
90 void ExtensionSettingsUIBrowserTest::SetAutoConfirmUninstall() {
91 uninstall_auto_confirm_
.reset(new extensions::ScopedTestDialogAutoConfirm(
92 extensions::ScopedTestDialogAutoConfirm::ACCEPT
));
95 void ExtensionSettingsUIBrowserTest::EnableErrorConsole() {
96 error_console_override_
.reset(new extensions::FeatureSwitch::ScopedOverride(
97 extensions::FeatureSwitch::error_console(), true));
100 void ExtensionSettingsUIBrowserTest::ShrinkWebContentsView() {
101 content::WebContents
* web_contents
=
102 browser()->tab_strip_model()->GetActiveWebContents();
104 ResizeWebContents(web_contents
, gfx::Size(400, 400));
107 class MockAutoConfirmExtensionInstallPrompt
: public ExtensionInstallPrompt
{
109 explicit MockAutoConfirmExtensionInstallPrompt(
110 content::WebContents
* web_contents
)
111 : ExtensionInstallPrompt(web_contents
) {}
113 // Proceed without confirmation prompt.
114 void ConfirmInstall(Delegate
* delegate
,
115 const Extension
* extension
,
116 const ShowDialogCallback
& show_dialog_callback
) override
{
117 delegate
->InstallUIProceed();
121 const Extension
* ExtensionSettingsUIBrowserTest::InstallUnpackedExtension(
122 const base::FilePath
& path
) {
126 Profile
* profile
= GetProfile();
127 ExtensionService
* service
=
128 extensions::ExtensionSystem::Get(profile
)->extension_service();
129 service
->set_show_extensions_prompts(false);
130 extensions::ExtensionRegistry
* registry
=
131 extensions::ExtensionRegistry::Get(profile
);
132 extensions::TestExtensionRegistryObserver
observer(registry
);
133 extensions::UnpackedInstaller::Create(service
)->Load(path
);
134 base::FilePath extension_path
= base::MakeAbsoluteFilePath(path
);
135 const Extension
* extension
= nullptr;
137 extension
= observer
.WaitForExtensionLoaded();
138 } while (extension
->path() != extension_path
);
143 const Extension
* ExtensionSettingsUIBrowserTest::InstallExtension(
144 const base::FilePath
& path
) {
145 Profile
* profile
= GetProfile();
146 ExtensionService
* service
=
147 extensions::ExtensionSystem::Get(profile
)->extension_service();
148 extensions::ExtensionRegistry
* registry
=
149 extensions::ExtensionRegistry::Get(profile
);
150 service
->set_show_extensions_prompts(false);
151 size_t num_before
= registry
->enabled_extensions().size();
153 scoped_ptr
<ExtensionInstallPrompt
> install_ui
;
154 install_ui
.reset(new MockAutoConfirmExtensionInstallPrompt(
155 browser()->tab_strip_model()->GetActiveWebContents()));
157 base::FilePath crx_path
= path
;
158 DCHECK(crx_path
.Extension() == FILE_PATH_LITERAL(".crx"));
159 if (crx_path
.empty())
162 scoped_refptr
<extensions::CrxInstaller
> installer(
163 extensions::CrxInstaller::Create(service
, install_ui
.Pass()));
164 installer
->set_expected_id(std::string());
165 installer
->set_is_gallery_install(false);
166 installer
->set_install_source(extensions::Manifest::INTERNAL
);
167 installer
->set_install_immediately(true);
168 installer
->set_off_store_install_allow_reason(
169 extensions::CrxInstaller::OffStoreInstallAllowedInTest
);
172 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
173 content::Source
<extensions::CrxInstaller
>(installer
.get()));
175 installer
->InstallCrx(crx_path
);
180 size_t num_after
= registry
->enabled_extensions().size();
181 if (num_before
+ 1 != num_after
) {
182 VLOG(1) << "Num extensions before: " << base::IntToString(num_before
)
183 << " num after: " << base::IntToString(num_after
)
184 << " Installed extensions follow:";
186 for (const scoped_refptr
<const Extension
>& extension
:
187 registry
->enabled_extensions())
188 VLOG(1) << " " << extension
->id();
190 VLOG(1) << "Errors follow:";
191 const std::vector
<base::string16
>* errors
=
192 ExtensionErrorReporter::GetInstance()->GetErrors();
193 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
194 iter
!= errors
->end(); ++iter
)
200 if (!observer_
->WaitForExtensionViewsToLoad())
202 return service
->GetExtensionById(last_loaded_extension_id(), false);