blob: e76013aac47e482a14be5624aab1da57def58931 [file] [log] [blame]
[email protected]f484f8d52014-06-12 08:38:181// 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"
thestig18dfb7a52014-08-26 10:44:048#include "base/files/file_util.h"
[email protected]f484f8d52014-06-12 08:38:189#include "base/memory/ref_counted.h"
[email protected]f484f8d52014-06-12 08:38:1810#include "base/path_service.h"
pranay.kumar07300782015-05-04 14:03:5811#include "base/thread_task_runner_handle.h"
jam726de9f2015-06-02 15:36:0612#include "chrome/browser/extensions/component_loader.h"
[email protected]f484f8d52014-06-12 08:38:1813#include "chrome/browser/extensions/extension_error_reporter.h"
14#include "chrome/browser/extensions/extension_garbage_collector_factory.h"
15#include "chrome/browser/extensions/extension_service.h"
16#include "chrome/browser/extensions/test_extension_system.h"
17#include "chrome/browser/extensions/updater/extension_updater.h"
18#include "chrome/browser/prefs/browser_prefs.h"
[email protected]f484f8d52014-06-12 08:38:1819#include "chrome/common/chrome_constants.h"
20#include "chrome/common/chrome_paths.h"
anthonyvd404d6102015-02-28 00:04:2321#include "chrome/test/base/testing_browser_process.h"
[email protected]f484f8d52014-06-12 08:38:1822#include "chrome/test/base/testing_profile.h"
23#include "components/pref_registry/pref_registry_syncable.h"
sdefresne875d0782015-09-16 12:01:2824#include "components/syncable_prefs/pref_service_mock_factory.h"
25#include "components/syncable_prefs/pref_service_syncable.h"
[email protected]f484f8d52014-06-12 08:38:1826#include "content/public/browser/browser_context.h"
27#include "extensions/browser/extension_prefs.h"
28#include "extensions/browser/extension_registry.h"
29
30#if defined(OS_CHROMEOS)
31#include "chrome/browser/chromeos/extensions/install_limiter.h"
32#endif
33
34namespace extensions {
35
36namespace {
37
rdevlin.cronin529ad1a92015-02-26 21:27:3038// By default, we run on the IO loop.
39const int kThreadOptions = content::TestBrowserThreadBundle::IO_MAINLOOP;
40
[email protected]f484f8d52014-06-12 08:38:1841// Create a testing profile according to |params|.
42scoped_ptr<TestingProfile> BuildTestingProfile(
43 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) {
44 TestingProfile::Builder profile_builder;
45 // Create a PrefService that only contains user defined preference values.
46 PrefServiceMockFactory factory;
47 // If pref_file is empty, TestingProfile automatically creates
48 // TestingPrefServiceSyncable instance.
49 if (!params.pref_file.empty()) {
50 factory.SetUserPrefsFile(params.pref_file,
pranay.kumar07300782015-05-04 14:03:5851 base::ThreadTaskRunnerHandle::Get().get());
[email protected]f484f8d52014-06-12 08:38:1852 scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
53 new user_prefs::PrefRegistrySyncable);
54 scoped_ptr<PrefServiceSyncable> prefs(
55 factory.CreateSyncable(registry.get()));
56 chrome::RegisterUserProfilePrefs(registry.get());
57 profile_builder.SetPrefService(prefs.Pass());
58 }
59
[email protected]d20d0432014-06-12 17:14:0560 if (params.profile_is_supervised)
61 profile_builder.SetSupervisedUserId("asdf");
[email protected]f484f8d52014-06-12 08:38:1862
63 profile_builder.SetPath(params.profile_path);
64 return profile_builder.Build();
65}
66
67} // namespace
68
69ExtensionServiceTestBase::ExtensionServiceInitParams::
70 ExtensionServiceInitParams()
[email protected]d20d0432014-06-12 17:14:0571 : autoupdate_enabled(false),
72 is_first_run(true),
73 profile_is_supervised(false) {
[email protected]f484f8d52014-06-12 08:38:1874}
75
[email protected]f484f8d52014-06-12 08:38:1876ExtensionServiceTestBase::ExtensionServiceTestBase()
skyostil0becb332015-04-27 17:59:3777 : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions)),
78 service_(NULL),
anthonyvd404d6102015-02-28 00:04:2379 testing_local_state_(TestingBrowserProcess::GetGlobal()),
rdevlin.cronin529ad1a92015-02-26 21:27:3080 did_reset_thread_bundle_(false),
[email protected]f484f8d52014-06-12 08:38:1881 registry_(NULL) {
82 base::FilePath test_data_dir;
83 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir)) {
84 ADD_FAILURE();
85 return;
86 }
87 data_dir_ = test_data_dir.AppendASCII("extensions");
88}
89
90ExtensionServiceTestBase::~ExtensionServiceTestBase() {
rdevlin.cronin529ad1a92015-02-26 21:27:3091 // Parts of destruction have to happen on an IO thread, so if the thread
92 // bundle is reset, we need to change it back.
93 if (did_reset_thread_bundle_)
94 ResetThreadBundle(kThreadOptions);
95
[email protected]f484f8d52014-06-12 08:38:1896 // Why? Because |profile_| has to be destroyed before |at_exit_manager_|, but
97 // is declared above it in the class definition since it's protected.
98 profile_.reset();
99}
100
101ExtensionServiceTestBase::ExtensionServiceInitParams
102ExtensionServiceTestBase::CreateDefaultInitParams() {
103 ExtensionServiceInitParams params;
104 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
105 base::FilePath path = temp_dir_.path();
106 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
107 EXPECT_TRUE(base::DeleteFile(path, true));
108 base::File::Error error = base::File::FILE_OK;
109 EXPECT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error;
110 base::FilePath prefs_filename =
111 path.Append(FILE_PATH_LITERAL("TestPreferences"));
112 base::FilePath extensions_install_dir =
113 path.Append(FILE_PATH_LITERAL("Extensions"));
114 EXPECT_TRUE(base::DeleteFile(extensions_install_dir, true));
115 EXPECT_TRUE(base::CreateDirectoryAndGetError(extensions_install_dir, &error))
116 << error;
117
118 params.profile_path = path;
119 params.pref_file = prefs_filename;
120 params.extensions_install_dir = extensions_install_dir;
121 return params;
122}
123
124void ExtensionServiceTestBase::InitializeExtensionService(
125 const ExtensionServiceTestBase::ExtensionServiceInitParams& params) {
126 profile_ = BuildTestingProfile(params);
127 CreateExtensionService(params);
128
129 extensions_install_dir_ = params.extensions_install_dir;
130 registry_ = ExtensionRegistry::Get(profile_.get());
131
132 // Garbage collector is typically NULL during tests, so give it a build.
133 ExtensionGarbageCollectorFactory::GetInstance()->SetTestingFactoryAndUse(
134 profile_.get(), &ExtensionGarbageCollectorFactory::BuildInstanceFor);
135}
136
137void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
138 InitializeExtensionService(CreateDefaultInitParams());
139}
140
141void ExtensionServiceTestBase::InitializeInstalledExtensionService(
142 const base::FilePath& prefs_file,
143 const base::FilePath& source_install_dir) {
144 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
145 base::FilePath path = temp_dir_.path();
146
147 path = path.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
148 ASSERT_TRUE(base::DeleteFile(path, true));
149
150 base::File::Error error = base::File::FILE_OK;
151 ASSERT_TRUE(base::CreateDirectoryAndGetError(path, &error)) << error;
152
153 base::FilePath temp_prefs = path.Append(chrome::kPreferencesFilename);
154 ASSERT_TRUE(base::CopyFile(prefs_file, temp_prefs));
155
156 base::FilePath extensions_install_dir =
157 path.Append(FILE_PATH_LITERAL("Extensions"));
158 ASSERT_TRUE(base::DeleteFile(extensions_install_dir, true));
159 ASSERT_TRUE(
160 base::CopyDirectory(source_install_dir, extensions_install_dir, true));
161
162 ExtensionServiceInitParams params;
163 params.profile_path = path;
164 params.pref_file = temp_prefs;
165 params.extensions_install_dir = extensions_install_dir;
166 InitializeExtensionService(params);
167}
168
169void ExtensionServiceTestBase::InitializeGoodInstalledExtensionService() {
170 base::FilePath source_install_dir =
171 data_dir_.AppendASCII("good").AppendASCII("Extensions");
172 base::FilePath pref_path =
173 source_install_dir.DirName().Append(chrome::kPreferencesFilename);
174 InitializeInstalledExtensionService(pref_path, source_install_dir);
175}
176
177void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
178 ExtensionServiceInitParams params = CreateDefaultInitParams();
179 params.autoupdate_enabled = true;
180 InitializeExtensionService(params);
181 service_->updater()->Start();
[email protected]f484f8d52014-06-12 08:38:18182}
183
rdevlin.cronin529ad1a92015-02-26 21:27:30184void ExtensionServiceTestBase::ResetThreadBundle(int options) {
185 did_reset_thread_bundle_ = true;
186 thread_bundle_.reset();
187 thread_bundle_.reset(new content::TestBrowserThreadBundle(options));
188}
189
[email protected]f484f8d52014-06-12 08:38:18190void ExtensionServiceTestBase::SetUp() {
191 ExtensionErrorReporter::GetInstance()->ClearErrors();
192}
193
194void ExtensionServiceTestBase::SetUpTestCase() {
195 // Safe to call multiple times.
196 ExtensionErrorReporter::Init(false); // no noisy errors.
197}
198
199// These are declared in the .cc so that all inheritors don't need to know
200// that TestingProfile derives Profile derives BrowserContext.
201content::BrowserContext* ExtensionServiceTestBase::browser_context() {
202 return profile_.get();
203}
204
205Profile* ExtensionServiceTestBase::profile() {
206 return profile_.get();
207}
208
209void ExtensionServiceTestBase::CreateExtensionService(
210 const ExtensionServiceInitParams& params) {
211 TestExtensionSystem* system =
212 static_cast<TestExtensionSystem*>(ExtensionSystem::Get(profile_.get()));
juncaicf523332015-06-04 00:14:04213 if (!params.is_first_run)
214 ExtensionPrefs::Get(profile_.get())->SetAlertSystemFirstRun();
[email protected]f484f8d52014-06-12 08:38:18215
216 service_ =
217 system->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
218 params.extensions_install_dir,
219 params.autoupdate_enabled);
220
221 service_->SetFileTaskRunnerForTesting(
pranay.kumar07300782015-05-04 14:03:58222 base::ThreadTaskRunnerHandle::Get().get());
[email protected]f484f8d52014-06-12 08:38:18223 service_->set_extensions_enabled(true);
224 service_->set_show_extensions_prompts(false);
225 service_->set_install_updates_when_idle_for_test(false);
jam726de9f2015-06-02 15:36:06226 service_->component_loader()->set_ignore_whitelist_for_testing(true);
[email protected]f484f8d52014-06-12 08:38:18227
228 // When we start up, we want to make sure there is no external provider,
229 // since the ExtensionService on Windows will use the Registry as a default
230 // provider and if there is something already registered there then it will
231 // interfere with the tests. Those tests that need an external provider
232 // will register one specifically.
233 service_->ClearProvidersForTesting();
234
235#if defined(OS_CHROMEOS)
236 InstallLimiter::Get(profile_.get())->DisableForTest();
237#endif
238}
239
240} // namespace extensions