[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 5 | #include "base/file_util.h" |
| 6 | #include "base/message_loop.h" |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 7 | #include "base/message_loop_proxy.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 9 | #include "base/ref_counted.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 10 | #include "base/scoped_ptr.h" |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 11 | #include "base/string_number_conversions.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 13 | #include "base/threading/thread.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 14 | #include "base/utf_string_conversions.h" |
| 15 | #include "base/values.h" |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 16 | #include "chrome/common/json_pref_store.h" |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 17 | #include "chrome/common/chrome_paths.h" |
| 18 | #include "chrome/common/pref_names.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
| 21 | class JsonPrefStoreTest : public testing::Test { |
| 22 | protected: |
| 23 | virtual void SetUp() { |
[email protected] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 24 | message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 25 | // 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] | ea587b0 | 2010-05-21 15:01:35 | [diff] [blame] | 48 | // 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] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | // Test fallback behavior for a nonexistent file. |
| 54 | TEST_F(JsonPrefStoreTest, NonExistentFile) { |
| 55 | FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 56 | ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 57 | scoped_refptr<JsonPrefStore> pref_store = |
| 58 | new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 59 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 60 | pref_store->ReadPrefs()); |
| 61 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | // Test fallback behavior for an invalid file. |
| 65 | TEST_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] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 69 | scoped_refptr<JsonPrefStore> pref_store = |
| 70 | new JsonPrefStore(invalid_file, message_loop_proxy_.get()); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 71 | EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 72 | pref_store->ReadPrefs()); |
| 73 | EXPECT_FALSE(pref_store->ReadOnly()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 74 | |
| 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 | |
| 83 | TEST_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] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 90 | 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] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 94 | |
| 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] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 105 | const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
| 106 | const char kMaxTabs[] = "tabs.max_tabs"; |
| 107 | const char kLongIntPref[] = "long_int.pref"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 108 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 109 | std::string cnn("http://www.cnn.com"); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 110 | |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 111 | Value* actual; |
| 112 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 113 | pref_store->GetValue(prefs::kHomePage, &actual)); |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 114 | std::string string_value; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 115 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 116 | EXPECT_EQ(cnn, string_value); |
| 117 | |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 118 | const char kSomeDirectory[] = "some_directory"; |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 119 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 120 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 121 | FilePath::StringType path; |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 122 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 123 | EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); |
| 124 | FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 125 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 126 | pref_store->SetValue(kSomeDirectory, |
| 127 | Value::CreateStringValue(some_path.value())); |
| 128 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 129 | EXPECT_TRUE(actual->GetAsString(&path)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 130 | EXPECT_EQ(some_path.value(), path); |
| 131 | |
| 132 | // Test reading some other data types from sub-dictionaries. |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 133 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 134 | pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 135 | bool boolean = false; |
| 136 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 137 | EXPECT_TRUE(boolean); |
| 138 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 139 | pref_store->SetValue(kNewWindowsInTabs, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 140 | Value::CreateBooleanValue(false)); |
| 141 | EXPECT_EQ(PrefStore::READ_OK, |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 142 | pref_store->GetValue(kNewWindowsInTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 143 | EXPECT_TRUE(actual->GetAsBoolean(&boolean)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 144 | EXPECT_FALSE(boolean); |
| 145 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 146 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 147 | int integer = 0; |
| 148 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 149 | EXPECT_EQ(20, integer); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 150 | pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); |
| 151 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 152 | EXPECT_TRUE(actual->GetAsInteger(&integer)); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 153 | EXPECT_EQ(10, integer); |
| 154 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 155 | pref_store->SetValue(kLongIntPref, |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 156 | Value::CreateStringValue( |
| 157 | base::Int64ToString(214748364842LL))); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 158 | EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); |
[email protected] | f2d1f61 | 2010-12-09 15:10:17 | [diff] [blame] | 159 | EXPECT_TRUE(actual->GetAsString(&string_value)); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 160 | int64 value; |
[email protected] | 57ecc4b | 2010-08-11 03:02:51 | [diff] [blame] | 161 | base::StringToInt64(string_value, &value); |
[email protected] | e83326f | 2010-07-31 17:29:25 | [diff] [blame] | 162 | EXPECT_EQ(214748364842LL, value); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 163 | |
| 164 | // Serialize and compare to expected output. |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 165 | 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] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame^] | 168 | ASSERT_TRUE(pref_store->WritePrefs()); |
[email protected] | 277404c2 | 2010-04-22 13:09:45 | [diff] [blame] | 169 | MessageLoop::current()->RunAllPending(); |
| 170 | EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 171 | ASSERT_TRUE(file_util::Delete(output_file, false)); |
| 172 | } |