| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/path_service.h" | 6 #include "base/path_service.h" |
| 7 #include "chrome/common/chrome_paths.h" | 7 #include "chrome/common/chrome_paths.h" |
| 8 #include "chrome/common/json_value_serializer.h" | 8 #include "chrome/common/json_value_serializer.h" |
| 9 #include "chrome/common/notification_service.h" | 9 #include "chrome/common/notification_service.h" |
| 10 #include "chrome/common/notification_type.h" | 10 #include "chrome/common/notification_type.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/common/pref_service.h" | 12 #include "chrome/common/pref_service.h" |
| 13 #include "chrome/test/data/resource.h" | 13 #include "chrome/test/data/resource.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 class PrefServiceTest : public testing::Test { | 18 class PrefServiceTest : public testing::Test { |
| 19 protected: | 19 protected: |
| 20 virtual void SetUp() { | 20 virtual void SetUp() { |
| 21 // Name a subdirectory of the temp directory. | 21 // Name a subdirectory of the temp directory. |
| 22 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 22 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
| 23 file_util::AppendToPath(&test_dir_, L"PrefServiceTest"); | 23 test_dir_ = test_dir_.AppendASCII("PrefServiceTest"); |
| 24 | 24 |
| 25 // Create a fresh, empty copy of this directory. | 25 // Create a fresh, empty copy of this directory. |
| 26 file_util::Delete(test_dir_, true); | 26 file_util::Delete(test_dir_, true); |
| 27 file_util::CreateDirectory(test_dir_); | 27 file_util::CreateDirectory(test_dir_); |
| 28 | 28 |
| 29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
| 30 file_util::AppendToPath(&data_dir_, L"pref_service"); | 30 data_dir_ = data_dir_.AppendASCII("pref_service"); |
| 31 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 31 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
| 32 } | 32 } |
| 33 virtual void TearDown() { | 33 virtual void TearDown() { |
| 34 // Clean up test directory | 34 // Clean up test directory |
| 35 ASSERT_TRUE(file_util::Delete(test_dir_, false)); | 35 ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
| 36 ASSERT_FALSE(file_util::PathExists(test_dir_)); | 36 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
| 37 } | 37 } |
| 38 | 38 |
| 39 // the path to temporary directory used to contain the test operations | 39 // the path to temporary directory used to contain the test operations |
| 40 std::wstring test_dir_; | 40 FilePath test_dir_; |
| 41 // the path to the directory where the test data is stored | 41 // the path to the directory where the test data is stored |
| 42 std::wstring data_dir_; | 42 FilePath data_dir_; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 class TestPrefObserver : public NotificationObserver { | 45 class TestPrefObserver : public NotificationObserver { |
| 46 public: | 46 public: |
| 47 TestPrefObserver(const PrefService* prefs, const std::wstring& pref_name, | 47 TestPrefObserver(const PrefService* prefs, const std::wstring& pref_name, |
| 48 const std::wstring& new_pref_value) | 48 const std::wstring& new_pref_value) |
| 49 : observer_fired_(false), | 49 : observer_fired_(false), |
| 50 prefs_(prefs), | 50 prefs_(prefs), |
| 51 pref_name_(pref_name), | 51 pref_name_(pref_name), |
| 52 new_pref_value_(new_pref_value) { | 52 new_pref_value_(new_pref_value) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 78 const std::wstring pref_name_; | 78 const std::wstring pref_name_; |
| 79 std::wstring new_pref_value_; | 79 std::wstring new_pref_value_; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 } // anonymous namespace | 82 } // anonymous namespace |
| 83 | 83 |
| 84 TEST_F(PrefServiceTest, Basic) { | 84 TEST_F(PrefServiceTest, Basic) { |
| 85 PrefService prefs; | 85 PrefService prefs; |
| 86 | 86 |
| 87 // Test that it fails on nonexistent file. | 87 // Test that it fails on nonexistent file. |
| 88 std::wstring bogus_input_file = data_dir_; | 88 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 89 file_util::AppendToPath(&bogus_input_file, L"read.txt"); | |
| 90 EXPECT_FALSE(prefs.LoadPersistentPrefs(bogus_input_file)); | 89 EXPECT_FALSE(prefs.LoadPersistentPrefs(bogus_input_file)); |
| 91 | 90 |
| 92 // Test that the persistent value can be loaded. | 91 // Test that the persistent value can be loaded. |
| 93 std::wstring input_file = data_dir_; | 92 FilePath input_file = data_dir_.AppendASCII("read.json"); |
| 94 file_util::AppendToPath(&input_file, L"read.json"); | |
| 95 ASSERT_TRUE(file_util::PathExists(input_file)); | 93 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 96 ASSERT_TRUE(prefs.LoadPersistentPrefs(input_file)); | 94 ASSERT_TRUE(prefs.LoadPersistentPrefs(input_file)); |
| 97 | 95 |
| 98 // Register test prefs. | 96 // Register test prefs. |
| 99 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs"; | 97 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs"; |
| 100 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; | 98 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; |
| 101 prefs.RegisterStringPref(prefs::kHomePage, L""); | 99 prefs.RegisterStringPref(prefs::kHomePage, L""); |
| 102 prefs.RegisterBooleanPref(kNewWindowsInTabs, false); | 100 prefs.RegisterBooleanPref(kNewWindowsInTabs, false); |
| 103 prefs.RegisterIntegerPref(kMaxTabs, 0); | 101 prefs.RegisterIntegerPref(kMaxTabs, 0); |
| 104 | 102 |
| 105 std::wstring microsoft(L"http://www.microsoft.com"); | 103 std::wstring microsoft(L"http://www.microsoft.com"); |
| 106 std::wstring cnn(L"http://www.cnn.com"); | 104 std::wstring cnn(L"http://www.cnn.com"); |
| 107 std::wstring homepage(L"http://www.example.com"); | 105 std::wstring homepage(L"http://www.example.com"); |
| 108 | 106 |
| 109 EXPECT_EQ(cnn, prefs.GetString(prefs::kHomePage)); | 107 EXPECT_EQ(cnn, prefs.GetString(prefs::kHomePage)); |
| 110 | 108 |
| 109 const wchar_t kSomeDirectory[] = L"some_directory"; |
| 110 FilePath some_path(FILE_PATH_LITERAL("/usr/sbin/")); |
| 111 prefs.RegisterFilePathPref(kSomeDirectory, FilePath()); |
| 112 |
| 111 // Now test that the transient value overrides the persistent value, | 113 // Now test that the transient value overrides the persistent value, |
| 112 // without actually altering the persistent store. | 114 // without actually altering the persistent store. |
| 113 EXPECT_TRUE(prefs.transient()->SetString(prefs::kHomePage, microsoft)); | 115 EXPECT_TRUE(prefs.transient()->SetString(prefs::kHomePage, microsoft)); |
| 114 EXPECT_TRUE(prefs.transient()->GetString(prefs::kHomePage, &homepage)); | 116 EXPECT_TRUE(prefs.transient()->GetString(prefs::kHomePage, &homepage)); |
| 115 EXPECT_EQ(microsoft, homepage); | 117 EXPECT_EQ(microsoft, homepage); |
| 116 | 118 |
| 117 EXPECT_EQ(microsoft, prefs.GetString(prefs::kHomePage)); | 119 EXPECT_EQ(microsoft, prefs.GetString(prefs::kHomePage)); |
| 118 | 120 |
| 119 // Test reading some other data types from sub-dictionaries, and | 121 // Test reading some other data types from sub-dictionaries, and |
| 120 // writing to the persistent store. | 122 // writing to the persistent store. |
| 121 EXPECT_TRUE(prefs.GetBoolean(kNewWindowsInTabs)); | 123 EXPECT_TRUE(prefs.GetBoolean(kNewWindowsInTabs)); |
| 122 prefs.SetBoolean(kNewWindowsInTabs, false); | 124 prefs.SetBoolean(kNewWindowsInTabs, false); |
| 123 EXPECT_FALSE(prefs.GetBoolean(kNewWindowsInTabs)); | 125 EXPECT_FALSE(prefs.GetBoolean(kNewWindowsInTabs)); |
| 124 | 126 |
| 125 EXPECT_EQ(20, prefs.GetInteger(kMaxTabs)); | 127 EXPECT_EQ(20, prefs.GetInteger(kMaxTabs)); |
| 126 prefs.SetInteger(kMaxTabs, 10); | 128 prefs.SetInteger(kMaxTabs, 10); |
| 127 EXPECT_EQ(10, prefs.GetInteger(kMaxTabs)); | 129 EXPECT_EQ(10, prefs.GetInteger(kMaxTabs)); |
| 128 | 130 |
| 131 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), |
| 132 prefs.GetFilePath(kSomeDirectory).value()); |
| 133 prefs.SetFilePath(kSomeDirectory, some_path); |
| 134 EXPECT_EQ(some_path.value(), prefs.GetFilePath(kSomeDirectory).value()); |
| 135 |
| 129 // Serialize and compare to expected output. | 136 // Serialize and compare to expected output. |
| 130 std::wstring output_file = test_dir_; | 137 FilePath output_file = test_dir_.AppendASCII("write.json"); |
| 131 file_util::AppendToPath(&output_file, L"write.json"); | |
| 132 prefs.pref_filename_ = output_file; | 138 prefs.pref_filename_ = output_file; |
| 133 ASSERT_TRUE(prefs.SavePersistentPrefs(NULL)); | 139 ASSERT_TRUE(prefs.SavePersistentPrefs(NULL)); |
| 134 std::wstring golden_output_file = data_dir_; | 140 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
| 135 file_util::AppendToPath(&golden_output_file, L"write.golden.json"); | |
| 136 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 141 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 137 ASSERT_TRUE(file_util::ContentsEqual(golden_output_file, output_file)); | 142 ASSERT_TRUE(file_util::ContentsEqual(golden_output_file, output_file)); |
| 138 } | 143 } |
| 139 | 144 |
| 140 TEST_F(PrefServiceTest, Overlay) { | 145 TEST_F(PrefServiceTest, Overlay) { |
| 141 const std::string transient = | 146 const std::string transient = |
| 142 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\"," | 147 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\"," |
| 143 "\"dictionary\":{\"value\":\"transient\"}," | 148 "\"dictionary\":{\"value\":\"transient\"}," |
| 144 "\"list\":[\"transient\"]}"; | 149 "\"list\":[\"transient\"]}"; |
| 145 | 150 |
| 146 std::wstring persistent_string(L"persistent"); | 151 std::wstring persistent_string(L"persistent"); |
| 147 std::wstring transient_string(L"transient"); | 152 std::wstring transient_string(L"transient"); |
| 148 | 153 |
| 149 PrefService prefs; | 154 PrefService prefs; |
| 150 | 155 |
| 151 std::wstring persistent_file = data_dir_; | 156 FilePath persistent_file = data_dir_.AppendASCII("overlay.json"); |
| 152 file_util::AppendToPath(&persistent_file, L"overlay.json"); | |
| 153 EXPECT_TRUE(prefs.LoadPersistentPrefs(persistent_file)); | 157 EXPECT_TRUE(prefs.LoadPersistentPrefs(persistent_file)); |
| 154 | 158 |
| 155 Value* transient_value; | 159 Value* transient_value; |
| 156 { | 160 { |
| 157 JSONStringValueSerializer serializer(transient); | 161 JSONStringValueSerializer serializer(transient); |
| 158 transient_value = serializer.Deserialize(NULL); | 162 transient_value = serializer.Deserialize(NULL); |
| 159 ASSERT_TRUE(transient_value); | 163 ASSERT_TRUE(transient_value); |
| 160 } | 164 } |
| 161 prefs.transient()->Set(transient_string, transient_value); | 165 prefs.transient()->Set(transient_string, transient_value); |
| 162 | 166 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 ASSERT_TRUE(member_value); | 272 ASSERT_TRUE(member_value); |
| 269 std::wstring result_string; | 273 std::wstring result_string; |
| 270 ASSERT_TRUE(member_value->GetAsString(&result_string)); | 274 ASSERT_TRUE(member_value->GetAsString(&result_string)); |
| 271 ASSERT_EQ(transient_string, result_string); | 275 ASSERT_EQ(transient_string, result_string); |
| 272 } | 276 } |
| 273 } | 277 } |
| 274 | 278 |
| 275 TEST_F(PrefServiceTest, Observers) { | 279 TEST_F(PrefServiceTest, Observers) { |
| 276 PrefService prefs; | 280 PrefService prefs; |
| 277 | 281 |
| 278 std::wstring input_file = data_dir_; | 282 FilePath input_file = data_dir_.AppendASCII("read.json"); |
| 279 file_util::AppendToPath(&input_file, L"read.json"); | |
| 280 EXPECT_TRUE(file_util::PathExists(input_file)); | 283 EXPECT_TRUE(file_util::PathExists(input_file)); |
| 281 EXPECT_TRUE(prefs.LoadPersistentPrefs(input_file)); | 284 EXPECT_TRUE(prefs.LoadPersistentPrefs(input_file)); |
| 282 | 285 |
| 283 const wchar_t pref_name[] = L"homepage"; | 286 const wchar_t pref_name[] = L"homepage"; |
| 284 prefs.RegisterStringPref(pref_name, L""); | 287 prefs.RegisterStringPref(pref_name, L""); |
| 285 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name)); | 288 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name)); |
| 286 | 289 |
| 287 const std::wstring new_pref_value(L"http://www.google.com/"); | 290 const std::wstring new_pref_value(L"http://www.google.com/"); |
| 288 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 291 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 289 prefs.AddPrefObserver(pref_name, &obs); | 292 prefs.AddPrefObserver(pref_name, &obs); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 309 obs2.Reset(new_pref_value); | 312 obs2.Reset(new_pref_value); |
| 310 // This should only fire the observer in obs2. | 313 // This should only fire the observer in obs2. |
| 311 prefs.SetString(pref_name, new_pref_value); | 314 prefs.SetString(pref_name, new_pref_value); |
| 312 EXPECT_FALSE(obs.observer_fired()); | 315 EXPECT_FALSE(obs.observer_fired()); |
| 313 EXPECT_TRUE(obs2.observer_fired()); | 316 EXPECT_TRUE(obs2.observer_fired()); |
| 314 | 317 |
| 315 // Ok, clean up. | 318 // Ok, clean up. |
| 316 prefs.RemovePrefObserver(pref_name, &obs2); | 319 prefs.RemovePrefObserver(pref_name, &obs2); |
| 317 } | 320 } |
| 318 | 321 |
| 322 // TODO(port): port this test to POSIX. |
| 323 #if defined(OS_WIN) |
| 319 TEST_F(PrefServiceTest, LocalizedPrefs) { | 324 TEST_F(PrefServiceTest, LocalizedPrefs) { |
| 320 PrefService prefs; | 325 PrefService prefs; |
| 321 const wchar_t kBoolean[] = L"boolean"; | 326 const wchar_t kBoolean[] = L"boolean"; |
| 322 const wchar_t kInteger[] = L"integer"; | 327 const wchar_t kInteger[] = L"integer"; |
| 323 const wchar_t kString[] = L"string"; | 328 const wchar_t kString[] = L"string"; |
| 324 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); | 329 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); |
| 325 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); | 330 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); |
| 326 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); | 331 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); |
| 327 | 332 |
| 328 // The locale default should take preference over the user default. | 333 // The locale default should take preference over the user default. |
| 329 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); | 334 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); |
| 330 EXPECT_EQ(1, prefs.GetInteger(kInteger)); | 335 EXPECT_EQ(1, prefs.GetInteger(kInteger)); |
| 331 EXPECT_EQ(L"hello", prefs.GetString(kString)); | 336 EXPECT_EQ(L"hello", prefs.GetString(kString)); |
| 332 | 337 |
| 333 prefs.SetBoolean(kBoolean, true); | 338 prefs.SetBoolean(kBoolean, true); |
| 334 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); | 339 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); |
| 335 prefs.SetInteger(kInteger, 5); | 340 prefs.SetInteger(kInteger, 5); |
| 336 EXPECT_EQ(5, prefs.GetInteger(kInteger)); | 341 EXPECT_EQ(5, prefs.GetInteger(kInteger)); |
| 337 prefs.SetString(kString, L"foo"); | 342 prefs.SetString(kString, L"foo"); |
| 338 EXPECT_EQ(L"foo", prefs.GetString(kString)); | 343 EXPECT_EQ(L"foo", prefs.GetString(kString)); |
| 339 } | 344 } |
| 345 #endif |
| 340 | 346 |
| 341 TEST_F(PrefServiceTest, NoObserverFire) { | 347 TEST_F(PrefServiceTest, NoObserverFire) { |
| 342 PrefService prefs; | 348 PrefService prefs; |
| 343 | 349 |
| 344 const wchar_t pref_name[] = L"homepage"; | 350 const wchar_t pref_name[] = L"homepage"; |
| 345 prefs.RegisterStringPref(pref_name, L""); | 351 prefs.RegisterStringPref(pref_name, L""); |
| 346 | 352 |
| 347 const std::wstring new_pref_value(L"http://www.google.com/"); | 353 const std::wstring new_pref_value(L"http://www.google.com/"); |
| 348 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 354 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 349 prefs.AddPrefObserver(pref_name, &obs); | 355 prefs.AddPrefObserver(pref_name, &obs); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 // Register the path. This doesn't set a value, so the path still shouldn't | 390 // Register the path. This doesn't set a value, so the path still shouldn't |
| 385 // exist. | 391 // exist. |
| 386 prefs.RegisterStringPref(path, std::wstring()); | 392 prefs.RegisterStringPref(path, std::wstring()); |
| 387 EXPECT_FALSE(prefs.HasPrefPath(path)); | 393 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 388 | 394 |
| 389 // Set a value and make sure we have a path. | 395 // Set a value and make sure we have a path. |
| 390 prefs.persistent_->SetString(path, L"blah"); | 396 prefs.persistent_->SetString(path, L"blah"); |
| 391 EXPECT_TRUE(prefs.HasPrefPath(path)); | 397 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 392 } | 398 } |
| 393 | 399 |
| OLD | NEW |