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/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_browser_process.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "components/pref_registry/pref_registry_syncable.h"
25 #include "content/public/browser/browser_context.h"
26 #include "extensions/browser/extension_prefs.h"
27 #include "extensions/browser/extension_registry.h"
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/chromeos/extensions/install_limiter.h"
33 namespace extensions
{
37 // By default, we run on the IO loop.
38 const int kThreadOptions
= content::TestBrowserThreadBundle::IO_MAINLOOP
;
40 // Create a testing profile according to |params|.
41 scoped_ptr
<TestingProfile
> BuildTestingProfile(
42 const ExtensionServiceTestBase::ExtensionServiceInitParams
& params
) {
43 TestingProfile::Builder profile_builder
;
44 // Create a PrefService that only contains user defined preference values.
45 PrefServiceMockFactory factory
;
46 // If pref_file is empty, TestingProfile automatically creates
47 // TestingPrefServiceSyncable instance.
48 if (!params
.pref_file
.empty()) {
49 factory
.SetUserPrefsFile(params
.pref_file
,
50 base::MessageLoopProxy::current().get());
51 scoped_refptr
<user_prefs::PrefRegistrySyncable
> registry(
52 new user_prefs::PrefRegistrySyncable
);
53 scoped_ptr
<PrefServiceSyncable
> prefs(
54 factory
.CreateSyncable(registry
.get()));
55 chrome::RegisterUserProfilePrefs(registry
.get());
56 profile_builder
.SetPrefService(prefs
.Pass());
59 if (params
.profile_is_supervised
)
60 profile_builder
.SetSupervisedUserId("asdf");
62 profile_builder
.SetPath(params
.profile_path
);
63 return profile_builder
.Build();
68 ExtensionServiceTestBase::ExtensionServiceInitParams::
69 ExtensionServiceInitParams()
70 : autoupdate_enabled(false),
72 profile_is_supervised(false) {
75 ExtensionServiceTestBase::ExtensionServiceTestBase()
77 testing_local_state_(TestingBrowserProcess::GetGlobal()),
78 thread_bundle_(new content::TestBrowserThreadBundle(kThreadOptions
)),
79 did_reset_thread_bundle_(false),
81 base::FilePath test_data_dir
;
82 if (!PathService::Get(chrome::DIR_TEST_DATA
, &test_data_dir
)) {
86 data_dir_
= test_data_dir
.AppendASCII("extensions");
89 ExtensionServiceTestBase::~ExtensionServiceTestBase() {
90 // Parts of destruction have to happen on an IO thread, so if the thread
91 // bundle is reset, we need to change it back.
92 if (did_reset_thread_bundle_
)
93 ResetThreadBundle(kThreadOptions
);
95 // Why? Because |profile_| has to be destroyed before |at_exit_manager_|, but
96 // is declared above it in the class definition since it's protected.
100 ExtensionServiceTestBase::ExtensionServiceInitParams
101 ExtensionServiceTestBase::CreateDefaultInitParams() {
102 ExtensionServiceInitParams params
;
103 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
104 base::FilePath path
= temp_dir_
.path();
105 path
= path
.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
106 EXPECT_TRUE(base::DeleteFile(path
, true));
107 base::File::Error error
= base::File::FILE_OK
;
108 EXPECT_TRUE(base::CreateDirectoryAndGetError(path
, &error
)) << error
;
109 base::FilePath prefs_filename
=
110 path
.Append(FILE_PATH_LITERAL("TestPreferences"));
111 base::FilePath extensions_install_dir
=
112 path
.Append(FILE_PATH_LITERAL("Extensions"));
113 EXPECT_TRUE(base::DeleteFile(extensions_install_dir
, true));
114 EXPECT_TRUE(base::CreateDirectoryAndGetError(extensions_install_dir
, &error
))
117 params
.profile_path
= path
;
118 params
.pref_file
= prefs_filename
;
119 params
.extensions_install_dir
= extensions_install_dir
;
123 void ExtensionServiceTestBase::InitializeExtensionService(
124 const ExtensionServiceTestBase::ExtensionServiceInitParams
& params
) {
125 profile_
= BuildTestingProfile(params
);
126 CreateExtensionService(params
);
128 extensions_install_dir_
= params
.extensions_install_dir
;
129 registry_
= ExtensionRegistry::Get(profile_
.get());
131 // Garbage collector is typically NULL during tests, so give it a build.
132 ExtensionGarbageCollectorFactory::GetInstance()->SetTestingFactoryAndUse(
133 profile_
.get(), &ExtensionGarbageCollectorFactory::BuildInstanceFor
);
136 void ExtensionServiceTestBase::InitializeEmptyExtensionService() {
137 InitializeExtensionService(CreateDefaultInitParams());
140 void ExtensionServiceTestBase::InitializeInstalledExtensionService(
141 const base::FilePath
& prefs_file
,
142 const base::FilePath
& source_install_dir
) {
143 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
144 base::FilePath path
= temp_dir_
.path();
146 path
= path
.Append(FILE_PATH_LITERAL("TestingExtensionsPath"));
147 ASSERT_TRUE(base::DeleteFile(path
, true));
149 base::File::Error error
= base::File::FILE_OK
;
150 ASSERT_TRUE(base::CreateDirectoryAndGetError(path
, &error
)) << error
;
152 base::FilePath temp_prefs
= path
.Append(chrome::kPreferencesFilename
);
153 ASSERT_TRUE(base::CopyFile(prefs_file
, temp_prefs
));
155 base::FilePath extensions_install_dir
=
156 path
.Append(FILE_PATH_LITERAL("Extensions"));
157 ASSERT_TRUE(base::DeleteFile(extensions_install_dir
, true));
159 base::CopyDirectory(source_install_dir
, extensions_install_dir
, true));
161 ExtensionServiceInitParams params
;
162 params
.profile_path
= path
;
163 params
.pref_file
= temp_prefs
;
164 params
.extensions_install_dir
= extensions_install_dir
;
165 InitializeExtensionService(params
);
168 void ExtensionServiceTestBase::InitializeGoodInstalledExtensionService() {
169 base::FilePath source_install_dir
=
170 data_dir_
.AppendASCII("good").AppendASCII("Extensions");
171 base::FilePath pref_path
=
172 source_install_dir
.DirName().Append(chrome::kPreferencesFilename
);
173 InitializeInstalledExtensionService(pref_path
, source_install_dir
);
176 void ExtensionServiceTestBase::InitializeExtensionServiceWithUpdater() {
177 ExtensionServiceInitParams params
= CreateDefaultInitParams();
178 params
.autoupdate_enabled
= true;
179 InitializeExtensionService(params
);
180 service_
->updater()->Start();
183 void ExtensionServiceTestBase::ResetThreadBundle(int options
) {
184 did_reset_thread_bundle_
= true;
185 thread_bundle_
.reset();
186 thread_bundle_
.reset(new content::TestBrowserThreadBundle(options
));
189 void ExtensionServiceTestBase::SetUp() {
190 ExtensionErrorReporter::GetInstance()->ClearErrors();
193 void ExtensionServiceTestBase::SetUpTestCase() {
194 // Safe to call multiple times.
195 ExtensionErrorReporter::Init(false); // no noisy errors.
198 // These are declared in the .cc so that all inheritors don't need to know
199 // that TestingProfile derives Profile derives BrowserContext.
200 content::BrowserContext
* ExtensionServiceTestBase::browser_context() {
201 return profile_
.get();
204 Profile
* ExtensionServiceTestBase::profile() {
205 return profile_
.get();
208 void ExtensionServiceTestBase::CreateExtensionService(
209 const ExtensionServiceInitParams
& params
) {
210 TestExtensionSystem
* system
=
211 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile_
.get()));
212 if (!params
.is_first_run
) {
213 ExtensionPrefs
* prefs
= system
->CreateExtensionPrefs(
214 base::CommandLine::ForCurrentProcess(), params
.extensions_install_dir
);
215 prefs
->SetAlertSystemFirstRun();
219 system
->CreateExtensionService(base::CommandLine::ForCurrentProcess(),
220 params
.extensions_install_dir
,
221 params
.autoupdate_enabled
);
223 service_
->SetFileTaskRunnerForTesting(
224 base::MessageLoopProxy::current().get());
225 service_
->set_extensions_enabled(true);
226 service_
->set_show_extensions_prompts(false);
227 service_
->set_install_updates_when_idle_for_test(false);
229 // When we start up, we want to make sure there is no external provider,
230 // since the ExtensionService on Windows will use the Registry as a default
231 // provider and if there is something already registered there then it will
232 // interfere with the tests. Those tests that need an external provider
233 // will register one specifically.
234 service_
->ClearProvidersForTesting();
236 #if defined(OS_CHROMEOS)
237 InstallLimiter::Get(profile_
.get())->DisableForTest();
241 } // namespace extensions