Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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_TEST_EXTENSION_PREFS_H_
      6 #define CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_PREFS_H_
      7 
      8 #include <string>
      9 
     10 #include "base/files/scoped_temp_dir.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "chrome/common/extensions/manifest.h"
     14 
     15 class ExtensionPrefValueMap;
     16 class PrefService;
     17 class PrefServiceSyncable;
     18 
     19 namespace base {
     20 class DictionaryValue;
     21 class SequencedTaskRunner;
     22 }
     23 
     24 namespace user_prefs {
     25 class PrefRegistrySyncable;
     26 }
     27 
     28 namespace extensions {
     29 class Extension;
     30 class ExtensionPrefs;
     31 
     32 // This is a test class intended to make it easier to work with ExtensionPrefs
     33 // in tests.
     34 class TestExtensionPrefs {
     35  public:
     36   explicit TestExtensionPrefs(base::SequencedTaskRunner* task_runner);
     37   virtual ~TestExtensionPrefs();
     38 
     39   ExtensionPrefs* prefs() { return prefs_.get(); }
     40   const ExtensionPrefs& const_prefs() const {
     41       return *prefs_.get();
     42   }
     43   PrefService* pref_service();
     44   const scoped_refptr<user_prefs::PrefRegistrySyncable>& pref_registry();
     45   void ResetPrefRegistry();
     46   const base::FilePath& temp_dir() const { return temp_dir_.path(); }
     47   const base::FilePath& extensions_dir() const { return extensions_dir_; }
     48   ExtensionPrefValueMap* extension_pref_value_map() {
     49     return extension_pref_value_map_.get();
     50   }
     51 
     52   // This will cause the ExtensionPrefs to be deleted and recreated, based on
     53   // any existing backing file we had previously created.
     54   void RecreateExtensionPrefs();
     55 
     56   // Creates a new Extension with the given name in our temp dir, adds it to
     57   // our ExtensionPrefs, and returns it.
     58   scoped_refptr<Extension> AddExtension(std::string name);
     59 
     60   // As above, but the extension is an app.
     61   scoped_refptr<Extension> AddApp(std::string name);
     62 
     63   // Similar to AddExtension, but takes a dictionary with manifest values.
     64   scoped_refptr<Extension> AddExtensionWithManifest(
     65       const base::DictionaryValue& manifest,
     66       Manifest::Location location);
     67 
     68   // Similar to AddExtension, but takes a dictionary with manifest values
     69   // and extension flags.
     70   scoped_refptr<Extension> AddExtensionWithManifestAndFlags(
     71       const base::DictionaryValue& manifest,
     72       Manifest::Location location,
     73       int extra_flags);
     74 
     75   // Similar to AddExtension, this adds a new test Extension. This is useful for
     76   // cases when you don't need the Extension object, but just the id it was
     77   // assigned.
     78   std::string AddExtensionAndReturnId(std::string name);
     79 
     80   PrefService* CreateIncognitoPrefService() const;
     81 
     82   // Allows disabling the loading of preferences of extensions. Becomes
     83   // active after calling RecreateExtensionPrefs(). Defaults to false.
     84   void set_extensions_disabled(bool extensions_disabled);
     85 
     86  protected:
     87   base::ScopedTempDir temp_dir_;
     88   base::FilePath preferences_file_;
     89   base::FilePath extensions_dir_;
     90   scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_;
     91   scoped_ptr<PrefServiceSyncable> pref_service_;
     92   scoped_ptr<ExtensionPrefs> prefs_;
     93   scoped_ptr<ExtensionPrefValueMap> extension_pref_value_map_;
     94   const scoped_refptr<base::SequencedTaskRunner> task_runner_;
     95 
     96  private:
     97   bool extensions_disabled_;
     98   DISALLOW_COPY_AND_ASSIGN(TestExtensionPrefs);
     99 };
    100 
    101 }  // namespace extensions
    102 
    103 #endif  // CHROME_BROWSER_EXTENSIONS_TEST_EXTENSION_PREFS_H_
    104