blob: 175ef2c53b9b45c0a88f30c95b793887982108bd [file] [log] [blame]
[email protected]ea3e4972012-04-12 03:41:371// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]277404c22010-04-22 13:09:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]00c87822012-11-27 19:09:175#include "base/prefs/json_pref_store.h"
6
[email protected]277404c22010-04-22 13:09:457#include "base/file_util.h"
[email protected]ea1a3f62012-11-16 20:34:238#include "base/files/scoped_temp_dir.h"
[email protected]3b63f8f42011-03-28 01:54:159#include "base/memory/ref_counted.h"
10#include "base/memory/scoped_ptr.h"
[email protected]277404c22010-04-22 13:09:4511#include "base/path_service.h"
[email protected]f7b98b32013-02-05 08:14:1512#include "base/run_loop.h"
[email protected]277404c22010-04-22 13:09:4513#include "base/string_util.h"
[email protected]dfa049e2013-02-07 02:57:2214#include "base/strings/string_number_conversions.h"
[email protected]0de615a2012-11-08 04:40:5915#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0616#include "base/threading/thread.h"
[email protected]277404c22010-04-22 13:09:4517#include "base/utf_string_conversions.h"
18#include "base/values.h"
[email protected]845b43a82011-05-11 10:14:4319#include "testing/gmock/include/gmock/gmock.h"
[email protected]277404c22010-04-22 13:09:4520#include "testing/gtest/include/gtest/gtest.h"
21
[email protected]7e3ec42c2012-12-16 05:13:2122namespace base {
[email protected]845b43a82011-05-11 10:14:4323namespace {
24
[email protected]5bfdcfd2012-11-22 22:08:2425const char kHomePage[] = "homepage";
26
[email protected]845b43a82011-05-11 10:14:4327class MockPrefStoreObserver : public PrefStore::Observer {
28 public:
29 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
30 MOCK_METHOD1(OnInitializationCompleted, void (bool));
31};
32
33class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
34 public:
35 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
36};
37
38} // namespace
39
[email protected]277404c22010-04-22 13:09:4540class JsonPrefStoreTest : public testing::Test {
41 protected:
[email protected]0de615a2012-11-08 04:40:5942 virtual void SetUp() OVERRIDE {
[email protected]3a305db2011-04-12 13:40:5343 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]277404c22010-04-22 13:09:4544
[email protected]00c87822012-11-27 19:09:1745 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &data_dir_));
46 data_dir_ = data_dir_.AppendASCII("base");
47 data_dir_ = data_dir_.AppendASCII("prefs");
48 data_dir_ = data_dir_.AppendASCII("test");
49 data_dir_ = data_dir_.AppendASCII("data");
[email protected]277404c22010-04-22 13:09:4550 data_dir_ = data_dir_.AppendASCII("pref_service");
[email protected]7e3ec42c2012-12-16 05:13:2151 LOG(WARNING) << data_dir_.value().c_str();
[email protected]277404c22010-04-22 13:09:4552 ASSERT_TRUE(file_util::PathExists(data_dir_));
53 }
54
[email protected]3a305db2011-04-12 13:40:5355 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:2356 base::ScopedTempDir temp_dir_;
[email protected]3a305db2011-04-12 13:40:5357 // The path to the directory where the test data is stored.
[email protected]023ad6ab2013-02-17 05:07:2358 base::FilePath data_dir_;
[email protected]ea587b02010-05-21 15:01:3559 // A message loop that we can use as the file thread message loop.
60 MessageLoop message_loop_;
[email protected]277404c22010-04-22 13:09:4561};
62
63// Test fallback behavior for a nonexistent file.
64TEST_F(JsonPrefStoreTest, NonExistentFile) {
[email protected]023ad6ab2013-02-17 05:07:2365 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
[email protected]277404c22010-04-22 13:09:4566 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
[email protected]9a8c4022011-01-25 14:25:3367 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:5968 new JsonPrefStore(
69 bogus_input_file, message_loop_.message_loop_proxy());
[email protected]f2d1f612010-12-09 15:10:1770 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
[email protected]9a8c4022011-01-25 14:25:3371 pref_store->ReadPrefs());
72 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4573}
74
75// Test fallback behavior for an invalid file.
76TEST_F(JsonPrefStoreTest, InvalidFile) {
[email protected]023ad6ab2013-02-17 05:07:2377 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
78 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
[email protected]277404c22010-04-22 13:09:4579 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file));
[email protected]9a8c4022011-01-25 14:25:3380 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:5981 new JsonPrefStore(
82 invalid_file, message_loop_.message_loop_proxy());
[email protected]f2d1f612010-12-09 15:10:1783 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
[email protected]9a8c4022011-01-25 14:25:3384 pref_store->ReadPrefs());
85 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4586
87 // The file should have been moved aside.
88 EXPECT_FALSE(file_util::PathExists(invalid_file));
[email protected]023ad6ab2013-02-17 05:07:2389 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
[email protected]277404c22010-04-22 13:09:4590 EXPECT_TRUE(file_util::PathExists(moved_aside));
91 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
92 moved_aside));
93}
94
[email protected]845b43a82011-05-11 10:14:4395// This function is used to avoid code duplication while testing synchronous and
96// asynchronous version of the JsonPrefStore loading.
[email protected]0de615a2012-11-08 04:40:5997void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
[email protected]023ad6ab2013-02-17 05:07:2398 const base::FilePath& output_file,
99 const base::FilePath& golden_output_file) {
[email protected]57ecc4b2010-08-11 03:02:51100 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
101 const char kMaxTabs[] = "tabs.max_tabs";
102 const char kLongIntPref[] = "long_int.pref";
[email protected]277404c22010-04-22 13:09:45103
[email protected]57ecc4b2010-08-11 03:02:51104 std::string cnn("http://www.cnn.com");
[email protected]277404c22010-04-22 13:09:45105
[email protected]68bf41a2011-03-25 16:38:31106 const Value* actual;
[email protected]5bfdcfd2012-11-22 22:08:24107 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual));
[email protected]57ecc4b2010-08-11 03:02:51108 std::string string_value;
[email protected]f2d1f612010-12-09 15:10:17109 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]277404c22010-04-22 13:09:45110 EXPECT_EQ(cnn, string_value);
111
[email protected]57ecc4b2010-08-11 03:02:51112 const char kSomeDirectory[] = "some_directory";
[email protected]277404c22010-04-22 13:09:45113
[email protected]892f1d62012-11-08 18:24:34114 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
[email protected]023ad6ab2013-02-17 05:07:23115 base::FilePath::StringType path;
[email protected]f2d1f612010-12-09 15:10:17116 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]023ad6ab2013-02-17 05:07:23117 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path);
118 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
[email protected]f2d1f612010-12-09 15:10:17119
[email protected]7e3ec42c2012-12-16 05:13:21120 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value()));
[email protected]892f1d62012-11-08 18:24:34121 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
[email protected]f2d1f612010-12-09 15:10:17122 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]277404c22010-04-22 13:09:45123 EXPECT_EQ(some_path.value(), path);
124
125 // Test reading some other data types from sub-dictionaries.
[email protected]892f1d62012-11-08 18:24:34126 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17127 bool boolean = false;
128 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45129 EXPECT_TRUE(boolean);
130
[email protected]7e3ec42c2012-12-16 05:13:21131 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false));
[email protected]892f1d62012-11-08 18:24:34132 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17133 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45134 EXPECT_FALSE(boolean);
135
[email protected]892f1d62012-11-08 18:24:34136 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17137 int integer = 0;
138 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45139 EXPECT_EQ(20, integer);
[email protected]7e3ec42c2012-12-16 05:13:21140 pref_store->SetValue(kMaxTabs, new FundamentalValue(10));
[email protected]892f1d62012-11-08 18:24:34141 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17142 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45143 EXPECT_EQ(10, integer);
144
[email protected]9a8c4022011-01-25 14:25:33145 pref_store->SetValue(kLongIntPref,
[email protected]7e3ec42c2012-12-16 05:13:21146 new StringValue(base::Int64ToString(214748364842LL)));
[email protected]892f1d62012-11-08 18:24:34147 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual));
[email protected]f2d1f612010-12-09 15:10:17148 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]e83326f2010-07-31 17:29:25149 int64 value;
[email protected]57ecc4b2010-08-11 03:02:51150 base::StringToInt64(string_value, &value);
[email protected]e83326f2010-07-31 17:29:25151 EXPECT_EQ(214748364842LL, value);
[email protected]277404c22010-04-22 13:09:45152
153 // Serialize and compare to expected output.
[email protected]277404c22010-04-22 13:09:45154 ASSERT_TRUE(file_util::PathExists(golden_output_file));
[email protected]fbe17c8a2011-12-27 16:41:48155 pref_store->CommitPendingWrite();
[email protected]f7b98b32013-02-05 08:14:15156 RunLoop().RunUntilIdle();
[email protected]277404c22010-04-22 13:09:45157 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
158 ASSERT_TRUE(file_util::Delete(output_file, false));
159}
[email protected]845b43a82011-05-11 10:14:43160
161TEST_F(JsonPrefStoreTest, Basic) {
162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
163 temp_dir_.path().AppendASCII("write.json")));
164
165 // Test that the persistent value can be loaded.
[email protected]023ad6ab2013-02-17 05:07:23166 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
[email protected]845b43a82011-05-11 10:14:43167 ASSERT_TRUE(file_util::PathExists(input_file));
168 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:59169 new JsonPrefStore(
170 input_file, message_loop_.message_loop_proxy());
[email protected]845b43a82011-05-11 10:14:43171 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
172 ASSERT_FALSE(pref_store->ReadOnly());
173
174 // The JSON file looks like this:
175 // {
176 // "homepage": "http://www.cnn.com",
177 // "some_directory": "/usr/local/",
178 // "tabs": {
179 // "new_windows_in_tabs": true,
180 // "max_tabs": 20
181 // }
182 // }
183
184 RunBasicJsonPrefStoreTest(pref_store,
185 input_file,
186 data_dir_.AppendASCII("write.golden.json"));
187}
188
189TEST_F(JsonPrefStoreTest, BasicAsync) {
190 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
191 temp_dir_.path().AppendASCII("write.json")));
192
193 // Test that the persistent value can be loaded.
[email protected]023ad6ab2013-02-17 05:07:23194 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
[email protected]845b43a82011-05-11 10:14:43195 ASSERT_TRUE(file_util::PathExists(input_file));
196 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:59197 new JsonPrefStore(
198 input_file, message_loop_.message_loop_proxy());
[email protected]845b43a82011-05-11 10:14:43199
[email protected]0de615a2012-11-08 04:40:59200 {
201 MockPrefStoreObserver mock_observer;
202 pref_store->AddObserver(&mock_observer);
[email protected]845b43a82011-05-11 10:14:43203
[email protected]0de615a2012-11-08 04:40:59204 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
205 pref_store->ReadPrefsAsync(mock_error_delegate);
[email protected]845b43a82011-05-11 10:14:43206
[email protected]0de615a2012-11-08 04:40:59207 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
208 EXPECT_CALL(*mock_error_delegate,
209 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
[email protected]7ff48ca2013-02-06 16:56:19210 RunLoop().RunUntilIdle();
[email protected]0de615a2012-11-08 04:40:59211 pref_store->RemoveObserver(&mock_observer);
[email protected]845b43a82011-05-11 10:14:43212
[email protected]0de615a2012-11-08 04:40:59213 ASSERT_FALSE(pref_store->ReadOnly());
214 }
[email protected]845b43a82011-05-11 10:14:43215
216 // The JSON file looks like this:
217 // {
218 // "homepage": "http://www.cnn.com",
219 // "some_directory": "/usr/local/",
220 // "tabs": {
221 // "new_windows_in_tabs": true,
222 // "max_tabs": 20
223 // }
224 // }
225
226 RunBasicJsonPrefStoreTest(pref_store,
227 input_file,
228 data_dir_.AppendASCII("write.golden.json"));
229}
230
231// Tests asynchronous reading of the file when there is no file.
232TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
[email protected]023ad6ab2013-02-17 05:07:23233 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
[email protected]845b43a82011-05-11 10:14:43234 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
235 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:59236 new JsonPrefStore(
237 bogus_input_file, message_loop_.message_loop_proxy());
[email protected]845b43a82011-05-11 10:14:43238 MockPrefStoreObserver mock_observer;
239 pref_store->AddObserver(&mock_observer);
240
241 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
242 pref_store->ReadPrefsAsync(mock_error_delegate);
243
244 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
245 EXPECT_CALL(*mock_error_delegate,
246 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
[email protected]7ff48ca2013-02-06 16:56:19247 RunLoop().RunUntilIdle();
[email protected]845b43a82011-05-11 10:14:43248 pref_store->RemoveObserver(&mock_observer);
249
250 EXPECT_FALSE(pref_store->ReadOnly());
251}
[email protected]ea3e4972012-04-12 03:41:37252
253TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
[email protected]023ad6ab2013-02-17 05:07:23254 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
[email protected]ea3e4972012-04-12 03:41:37255
256 ASSERT_TRUE(file_util::CopyFile(
257 data_dir_.AppendASCII("read.need_empty_value.json"),
258 pref_file));
259
260 // Test that the persistent value can be loaded.
261 ASSERT_TRUE(file_util::PathExists(pref_file));
262 scoped_refptr<JsonPrefStore> pref_store =
[email protected]0de615a2012-11-08 04:40:59263 new JsonPrefStore(
264 pref_file, message_loop_.message_loop_proxy());
[email protected]ea3e4972012-04-12 03:41:37265 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
266 ASSERT_FALSE(pref_store->ReadOnly());
267
268 // The JSON file looks like this:
269 // {
270 // "list": [ 1 ],
271 // "list_needs_empty_value": [ 2 ],
272 // "dict": {
273 // "dummy": true,
274 // },
275 // "dict_needs_empty_value": {
276 // "dummy": true,
277 // },
278 // }
279
280 // Set flag to preserve empty values for the following keys.
281 pref_store->MarkNeedsEmptyValue("list_needs_empty_value");
282 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value");
283
284 // Set all keys to empty values.
285 pref_store->SetValue("list", new base::ListValue);
286 pref_store->SetValue("list_needs_empty_value", new base::ListValue);
287 pref_store->SetValue("dict", new base::DictionaryValue);
288 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue);
289
290 // Write to file.
291 pref_store->CommitPendingWrite();
[email protected]f7b98b32013-02-05 08:14:15292 RunLoop().RunUntilIdle();
[email protected]ea3e4972012-04-12 03:41:37293
294 // Compare to expected output.
[email protected]023ad6ab2013-02-17 05:07:23295 base::FilePath golden_output_file =
[email protected]ea3e4972012-04-12 03:41:37296 data_dir_.AppendASCII("write.golden.need_empty_value.json");
297 ASSERT_TRUE(file_util::PathExists(golden_output_file));
298 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file));
299}
[email protected]7e3ec42c2012-12-16 05:13:21300
301} // namespace base