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/common/extension_set.h"
31 using extensions::Extension
;
32 using extensions::TestManagementPolicyProvider
;
34 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest()
36 policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS
|
37 TestManagementPolicyProvider::MUST_REMAIN_ENABLED
|
38 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED
) {}
40 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {}
42 Profile
* ExtensionSettingsUIBrowserTest::GetProfile() {
44 profile_
= browser() ? browser()->profile() :
45 ProfileManager::GetActiveUserProfile();
50 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() {
51 WebUIBrowserTest::SetUpOnMainThread();
52 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
55 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() {
56 base::FilePath test_data_dir
;
57 if (!PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
)) {
61 base::FilePath extensions_data_dir
= test_data_dir
.AppendASCII("extensions");
62 InstallExtension(extensions_data_dir
.AppendASCII("good.crx"));
65 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() {
66 auto* extension_service
= extensions::ExtensionSystem::Get(GetProfile());
67 extension_service
->management_policy()->RegisterProvider(&policy_provider_
);
70 class MockAutoConfirmExtensionInstallPrompt
: public ExtensionInstallPrompt
{
72 explicit MockAutoConfirmExtensionInstallPrompt(
73 content::WebContents
* web_contents
)
74 : ExtensionInstallPrompt(web_contents
) {}
76 // Proceed without confirmation prompt.
77 void ConfirmInstall(Delegate
* delegate
,
78 const Extension
* extension
,
79 const ShowDialogCallback
& show_dialog_callback
) override
{
80 delegate
->InstallUIProceed();
84 const Extension
* ExtensionSettingsUIBrowserTest::InstallExtension(
85 const base::FilePath
& path
) {
86 Profile
* profile
= this->GetProfile();
87 ExtensionService
* service
=
88 extensions::ExtensionSystem::Get(profile
)->extension_service();
89 extensions::ExtensionRegistry
* registry
=
90 extensions::ExtensionRegistry::Get(profile
);
91 service
->set_show_extensions_prompts(false);
92 size_t num_before
= registry
->enabled_extensions().size();
94 scoped_ptr
<ExtensionInstallPrompt
> install_ui
;
95 install_ui
.reset(new MockAutoConfirmExtensionInstallPrompt(
96 browser()->tab_strip_model()->GetActiveWebContents()));
98 base::FilePath crx_path
= path
;
99 DCHECK(crx_path
.Extension() == FILE_PATH_LITERAL(".crx"));
100 if (crx_path
.empty())
103 scoped_refptr
<extensions::CrxInstaller
> installer(
104 extensions::CrxInstaller::Create(service
, install_ui
.Pass()));
105 installer
->set_expected_id(std::string());
106 installer
->set_is_gallery_install(false);
107 installer
->set_install_source(extensions::Manifest::INTERNAL
);
108 installer
->set_install_immediately(true);
109 installer
->set_off_store_install_allow_reason(
110 extensions::CrxInstaller::OffStoreInstallAllowedInTest
);
113 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
114 content::Source
<extensions::CrxInstaller
>(installer
.get()));
116 installer
->InstallCrx(crx_path
);
121 size_t num_after
= registry
->enabled_extensions().size();
122 if (num_before
+ 1 != num_after
) {
123 VLOG(1) << "Num extensions before: " << base::IntToString(num_before
)
124 << " num after: " << base::IntToString(num_after
)
125 << " Installed extensions follow:";
127 for (const scoped_refptr
<const Extension
>& extension
:
128 registry
->enabled_extensions())
129 VLOG(1) << " " << extension
->id();
131 VLOG(1) << "Errors follow:";
132 const std::vector
<base::string16
>* errors
=
133 ExtensionErrorReporter::GetInstance()->GetErrors();
134 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
135 iter
!= errors
->end(); ++iter
)
141 if (!observer_
->WaitForExtensionViewsToLoad())
143 return service
->GetExtensionById(last_loaded_extension_id(), false);