OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/path_service.h" |
| 12 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 13 #include "chrome/browser/extensions/extension_garbage_collector_factory.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/extensions/test_extension_system.h" |
| 16 #include "chrome/browser/extensions/updater/extension_updater.h" |
| 17 #include "chrome/browser/prefs/browser_prefs.h" |
| 18 #include "chrome/browser/prefs/pref_service_mock_factory.h" |
| 19 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 20 #include "chrome/common/chrome_constants.h" |
| 21 #include "chrome/common/chrome_paths.h" |
| 22 #include "chrome/test/base/testing_profile.h" |
| 23 #include "components/pref_registry/pref_registry_syncable.h" |
| 24 #include "content/public/browser/browser_context.h" |
| 25 #include "extensions/browser/extension_prefs.h" |
| 26 #include "extensions/browser/extension_registry.h" |
| 27 |
| 28 #if defined(OS_CHROMEOS) |
| 29 #include "chrome/browser/chromeos/extensions/install_limiter.h" |
| 30 #endif |
| 31 |
| 32 namespace extensions { |
| 33 |
| 34 namespace { |
| 35 |
| 36 // Create a testing profile according to |params|. |
| 37 scoped_ptr<TestingProfile> BuildTestingProfile( |
| 38 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) { |
| 39 TestingProfile::Builder profile_builder; |
| 40 // Create a PrefService that only contains user defined preference values. |
| 41 PrefServiceMockFactory factory; |
| 42 // If pref_file is empty, TestingProfile automatically creates |
| 43 // TestingPrefServiceSyncable instance. |
| 44 if (!params.pref_file.empty()) { |
| 45 factory.SetUserPrefsFile(params.pref_file, |
| 46 base::MessageLoopProxy::current().get()); |
| 47 scoped_refptr<user_prefs::PrefRegistrySyncable> registry( |
| 48 new user_prefs::PrefRegistrySyncable); |
| 49 scoped_ptr<PrefServiceSyncable> prefs( |
| 50 factory.CreateSyncable(registry.get())); |
| 51 chrome::RegisterUserProfilePrefs(registry.get()); |
| 52 profile_builder.SetPrefService(prefs.Pass()); |
| 53 } |
| 54 |
| 55 if (params.profile_is_managed) |
| 56 profile_builder.SetManagedUserId("asdf"); |
| 57 |
| 58 profile_builder.SetPath(params.profile_path); |
| 59 return profile_builder.Build(); |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 |
| 64 ExtensionServiceTestBase::ExtensionServiceInitParams:: |
| 65 ExtensionServiceInitParams() |
| 66 : autoupdate_enabled(false), is_first_run(true), profile_is_managed(false) { |
| 67 } |
| 68 |
| 69 // Our message loop may be used in tests which require it to be an IO loop. |
| 70 ExtensionServiceTestBase::ExtensionServiceTestBase() |
| 71 : service_(NULL), |
| 72 thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 73 registry_(NULL) { |
| 74 base::FilePath test_data_dir; |
| 75 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) { |
| 76 ADD_FAILURE(); |
| 77 return; |
| 78 } |
| 79 data_dir_ = test_data_dir.AppendASCII("extensions"); |
| 80 } |
| 81 |
| 82 ExtensionServiceTestBase::~ExtensionServiceTestBase() { |
| 83 // Why? Because |profile_| has to be destroyed before |at_exit_manager_|, but |
| 84 // is declared above it in the class definition since it's protected. |
| 85 profile_.reset(); |
| 86 } |
| 87 |
| 88 ExtensionServiceTestBase::ExtensionServiceInitParams |
| 89 ExtensionServiceTestBase::CreateDefaultInitParams() { |
| 90 ExtensionServiceInitParams params; |
| 91 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 92 base::FilePath path = temp_dir_.path(); |
| 93 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath")); |
| 94 EXPECT_TRUE(base::DeleteFile(path, true)); |
| 95 base::File::Error error = base::File::FILE_OK; |
| 96 EXPECT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error; |
| 97 base::FilePath prefs_filename = |
| 98 path.Append(FILE_PATH_LITERAL("TestPreferences")); |
| 99 base::FilePath extensions_install_dir = |
| 100 path.Append(FILE_PATH_LITERAL("Extensions")); |
| 101 EXPECT_TRUE(base::DeleteFile(extensions_install_dir, true)); |
| 102 EXPECT_TRUE(base::CreateDirectoryAndGetError(extensions_install_dir, &error)) |
| 103 << error; |
| 104 |
| 105 params.profile_path = path; |
| 106 params.pref_file = prefs_filename; |
| 107 params.extensions_install_dir = extensions_install_dir; |
| 108 return params; |
| 109 } |
| 110 |
| 111 void ExtensionServiceTestBase::InitializeExtensionService( |
| 112 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) { |
| 113 profile_ = BuildTestingProfile(params); |
| 114 CreateExtensionService(params); |
| 115 |
| 116 extensions_install_dir_ = params.extensions_install_dir; |
| 117 registry_ = ExtensionRegistry::Get(profile_.get()); |
| 118 |
| 119 // Garbage collector is typically NULL during tests, so give it a build. |
| 120 ExtensionGarbageCollectorFactory::GetInstance()->SetTestingFactoryAndUse( |
| 121 profile_.get(), &ExtensionGarbageCollectorFactory::BuildInstanceFor); |
| 122 } |
| 123 |
| 124 void ExtensionServiceTestBase::InitializeEmptyExtensionService() { |
| 125 InitializeExtensionService(CreateDefaultInitParams()); |
| 126 } |
| 127 |
| 128 void ExtensionServiceTestBase::InitializeInstalledExtensionService( |
| 129 const base::FilePath& prefs_file, |
| 130 const base::FilePath& source_install_dir) { |
| 131 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 132 base::FilePath path = temp_dir_.path(); |
| 133 |
| 134 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath")); |
| 135 ASSERT_TRUE(base::DeleteFile(path, true)); |
| 136 |
| 137 base::File::Error error = base::File::FILE_OK; |
| 138 ASSERT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error; |
| 139 |
| 140 base::FilePath temp_prefs = path.Append(chrome::kPreferencesFilename); |
| 141 ASSERT_TRUE(base::CopyFile(prefs_file, temp_prefs)); |
| 142 |
| 143 base::FilePath extensions_install_dir = |
| 144 path.Append(FILE_PATH_LITERAL("Extensions")); |
| 145 ASSERT_TRUE(base::DeleteFile(extensions_install_dir, true)); |
| 146 ASSERT_TRUE( |
| 147 base::CopyDirectory(source_install_dir, extensions_install_dir, true)); |
| 148 |
| 149 ExtensionServiceInitParams params; |
| 150 params.profile_path = path; |
| 151 params.pref_file = temp_prefs; |
| 152 params.extensions_install_dir = extensions_install_dir; |
| 153 InitializeExtensionService(params); |
| 154 } |
| 155 |
| 156 void ExtensionServiceTestBase::InitializeGoodInstalledExtensionService() { |
| 157 base::FilePath source_install_dir = |
| 158 data_dir_.AppendASCII("good").AppendASCII("Extensions"); |
| 159 base::FilePath pref_path = |
| 160 source_install_dir.DirName().Append(chrome::kPreferencesFilename); |
| 161 InitializeInstalledExtensionService(pref_path, source_install_dir); |
| 162 } |
| 163 |
| 164 void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() { |
| 165 ExtensionServiceInitParams params = CreateDefaultInitParams(); |
| 166 params.autoupdate_enabled = true; |
| 167 InitializeExtensionService(params); |
| 168 service_->updater()->Start(); |
| 169 } |
| 170 |
| 171 void ExtensionServiceTestBase::InitializeProcessManager() { |
| 172 static_cast<extensions::TestExtensionSystem*>( |
| 173 ExtensionSystem::Get(profile_.get()))->CreateProcessManager(); |
| 174 } |
| 175 |
| 176 void ExtensionServiceTestBase::SetUp() { |
| 177 ExtensionErrorReporter::GetInstance()->ClearErrors(); |
| 178 } |
| 179 |
| 180 void ExtensionServiceTestBase::SetUpTestCase() { |
| 181 // Safe to call multiple times. |
| 182 ExtensionErrorReporter::Init(false); // no noisy errors. |
| 183 } |
| 184 |
| 185 // These are declared in the .cc so that all inheritors don't need to know |
| 186 // that TestingProfile derives Profile derives BrowserContext. |
| 187 content::BrowserContext* ExtensionServiceTestBase::browser_context() { |
| 188 return profile_.get(); |
| 189 } |
| 190 |
| 191 Profile* ExtensionServiceTestBase::profile() { |
| 192 return profile_.get(); |
| 193 } |
| 194 |
| 195 void ExtensionServiceTestBase::CreateExtensionService( |
| 196 const ExtensionServiceInitParams& params) { |
| 197 TestExtensionSystem* system = |
| 198 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get())); |
| 199 if (!params.is_first_run) { |
| 200 ExtensionPrefs* prefs = system->CreateExtensionPrefs( |
| 201 base::CommandLine::ForCurrentProcess(), params.extensions_install_dir); |
| 202 prefs->SetAlertSystemFirstRun(); |
| 203 } |
| 204 |
| 205 service_ = |
| 206 system->CreateExtensionService(base::CommandLine::ForCurrentProcess(), |
| 207 params.extensions_install_dir, |
| 208 params.autoupdate_enabled); |
| 209 |
| 210 service_->SetFileTaskRunnerForTesting( |
| 211 base::MessageLoopProxy::current().get()); |
| 212 service_->set_extensions_enabled(true); |
| 213 service_->set_show_extensions_prompts(false); |
| 214 service_->set_install_updates_when_idle_for_test(false); |
| 215 |
| 216 // When we start up, we want to make sure there is no external provider, |
| 217 // since the ExtensionService on Windows will use the Registry as a default |
| 218 // provider and if there is something already registered there then it will |
| 219 // interfere with the tests. Those tests that need an external provider |
| 220 // will register one specifically. |
| 221 service_->ClearProvidersForTesting(); |
| 222 |
| 223 #if defined(OS_CHROMEOS) |
| 224 InstallLimiter::Get(profile_.get())->DisableForTest(); |
| 225 #endif |
| 226 } |
| 227 |
| 228 } // namespace extensions |
OLD | NEW |