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.
5 #include "chrome/browser/extensions/extension_service_test_base.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/path_service.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/extensions/component_loader.h"
13 #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"
19 #include "chrome/browser/prefs/pref_service_mock_factory.h"
20 #include "chrome/browser/prefs/pref_service_syncable.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "content/public/browser/browser_context.h"
27 #include "extensions/browser/extension_prefs.h"
28 #include "extensions/browser/extension_registry.h"
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/extensions/install_limiter.h"
34 namespace extensions
{
38 // By default, we run on the IO loop.
39 const int kThreadOptions
= content::TestBrowserThreadBundle::IO_MAINLOOP
;
41 // Create a testing profile according to |params|.
42 scoped_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
,
51 base::ThreadTaskRunnerHandle::Get().get());
52 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());
60 if (params
.profile_is_supervised
)
61 profile_builder
.SetSupervisedUserId("asdf");
63 profile_builder
.SetPath(params
.profile_path
);
64 return profile_builder
.Build();
69 ExtensionServiceTestBase::ExtensionServiceInitParams::
70 ExtensionServiceInitParams()
71 : autoupdate_enabled(false),
73 profile_is_supervised(false) {
76 ExtensionServiceTestBase::ExtensionServiceTestBase()
77 : thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions
)),
79 testing_local_state_(TestingBrowserProcess::GetGlobal()),
80 did_reset_thread_bundle_(false),
82 base::FilePath test_data_dir
;
83 if (!PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
)) {
87 data_dir_
= test_data_dir
.AppendASCII("extensions");
90 ExtensionServiceTestBase::~ExtensionServiceTestBase() {
91 // 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
);
96 // 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.
101 ExtensionServiceTestBase::ExtensionServiceInitParams
102 ExtensionServiceTestBase::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
))
118 params
.profile_path
= path
;
119 params
.pref_file
= prefs_filename
;
120 params
.extensions_install_dir
= extensions_install_dir
;
124 void ExtensionServiceTestBase::InitializeExtensionService(
125 const ExtensionServiceTestBase::ExtensionServiceInitParams
& params
) {
126 profile_
= BuildTestingProfile(params
);
127 CreateExtensionService(params
);
129 extensions_install_dir_
= params
.extensions_install_dir
;
130 registry_
= ExtensionRegistry::Get(profile_
.get());
132 // Garbage collector is typically NULL during tests, so give it a build.
133 ExtensionGarbageCollectorFactory::GetInstance()->SetTestingFactoryAndUse(
134 profile_
.get(), &ExtensionGarbageCollectorFactory::BuildInstanceFor
);
137 void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
138 InitializeExtensionService(CreateDefaultInitParams());
141 void 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();
147 path
= path
.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
148 ASSERT_TRUE(base::DeleteFile(path
, true));
150 base::File::Error error
= base::File::FILE_OK
;
151 ASSERT_TRUE(base::CreateDirectoryAndGetError(path
, &error
)) << error
;
153 base::FilePath temp_prefs
= path
.Append(chrome::kPreferencesFilename
);
154 ASSERT_TRUE(base::CopyFile(prefs_file
, temp_prefs
));
156 base::FilePath extensions_install_dir
=
157 path
.Append(FILE_PATH_LITERAL("Extensions"));
158 ASSERT_TRUE(base::DeleteFile(extensions_install_dir
, true));
160 base::CopyDirectory(source_install_dir
, extensions_install_dir
, true));
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
);
169 void 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
);
177 void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
178 ExtensionServiceInitParams params
= CreateDefaultInitParams();
179 params
.autoupdate_enabled
= true;
180 InitializeExtensionService(params
);
181 service_
->updater()->Start();
184 void ExtensionServiceTestBase::ResetThreadBundle(int options
) {
185 did_reset_thread_bundle_
= true;
186 thread_bundle_
.reset();
187 thread_bundle_
.reset(new content::TestBrowserThreadBundle(options
));
190 void ExtensionServiceTestBase::SetUp() {
191 ExtensionErrorReporter::GetInstance()->ClearErrors();
194 void ExtensionServiceTestBase::SetUpTestCase() {
195 // Safe to call multiple times.
196 ExtensionErrorReporter::Init(false); // no noisy errors.
199 // These are declared in the .cc so that all inheritors don't need to know
200 // that TestingProfile derives Profile derives BrowserContext.
201 content::BrowserContext
* ExtensionServiceTestBase::browser_context() {
202 return profile_
.get();
205 Profile
* ExtensionServiceTestBase::profile() {
206 return profile_
.get();
209 void ExtensionServiceTestBase::CreateExtensionService(
210 const ExtensionServiceInitParams
& params
) {
211 TestExtensionSystem
* system
=
212 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile_
.get()));
213 if (!params
.is_first_run
)
214 ExtensionPrefs::Get(profile_
.get())->SetAlertSystemFirstRun();
217 system
->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
218 params
.extensions_install_dir
,
219 params
.autoupdate_enabled
);
221 service_
->SetFileTaskRunnerForTesting(
222 base::ThreadTaskRunnerHandle::Get().get());
223 service_
->set_extensions_enabled(true);
224 service_
->set_show_extensions_prompts(false);
225 service_
->set_install_updates_when_idle_for_test(false);
226 service_
->component_loader()->set_ignore_whitelist_for_testing(true);
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();
235 #if defined(OS_CHROMEOS)
236 InstallLimiter::Get(profile_
.get())->DisableForTest();
240 } // namespace extensions