1 // Copyright (c) 2012 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/extensions/extension_browsertest.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/extensions/browsertest_util.h"
18 #include "chrome/browser/extensions/component_loader.h"
19 #include "chrome/browser/extensions/crx_installer.h"
20 #include "chrome/browser/extensions/extension_creator.h"
21 #include "chrome/browser/extensions/extension_error_reporter.h"
22 #include "chrome/browser/extensions/extension_install_prompt.h"
23 #include "chrome/browser/extensions/extension_install_prompt_show_params.h"
24 #include "chrome/browser/extensions/extension_service.h"
25 #include "chrome/browser/extensions/extension_util.h"
26 #include "chrome/browser/extensions/unpacked_installer.h"
27 #include "chrome/browser/extensions/updater/extension_cache_fake.h"
28 #include "chrome/browser/extensions/updater/extension_updater.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/browser/ui/browser.h"
32 #include "chrome/browser/ui/browser_window.h"
33 #include "chrome/browser/ui/tabs/tab_strip_model.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/chrome_version_info.h"
37 #include "content/public/browser/navigation_controller.h"
38 #include "content/public/browser/navigation_entry.h"
39 #include "content/public/browser/notification_registrar.h"
40 #include "content/public/browser/notification_service.h"
41 #include "content/public/browser/render_view_host.h"
42 #include "content/public/test/browser_test_utils.h"
43 #include "content/public/test/test_utils.h"
44 #include "extensions/browser/extension_host.h"
45 #include "extensions/browser/extension_prefs.h"
46 #include "extensions/browser/extension_registry.h"
47 #include "extensions/browser/extension_system.h"
48 #include "extensions/browser/notification_types.h"
49 #include "extensions/browser/uninstall_reason.h"
50 #include "extensions/common/constants.h"
51 #include "extensions/common/extension_set.h"
52 #include "sync/api/string_ordinal.h"
54 #if defined(OS_CHROMEOS)
55 #include "chromeos/chromeos_switches.h"
58 using extensions::Extension
;
59 using extensions::ExtensionCreator
;
60 using extensions::ExtensionRegistry
;
61 using extensions::FeatureSwitch
;
62 using extensions::Manifest
;
64 ExtensionBrowserTest::ExtensionBrowserTest()
67 #if defined(OS_CHROMEOS)
68 set_chromeos_user_(true),
70 // Default channel is STABLE but override with UNKNOWN so that unlaunched
71 // or incomplete APIs can write tests.
72 current_channel_(chrome::VersionInfo::CHANNEL_UNKNOWN
),
73 override_prompt_for_external_extensions_(
74 FeatureSwitch::prompt_for_external_extensions(),
77 user_desktop_override_(base::DIR_USER_DESKTOP
),
78 common_desktop_override_(base::DIR_COMMON_DESKTOP
),
79 user_quick_launch_override_(base::DIR_USER_QUICK_LAUNCH
),
80 start_menu_override_(base::DIR_START_MENU
),
81 common_start_menu_override_(base::DIR_COMMON_START_MENU
),
84 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
87 ExtensionBrowserTest::~ExtensionBrowserTest() {
90 Profile
* ExtensionBrowserTest::profile() {
93 profile_
= browser()->profile();
95 profile_
= ProfileManager::GetActiveUserProfile();
101 const Extension
* ExtensionBrowserTest::GetExtensionByPath(
102 const extensions::ExtensionSet
& extensions
,
103 const base::FilePath
& path
) {
104 base::FilePath extension_path
= base::MakeAbsoluteFilePath(path
);
105 EXPECT_TRUE(!extension_path
.empty());
106 for (const scoped_refptr
<const Extension
>& extension
: extensions
) {
107 if (extension
->path() == extension_path
) {
108 return extension
.get();
114 void ExtensionBrowserTest::SetUp() {
115 test_extension_cache_
.reset(new extensions::ExtensionCacheFake());
116 InProcessBrowserTest::SetUp();
119 void ExtensionBrowserTest::SetUpCommandLine(base::CommandLine
* command_line
) {
120 PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir_
);
121 test_data_dir_
= test_data_dir_
.AppendASCII("extensions");
122 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
124 #if defined(OS_CHROMEOS)
125 if (set_chromeos_user_
) {
126 // This makes sure that we create the Default profile first, with no
127 // ExtensionService and then the real profile with one, as we do when
128 // running on chromeos.
129 command_line
->AppendSwitchASCII(chromeos::switches::kLoginUser
,
130 "TestUser@gmail.com");
131 command_line
->AppendSwitchASCII(chromeos::switches::kLoginProfile
, "user");
136 void ExtensionBrowserTest::SetUpOnMainThread() {
137 InProcessBrowserTest::SetUpOnMainThread();
138 observer_
.reset(new ExtensionTestNotificationObserver(browser()));
139 if (extension_service()->updater()) {
140 extension_service()->updater()->SetExtensionCacheForTesting(
141 test_extension_cache_
.get());
145 const Extension
* ExtensionBrowserTest::LoadExtension(
146 const base::FilePath
& path
) {
147 return LoadExtensionWithFlags(path
, kFlagEnableFileAccess
);
150 const Extension
* ExtensionBrowserTest::LoadExtensionIncognito(
151 const base::FilePath
& path
) {
152 return LoadExtensionWithFlags(path
,
153 kFlagEnableFileAccess
| kFlagEnableIncognito
);
156 const Extension
* ExtensionBrowserTest::LoadExtensionWithFlags(
157 const base::FilePath
& path
, int flags
) {
158 return LoadExtensionWithInstallParam(path
, flags
, std::string());
161 const extensions::Extension
*
162 ExtensionBrowserTest::LoadExtensionWithInstallParam(
163 const base::FilePath
& path
,
165 const std::string
& install_param
) {
166 ExtensionService
* service
= extensions::ExtensionSystem::Get(
167 profile())->extension_service();
168 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
170 observer_
->Watch(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
171 content::NotificationService::AllSources());
173 scoped_refptr
<extensions::UnpackedInstaller
> installer(
174 extensions::UnpackedInstaller::Create(service
));
175 installer
->set_prompt_for_plugins(false);
176 installer
->set_require_modern_manifest_version(
177 (flags
& kFlagAllowOldManifestVersions
) == 0);
178 installer
->Load(path
);
183 // Find the loaded extension by its path. See crbug.com/59531 for why
184 // we cannot just use last_loaded_extension_id().
185 const Extension
* extension
=
186 GetExtensionByPath(registry
->enabled_extensions(), path
);
190 if (!(flags
& kFlagIgnoreManifestWarnings
)) {
191 const std::vector
<extensions::InstallWarning
>& install_warnings
=
192 extension
->install_warnings();
193 if (!install_warnings
.empty()) {
194 std::string install_warnings_message
= base::StringPrintf(
195 "Unexpected warnings when loading test extension %s:\n",
196 path
.AsUTF8Unsafe().c_str());
198 for (std::vector
<extensions::InstallWarning
>::const_iterator it
=
199 install_warnings
.begin(); it
!= install_warnings
.end(); ++it
) {
200 install_warnings_message
+= " " + it
->message
+ "\n";
203 EXPECT_EQ(0u, extension
->install_warnings().size())
204 << install_warnings_message
;
209 const std::string extension_id
= extension
->id();
211 if (!install_param
.empty()) {
212 extensions::ExtensionPrefs::Get(profile())
213 ->SetInstallParam(extension_id
, install_param
);
214 // Re-enable the extension if needed.
215 if (registry
->enabled_extensions().Contains(extension_id
)) {
216 content::WindowedNotificationObserver
load_signal(
217 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
218 content::Source
<Profile
>(profile()));
219 // Reload the extension so that the
220 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED
221 // observers may access |install_param|.
222 service
->ReloadExtension(extension_id
);
224 extension
= service
->GetExtensionById(extension_id
, false);
225 CHECK(extension
) << extension_id
<< " not found after reloading.";
229 // Toggling incognito or file access will reload the extension, so wait for
230 // the reload and grab the new extension instance. The default state is
231 // incognito disabled and file access enabled, so we don't wait in those
234 content::WindowedNotificationObserver
load_signal(
235 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
236 content::Source
<Profile
>(profile()));
237 CHECK(!extensions::util::IsIncognitoEnabled(extension_id
, profile()))
238 << extension_id
<< " is enabled in incognito, but shouldn't be";
240 if (flags
& kFlagEnableIncognito
) {
241 extensions::util::SetIsIncognitoEnabled(extension_id
, profile(), true);
243 extension
= service
->GetExtensionById(extension_id
, false);
244 CHECK(extension
) << extension_id
<< " not found after reloading.";
249 content::WindowedNotificationObserver
load_signal(
250 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
251 content::Source
<Profile
>(profile()));
252 CHECK(extensions::util::AllowFileAccess(extension_id
, profile()));
253 if (!(flags
& kFlagEnableFileAccess
)) {
254 extensions::util::SetAllowFileAccess(extension_id
, profile(), false);
256 extension
= service
->GetExtensionById(extension_id
, false);
257 CHECK(extension
) << extension_id
<< " not found after reloading.";
261 if (!observer_
->WaitForExtensionViewsToLoad())
267 const Extension
* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest(
268 const base::FilePath
& path
,
269 const base::FilePath::CharType
* manifest_relative_path
) {
270 ExtensionService
* service
= extensions::ExtensionSystem::Get(
271 profile())->extension_service();
272 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
274 std::string manifest
;
275 if (!base::ReadFileToString(path
.Append(manifest_relative_path
), &manifest
)) {
279 std::string extension_id
= service
->component_loader()->Add(manifest
, path
);
280 const Extension
* extension
=
281 registry
->enabled_extensions().GetByID(extension_id
);
284 observer_
->set_last_loaded_extension_id(extension
->id());
288 const Extension
* ExtensionBrowserTest::LoadExtensionAsComponent(
289 const base::FilePath
& path
) {
290 return LoadExtensionAsComponentWithManifest(path
,
291 extensions::kManifestFilename
);
294 base::FilePath
ExtensionBrowserTest::PackExtension(
295 const base::FilePath
& dir_path
) {
296 base::FilePath crx_path
= temp_dir_
.path().AppendASCII("temp.crx");
297 if (!base::DeleteFile(crx_path
, false)) {
298 ADD_FAILURE() << "Failed to delete crx: " << crx_path
.value();
299 return base::FilePath();
302 // Look for PEM files with the same name as the directory.
303 base::FilePath pem_path
=
304 dir_path
.ReplaceExtension(FILE_PATH_LITERAL(".pem"));
305 base::FilePath pem_path_out
;
307 if (!base::PathExists(pem_path
)) {
308 pem_path
= base::FilePath();
309 pem_path_out
= crx_path
.DirName().AppendASCII("temp.pem");
310 if (!base::DeleteFile(pem_path_out
, false)) {
311 ADD_FAILURE() << "Failed to delete pem: " << pem_path_out
.value();
312 return base::FilePath();
316 return PackExtensionWithOptions(dir_path
, crx_path
, pem_path
, pem_path_out
);
319 base::FilePath
ExtensionBrowserTest::PackExtensionWithOptions(
320 const base::FilePath
& dir_path
,
321 const base::FilePath
& crx_path
,
322 const base::FilePath
& pem_path
,
323 const base::FilePath
& pem_out_path
) {
324 if (!base::PathExists(dir_path
)) {
325 ADD_FAILURE() << "Extension dir not found: " << dir_path
.value();
326 return base::FilePath();
329 if (!base::PathExists(pem_path
) && pem_out_path
.empty()) {
330 ADD_FAILURE() << "Must specify a PEM file or PEM output path";
331 return base::FilePath();
334 scoped_ptr
<ExtensionCreator
> creator(new ExtensionCreator());
335 if (!creator
->Run(dir_path
,
339 ExtensionCreator::kOverwriteCRX
)) {
340 ADD_FAILURE() << "ExtensionCreator::Run() failed: "
341 << creator
->error_message();
342 return base::FilePath();
345 if (!base::PathExists(crx_path
)) {
346 ADD_FAILURE() << crx_path
.value() << " was not created.";
347 return base::FilePath();
352 // This class is used to simulate an installation abort by the user.
353 class MockAbortExtensionInstallPrompt
: public ExtensionInstallPrompt
{
355 MockAbortExtensionInstallPrompt() : ExtensionInstallPrompt(NULL
) {
358 // Simulate a user abort on an extension installation.
359 void ConfirmInstall(Delegate
* delegate
,
360 const Extension
* extension
,
361 const ShowDialogCallback
& show_dialog_callback
) override
{
362 delegate
->InstallUIAbort(true);
363 base::MessageLoopForUI::current()->Quit();
366 void OnInstallSuccess(const Extension
* extension
, SkBitmap
* icon
) override
{}
368 void OnInstallFailure(const extensions::CrxInstallError
& error
) override
{}
371 class MockAutoConfirmExtensionInstallPrompt
: public ExtensionInstallPrompt
{
373 explicit MockAutoConfirmExtensionInstallPrompt(
374 content::WebContents
* web_contents
)
375 : ExtensionInstallPrompt(web_contents
) {}
377 // Proceed without confirmation prompt.
378 void ConfirmInstall(Delegate
* delegate
,
379 const Extension
* extension
,
380 const ShowDialogCallback
& show_dialog_callback
) override
{
381 delegate
->InstallUIProceed();
385 const Extension
* ExtensionBrowserTest::UpdateExtensionWaitForIdle(
386 const std::string
& id
,
387 const base::FilePath
& path
,
388 int expected_change
) {
389 return InstallOrUpdateExtension(id
,
391 INSTALL_UI_TYPE_NONE
,
400 const Extension
* ExtensionBrowserTest::InstallExtensionFromWebstore(
401 const base::FilePath
& path
,
402 int expected_change
) {
403 return InstallOrUpdateExtension(std::string(),
405 INSTALL_UI_TYPE_NONE
,
409 Extension::FROM_WEBSTORE
,
414 const Extension
* ExtensionBrowserTest::InstallOrUpdateExtension(
415 const std::string
& id
,
416 const base::FilePath
& path
,
417 InstallUIType ui_type
,
418 int expected_change
) {
419 return InstallOrUpdateExtension(id
,
430 const Extension
* ExtensionBrowserTest::InstallOrUpdateExtension(
431 const std::string
& id
,
432 const base::FilePath
& path
,
433 InstallUIType ui_type
,
436 Extension::InitFromValueFlags creation_flags
) {
437 return InstallOrUpdateExtension(id
,
448 const Extension
* ExtensionBrowserTest::InstallOrUpdateExtension(
449 const std::string
& id
,
450 const base::FilePath
& path
,
451 InstallUIType ui_type
,
453 Manifest::Location install_source
) {
454 return InstallOrUpdateExtension(id
,
465 const Extension
* ExtensionBrowserTest::InstallOrUpdateExtension(
466 const std::string
& id
,
467 const base::FilePath
& path
,
468 InstallUIType ui_type
,
470 Manifest::Location install_source
,
472 Extension::InitFromValueFlags creation_flags
,
473 bool install_immediately
,
475 ExtensionService
* service
=
476 extensions::ExtensionSystem::Get(profile())->extension_service();
477 ExtensionRegistry
* registry
= ExtensionRegistry::Get(profile());
478 service
->set_show_extensions_prompts(false);
479 size_t num_before
= registry
->enabled_extensions().size();
482 scoped_ptr
<ExtensionInstallPrompt
> install_ui
;
483 if (ui_type
== INSTALL_UI_TYPE_CANCEL
) {
484 install_ui
.reset(new MockAbortExtensionInstallPrompt());
485 } else if (ui_type
== INSTALL_UI_TYPE_NORMAL
) {
486 install_ui
.reset(new ExtensionInstallPrompt(
487 browser
->tab_strip_model()->GetActiveWebContents()));
488 } else if (ui_type
== INSTALL_UI_TYPE_AUTO_CONFIRM
) {
489 install_ui
.reset(new MockAutoConfirmExtensionInstallPrompt(
490 browser
->tab_strip_model()->GetActiveWebContents()));
493 // TODO(tessamac): Update callers to always pass an unpacked extension
494 // and then always pack the extension here.
495 base::FilePath crx_path
= path
;
496 if (crx_path
.Extension() != FILE_PATH_LITERAL(".crx")) {
497 crx_path
= PackExtension(path
);
499 if (crx_path
.empty())
502 scoped_refptr
<extensions::CrxInstaller
> installer(
503 extensions::CrxInstaller::Create(service
, install_ui
.Pass()));
504 installer
->set_expected_id(id
);
505 installer
->set_creation_flags(creation_flags
);
506 installer
->set_install_source(install_source
);
507 installer
->set_install_immediately(install_immediately
);
508 installer
->set_is_ephemeral(is_ephemeral
);
509 if (!installer
->is_gallery_install()) {
510 installer
->set_off_store_install_allow_reason(
511 extensions::CrxInstaller::OffStoreInstallAllowedInTest
);
515 extensions::NOTIFICATION_CRX_INSTALLER_DONE
,
516 content::Source
<extensions::CrxInstaller
>(installer
.get()));
518 installer
->InstallCrx(crx_path
);
523 size_t num_after
= registry
->enabled_extensions().size();
524 EXPECT_EQ(num_before
+ expected_change
, num_after
);
525 if (num_before
+ expected_change
!= num_after
) {
526 VLOG(1) << "Num extensions before: " << base::IntToString(num_before
)
527 << " num after: " << base::IntToString(num_after
)
528 << " Installed extensions follow:";
530 for (const scoped_refptr
<const Extension
>& extension
:
531 registry
->enabled_extensions())
532 VLOG(1) << " " << extension
->id();
534 VLOG(1) << "Errors follow:";
535 const std::vector
<base::string16
>* errors
=
536 ExtensionErrorReporter::GetInstance()->GetErrors();
537 for (std::vector
<base::string16
>::const_iterator iter
= errors
->begin();
538 iter
!= errors
->end(); ++iter
)
544 if (!observer_
->WaitForExtensionViewsToLoad())
546 return service
->GetExtensionById(last_loaded_extension_id(), false);
549 void ExtensionBrowserTest::ReloadExtension(const std::string extension_id
) {
550 observer_
->Watch(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED
,
551 content::NotificationService::AllSources());
553 ExtensionService
* service
=
554 extensions::ExtensionSystem::Get(profile())->extension_service();
555 service
->ReloadExtension(extension_id
);
558 observer_
->WaitForExtensionViewsToLoad();
561 void ExtensionBrowserTest::UnloadExtension(const std::string
& extension_id
) {
562 ExtensionService
* service
= extensions::ExtensionSystem::Get(
563 profile())->extension_service();
564 service
->UnloadExtension(extension_id
,
565 extensions::UnloadedExtensionInfo::REASON_DISABLE
);
568 void ExtensionBrowserTest::UninstallExtension(const std::string
& extension_id
) {
569 ExtensionService
* service
= extensions::ExtensionSystem::Get(
570 profile())->extension_service();
571 service
->UninstallExtension(extension_id
,
572 extensions::UNINSTALL_REASON_FOR_TESTING
,
573 base::Bind(&base::DoNothing
),
577 void ExtensionBrowserTest::DisableExtension(const std::string
& extension_id
) {
578 ExtensionService
* service
= extensions::ExtensionSystem::Get(
579 profile())->extension_service();
580 service
->DisableExtension(extension_id
, Extension::DISABLE_USER_ACTION
);
583 void ExtensionBrowserTest::EnableExtension(const std::string
& extension_id
) {
584 ExtensionService
* service
= extensions::ExtensionSystem::Get(
585 profile())->extension_service();
586 service
->EnableExtension(extension_id
);
589 void ExtensionBrowserTest::OpenWindow(content::WebContents
* contents
,
591 bool newtab_process_should_equal_opener
,
592 content::WebContents
** newtab_result
) {
593 content::WindowedNotificationObserver
windowed_observer(
594 content::NOTIFICATION_LOAD_STOP
,
595 content::NotificationService::AllSources());
596 ASSERT_TRUE(content::ExecuteScript(contents
,
597 "window.open('" + url
.spec() + "');"));
599 // The above window.open call is not user-initiated, so it will create
600 // a popup window instead of a new tab in current window.
601 // The stop notification will come from the new tab.
602 windowed_observer
.Wait();
603 content::NavigationController
* controller
=
604 content::Source
<content::NavigationController
>(
605 windowed_observer
.source()).ptr();
606 content::WebContents
* newtab
= controller
->GetWebContents();
608 EXPECT_EQ(url
, controller
->GetLastCommittedEntry()->GetURL());
609 if (newtab_process_should_equal_opener
)
610 EXPECT_EQ(contents
->GetRenderProcessHost(), newtab
->GetRenderProcessHost());
612 EXPECT_NE(contents
->GetRenderProcessHost(), newtab
->GetRenderProcessHost());
615 *newtab_result
= newtab
;
618 void ExtensionBrowserTest::NavigateInRenderer(content::WebContents
* contents
,
621 content::WindowedNotificationObserver
windowed_observer(
622 content::NOTIFICATION_LOAD_STOP
,
623 content::NotificationService::AllSources());
624 ASSERT_TRUE(content::ExecuteScriptAndExtractBool(
626 "window.addEventListener('unload', function() {"
627 " window.domAutomationController.send(true);"
629 "window.location = '" + url
.spec() + "';",
632 windowed_observer
.Wait();
633 EXPECT_EQ(url
, contents
->GetController().GetLastCommittedEntry()->GetURL());
636 extensions::ExtensionHost
* ExtensionBrowserTest::FindHostWithPath(
637 extensions::ProcessManager
* manager
,
638 const std::string
& path
,
639 int expected_hosts
) {
640 extensions::ExtensionHost
* host
= NULL
;
642 extensions::ProcessManager::ExtensionHostSet background_hosts
=
643 manager
->background_hosts();
644 for (extensions::ProcessManager::const_iterator iter
=
645 background_hosts
.begin();
646 iter
!= background_hosts
.end();
648 if ((*iter
)->GetURL().path() == path
) {
654 EXPECT_EQ(expected_hosts
, num_hosts
);
658 std::string
ExtensionBrowserTest::ExecuteScriptInBackgroundPage(
659 const std::string
& extension_id
,
660 const std::string
& script
) {
661 return extensions::browsertest_util::ExecuteScriptInBackgroundPage(
662 profile(), extension_id
, script
);
665 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait(
666 const std::string
& extension_id
,
667 const std::string
& script
) {
668 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait(
669 profile(), extension_id
, script
);