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/extension_registry.h"
28 #include "extensions/browser/extension_system.h"
29 #include "extensions/browser/test_extension_registry_observer.h"
30 #include "extensions/common/extension_set.h"
32 using extensions::Extension
;
33 using extensions::TestManagementPolicyProvider
;
35 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
37 policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
|
38 TestManagementPolicyProvider::MUST_REMAIN_ENABLED
|
39 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED
) {}
41 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
43 Profile
* ExtensionSettingsUIBrowserTest::GetProfile() {
45 profile_
= browser() ? browser()->profile() :
46 ProfileManager::GetActiveUserProfile();
51 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
52 WebUIBrowserTest::SetUpOnMainThread();
53 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
56 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
57 base::FilePath test_data_dir
;
58 if (!PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
)) {
62 base::FilePath extensions_data_dir
= test_data_dir
.AppendASCII("extensions");
63 EXPECT_TRUE(InstallExtension(extensions_data_dir
.AppendASCII("good.crx")));
66 void ExtensionSettingsUIBrowserTest::InstallErrorsExtension() {
67 base::FilePath test_data_dir
;
68 if (!PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
)) {
72 base::FilePath extensions_data_dir
= test_data_dir
.AppendASCII("extensions");
73 extensions_data_dir
= extensions_data_dir
.AppendASCII("error_console");
74 EXPECT_TRUE(InstallUnpackedExtension(
75 extensions_data_dir
.AppendASCII("runtime_and_manifest_errors"),
76 "pdlpifnclfacjobnmbpngemkalkjamnf"));
79 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() {
80 auto* extension_service
= extensions::ExtensionSystem::Get(GetProfile());
81 extension_service
->management_policy()->RegisterProvider(&policy_provider_
);
84 class MockAutoConfirmExtensionInstallPrompt
: public ExtensionInstallPrompt
{
86 explicit MockAutoConfirmExtensionInstallPrompt(
87 content::WebContents
* web_contents
)
88 : ExtensionInstallPrompt(web_contents
) {}
90 // Proceed without confirmation prompt.
91 void ConfirmInstall(Delegate
* delegate
,
92 const Extension
* extension
,
93 const ShowDialogCallback
& show_dialog_callback
) override
{
94 delegate
->InstallUIProceed();
98 const Extension
* ExtensionSettingsUIBrowserTest::InstallUnpackedExtension(
99 const base::FilePath
& path
, const char* id
) {
103 Profile
* profile
= GetProfile();
104 ExtensionService
* service
=
105 extensions::ExtensionSystem::Get(profile
)->extension_service();
106 service
->set_show_extensions_prompts(false);
107 extensions::ExtensionRegistry
* registry
=
108 extensions::ExtensionRegistry::Get(profile
);
109 extensions::TestExtensionRegistryObserver
observer(registry
, id
);
111 extensions::UnpackedInstaller::Create(service
)->Load(path
);
113 // Test will timeout if extension is not loaded.
114 observer
.WaitForExtensionLoaded();
115 return service
->GetExtensionById(last_loaded_extension_id(), false);
118 const Extension
* ExtensionSettingsUIBrowserTest::InstallExtension(
119 const base::FilePath
& path
) {
120 Profile
* profile
= GetProfile();
121 ExtensionService
* service
=
122 extensions::ExtensionSystem::Get(profile
)->extension_service();
123 extensions::ExtensionRegistry
* registry
=
124 extensions::ExtensionRegistry::Get(profile
);
125 service
->set_show_extensions_prompts(false);
126 size_t num_before
= registry
->enabled_extensions().size();
128 scoped_ptr
<ExtensionInstallPrompt
> install_ui
;
129 install_ui
.reset(new MockAutoConfirmExtensionInstallPrompt(
130 browser()->tab_strip_model()->GetActiveWebContents()));
132 base::FilePath crx_path
= path
;
133 DCHECK(crx_path
.Extension() == FILE_PATH_LITERAL(".crx"));
134 if (crx_path
.empty())
137 scoped_refptr
<extensions::CrxInstaller
> installer(
138 extensions::CrxInstaller::Create(service
, install_ui
.Pass()));
139 installer
->set_expected_id(std::string());
140 installer
->set_is_gallery_install(false);
141 installer
->set_install_source(extensions::Manifest::INTERNAL
);
142 installer
->set_install_immediately(true);
143 installer
->set_off_store_install_allow_reason(
144 extensions::CrxInstaller::OffStoreInstallAllowedInTest
);
147 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
148 content::Source
<extensions::CrxInstaller
>(installer
.get()));
150 installer
->InstallCrx(crx_path
);
155 size_t num_after
= registry
->enabled_extensions().size();
156 if (num_before
+ 1 != num_after
) {
157 VLOG(1) << "Num extensions before: " << base::IntToString(num_before
)
158 << " num after: " << base::IntToString(num_after
)
159 << " Installed extensions follow:";
161 for (const scoped_refptr
<const Extension
>& extension
:
162 registry
->enabled_extensions())
163 VLOG(1) << " " << extension
->id();
165 VLOG(1) << "Errors follow:";
166 const std::vector
<base::string16
>* errors
=
167 ExtensionErrorReporter::GetInstance()->GetErrors();
168 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
169 iter
!= errors
->end(); ++iter
)
175 if (!observer_
->WaitForExtensionViewsToLoad())
177 return service
->GetExtensionById(last_loaded_extension_id(), false);