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 "content/public/test/test_browser_thread_bundle.h"
13 #include "content/public/test/test_utils.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
17 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/device_settings_service.h"
22 class ExtensionService;
30 namespace extensions {
32 class ExtensionRegistry;
33 class ManagementPolicy;
35 // A unittest infrastructure which creates an ExtensionService. Whenever
36 // possible, use this instead of creating a browsertest.
37 // Note: Before adding methods to this class, please, please, please think about
38 // whether they should go here or in a more specific subclass. Lots of things
39 // need an ExtensionService, but they don't all need to know how you want yours
41 class ExtensionServiceTestBase : public testing::Test {
43 struct ExtensionServiceInitParams {
44 base::FilePath profile_path;
45 base::FilePath pref_file;
46 base::FilePath extensions_install_dir;
47 bool autoupdate_enabled; // defaults to false.
48 bool is_first_run; // defaults to true.
49 bool profile_is_supervised; // defaults to false.
51 // Though you could use this constructor, you probably want to use
52 // CreateDefaultInitParams(), and then make a change or two.
53 ExtensionServiceInitParams();
56 // Public because parameterized test cases need it to be, or else the compiler
58 static void SetUpTestCase(); // faux-verride (static override).
61 ExtensionServiceTestBase();
62 ~ExtensionServiceTestBase() override;
64 // testing::Test implementation.
65 void SetUp() override;
67 // Create a set of InitParams to install an ExtensionService into |temp_dir_|.
68 ExtensionServiceInitParams CreateDefaultInitParams();
70 // Initialize an ExtensionService according to the given |params|.
71 void InitializeExtensionService(const ExtensionServiceInitParams& params);
73 // Initialize an empty ExtensionService using the default init params.
74 void InitializeEmptyExtensionService();
76 // Initialize an ExtensionService with the associated |prefs_file| and
77 // |source_install_dir|.
78 void InitializeInstalledExtensionService(
79 const base::FilePath& prefs_file,
80 const base::FilePath& source_install_dir);
82 // Initialize an ExtensionService with a few already-installed extensions.
83 void InitializeGoodInstalledExtensionService();
85 // Initialize an ExtensionService with autoupdate enabled.
86 void InitializeExtensionServiceWithUpdater();
88 // TODO(rdevlin.cronin): Pull out more methods from ExtensionServiceTest that
89 // are commonly used and/or reimplemented. For instance, methods to install
90 // extensions from various locations, etc.
92 content::BrowserContext* browser_context();
94 ExtensionService* service() { return service_; }
95 ExtensionRegistry* registry() { return registry_; }
96 const base::FilePath& extensions_install_dir() const {
97 return extensions_install_dir_;
99 const base::FilePath& data_dir() const { return data_dir_; }
100 const base::ScopedTempDir& temp_dir() const { return temp_dir_; }
102 // It's unfortunate that these are exposed to subclasses (rather than used
103 // through the accessor methods above), but too many tests already use them
106 // The associated testing profile.
107 scoped_ptr<TestingProfile> profile_;
109 // The ExtensionService, whose lifetime is managed by |profile|'s
111 ExtensionService* service_;
114 void CreateExtensionService(const ExtensionServiceInitParams& params);
116 // Destroy temp_dir_ after thread_bundle_ so clean-up tasks can still use the
118 base::ScopedTempDir temp_dir_;
120 // Destroying at_exit_manager_ will delete all LazyInstances, so it must come
121 // after thread_bundle_ in the destruction order.
122 base::ShadowingAtExitManager at_exit_manager_;
123 content::TestBrowserThreadBundle thread_bundle_;
125 // The directory into which extensions are installed.
126 base::FilePath extensions_install_dir_;
128 // chrome/test/data/extensions/
129 base::FilePath data_dir_;
131 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
133 // The associated ExtensionRegistry, for convenience.
134 extensions::ExtensionRegistry* registry_;
136 #if defined OS_CHROMEOS
137 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
138 chromeos::ScopedTestCrosSettings test_cros_settings_;
139 chromeos::ScopedTestUserManager test_user_manager_;
143 } // namespace extensions
145 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_