Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(751)

Side by Side Diff: chrome/browser/extensions/extension_service_unittest.cc

Issue 9369013: Take extensions out of Profile into a profile-keyed service, ExtensionSystem. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no crashy Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/extensions/extension_service_unittest.h" 5 #include "chrome/browser/extensions/extension_service_unittest.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 15 matching lines...) Expand all
26 #include "base/utf_string_conversions.h" 26 #include "base/utf_string_conversions.h"
27 #include "base/version.h" 27 #include "base/version.h"
28 #include "chrome/browser/browser_process.h" 28 #include "chrome/browser/browser_process.h"
29 #include "chrome/browser/extensions/component_loader.h" 29 #include "chrome/browser/extensions/component_loader.h"
30 #include "chrome/browser/extensions/crx_installer.h" 30 #include "chrome/browser/extensions/crx_installer.h"
31 #include "chrome/browser/extensions/extension_creator.h" 31 #include "chrome/browser/extensions/extension_creator.h"
32 #include "chrome/browser/extensions/extension_error_reporter.h" 32 #include "chrome/browser/extensions/extension_error_reporter.h"
33 #include "chrome/browser/extensions/extension_service.h" 33 #include "chrome/browser/extensions/extension_service.h"
34 #include "chrome/browser/extensions/extension_special_storage_policy.h" 34 #include "chrome/browser/extensions/extension_special_storage_policy.h"
35 #include "chrome/browser/extensions/extension_sync_data.h" 35 #include "chrome/browser/extensions/extension_sync_data.h"
36 #include "chrome/browser/extensions/extension_system.h"
37 #include "chrome/browser/extensions/extension_system_factory.h"
36 #include "chrome/browser/extensions/extension_updater.h" 38 #include "chrome/browser/extensions/extension_updater.h"
37 #include "chrome/browser/extensions/external_extension_provider_impl.h" 39 #include "chrome/browser/extensions/external_extension_provider_impl.h"
38 #include "chrome/browser/extensions/external_extension_provider_interface.h" 40 #include "chrome/browser/extensions/external_extension_provider_interface.h"
39 #include "chrome/browser/extensions/external_pref_extension_loader.h" 41 #include "chrome/browser/extensions/external_pref_extension_loader.h"
40 #include "chrome/browser/extensions/extension_sorting.h" 42 #include "chrome/browser/extensions/extension_sorting.h"
41 #include "chrome/browser/extensions/installed_loader.h" 43 #include "chrome/browser/extensions/installed_loader.h"
42 #include "chrome/browser/extensions/pack_extension_job.cc" 44 #include "chrome/browser/extensions/pack_extension_job.cc"
43 #include "chrome/browser/extensions/pending_extension_info.h" 45 #include "chrome/browser/extensions/pending_extension_info.h"
44 #include "chrome/browser/extensions/pending_extension_manager.h" 46 #include "chrome/browser/extensions/pending_extension_manager.h"
47 #include "chrome/browser/extensions/test_extension_system.h"
45 #include "chrome/browser/extensions/unpacked_installer.h" 48 #include "chrome/browser/extensions/unpacked_installer.h"
46 #include "chrome/browser/plugin_prefs_factory.h" 49 #include "chrome/browser/plugin_prefs_factory.h"
47 #include "chrome/browser/prefs/browser_prefs.h" 50 #include "chrome/browser/prefs/browser_prefs.h"
48 #include "chrome/browser/prefs/pref_service_mock_builder.h" 51 #include "chrome/browser/prefs/pref_service_mock_builder.h"
49 #include "chrome/browser/prefs/scoped_user_pref_update.h" 52 #include "chrome/browser/prefs/scoped_user_pref_update.h"
50 #include "chrome/browser/sync/protocol/app_specifics.pb.h" 53 #include "chrome/browser/sync/protocol/app_specifics.pb.h"
51 #include "chrome/browser/sync/protocol/extension_specifics.pb.h" 54 #include "chrome/browser/sync/protocol/extension_specifics.pb.h"
55 #include "chrome/common/chrome_constants.h"
52 #include "chrome/common/chrome_notification_types.h" 56 #include "chrome/common/chrome_notification_types.h"
53 #include "chrome/common/chrome_paths.h" 57 #include "chrome/common/chrome_paths.h"
54 #include "chrome/common/chrome_switches.h" 58 #include "chrome/common/chrome_switches.h"
55 #include "chrome/common/extensions/extension.h" 59 #include "chrome/common/extensions/extension.h"
56 #include "chrome/common/extensions/extension_constants.h" 60 #include "chrome/common/extensions/extension_constants.h"
57 #include "chrome/common/extensions/extension_resource.h" 61 #include "chrome/common/extensions/extension_resource.h"
58 #include "chrome/common/extensions/url_pattern.h" 62 #include "chrome/common/extensions/url_pattern.h"
59 #include "chrome/common/pref_names.h" 63 #include "chrome/common/pref_names.h"
60 #include "chrome/common/string_ordinal.h" 64 #include "chrome/common/string_ordinal.h"
61 #include "chrome/common/url_constants.h" 65 #include "chrome/common/url_constants.h"
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 362
359 private: 363 private:
360 int ids_found_; 364 int ids_found_;
361 FilePath fake_base_path_; 365 FilePath fake_base_path_;
362 scoped_ptr<ExternalExtensionProviderImpl> provider_; 366 scoped_ptr<ExternalExtensionProviderImpl> provider_;
363 scoped_ptr<DictionaryValue> prefs_; 367 scoped_ptr<DictionaryValue> prefs_;
364 368
365 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor); 369 DISALLOW_COPY_AND_ASSIGN(MockProviderVisitor);
366 }; 370 };
367 371
368 class ExtensionTestingProfile : public TestingProfile {
369 public:
370 ExtensionTestingProfile() : service_(NULL) {
371 }
372
373 void set_extensions_service(ExtensionService* service) {
374 service_ = service;
375 }
376 virtual ExtensionService* GetExtensionService() { return service_; }
377
378 private:
379 ExtensionService* service_;
380 };
381
382 // Our message loop may be used in tests which require it to be an IO loop. 372 // Our message loop may be used in tests which require it to be an IO loop.
383 ExtensionServiceTestBase::ExtensionServiceTestBase() 373 ExtensionServiceTestBase::ExtensionServiceTestBase()
384 : loop_(MessageLoop::TYPE_IO), 374 : loop_(MessageLoop::TYPE_IO),
385 service_(NULL), 375 service_(NULL),
386 expected_extensions_count_(0), 376 expected_extensions_count_(0),
387 ui_thread_(BrowserThread::UI, &loop_), 377 ui_thread_(BrowserThread::UI, &loop_),
388 db_thread_(BrowserThread::DB, &loop_), 378 db_thread_(BrowserThread::DB, &loop_),
389 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &loop_), 379 webkit_thread_(BrowserThread::WEBKIT_DEPRECATED, &loop_),
390 file_thread_(BrowserThread::FILE, &loop_), 380 file_thread_(BrowserThread::FILE, &loop_),
391 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, &loop_), 381 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, &loop_),
(...skipping 12 matching lines...) Expand all
404 // (they are used in the destruction process). 394 // (they are used in the destruction process).
405 service_ = NULL; 395 service_ = NULL;
406 MessageLoop::current()->RunAllPending(); 396 MessageLoop::current()->RunAllPending();
407 profile_.reset(NULL); 397 profile_.reset(NULL);
408 MessageLoop::current()->RunAllPending(); 398 MessageLoop::current()->RunAllPending();
409 } 399 }
410 400
411 void ExtensionServiceTestBase::InitializeExtensionService( 401 void ExtensionServiceTestBase::InitializeExtensionService(
412 const FilePath& pref_file, const FilePath& extensions_install_dir, 402 const FilePath& pref_file, const FilePath& extensions_install_dir,
413 bool autoupdate_enabled) { 403 bool autoupdate_enabled) {
414 ExtensionTestingProfile* profile = new ExtensionTestingProfile(); 404 TestingProfile* profile = new TestingProfile();
415 // Create a PrefService that only contains user defined preference values. 405 // Create a PrefService that only contains user defined preference values.
416 PrefService* prefs = 406 PrefService* prefs =
417 PrefServiceMockBuilder().WithUserFilePrefs(pref_file).Create(); 407 PrefServiceMockBuilder().WithUserFilePrefs(pref_file).Create();
418 Profile::RegisterUserPrefs(prefs); 408 Profile::RegisterUserPrefs(prefs);
419 browser::RegisterUserPrefs(prefs); 409 browser::RegisterUserPrefs(prefs);
420 profile->SetPrefService(prefs); 410 profile->SetPrefService(prefs);
421 411
422 PluginPrefsFactory::GetInstance()->ForceRegisterPrefsForTest(prefs); 412 PluginPrefsFactory::GetInstance()->ForceRegisterPrefsForTest(prefs);
423 413
424 profile_.reset(profile); 414 profile_.reset(profile);
425 415
426 service_ = profile->CreateExtensionService( 416 service_ = static_cast<TestExtensionSystem*>(
427 CommandLine::ForCurrentProcess(), 417 ExtensionSystemFactory::GetForProfile(profile))->CreateExtensionService(
428 extensions_install_dir, 418 CommandLine::ForCurrentProcess(),
429 autoupdate_enabled); 419 extensions_install_dir,
420 autoupdate_enabled);
430 service_->set_extensions_enabled(true); 421 service_->set_extensions_enabled(true);
431 service_->set_show_extensions_prompts(false); 422 service_->set_show_extensions_prompts(false);
432 profile->set_extensions_service(service_);
433 423
434 // When we start up, we want to make sure there is no external provider, 424 // When we start up, we want to make sure there is no external provider,
435 // since the ExtensionService on Windows will use the Registry as a default 425 // since the ExtensionService on Windows will use the Registry as a default
436 // provider and if there is something already registered there then it will 426 // provider and if there is something already registered there then it will
437 // interfere with the tests. Those tests that need an external provider 427 // interfere with the tests. Those tests that need an external provider
438 // will register one specifically. 428 // will register one specifically.
439 service_->ClearProvidersForTesting(); 429 service_->ClearProvidersForTesting();
440 430
441 expected_extensions_count_ = 0; 431 expected_extensions_count_ = 0;
442 } 432 }
(...skipping 13 matching lines...) Expand all
456 file_util::CopyDirectory(source_install_dir, extensions_install_dir_, true); 446 file_util::CopyDirectory(source_install_dir, extensions_install_dir_, true);
457 447
458 InitializeExtensionService(temp_prefs, extensions_install_dir_, false); 448 InitializeExtensionService(temp_prefs, extensions_install_dir_, false);
459 } 449 }
460 450
461 void ExtensionServiceTestBase::InitializeEmptyExtensionService() { 451 void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
462 InitializeExtensionServiceHelper(false); 452 InitializeExtensionServiceHelper(false);
463 } 453 }
464 454
465 void ExtensionServiceTestBase::InitializeExtensionProcessManager() { 455 void ExtensionServiceTestBase::InitializeExtensionProcessManager() {
466 profile_->CreateExtensionProcessManager(); 456 static_cast<TestExtensionSystem*>(
457 ExtensionSystemFactory::GetForProfile(profile_.get()))->
458 CreateExtensionProcessManager();
467 } 459 }
468 460
469 void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() { 461 void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
470 InitializeExtensionServiceHelper(true); 462 InitializeExtensionServiceHelper(true);
471 service_->updater()->Start(); 463 service_->updater()->Start();
472 } 464 }
473 465
474 void ExtensionServiceTestBase::InitializeExtensionServiceHelper( 466 void ExtensionServiceTestBase::InitializeExtensionServiceHelper(
475 bool autoupdate_enabled) { 467 bool autoupdate_enabled) {
476 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 468 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
477 FilePath path_ = temp_dir_.path(); 469 FilePath path_ = temp_dir_.path();
478 path_ = path_.Append(FILE_PATH_LITERAL("TestingExtensionsPath")); 470 path_ = path_.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
479 file_util::Delete(path_, true); 471 file_util::Delete(path_, true);
480 file_util::CreateDirectory(path_); 472 file_util::CreateDirectory(path_);
481 FilePath prefs_filename = path_ 473 FilePath prefs_filename = path_
482 .Append(FILE_PATH_LITERAL("TestPreferences")); 474 .Append(FILE_PATH_LITERAL("TestPreferences"));
483 extensions_install_dir_ = path_.Append(FILE_PATH_LITERAL("Extensions")); 475 extensions_install_dir_ = path_.Append(FILE_PATH_LITERAL("Extensions"));
484 file_util::Delete(extensions_install_dir_, true); 476 file_util::Delete(extensions_install_dir_, true);
485 file_util::CreateDirectory(extensions_install_dir_); 477 file_util::CreateDirectory(extensions_install_dir_);
486 478
487 InitializeExtensionService(prefs_filename, extensions_install_dir_, 479 InitializeExtensionService(prefs_filename, extensions_install_dir_,
488 autoupdate_enabled); 480 autoupdate_enabled);
489 } 481 }
490 482
491 void ExtensionServiceTestBase::InitializeRequestContext() { 483 void ExtensionServiceTestBase::InitializeRequestContext() {
492 ASSERT_TRUE(profile_.get()); 484 ASSERT_TRUE(profile_.get());
493 ExtensionTestingProfile* profile = 485 TestingProfile* profile =
494 static_cast<ExtensionTestingProfile*>(profile_.get()); 486 static_cast<TestingProfile*>(profile_.get());
495 profile->CreateRequestContext(); 487 profile->CreateRequestContext();
496 } 488 }
497 489
498 // static 490 // static
499 void ExtensionServiceTestBase::SetUpTestCase() { 491 void ExtensionServiceTestBase::SetUpTestCase() {
500 ExtensionErrorReporter::Init(false); // no noisy errors 492 ExtensionErrorReporter::Init(false); // no noisy errors
501 } 493 }
502 494
503 void ExtensionServiceTestBase::SetUp() { 495 void ExtensionServiceTestBase::SetUp() {
504 ExtensionErrorReporter::GetInstance()->ClearErrors(); 496 ExtensionErrorReporter::GetInstance()->ClearErrors();
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 int expected_installed_extension_count = 723 int expected_installed_extension_count =
732 (expected_state >= INSTALLED) ? 1 : 0; 724 (expected_state >= INSTALLED) ? 1 : 0;
733 int expected_enabled_extension_count = 725 int expected_enabled_extension_count =
734 (expected_state >= ENABLED) ? 1 : 0; 726 (expected_state >= ENABLED) ? 1 : 0;
735 EXPECT_EQ(expected_installed_extension_count, 727 EXPECT_EQ(expected_installed_extension_count,
736 installed_extension_count); 728 installed_extension_count);
737 EXPECT_EQ(expected_enabled_extension_count, 729 EXPECT_EQ(expected_enabled_extension_count,
738 enabled_extension_count); 730 enabled_extension_count);
739 } 731 }
740 732
741 // Update() should delete the temporary input file. 733 // Update() should the temporary input file.
742 EXPECT_FALSE(file_util::PathExists(path)); 734 EXPECT_FALSE(file_util::PathExists(path));
743 } 735 }
744 736
745 void TerminateExtension(const std::string& id) { 737 void TerminateExtension(const std::string& id) {
746 const Extension* extension = service_->GetInstalledExtension(id); 738 const Extension* extension = service_->GetInstalledExtension(id);
747 if (!extension) { 739 if (!extension) {
748 ADD_FAILURE(); 740 ADD_FAILURE();
749 return; 741 return;
750 } 742 }
751 service_->TrackTerminatedExtensionForTest(extension); 743 service_->TrackTerminatedExtensionForTest(extension);
(...skipping 3061 matching lines...) Expand 10 before | Expand all | Expand 10 after
3813 content::TestBrowserThread ui_thread(BrowserThread::UI, &loop); 3805 content::TestBrowserThread ui_thread(BrowserThread::UI, &loop);
3814 content::TestBrowserThread file_thread(BrowserThread::FILE, &loop); 3806 content::TestBrowserThread file_thread(BrowserThread::FILE, &loop);
3815 scoped_ptr<CommandLine> command_line; 3807 scoped_ptr<CommandLine> command_line;
3816 FilePath install_dir = profile->GetPath() 3808 FilePath install_dir = profile->GetPath()
3817 .AppendASCII(ExtensionService::kInstallDirectoryName); 3809 .AppendASCII(ExtensionService::kInstallDirectoryName);
3818 webkit::npapi::MockPluginList plugin_list(NULL, 0); 3810 webkit::npapi::MockPluginList plugin_list(NULL, 0);
3819 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list); 3811 PluginService::GetInstance()->SetPluginListForTesting(&plugin_list);
3820 3812
3821 // By default, we are enabled. 3813 // By default, we are enabled.
3822 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM)); 3814 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
3823 // Owned by |profile|. 3815 ExtensionService* service = static_cast<TestExtensionSystem*>(
3824 ExtensionService* service = 3816 ExtensionSystemFactory::GetForProfile(profile.get()))->
3825 profile->CreateExtensionService(command_line.get(), 3817 CreateExtensionService(
3826 install_dir, 3818 command_line.get(),
3827 false); 3819 install_dir,
3820 false);
3828 EXPECT_TRUE(service->extensions_enabled()); 3821 EXPECT_TRUE(service->extensions_enabled());
3829 service->Init(); 3822 service->Init();
3830 loop.RunAllPending(); 3823 loop.RunAllPending();
3831 EXPECT_TRUE(recorder.ready()); 3824 EXPECT_TRUE(recorder.ready());
3832 3825
3833 // If either the command line or pref is set, we are disabled. 3826 // If either the command line or pref is set, we are disabled.
3834 recorder.set_ready(false); 3827 recorder.set_ready(false);
3835 profile.reset(new TestingProfile()); 3828 profile.reset(new TestingProfile());
3836 command_line->AppendSwitch(switches::kDisableExtensions); 3829 command_line->AppendSwitch(switches::kDisableExtensions);
3837 service = profile->CreateExtensionService(command_line.get(), 3830 service = static_cast<TestExtensionSystem*>(
3838 install_dir, 3831 ExtensionSystemFactory::GetForProfile(profile.get()))->
3839 false); 3832 CreateExtensionService(
3833 command_line.get(),
3834 install_dir,
3835 false);
3840 EXPECT_FALSE(service->extensions_enabled()); 3836 EXPECT_FALSE(service->extensions_enabled());
3841 service->Init(); 3837 service->Init();
3842 loop.RunAllPending(); 3838 loop.RunAllPending();
3843 EXPECT_TRUE(recorder.ready()); 3839 EXPECT_TRUE(recorder.ready());
3844 3840
3845 recorder.set_ready(false); 3841 recorder.set_ready(false);
3846 profile.reset(new TestingProfile()); 3842 profile.reset(new TestingProfile());
3847 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true); 3843 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
3848 service = profile->CreateExtensionService(command_line.get(), 3844 service = static_cast<TestExtensionSystem*>(
3849 install_dir, 3845 ExtensionSystemFactory::GetForProfile(profile.get()))->
3850 false); 3846 CreateExtensionService(
3847 command_line.get(),
3848 install_dir,
3849 false);
3851 EXPECT_FALSE(service->extensions_enabled()); 3850 EXPECT_FALSE(service->extensions_enabled());
3852 service->Init(); 3851 service->Init();
3853 loop.RunAllPending(); 3852 loop.RunAllPending();
3854 EXPECT_TRUE(recorder.ready()); 3853 EXPECT_TRUE(recorder.ready());
3855 3854
3856 recorder.set_ready(false); 3855 recorder.set_ready(false);
3857 profile.reset(new TestingProfile()); 3856 profile.reset(new TestingProfile());
3858 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true); 3857 profile->GetPrefs()->SetBoolean(prefs::kDisableExtensions, true);
3859 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM)); 3858 command_line.reset(new CommandLine(CommandLine::NO_PROGRAM));
3860 service = profile->CreateExtensionService(command_line.get(), 3859 service = static_cast<TestExtensionSystem*>(
3861 install_dir, 3860 ExtensionSystemFactory::GetForProfile(profile.get()))->
3862 false); 3861 CreateExtensionService(
3862 command_line.get(),
3863 install_dir,
3864 false);
3863 EXPECT_FALSE(service->extensions_enabled()); 3865 EXPECT_FALSE(service->extensions_enabled());
3864 service->Init(); 3866 service->Init();
3865 loop.RunAllPending(); 3867 loop.RunAllPending();
3866 EXPECT_TRUE(recorder.ready()); 3868 EXPECT_TRUE(recorder.ready());
3867 3869
3868 // Explicitly delete all the resources used in this test. 3870 // Explicitly delete all the resources used in this test.
3869 profile.reset(); 3871 profile.reset();
3870 service = NULL; 3872 service = NULL;
3871 } 3873 }
3872 3874
(...skipping 911 matching lines...) Expand 10 before | Expand all | Expand 10 after
4784 ASSERT_FALSE(AddPendingSyncInstall()); 4786 ASSERT_FALSE(AddPendingSyncInstall());
4785 4787
4786 // Wait for the external source to install. 4788 // Wait for the external source to install.
4787 WaitForCrxInstall(crx_path_, INSTALL_NEW); 4789 WaitForCrxInstall(crx_path_, INSTALL_NEW);
4788 ASSERT_TRUE(IsCrxInstalled()); 4790 ASSERT_TRUE(IsCrxInstalled());
4789 4791
4790 // Now that the extension is installed, sync request should fail 4792 // Now that the extension is installed, sync request should fail
4791 // because the extension is already installed. 4793 // because the extension is already installed.
4792 ASSERT_FALSE(AddPendingSyncInstall()); 4794 ASSERT_FALSE(AddPendingSyncInstall());
4793 } 4795 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698