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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
8 #include "base/at_exit.h"
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "chrome/test/base/scoped_testing_local_state.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/chromeos/settings/device_settings_service.h"
23 class ExtensionService
;
31 namespace extensions
{
33 class ExtensionRegistry
;
34 class ManagementPolicy
;
36 // A unittest infrastructure which creates an ExtensionService. Whenever
37 // possible, use this instead of creating a browsertest.
38 // Note: Before adding methods to this class, please, please, please think about
39 // whether they should go here or in a more specific subclass. Lots of things
40 // need an ExtensionService, but they don't all need to know how you want yours
42 class ExtensionServiceTestBase
: public testing::Test
{
44 struct ExtensionServiceInitParams
{
45 base::FilePath profile_path
;
46 base::FilePath pref_file
;
47 base::FilePath extensions_install_dir
;
48 bool autoupdate_enabled
; // defaults to false.
49 bool is_first_run
; // defaults to true.
50 bool profile_is_supervised
; // defaults to false.
52 // Though you could use this constructor, you probably want to use
53 // CreateDefaultInitParams(), and then make a change or two.
54 ExtensionServiceInitParams();
57 // Public because parameterized test cases need it to be, or else the compiler
59 static void SetUpTestCase(); // faux-verride (static override).
62 ExtensionServiceTestBase();
63 ~ExtensionServiceTestBase() override
;
65 // testing::Test implementation.
66 void SetUp() override
;
68 // Create a set of InitParams to install an ExtensionService into |temp_dir_|.
69 ExtensionServiceInitParams
CreateDefaultInitParams();
71 // Initialize an ExtensionService according to the given |params|.
72 void InitializeExtensionService(const ExtensionServiceInitParams
& params
);
74 // Initialize an empty ExtensionService using the default init params.
75 void InitializeEmptyExtensionService();
77 // Initialize an ExtensionService with the associated |prefs_file| and
78 // |source_install_dir|.
79 void InitializeInstalledExtensionService(
80 const base::FilePath
& prefs_file
,
81 const base::FilePath
& source_install_dir
);
83 // Initialize an ExtensionService with a few already-installed extensions.
84 void InitializeGoodInstalledExtensionService();
86 // Initialize an ExtensionService with autoupdate enabled.
87 void InitializeExtensionServiceWithUpdater();
89 // Resets the browser thread bundle to one with |options|.
90 void ResetThreadBundle(int options
);
92 // TODO(rdevlin.cronin): Pull out more methods from ExtensionServiceTest that
93 // are commonly used and/or reimplemented. For instance, methods to install
94 // extensions from various locations, etc.
96 content::BrowserContext
* browser_context();
98 ExtensionService
* service() { return service_
; }
99 ExtensionRegistry
* registry() { return registry_
; }
100 const base::FilePath
& extensions_install_dir() const {
101 return extensions_install_dir_
;
103 const base::FilePath
& data_dir() const { return data_dir_
; }
104 const base::ScopedTempDir
& temp_dir() const { return temp_dir_
; }
107 // Destroying at_exit_manager_ will delete all LazyInstances, so it must come
108 // after thread_bundle_ in the destruction order.
109 base::ShadowingAtExitManager at_exit_manager_
;
110 scoped_ptr
<content::TestBrowserThreadBundle
> thread_bundle_
;
113 // It's unfortunate that these are exposed to subclasses (rather than used
114 // through the accessor methods above), but too many tests already use them
117 // The associated testing profile.
118 scoped_ptr
<TestingProfile
> profile_
;
120 // The ExtensionService, whose lifetime is managed by |profile|'s
122 ExtensionService
* service_
;
123 ScopedTestingLocalState testing_local_state_
;
126 void CreateExtensionService(const ExtensionServiceInitParams
& params
);
128 // Destroy temp_dir_ after thread_bundle_ so clean-up tasks can still use the
130 base::ScopedTempDir temp_dir_
;
132 // Whether or not the thread bundle was reset in the test.
133 bool did_reset_thread_bundle_
;
135 // The directory into which extensions are installed.
136 base::FilePath extensions_install_dir_
;
138 // chrome/test/data/extensions/
139 base::FilePath data_dir_
;
141 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_
;
143 // The associated ExtensionRegistry, for convenience.
144 extensions::ExtensionRegistry
* registry_
;
146 #if defined OS_CHROMEOS
147 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
148 chromeos::ScopedTestCrosSettings test_cros_settings_
;
149 chromeos::ScopedTestUserManager test_user_manager_
;
153 } // namespace extensions
155 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_