blob: 34e1b8a9ede63dcf76b4b5edeaa242c180f1bc8b [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]dfa049e2013-02-07 02:57:2213#include "base/strings/string_number_conversions.h"
[email protected]d529cb02013-06-10 19:06:5714#include "base/strings/string_util.h"
[email protected]a4ea1f12013-06-07 18:37:0715#include "base/strings/utf_string_conversions.h"
[email protected]0de615a2012-11-08 04:40:5916#include "base/threading/sequenced_worker_pool.h"
[email protected]34b99632011-01-01 01:01:0617#include "base/threading/thread.h"
[email protected]277404c22010-04-22 13:09:4518#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]fd1d067b2013-04-07 16:27:4645 ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
[email protected]00c87822012-11-27 19:09:1746 data_dir_ = data_dir_.AppendASCII("prefs");
[email protected]7567484142013-07-11 17:36:0747 ASSERT_TRUE(PathExists(data_dir_));
[email protected]277404c22010-04-22 13:09:4548 }
49
[email protected]3a305db2011-04-12 13:40:5350 // The path to temporary directory used to contain the test operations.
[email protected]ea1a3f62012-11-16 20:34:2351 base::ScopedTempDir temp_dir_;
[email protected]3a305db2011-04-12 13:40:5352 // The path to the directory where the test data is stored.
[email protected]023ad6ab2013-02-17 05:07:2353 base::FilePath data_dir_;
[email protected]ea587b02010-05-21 15:01:3554 // A message loop that we can use as the file thread message loop.
55 MessageLoop message_loop_;
[email protected]277404c22010-04-22 13:09:4556};
57
58// Test fallback behavior for a nonexistent file.
59TEST_F(JsonPrefStoreTest, NonExistentFile) {
[email protected]023ad6ab2013-02-17 05:07:2360 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
[email protected]7567484142013-07-11 17:36:0761 ASSERT_FALSE(PathExists(bogus_input_file));
[email protected]cadac622013-06-11 16:46:3662 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
63 bogus_input_file, message_loop_.message_loop_proxy().get());
[email protected]f2d1f612010-12-09 15:10:1764 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
[email protected]9a8c4022011-01-25 14:25:3365 pref_store->ReadPrefs());
66 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4567}
68
69// Test fallback behavior for an invalid file.
70TEST_F(JsonPrefStoreTest, InvalidFile) {
[email protected]023ad6ab2013-02-17 05:07:2371 base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
72 base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
[email protected]f0ff2ad2013-07-09 17:42:2673 ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file));
[email protected]9a8c4022011-01-25 14:25:3374 scoped_refptr<JsonPrefStore> pref_store =
[email protected]cadac622013-06-11 16:46:3675 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get());
[email protected]f2d1f612010-12-09 15:10:1776 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
[email protected]9a8c4022011-01-25 14:25:3377 pref_store->ReadPrefs());
78 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4579
80 // The file should have been moved aside.
[email protected]7567484142013-07-11 17:36:0781 EXPECT_FALSE(PathExists(invalid_file));
[email protected]023ad6ab2013-02-17 05:07:2382 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
[email protected]7567484142013-07-11 17:36:0783 EXPECT_TRUE(PathExists(moved_aside));
[email protected]dcd16612013-07-15 20:18:0984 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside));
[email protected]277404c22010-04-22 13:09:4585}
86
[email protected]845b43a82011-05-11 10:14:4387// This function is used to avoid code duplication while testing synchronous and
88// asynchronous version of the JsonPrefStore loading.
[email protected]0de615a2012-11-08 04:40:5989void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
[email protected]023ad6ab2013-02-17 05:07:2390 const base::FilePath& output_file,
91 const base::FilePath& golden_output_file) {
[email protected]57ecc4b2010-08-11 03:02:5192 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
93 const char kMaxTabs[] = "tabs.max_tabs";
94 const char kLongIntPref[] = "long_int.pref";
[email protected]277404c22010-04-22 13:09:4595
[email protected]57ecc4b2010-08-11 03:02:5196 std::string cnn("http://www.cnn.com");
[email protected]277404c22010-04-22 13:09:4597
[email protected]68bf41a2011-03-25 16:38:3198 const Value* actual;
[email protected]5bfdcfd2012-11-22 22:08:2499 EXPECT_TRUE(pref_store->GetValue(kHomePage, &actual));
[email protected]57ecc4b2010-08-11 03:02:51100 std::string string_value;
[email protected]f2d1f612010-12-09 15:10:17101 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]277404c22010-04-22 13:09:45102 EXPECT_EQ(cnn, string_value);
103
[email protected]57ecc4b2010-08-11 03:02:51104 const char kSomeDirectory[] = "some_directory";
[email protected]277404c22010-04-22 13:09:45105
[email protected]892f1d62012-11-08 18:24:34106 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
[email protected]023ad6ab2013-02-17 05:07:23107 base::FilePath::StringType path;
[email protected]f2d1f612010-12-09 15:10:17108 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]023ad6ab2013-02-17 05:07:23109 EXPECT_EQ(base::FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path);
110 base::FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
[email protected]f2d1f612010-12-09 15:10:17111
[email protected]7e3ec42c2012-12-16 05:13:21112 pref_store->SetValue(kSomeDirectory, new StringValue(some_path.value()));
[email protected]892f1d62012-11-08 18:24:34113 EXPECT_TRUE(pref_store->GetValue(kSomeDirectory, &actual));
[email protected]f2d1f612010-12-09 15:10:17114 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]277404c22010-04-22 13:09:45115 EXPECT_EQ(some_path.value(), path);
116
117 // Test reading some other data types from sub-dictionaries.
[email protected]892f1d62012-11-08 18:24:34118 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17119 bool boolean = false;
120 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45121 EXPECT_TRUE(boolean);
122
[email protected]7e3ec42c2012-12-16 05:13:21123 pref_store->SetValue(kNewWindowsInTabs, new FundamentalValue(false));
[email protected]892f1d62012-11-08 18:24:34124 EXPECT_TRUE(pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17125 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45126 EXPECT_FALSE(boolean);
127
[email protected]892f1d62012-11-08 18:24:34128 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17129 int integer = 0;
130 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45131 EXPECT_EQ(20, integer);
[email protected]7e3ec42c2012-12-16 05:13:21132 pref_store->SetValue(kMaxTabs, new FundamentalValue(10));
[email protected]892f1d62012-11-08 18:24:34133 EXPECT_TRUE(pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17134 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45135 EXPECT_EQ(10, integer);
136
[email protected]9a8c4022011-01-25 14:25:33137 pref_store->SetValue(kLongIntPref,
[email protected]7e3ec42c2012-12-16 05:13:21138 new StringValue(base::Int64ToString(214748364842LL)));
[email protected]892f1d62012-11-08 18:24:34139 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual));
[email protected]f2d1f612010-12-09 15:10:17140 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]e83326f2010-07-31 17:29:25141 int64 value;
[email protected]57ecc4b2010-08-11 03:02:51142 base::StringToInt64(string_value, &value);
[email protected]e83326f2010-07-31 17:29:25143 EXPECT_EQ(214748364842LL, value);
[email protected]277404c22010-04-22 13:09:45144
145 // Serialize and compare to expected output.
[email protected]7567484142013-07-11 17:36:07146 ASSERT_TRUE(PathExists(golden_output_file));
[email protected]fbe17c8a2011-12-27 16:41:48147 pref_store->CommitPendingWrite();
[email protected]f7b98b32013-02-05 08:14:15148 RunLoop().RunUntilIdle();
[email protected]dcd16612013-07-15 20:18:09149 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file));
[email protected]dd3aa792013-07-16 19:10:23150 ASSERT_TRUE(base::DeleteFile(output_file, false));
[email protected]277404c22010-04-22 13:09:45151}
[email protected]845b43a82011-05-11 10:14:43152
153TEST_F(JsonPrefStoreTest, Basic) {
[email protected]f0ff2ad2013-07-09 17:42:26154 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
[email protected]845b43a82011-05-11 10:14:43155 temp_dir_.path().AppendASCII("write.json")));
156
157 // Test that the persistent value can be loaded.
[email protected]023ad6ab2013-02-17 05:07:23158 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
[email protected]7567484142013-07-11 17:36:07159 ASSERT_TRUE(PathExists(input_file));
[email protected]845b43a82011-05-11 10:14:43160 scoped_refptr<JsonPrefStore> pref_store =
[email protected]cadac622013-06-11 16:46:36161 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get());
[email protected]845b43a82011-05-11 10:14:43162 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
163 ASSERT_FALSE(pref_store->ReadOnly());
164
165 // The JSON file looks like this:
166 // {
167 // "homepage": "http://www.cnn.com",
168 // "some_directory": "/usr/local/",
169 // "tabs": {
170 // "new_windows_in_tabs": true,
171 // "max_tabs": 20
172 // }
173 // }
174
[email protected]3703e922013-05-31 21:37:53175 RunBasicJsonPrefStoreTest(
176 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
[email protected]845b43a82011-05-11 10:14:43177}
178
179TEST_F(JsonPrefStoreTest, BasicAsync) {
[email protected]f0ff2ad2013-07-09 17:42:26180 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
[email protected]845b43a82011-05-11 10:14:43181 temp_dir_.path().AppendASCII("write.json")));
182
183 // Test that the persistent value can be loaded.
[email protected]023ad6ab2013-02-17 05:07:23184 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
[email protected]7567484142013-07-11 17:36:07185 ASSERT_TRUE(PathExists(input_file));
[email protected]845b43a82011-05-11 10:14:43186 scoped_refptr<JsonPrefStore> pref_store =
[email protected]cadac622013-06-11 16:46:36187 new JsonPrefStore(input_file, message_loop_.message_loop_proxy().get());
[email protected]845b43a82011-05-11 10:14:43188
[email protected]0de615a2012-11-08 04:40:59189 {
190 MockPrefStoreObserver mock_observer;
191 pref_store->AddObserver(&mock_observer);
[email protected]845b43a82011-05-11 10:14:43192
[email protected]0de615a2012-11-08 04:40:59193 MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
194 pref_store->ReadPrefsAsync(mock_error_delegate);
[email protected]845b43a82011-05-11 10:14:43195
[email protected]0de615a2012-11-08 04:40:59196 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
197 EXPECT_CALL(*mock_error_delegate,
198 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
[email protected]7ff48ca2013-02-06 16:56:19199 RunLoop().RunUntilIdle();
[email protected]0de615a2012-11-08 04:40:59200 pref_store->RemoveObserver(&mock_observer);
[email protected]845b43a82011-05-11 10:14:43201
[email protected]0de615a2012-11-08 04:40:59202 ASSERT_FALSE(pref_store->ReadOnly());
203 }
[email protected]845b43a82011-05-11 10:14:43204
205 // The JSON file looks like this:
206 // {
207 // "homepage": "http://www.cnn.com",
208 // "some_directory": "/usr/local/",
209 // "tabs": {
210 // "new_windows_in_tabs": true,
211 // "max_tabs": 20
212 // }
213 // }
214
[email protected]3703e922013-05-31 21:37:53215 RunBasicJsonPrefStoreTest(
216 pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
[email protected]845b43a82011-05-11 10:14:43217}
218
219// Tests asynchronous reading of the file when there is no file.
220TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
[email protected]023ad6ab2013-02-17 05:07:23221 base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
[email protected]7567484142013-07-11 17:36:07222 ASSERT_FALSE(PathExists(bogus_input_file));
[email protected]cadac622013-06-11 16:46:36223 scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
224 bogus_input_file, message_loop_.message_loop_proxy().get());
[email protected]845b43a82011-05-11 10:14:43225 MockPrefStoreObserver mock_observer;
226 pref_store->AddObserver(&mock_observer);
227
228 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
229 pref_store->ReadPrefsAsync(mock_error_delegate);
230
231 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
232 EXPECT_CALL(*mock_error_delegate,
233 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
[email protected]7ff48ca2013-02-06 16:56:19234 RunLoop().RunUntilIdle();
[email protected]845b43a82011-05-11 10:14:43235 pref_store->RemoveObserver(&mock_observer);
236
237 EXPECT_FALSE(pref_store->ReadOnly());
238}
[email protected]ea3e4972012-04-12 03:41:37239
240TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
[email protected]023ad6ab2013-02-17 05:07:23241 base::FilePath pref_file = temp_dir_.path().AppendASCII("write.json");
[email protected]ea3e4972012-04-12 03:41:37242
[email protected]f0ff2ad2013-07-09 17:42:26243 ASSERT_TRUE(base::CopyFile(
[email protected]ea3e4972012-04-12 03:41:37244 data_dir_.AppendASCII("read.need_empty_value.json"),
245 pref_file));
246
247 // Test that the persistent value can be loaded.
[email protected]7567484142013-07-11 17:36:07248 ASSERT_TRUE(PathExists(pref_file));
[email protected]ea3e4972012-04-12 03:41:37249 scoped_refptr<JsonPrefStore> pref_store =
[email protected]cadac622013-06-11 16:46:36250 new JsonPrefStore(pref_file, message_loop_.message_loop_proxy().get());
[email protected]ea3e4972012-04-12 03:41:37251 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
252 ASSERT_FALSE(pref_store->ReadOnly());
253
254 // The JSON file looks like this:
255 // {
256 // "list": [ 1 ],
257 // "list_needs_empty_value": [ 2 ],
258 // "dict": {
259 // "dummy": true,
260 // },
261 // "dict_needs_empty_value": {
262 // "dummy": true,
263 // },
264 // }
265
266 // Set flag to preserve empty values for the following keys.
267 pref_store->MarkNeedsEmptyValue("list_needs_empty_value");
268 pref_store->MarkNeedsEmptyValue("dict_needs_empty_value");
269
270 // Set all keys to empty values.
271 pref_store->SetValue("list", new base::ListValue);
272 pref_store->SetValue("list_needs_empty_value", new base::ListValue);
273 pref_store->SetValue("dict", new base::DictionaryValue);
274 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue);
275
276 // Write to file.
277 pref_store->CommitPendingWrite();
[email protected]f7b98b32013-02-05 08:14:15278 RunLoop().RunUntilIdle();
[email protected]ea3e4972012-04-12 03:41:37279
280 // Compare to expected output.
[email protected]023ad6ab2013-02-17 05:07:23281 base::FilePath golden_output_file =
[email protected]ea3e4972012-04-12 03:41:37282 data_dir_.AppendASCII("write.golden.need_empty_value.json");
[email protected]7567484142013-07-11 17:36:07283 ASSERT_TRUE(PathExists(golden_output_file));
[email protected]dcd16612013-07-15 20:18:09284 EXPECT_TRUE(TextContentsEqual(golden_output_file, pref_file));
[email protected]ea3e4972012-04-12 03:41:37285}
[email protected]7e3ec42c2012-12-16 05:13:21286
287} // namespace base