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