Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / test_extension_prefs.cc
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 #include "chrome/browser/extensions/test_extension_prefs.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/prefs/pref_value_store.h"
15 #include "base/run_loop.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "base/values.h"
19 #include "chrome/browser/prefs/pref_service_mock_factory.h"
20 #include "chrome/browser/prefs/pref_service_syncable.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "components/crx_file/id_util.h"
23 #include "components/pref_registry/pref_registry_syncable.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "extensions/browser/extension_pref_store.h"
26 #include "extensions/browser/extension_pref_value_map.h"
27 #include "extensions/browser/extension_prefs.h"
28 #include "extensions/browser/extensions_browser_client.h"
29 #include "extensions/common/extension.h"
30 #include "extensions/common/manifest_constants.h"
31 #include "sync/api/string_ordinal.h"
32 #include "testing/gtest/include/gtest/gtest.h"
33
34 using content::BrowserThread;
35
36 namespace extensions {
37
38 namespace {
39
40 // A TimeProvider which returns an incrementally later time each time
41 // GetCurrentTime is called.
42 class IncrementalTimeProvider : public ExtensionPrefs::TimeProvider {
43  public:
44   IncrementalTimeProvider() : current_time_(base::Time::Now()) {
45   }
46
47   ~IncrementalTimeProvider() override {}
48
49   base::Time GetCurrentTime() const override {
50     current_time_ += base::TimeDelta::FromSeconds(10);
51     return current_time_;
52   }
53
54  private:
55   DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider);
56
57   mutable base::Time current_time_;
58 };
59
60 }  // namespace
61
62 TestExtensionPrefs::TestExtensionPrefs(
63     const scoped_refptr<base::SequencedTaskRunner>& task_runner)
64     : task_runner_(task_runner), extensions_disabled_(false) {
65   EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
66   preferences_file_ = temp_dir_.path().Append(chrome::kPreferencesFilename);
67   extensions_dir_ = temp_dir_.path().AppendASCII("Extensions");
68   EXPECT_TRUE(base::CreateDirectory(extensions_dir_));
69
70   ResetPrefRegistry();
71   RecreateExtensionPrefs();
72 }
73
74 TestExtensionPrefs::~TestExtensionPrefs() {
75 }
76
77 PrefService* TestExtensionPrefs::pref_service() {
78   return pref_service_.get();
79 }
80
81 const scoped_refptr<user_prefs::PrefRegistrySyncable>&
82 TestExtensionPrefs::pref_registry() {
83   return pref_registry_;
84 }
85
86 void TestExtensionPrefs::ResetPrefRegistry() {
87   pref_registry_ = new user_prefs::PrefRegistrySyncable;
88   ExtensionPrefs::RegisterProfilePrefs(pref_registry_.get());
89 }
90
91 void TestExtensionPrefs::RecreateExtensionPrefs() {
92   // We persist and reload the PrefService's PrefStores because this process
93   // deletes all empty dictionaries. The ExtensionPrefs implementation
94   // needs to be able to handle this situation.
95   if (pref_service_) {
96     // Commit a pending write (which posts a task to task_runner_) and wait for
97     // it to finish.
98     pref_service_->CommitPendingWrite();
99     base::RunLoop run_loop;
100     ASSERT_TRUE(
101         task_runner_->PostTaskAndReply(
102             FROM_HERE,
103             base::Bind(&base::DoNothing),
104             run_loop.QuitClosure()));
105     run_loop.Run();
106   }
107
108   extension_pref_value_map_.reset(new ExtensionPrefValueMap);
109   PrefServiceMockFactory factory;
110   factory.SetUserPrefsFile(preferences_file_, task_runner_.get());
111   factory.set_extension_prefs(
112       new ExtensionPrefStore(extension_pref_value_map_.get(), false));
113   pref_service_ = factory.CreateSyncable(pref_registry_.get()).Pass();
114
115   prefs_.reset(ExtensionPrefs::Create(
116       pref_service_.get(),
117       temp_dir_.path(),
118       extension_pref_value_map_.get(),
119       ExtensionsBrowserClient::Get()->CreateAppSorting().Pass(),
120       extensions_disabled_,
121       std::vector<ExtensionPrefsObserver*>(),
122       // Guarantee that no two extensions get the same installation time
123       // stamp and we can reliably assert the installation order in the tests.
124       scoped_ptr<ExtensionPrefs::TimeProvider>(new IncrementalTimeProvider())));
125 }
126
127 scoped_refptr<Extension> TestExtensionPrefs::AddExtension(
128     const std::string& name) {
129   base::DictionaryValue dictionary;
130   dictionary.SetString(manifest_keys::kName, name);
131   dictionary.SetString(manifest_keys::kVersion, "0.1");
132   return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
133 }
134
135 scoped_refptr<Extension> TestExtensionPrefs::AddApp(const std::string& name) {
136   base::DictionaryValue dictionary;
137   dictionary.SetString(manifest_keys::kName, name);
138   dictionary.SetString(manifest_keys::kVersion, "0.1");
139   dictionary.SetString(manifest_keys::kApp, "true");
140   dictionary.SetString(manifest_keys::kLaunchWebURL, "http://example.com");
141   return AddExtensionWithManifest(dictionary, Manifest::INTERNAL);
142
143 }
144
145 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest(
146     const base::DictionaryValue& manifest, Manifest::Location location) {
147   return AddExtensionWithManifestAndFlags(manifest, location,
148                                           Extension::NO_FLAGS);
149 }
150
151 scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
152     const base::DictionaryValue& manifest,
153     Manifest::Location location,
154     int extra_flags) {
155   std::string name;
156   EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name));
157   base::FilePath path =  extensions_dir_.AppendASCII(name);
158   std::string errors;
159   scoped_refptr<Extension> extension = Extension::Create(
160       path, location, manifest, extra_flags, &errors);
161   EXPECT_TRUE(extension.get()) << errors;
162   if (!extension.get())
163     return NULL;
164
165   EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id()));
166   prefs_->OnExtensionInstalled(extension.get(),
167                                Extension::ENABLED,
168                                syncer::StringOrdinal::CreateInitialOrdinal(),
169                                std::string());
170   return extension;
171 }
172
173 std::string TestExtensionPrefs::AddExtensionAndReturnId(
174     const std::string& name) {
175   scoped_refptr<Extension> extension(AddExtension(name));
176   return extension->id();
177 }
178
179 PrefService* TestExtensionPrefs::CreateIncognitoPrefService() const {
180   return pref_service_->CreateIncognitoPrefService(
181       new ExtensionPrefStore(extension_pref_value_map_.get(), true));
182 }
183
184 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
185   extensions_disabled_ = extensions_disabled;
186 }
187
188 }  // namespace extensions