1 // Copyright (c) 2011 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 "base/file_util.h" 6 #include "base/memory/ref_counted.h" 7 #include "base/memory/scoped_ptr.h" 8 #include "base/message_loop.h" 9 #include "base/message_loop_proxy.h" 10 #include "base/path_service.h" 11 #include "base/string_number_conversions.h" 12 #include "base/string_util.h" 13 #include "base/threading/thread.h" 14 #include "base/utf_string_conversions.h" 15 #include "base/values.h" 16 #include "base/memory/scoped_temp_dir.h" 17 #include "chrome/common/json_pref_store.h" 18 #include "chrome/common/chrome_paths.h" 19 #include "chrome/common/pref_names.h" 20 #include "testing/gtest/include/gtest/gtest.h" 21 22 class JsonPrefStoreTest : public testing::Test { 23 protected: 24 virtual void SetUp() { 25 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); 26 27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 28 29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); 30 data_dir_ = data_dir_.AppendASCII("pref_service"); 31 ASSERT_TRUE(file_util::PathExists(data_dir_)); 32 } 33 34 // The path to temporary directory used to contain the test operations. 35 ScopedTempDir temp_dir_; 36 // The path to the directory where the test data is stored. 37 FilePath data_dir_; 38 // A message loop that we can use as the file thread message loop. 39 MessageLoop message_loop_; 40 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; 41 }; 42 43 // Test fallback behavior for a nonexistent file. 44 TEST_F(JsonPrefStoreTest, NonExistentFile) { 45 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); 46 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); 47 scoped_refptr<JsonPrefStore> pref_store = 48 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); 49 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, 50 pref_store->ReadPrefs()); 51 EXPECT_FALSE(pref_store->ReadOnly()); 52 } 53 54 // Test fallback behavior for an invalid file. 55 TEST_F(JsonPrefStoreTest, InvalidFile) { 56 FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json"); 57 FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json"); 58 ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file)); 59 scoped_refptr<JsonPrefStore> pref_store = 60 new JsonPrefStore(invalid_file, message_loop_proxy_.get()); 61 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, 62 pref_store->ReadPrefs()); 63 EXPECT_FALSE(pref_store->ReadOnly()); 64 65 // The file should have been moved aside. 66 EXPECT_FALSE(file_util::PathExists(invalid_file)); 67 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); 68 EXPECT_TRUE(file_util::PathExists(moved_aside)); 69 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, 70 moved_aside)); 71 } 72 73 TEST_F(JsonPrefStoreTest, Basic) { 74 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), 75 temp_dir_.path().AppendASCII("write.json"))); 76 77 // Test that the persistent value can be loaded. 78 FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 79 ASSERT_TRUE(file_util::PathExists(input_file)); 80 scoped_refptr<JsonPrefStore> pref_store = 81 new JsonPrefStore(input_file, message_loop_proxy_.get()); 82 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); 83 ASSERT_FALSE(pref_store->ReadOnly()); 84 85 // The JSON file looks like this: 86 // { 87 // "homepage": "http://www.cnn.com", 88 // "some_directory": "/usr/local/", 89 // "tabs": { 90 // "new_windows_in_tabs": true, 91 // "max_tabs": 20 92 // } 93 // } 94 95 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; 96 const char kMaxTabs[] = "tabs.max_tabs"; 97 const char kLongIntPref[] = "long_int.pref"; 98 99 std::string cnn("http://www.cnn.com"); 100 101 const Value* actual; 102 EXPECT_EQ(PrefStore::READ_OK, 103 pref_store->GetValue(prefs::kHomePage, &actual)); 104 std::string string_value; 105 EXPECT_TRUE(actual->GetAsString(&string_value)); 106 EXPECT_EQ(cnn, string_value); 107 108 const char kSomeDirectory[] = "some_directory"; 109 110 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); 111 FilePath::StringType path; 112 EXPECT_TRUE(actual->GetAsString(&path)); 113 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), path); 114 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); 115 116 pref_store->SetValue(kSomeDirectory, 117 Value::CreateStringValue(some_path.value())); 118 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kSomeDirectory, &actual)); 119 EXPECT_TRUE(actual->GetAsString(&path)); 120 EXPECT_EQ(some_path.value(), path); 121 122 // Test reading some other data types from sub-dictionaries. 123 EXPECT_EQ(PrefStore::READ_OK, 124 pref_store->GetValue(kNewWindowsInTabs, &actual)); 125 bool boolean = false; 126 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); 127 EXPECT_TRUE(boolean); 128 129 pref_store->SetValue(kNewWindowsInTabs, 130 Value::CreateBooleanValue(false)); 131 EXPECT_EQ(PrefStore::READ_OK, 132 pref_store->GetValue(kNewWindowsInTabs, &actual)); 133 EXPECT_TRUE(actual->GetAsBoolean(&boolean)); 134 EXPECT_FALSE(boolean); 135 136 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); 137 int integer = 0; 138 EXPECT_TRUE(actual->GetAsInteger(&integer)); 139 EXPECT_EQ(20, integer); 140 pref_store->SetValue(kMaxTabs, Value::CreateIntegerValue(10)); 141 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kMaxTabs, &actual)); 142 EXPECT_TRUE(actual->GetAsInteger(&integer)); 143 EXPECT_EQ(10, integer); 144 145 pref_store->SetValue(kLongIntPref, 146 Value::CreateStringValue( 147 base::Int64ToString(214748364842LL))); 148 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); 149 EXPECT_TRUE(actual->GetAsString(&string_value)); 150 int64 value; 151 base::StringToInt64(string_value, &value); 152 EXPECT_EQ(214748364842LL, value); 153 154 // Serialize and compare to expected output. 155 FilePath output_file = input_file; 156 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); 157 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 158 ASSERT_TRUE(pref_store->WritePrefs()); 159 MessageLoop::current()->RunAllPending(); 160 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 161 ASSERT_TRUE(file_util::Delete(output_file, false)); 162 } 163