| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h" | 5 #include "chrome/browser/ui/webui/extensions/extension_settings_browsertest.h" |
| 6 | 6 |
| 7 #include <stddef.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/path_service.h" | 7 #include "base/path_service.h" |
| 12 #include "base/strings/string_number_conversions.h" | 8 #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #include "chrome/browser/extensions/crx_installer.h" | |
| 15 #include "chrome/browser/extensions/extension_error_reporter.h" | |
| 16 #include "chrome/browser/extensions/extension_install_prompt.h" | |
| 17 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | |
| 18 #include "chrome/browser/extensions/extension_service.h" | |
| 19 #include "chrome/browser/extensions/unpacked_installer.h" | 9 #include "chrome/browser/extensions/unpacked_installer.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" | |
| 22 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 23 #include "chrome/browser/ui/browser_window.h" | |
| 24 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 25 #include "chrome/browser/ui/web_contents_sizer.h" | 13 #include "chrome/browser/ui/web_contents_sizer.h" |
| 26 #include "chrome/common/chrome_paths.h" | 14 #include "chrome/common/chrome_paths.h" |
| 27 #include "content/public/browser/notification_registrar.h" | 15 #include "extensions/browser/extension_dialog_auto_confirm.h" |
| 28 #include "content/public/browser/notification_service.h" | |
| 29 #include "content/public/browser/render_view_host.h" | |
| 30 #include "content/public/test/browser_test_utils.h" | |
| 31 #include "content/public/test/test_utils.h" | |
| 32 #include "extensions/browser/extension_registry.h" | |
| 33 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/browser/test_extension_registry_observer.h" | |
| 35 #include "extensions/common/extension_set.h" | |
| 36 | 17 |
| 37 using extensions::Extension; | 18 using extensions::Extension; |
| 38 using extensions::TestManagementPolicyProvider; | 19 using extensions::TestManagementPolicyProvider; |
| 39 | 20 |
| 40 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest() | 21 ExtensionSettingsUIBrowserTest::ExtensionSettingsUIBrowserTest() |
| 41 : profile_(nullptr), | 22 : policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS | |
| 42 policy_provider_(TestManagementPolicyProvider::PROHIBIT_MODIFY_STATUS | | |
| 43 TestManagementPolicyProvider::MUST_REMAIN_ENABLED | | 23 TestManagementPolicyProvider::MUST_REMAIN_ENABLED | |
| 44 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED) { | 24 TestManagementPolicyProvider::MUST_REMAIN_INSTALLED) { |
| 45 CHECK(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); | 25 CHECK(PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_)); |
| 46 test_data_dir_ = test_data_dir_.AppendASCII("extensions"); | 26 test_data_dir_ = test_data_dir_.AppendASCII("extensions"); |
| 47 } | 27 } |
| 48 | 28 |
| 49 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {} | 29 ExtensionSettingsUIBrowserTest::~ExtensionSettingsUIBrowserTest() {} |
| 50 | 30 |
| 51 Profile* ExtensionSettingsUIBrowserTest::GetProfile() { | |
| 52 if (!profile_) { | |
| 53 profile_ = browser() ? browser()->profile() : | |
| 54 ProfileManager::GetActiveUserProfile(); | |
| 55 } | |
| 56 return profile_; | |
| 57 } | |
| 58 | |
| 59 void ExtensionSettingsUIBrowserTest::SetUpOnMainThread() { | |
| 60 WebUIBrowserTest::SetUpOnMainThread(); | |
| 61 observer_.reset( | |
| 62 new extensions::ChromeExtensionTestNotificationObserver(browser())); | |
| 63 } | |
| 64 | |
| 65 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() { | 31 void ExtensionSettingsUIBrowserTest::InstallGoodExtension() { |
| 66 EXPECT_TRUE(InstallExtension(test_data_dir_.AppendASCII("good.crx"))); | 32 EXPECT_TRUE(InstallExtension(test_data_dir_.AppendASCII("good.crx"))); |
| 67 } | 33 } |
| 68 | 34 |
| 69 void ExtensionSettingsUIBrowserTest::InstallErrorsExtension() { | 35 void ExtensionSettingsUIBrowserTest::InstallErrorsExtension() { |
| 70 EXPECT_TRUE(InstallUnpackedExtension( | 36 EXPECT_TRUE( |
| 71 test_data_dir_.AppendASCII("error_console") | 37 InstallExtension(test_data_dir_.AppendASCII("error_console") |
| 72 .AppendASCII("runtime_and_manifest_errors"))); | 38 .AppendASCII("runtime_and_manifest_errors"))); |
| 73 } | 39 } |
| 74 | 40 |
| 75 void ExtensionSettingsUIBrowserTest::InstallSharedModule() { | 41 void ExtensionSettingsUIBrowserTest::InstallSharedModule() { |
| 76 base::FilePath shared_module_path = | 42 base::FilePath shared_module_path = |
| 77 test_data_dir_.AppendASCII("api_test").AppendASCII("shared_module"); | 43 test_data_dir_.AppendASCII("api_test").AppendASCII("shared_module"); |
| 78 EXPECT_TRUE(InstallUnpackedExtension( | 44 EXPECT_TRUE(InstallExtension(shared_module_path.AppendASCII("shared"))); |
| 79 shared_module_path.AppendASCII("shared"))); | 45 EXPECT_TRUE(InstallExtension(shared_module_path.AppendASCII("import_pass"))); |
| 80 EXPECT_TRUE(InstallUnpackedExtension( | |
| 81 shared_module_path.AppendASCII("import_pass"))); | |
| 82 } | 46 } |
| 83 | 47 |
| 84 void ExtensionSettingsUIBrowserTest::InstallPackagedApp() { | 48 void ExtensionSettingsUIBrowserTest::InstallPackagedApp() { |
| 85 EXPECT_TRUE(InstallUnpackedExtension( | 49 EXPECT_TRUE(InstallExtension(test_data_dir_.AppendASCII("packaged_app"))); |
| 86 test_data_dir_.AppendASCII("packaged_app"))); | |
| 87 } | 50 } |
| 88 | 51 |
| 89 void ExtensionSettingsUIBrowserTest::InstallHostedApp() { | 52 void ExtensionSettingsUIBrowserTest::InstallHostedApp() { |
| 90 EXPECT_TRUE(InstallUnpackedExtension( | 53 EXPECT_TRUE(InstallExtension(test_data_dir_.AppendASCII("hosted_app"))); |
| 91 test_data_dir_.AppendASCII("hosted_app"))); | |
| 92 } | 54 } |
| 93 | 55 |
| 94 void ExtensionSettingsUIBrowserTest::InstallPlatformApp() { | 56 void ExtensionSettingsUIBrowserTest::InstallPlatformApp() { |
| 95 EXPECT_TRUE(InstallUnpackedExtension( | 57 EXPECT_TRUE(InstallExtension( |
| 96 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"))); | 58 test_data_dir_.AppendASCII("platform_apps").AppendASCII("minimal"))); |
| 97 } | 59 } |
| 98 | 60 |
| 99 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() { | 61 void ExtensionSettingsUIBrowserTest::AddManagedPolicyProvider() { |
| 100 auto* extension_service = extensions::ExtensionSystem::Get(GetProfile()); | 62 extensions::ExtensionSystem* extension_system = |
| 101 extension_service->management_policy()->RegisterProvider(&policy_provider_); | 63 extensions::ExtensionSystem::Get(browser()->profile()); |
| 64 extension_system->management_policy()->RegisterProvider(&policy_provider_); |
| 102 } | 65 } |
| 103 | 66 |
| 104 void ExtensionSettingsUIBrowserTest::SetAutoConfirmUninstall() { | 67 void ExtensionSettingsUIBrowserTest::SetAutoConfirmUninstall() { |
| 105 uninstall_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm( | 68 uninstall_auto_confirm_.reset(new extensions::ScopedTestDialogAutoConfirm( |
| 106 extensions::ScopedTestDialogAutoConfirm::ACCEPT)); | 69 extensions::ScopedTestDialogAutoConfirm::ACCEPT)); |
| 107 } | 70 } |
| 108 | 71 |
| 109 void ExtensionSettingsUIBrowserTest::EnableErrorConsole() { | 72 void ExtensionSettingsUIBrowserTest::EnableErrorConsole() { |
| 110 error_console_override_.reset(new extensions::FeatureSwitch::ScopedOverride( | 73 error_console_override_.reset(new extensions::FeatureSwitch::ScopedOverride( |
| 111 extensions::FeatureSwitch::error_console(), true)); | 74 extensions::FeatureSwitch::error_console(), true)); |
| 112 } | 75 } |
| 113 | 76 |
| 114 void ExtensionSettingsUIBrowserTest::ShrinkWebContentsView() { | 77 void ExtensionSettingsUIBrowserTest::ShrinkWebContentsView() { |
| 115 content::WebContents* web_contents = | 78 content::WebContents* web_contents = |
| 116 browser()->tab_strip_model()->GetActiveWebContents(); | 79 browser()->tab_strip_model()->GetActiveWebContents(); |
| 117 CHECK(web_contents); | 80 CHECK(web_contents); |
| 118 ResizeWebContents(web_contents, gfx::Rect(0, 0, 400, 400)); | 81 ResizeWebContents(web_contents, gfx::Rect(0, 0, 400, 400)); |
| 119 } | 82 } |
| 120 | 83 |
| 121 const Extension* ExtensionSettingsUIBrowserTest::InstallUnpackedExtension( | |
| 122 const base::FilePath& path) { | |
| 123 if (path.empty()) | |
| 124 return nullptr; | |
| 125 | |
| 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; | |
| 136 do { | |
| 137 extension = observer.WaitForExtensionLoaded(); | |
| 138 } while (extension->path() != extension_path); | |
| 139 | |
| 140 return extension; | |
| 141 } | |
| 142 | |
| 143 const Extension* ExtensionSettingsUIBrowserTest::InstallExtension( | 84 const Extension* ExtensionSettingsUIBrowserTest::InstallExtension( |
| 144 const base::FilePath& path) { | 85 const base::FilePath& path) { |
| 145 Profile* profile = GetProfile(); | 86 extensions::ChromeTestExtensionLoader loader(browser()->profile()); |
| 146 ExtensionService* service = | 87 loader.set_ignore_manifest_warnings(true); |
| 147 extensions::ExtensionSystem::Get(profile)->extension_service(); | 88 return loader.LoadExtension(path).get(); |
| 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(); | |
| 152 { | |
| 153 extensions::ScopedTestDialogAutoConfirm auto_confirm( | |
| 154 extensions::ScopedTestDialogAutoConfirm::ACCEPT); | |
| 155 std::unique_ptr<ExtensionInstallPrompt> install_ui( | |
| 156 new ExtensionInstallPrompt( | |
| 157 browser()->tab_strip_model()->GetActiveWebContents())); | |
| 158 | |
| 159 base::FilePath crx_path = path; | |
| 160 DCHECK(crx_path.Extension() == FILE_PATH_LITERAL(".crx")); | |
| 161 if (crx_path.empty()) | |
| 162 return nullptr; | |
| 163 | |
| 164 scoped_refptr<extensions::CrxInstaller> installer( | |
| 165 extensions::CrxInstaller::Create(service, std::move(install_ui))); | |
| 166 installer->set_expected_id(std::string()); | |
| 167 installer->set_is_gallery_install(false); | |
| 168 installer->set_install_source(extensions::Manifest::INTERNAL); | |
| 169 installer->set_install_immediately(true); | |
| 170 installer->set_off_store_install_allow_reason( | |
| 171 extensions::CrxInstaller::OffStoreInstallAllowedInTest); | |
| 172 | |
| 173 observer_->Watch( | |
| 174 extensions::NOTIFICATION_CRX_INSTALLER_DONE, | |
| 175 content::Source<extensions::CrxInstaller>(installer.get())); | |
| 176 | |
| 177 installer->InstallCrx(crx_path); | |
| 178 | |
| 179 observer_->Wait(); | |
| 180 } | |
| 181 | |
| 182 size_t num_after = registry->enabled_extensions().size(); | |
| 183 if (num_before + 1 != num_after) { | |
| 184 VLOG(1) << "Num extensions before: " << base::SizeTToString(num_before) | |
| 185 << " num after: " << base::SizeTToString(num_after) | |
| 186 << " Installed extensions follow:"; | |
| 187 | |
| 188 for (const scoped_refptr<const Extension>& extension : | |
| 189 registry->enabled_extensions()) | |
| 190 VLOG(1) << " " << extension->id(); | |
| 191 | |
| 192 VLOG(1) << "Errors follow:"; | |
| 193 const std::vector<base::string16>* errors = | |
| 194 ExtensionErrorReporter::GetInstance()->GetErrors(); | |
| 195 for (std::vector<base::string16>::const_iterator iter = errors->begin(); | |
| 196 iter != errors->end(); ++iter) | |
| 197 VLOG(1) << *iter; | |
| 198 | |
| 199 return nullptr; | |
| 200 } | |
| 201 | |
| 202 if (!observer_->WaitForExtensionViewsToLoad()) | |
| 203 return nullptr; | |
| 204 return service->GetExtensionById(last_loaded_extension_id(), false); | |
| 205 } | 89 } |
| OLD | NEW |