blob: 1b98aa9bfd93165cbd261c9de12e14ba7566b8e1 [file] [log] [blame]
[email protected]9a8c4022011-01-25 14:25:331// Copyright (c) 2011 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]277404c22010-04-22 13:09:455#include "base/file_util.h"
6#include "base/message_loop.h"
[email protected]ea587b02010-05-21 15:01:357#include "base/message_loop_proxy.h"
[email protected]277404c22010-04-22 13:09:458#include "base/path_service.h"
[email protected]9a8c4022011-01-25 14:25:339#include "base/ref_counted.h"
[email protected]277404c22010-04-22 13:09:4510#include "base/scoped_ptr.h"
[email protected]e83326f2010-07-31 17:29:2511#include "base/string_number_conversions.h"
[email protected]277404c22010-04-22 13:09:4512#include "base/string_util.h"
[email protected]34b99632011-01-01 01:01:0613#include "base/threading/thread.h"
[email protected]277404c22010-04-22 13:09:4514#include "base/utf_string_conversions.h"
15#include "base/values.h"
[email protected]ea587b02010-05-21 15:01:3516#include "chrome/common/json_pref_store.h"
[email protected]277404c22010-04-22 13:09:4517#include "chrome/common/chrome_paths.h"
18#include "chrome/common/pref_names.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
21class JsonPrefStoreTest : public testing::Test {
22 protected:
23 virtual void SetUp() {
[email protected]ea587b02010-05-21 15:01:3524 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread();
[email protected]277404c22010-04-22 13:09:4525 // Name a subdirectory of the temp directory.
26 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
27 test_dir_ = test_dir_.AppendASCII("JsonPrefStoreTest");
28
29 // Create a fresh, empty copy of this directory.
30 file_util::Delete(test_dir_, true);
31 file_util::CreateDirectory(test_dir_);
32
33 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
34 data_dir_ = data_dir_.AppendASCII("pref_service");
35 ASSERT_TRUE(file_util::PathExists(data_dir_));
36 }
37
38 virtual void TearDown() {
39 // Clean up test directory
40 ASSERT_TRUE(file_util::Delete(test_dir_, true));
41 ASSERT_FALSE(file_util::PathExists(test_dir_));
42 }
43
44 // the path to temporary directory used to contain the test operations
45 FilePath test_dir_;
46 // the path to the directory where the test data is stored
47 FilePath data_dir_;
[email protected]ea587b02010-05-21 15:01:3548 // A message loop that we can use as the file thread message loop.
49 MessageLoop message_loop_;
50 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
[email protected]277404c22010-04-22 13:09:4551};
52
53// Test fallback behavior for a nonexistent file.
54TEST_F(JsonPrefStoreTest, NonExistentFile) {
55 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
56 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
[email protected]9a8c4022011-01-25 14:25:3357 scoped_refptr<JsonPrefStore> pref_store =
58 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get());
[email protected]f2d1f612010-12-09 15:10:1759 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
[email protected]9a8c4022011-01-25 14:25:3360 pref_store->ReadPrefs());
61 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4562}
63
64// Test fallback behavior for an invalid file.
65TEST_F(JsonPrefStoreTest, InvalidFile) {
66 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
67 FilePath invalid_file = test_dir_.AppendASCII("invalid.json");
68 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file));
[email protected]9a8c4022011-01-25 14:25:3369 scoped_refptr<JsonPrefStore> pref_store =
70 new JsonPrefStore(invalid_file, message_loop_proxy_.get());
[email protected]f2d1f612010-12-09 15:10:1771 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
[email protected]9a8c4022011-01-25 14:25:3372 pref_store->ReadPrefs());
73 EXPECT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4574
75 // The file should have been moved aside.
76 EXPECT_FALSE(file_util::PathExists(invalid_file));
77 FilePath moved_aside = test_dir_.AppendASCII("invalid.bad");
78 EXPECT_TRUE(file_util::PathExists(moved_aside));
79 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
80 moved_aside));
81}
82
83TEST_F(JsonPrefStoreTest, Basic) {
84 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
85 test_dir_.AppendASCII("write.json")));
86
87 // Test that the persistent value can be loaded.
88 FilePath input_file = test_dir_.AppendASCII("write.json");
89 ASSERT_TRUE(file_util::PathExists(input_file));
[email protected]9a8c4022011-01-25 14:25:3390 scoped_refptr<JsonPrefStore> pref_store =
91 new JsonPrefStore(input_file, message_loop_proxy_.get());
92 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
93 ASSERT_FALSE(pref_store->ReadOnly());
[email protected]277404c22010-04-22 13:09:4594
95 // The JSON file looks like this:
96 // {
97 // "homepage": "http://www.cnn.com",
98 // "some_directory": "/usr/local/",
99 // "tabs": {
100 // "new_windows_in_tabs": true,
101 // "max_tabs": 20
102 // }
103 // }
104
[email protected]57ecc4b2010-08-11 03:02:51105 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
106 const char kMaxTabs[] = "tabs.max_tabs";
107 const char kLongIntPref[] = "long_int.pref";
[email protected]277404c22010-04-22 13:09:45108
[email protected]57ecc4b2010-08-11 03:02:51109 std::string cnn("http://www.cnn.com");
[email protected]277404c22010-04-22 13:09:45110
[email protected]f2d1f612010-12-09 15:10:17111 Value* actual;
112 EXPECT_EQ(PrefStore::READ_OK,
[email protected]9a8c4022011-01-25 14:25:33113 pref_store->GetValue(prefs::kHomePage, &actual));
[email protected]57ecc4b2010-08-11 03:02:51114 std::string string_value;
[email protected]f2d1f612010-12-09 15:10:17115 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]277404c22010-04-22 13:09:45116 EXPECT_EQ(cnn, string_value);
117
[email protected]57ecc4b2010-08-11 03:02:51118 const char kSomeDirectory[] = "some_directory";
[email protected]277404c22010-04-22 13:09:45119
[email protected]9a8c4022011-01-25 14:25:33120 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual));
[email protected]277404c22010-04-22 13:09:45121 FilePath::StringType path;
[email protected]f2d1f612010-12-09 15:10:17122 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]277404c22010-04-22 13:09:45123 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path);
124 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/"));
[email protected]f2d1f612010-12-09 15:10:17125
[email protected]9a8c4022011-01-25 14:25:33126 pref_store->SetValue(kSomeDirectory,
127 Value::CreateStringValue(some_path.value()));
128 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual));
[email protected]f2d1f612010-12-09 15:10:17129 EXPECT_TRUE(actual->GetAsString(&path));
[email protected]277404c22010-04-22 13:09:45130 EXPECT_EQ(some_path.value(), path);
131
132 // Test reading some other data types from sub-dictionaries.
[email protected]f2d1f612010-12-09 15:10:17133 EXPECT_EQ(PrefStore::READ_OK,
[email protected]9a8c4022011-01-25 14:25:33134 pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17135 bool boolean = false;
136 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45137 EXPECT_TRUE(boolean);
138
[email protected]9a8c4022011-01-25 14:25:33139 pref_store->SetValue(kNewWindowsInTabs,
[email protected]f2d1f612010-12-09 15:10:17140 Value::CreateBooleanValue(false));
141 EXPECT_EQ(PrefStore::READ_OK,
[email protected]9a8c4022011-01-25 14:25:33142 pref_store->GetValue(kNewWindowsInTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17143 EXPECT_TRUE(actual->GetAsBoolean(&boolean));
[email protected]277404c22010-04-22 13:09:45144 EXPECT_FALSE(boolean);
145
[email protected]9a8c4022011-01-25 14:25:33146 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17147 int integer = 0;
148 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45149 EXPECT_EQ(20, integer);
[email protected]9a8c4022011-01-25 14:25:33150 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10));
151 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual));
[email protected]f2d1f612010-12-09 15:10:17152 EXPECT_TRUE(actual->GetAsInteger(&integer));
[email protected]277404c22010-04-22 13:09:45153 EXPECT_EQ(10, integer);
154
[email protected]9a8c4022011-01-25 14:25:33155 pref_store->SetValue(kLongIntPref,
[email protected]f2d1f612010-12-09 15:10:17156 Value::CreateStringValue(
157 base::Int64ToString(214748364842LL)));
[email protected]9a8c4022011-01-25 14:25:33158 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual));
[email protected]f2d1f612010-12-09 15:10:17159 EXPECT_TRUE(actual->GetAsString(&string_value));
[email protected]e83326f2010-07-31 17:29:25160 int64 value;
[email protected]57ecc4b2010-08-11 03:02:51161 base::StringToInt64(string_value, &value);
[email protected]e83326f2010-07-31 17:29:25162 EXPECT_EQ(214748364842LL, value);
[email protected]277404c22010-04-22 13:09:45163
164 // Serialize and compare to expected output.
[email protected]277404c22010-04-22 13:09:45165 FilePath output_file = input_file;
166 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json");
167 ASSERT_TRUE(file_util::PathExists(golden_output_file));
[email protected]9a8c4022011-01-25 14:25:33168 ASSERT_TRUE(pref_store->WritePrefs());
[email protected]277404c22010-04-22 13:09:45169 MessageLoop::current()->RunAllPending();
170 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
171 ASSERT_TRUE(file_util::Delete(output_file, false));
172}