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.
4 
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
7 
8 #include <stddef.h>
9 
10 #include <memory>
11 #include <string>
12 
13 #include "base/at_exit.h"
14 #include "base/files/file_path.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/macros.h"
17 #include "build/build_config.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "components/policy/core/common/mock_configuration_policy_provider.h"
20 #include "components/policy/core/common/policy_service.h"
21 #include "content/public/test/browser_task_environment.h"
22 #include "content/public/test/test_renderer_host.h"
23 #include "content/public/test/test_utils.h"
24 #include "extensions/browser/sandboxed_unpacker.h"
25 #include "extensions/common/extension.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27 
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
30 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
31 #endif
32 
33 class Profile;
34 class TestingProfile;
35 
36 namespace content {
37 class BrowserContext;
38 class BrowserTaskEnvironment;
39 }
40 
41 namespace sync_preferences {
42 class TestingPrefServiceSyncable;
43 }
44 
45 namespace extensions {
46 
47 class ExtensionRegistry;
48 class ExtensionService;
49 
50 // A unittest infrastructure which creates an ExtensionService. Whenever
51 // possible, use this instead of creating a browsertest.
52 // Note: Before adding methods to this class, please, please, please think about
53 // whether they should go here or in a more specific subclass. Lots of things
54 // need an ExtensionService, but they don't all need to know how you want yours
55 // to be initialized.
56 class ExtensionServiceTestBase : public testing::Test {
57  public:
58   struct ExtensionServiceInitParams {
59     base::FilePath profile_path;
60     base::FilePath pref_file;
61     base::FilePath extensions_install_dir;
62     bool autoupdate_enabled = false;
63     bool extensions_enabled = true;
64     bool is_first_run = true;
65     bool profile_is_supervised = false;
66     bool enable_bookmark_model = false;
67 
68     policy::PolicyService* policy_service = nullptr;
69 
70     // Though you could use this constructor, you probably want to use
71     // CreateDefaultInitParams(), and then make a change or two.
72     ExtensionServiceInitParams();
73     ExtensionServiceInitParams(const ExtensionServiceInitParams& other);
74   };
75 
76   // Public because parameterized test cases need it to be, or else the compiler
77   // barfs.
78   static void SetUpTestCase();  // faux-verride (static override).
79 
80  protected:
81   ExtensionServiceTestBase();
82   // Alternatively, a subclass may pass a BrowserTaskEnvironment directly.
83   explicit ExtensionServiceTestBase(
84       std::unique_ptr<content::BrowserTaskEnvironment> task_environment);
85 
86   ~ExtensionServiceTestBase() override;
87 
88   // testing::Test implementation.
89   void SetUp() override;
90   void TearDown() override;
91 
92   // Create a set of InitParams to install an ExtensionService into |temp_dir_|.
93   ExtensionServiceInitParams CreateDefaultInitParams();
94 
95   // Initialize an ExtensionService according to the given |params|.
96   virtual void InitializeExtensionService(
97       const ExtensionServiceInitParams& params);
98 
99   // Initialize an empty ExtensionService using the default init params.
100   void InitializeEmptyExtensionService();
101 
102   // Initialize an ExtensionService with the associated |prefs_file| and
103   // |source_install_dir|.
104   void InitializeInstalledExtensionService(
105       const base::FilePath& prefs_file,
106       const base::FilePath& source_install_dir);
107 
108   // Initialize an ExtensionService with a few already-installed extensions.
109   void InitializeGoodInstalledExtensionService();
110 
111   // Initialize an ExtensionService with autoupdate enabled.
112   void InitializeExtensionServiceWithUpdater();
113 
114   // Initializes an ExtensionService without extensions enabled.
115   void InitializeExtensionServiceWithExtensionsDisabled();
116 
117   // Helpers to check the existence and values of extension prefs.
118   size_t GetPrefKeyCount();
119   void ValidatePrefKeyCount(size_t count);
120   testing::AssertionResult ValidateBooleanPref(
121       const std::string& extension_id,
122       const std::string& pref_path,
123       bool expected_val);
124   void ValidateIntegerPref(const std::string& extension_id,
125                            const std::string& pref_path,
126                            int expected_val);
127   void ValidateStringPref(const std::string& extension_id,
128                           const std::string& pref_path,
129                           const std::string& expected_val);
130 
131   // TODO(rdevlin.cronin): Pull out more methods from ExtensionServiceTest that
132   // are commonly used and/or reimplemented. For instance, methods to install
133   // extensions from various locations, etc.
134 
135   content::BrowserContext* browser_context();
136   Profile* profile();
testing_profile()137   TestingProfile* testing_profile() { return profile_.get(); }
138   sync_preferences::TestingPrefServiceSyncable* testing_pref_service();
service()139   ExtensionService* service() { return service_; }
registry()140   ExtensionRegistry* registry() { return registry_; }
extensions_install_dir()141   const base::FilePath& extensions_install_dir() const {
142     return extensions_install_dir_;
143   }
data_dir()144   const base::FilePath& data_dir() const { return data_dir_; }
temp_dir()145   const base::ScopedTempDir& temp_dir() const { return temp_dir_; }
task_environment()146   content::BrowserTaskEnvironment* task_environment() {
147     return task_environment_.get();
148   }
policy_provider()149   policy::MockConfigurationPolicyProvider* policy_provider() {
150     return &policy_provider_;
151   }
152 
153  private:
154   // Must be declared before anything that may make use of the
155   // directory so as to ensure files are closed before cleanup.
156   base::ScopedTempDir temp_dir_;
157 
158   // Destroying at_exit_manager_ will delete all LazyInstances, so it must come
159   // after task_environment_ in the destruction order.
160   base::ShadowingAtExitManager at_exit_manager_;
161 
162   // The MessageLoop is used by RenderViewHostTestEnabler, so this must be
163   // created before it.
164   std::unique_ptr<content::BrowserTaskEnvironment> task_environment_;
165 
166   // Enable creation of WebContents without initializing a renderer.
167   content::RenderViewHostTestEnabler rvh_test_enabler_;
168 
169   // Provides policies for the PolicyService below, so this must be created
170   // before it.
171   policy::MockConfigurationPolicyProvider policy_provider_;
172 
173   // PolicyService for the testing profile, so unit tests can use custom
174   // policies.
175   std::unique_ptr<policy::PolicyService> policy_service_;
176 
177  protected:
178   // It's unfortunate that these are exposed to subclasses (rather than used
179   // through the accessor methods above), but too many tests already use them
180   // directly.
181 
182   // The associated testing profile.
183   std::unique_ptr<TestingProfile> profile_;
184 
185   // The ExtensionService, whose lifetime is managed by |profile|'s
186   // ExtensionSystem.
187   ExtensionService* service_;
188   ScopedTestingLocalState testing_local_state_;
189 
190  private:
191   void CreateExtensionService(const ExtensionServiceInitParams& params);
192 
193   // The directory into which extensions are installed.
194   base::FilePath extensions_install_dir_;
195 
196   // chrome/test/data/extensions/
197   base::FilePath data_dir_;
198 
199   content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
200 
201   // The associated ExtensionRegistry, for convenience.
202   extensions::ExtensionRegistry* registry_;
203 
204 #if defined OS_CHROMEOS
205   chromeos::ScopedCrosSettingsTestHelper cros_settings_test_helper_;
206   chromeos::ScopedTestUserManager test_user_manager_;
207 #endif
208 
209   // An override that ignores CRX3 publisher signatures.
210   SandboxedUnpacker::ScopedVerifierFormatOverrideForTest
211       verifier_format_override_;
212 
213   DISALLOW_COPY_AND_ASSIGN(ExtensionServiceTestBase);
214 };
215 
216 }  // namespace extensions
217 
218 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
219