Home | History | Annotate | Download | only in extensions
      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 "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"
     15 
     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"
     20 #endif
     21 
     22 class ExtensionService;
     23 class Profile;
     24 class TestingProfile;
     25 
     26 namespace content {
     27 class BrowserContext;
     28 }
     29 
     30 namespace extensions {
     31 
     32 class ExtensionRegistry;
     33 class ManagementPolicy;
     34 
     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
     40 // to be initialized.
     41 class ExtensionServiceTestBase : public testing::Test {
     42  public:
     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.
     50 
     51     // Though you could use this constructor, you probably want to use
     52     // CreateDefaultInitParams(), and then make a change or two.
     53     ExtensionServiceInitParams();
     54   };
     55 
     56  protected:
     57   ExtensionServiceTestBase();
     58   virtual ~ExtensionServiceTestBase();
     59 
     60   // testing::Test implementation.
     61   virtual void SetUp() OVERRIDE;
     62   static void SetUpTestCase();  // faux-verride (static override).
     63 
     64   // Create a set of InitParams to install an ExtensionService into |temp_dir_|.
     65   ExtensionServiceInitParams CreateDefaultInitParams();
     66 
     67   // Initialize an ExtensionService according to the given |params|.
     68   void InitializeExtensionService(const ExtensionServiceInitParams& params);
     69 
     70   // Initialize an empty ExtensionService using the default init params.
     71   void InitializeEmptyExtensionService();
     72 
     73   // Initialize an ExtensionService with the associated |prefs_file| and
     74   // |source_install_dir|.
     75   void InitializeInstalledExtensionService(
     76       const base::FilePath& prefs_file,
     77       const base::FilePath& source_install_dir);
     78 
     79   // Initialize an ExtensionService with a few already-installed extensions.
     80   void InitializeGoodInstalledExtensionService();
     81 
     82   // Initialize an ExtensionService with autoupdate enabled.
     83   void InitializeExtensionServiceWithUpdater();
     84 
     85   // Initialize the associated ProcessManager.
     86   void InitializeProcessManager();
     87 
     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.
     91 
     92   content::BrowserContext* browser_context();
     93   Profile* profile();
     94   ExtensionService* service() { return service_; }
     95   ExtensionRegistry* registry() { return registry_; }
     96   const base::FilePath& extensions_install_dir() const {
     97     return extensions_install_dir_;
     98   }
     99   const base::FilePath& data_dir() const { return data_dir_; }
    100   const base::ScopedTempDir& temp_dir() const { return temp_dir_; }
    101 
    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
    104   // directly.
    105 
    106   // The associated testing profile.
    107   scoped_ptr<TestingProfile> profile_;
    108 
    109   // The ExtensionService, whose lifetime is managed by |profile|'s
    110   // ExtensionSystem.
    111   ExtensionService* service_;
    112 
    113  private:
    114   void CreateExtensionService(const ExtensionServiceInitParams& params);
    115 
    116   // Destroy temp_dir_ after thread_bundle_ so clean-up tasks can still use the
    117   // directory.
    118   base::ScopedTempDir temp_dir_;
    119 
    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_;
    124 
    125   // The directory into which extensions are installed.
    126   base::FilePath extensions_install_dir_;
    127 
    128   // chrome/test/data/extensions/
    129   base::FilePath data_dir_;
    130 
    131   content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
    132 
    133   // The associated ExtensionRegistry, for convenience.
    134   extensions::ExtensionRegistry* registry_;
    135 
    136 #if defined OS_CHROMEOS
    137   chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
    138   chromeos::ScopedTestCrosSettings test_cros_settings_;
    139   chromeos::ScopedTestUserManager test_user_manager_;
    140 #endif
    141 };
    142 
    143 }  // namespace extensions
    144 
    145 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_SERVICE_TEST_BASE_H_
    146