| 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 "app/test/data/resource.h" | 5 #include "app/test/data/resource.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/message_loop.h" |
| 7 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "chrome/browser/chrome_thread.h" |
| 8 #include "chrome/common/chrome_paths.h" | 10 #include "chrome/common/chrome_paths.h" |
| 9 #include "chrome/common/json_value_serializer.h" | 11 #include "chrome/common/json_value_serializer.h" |
| 10 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/common/notification_type.h" | 13 #include "chrome/common/notification_type.h" |
| 12 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/common/pref_service.h" | 15 #include "chrome/common/pref_service.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 17 |
| 16 class PrefServiceTest : public testing::Test { | 18 class PrefServiceTest : public testing::Test { |
| 17 protected: | 19 protected: |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 bool observer_fired_; | 76 bool observer_fired_; |
| 75 const PrefService* prefs_; | 77 const PrefService* prefs_; |
| 76 const std::wstring pref_name_; | 78 const std::wstring pref_name_; |
| 77 std::wstring new_pref_value_; | 79 std::wstring new_pref_value_; |
| 78 }; | 80 }; |
| 79 | 81 |
| 80 TEST_F(PrefServiceTest, Basic) { | 82 TEST_F(PrefServiceTest, Basic) { |
| 81 { | 83 { |
| 82 // Test that it fails on nonexistent file. | 84 // Test that it fails on nonexistent file. |
| 83 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); | 85 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 84 PrefService prefs(bogus_input_file, NULL); | 86 PrefService prefs(bogus_input_file); |
| 85 EXPECT_FALSE(prefs.ReloadPersistentPrefs()); | 87 EXPECT_FALSE(prefs.ReloadPersistentPrefs()); |
| 86 } | 88 } |
| 87 | 89 |
| 88 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), | 90 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
| 89 test_dir_.AppendASCII("write.json"))); | 91 test_dir_.AppendASCII("write.json"))); |
| 90 | 92 |
| 91 // Test that the persistent value can be loaded. | 93 // Test that the persistent value can be loaded. |
| 92 FilePath input_file = test_dir_.AppendASCII("write.json"); | 94 FilePath input_file = test_dir_.AppendASCII("write.json"); |
| 93 ASSERT_TRUE(file_util::PathExists(input_file)); | 95 ASSERT_TRUE(file_util::PathExists(input_file)); |
| 94 PrefService prefs(input_file, NULL); | 96 PrefService prefs(input_file); |
| 95 ASSERT_TRUE(prefs.ReloadPersistentPrefs()); | 97 ASSERT_TRUE(prefs.ReloadPersistentPrefs()); |
| 96 | 98 |
| 97 // Register test prefs. | 99 // Register test prefs. |
| 98 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs"; | 100 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs"; |
| 99 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; | 101 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; |
| 100 const wchar_t kLongIntPref[] = L"long_int.pref"; | 102 const wchar_t kLongIntPref[] = L"long_int.pref"; |
| 101 prefs.RegisterStringPref(prefs::kHomePage, L""); | 103 prefs.RegisterStringPref(prefs::kHomePage, L""); |
| 102 prefs.RegisterBooleanPref(kNewWindowsInTabs, false); | 104 prefs.RegisterBooleanPref(kNewWindowsInTabs, false); |
| 103 prefs.RegisterIntegerPref(kMaxTabs, 0); | 105 prefs.RegisterIntegerPref(kMaxTabs, 0); |
| 104 prefs.RegisterStringPref(kLongIntPref, L"2147483648"); | 106 prefs.RegisterStringPref(kLongIntPref, L"2147483648"); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 134 EXPECT_EQ(2147483648LL, prefs.GetInt64(kLongIntPref)); | 136 EXPECT_EQ(2147483648LL, prefs.GetInt64(kLongIntPref)); |
| 135 prefs.SetInt64(kLongIntPref, 214748364842LL); | 137 prefs.SetInt64(kLongIntPref, 214748364842LL); |
| 136 EXPECT_EQ(214748364842LL, prefs.GetInt64(kLongIntPref)); | 138 EXPECT_EQ(214748364842LL, prefs.GetInt64(kLongIntPref)); |
| 137 | 139 |
| 138 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), | 140 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), |
| 139 prefs.GetFilePath(kSomeDirectory).value()); | 141 prefs.GetFilePath(kSomeDirectory).value()); |
| 140 prefs.SetFilePath(kSomeDirectory, some_path); | 142 prefs.SetFilePath(kSomeDirectory, some_path); |
| 141 EXPECT_EQ(some_path.value(), prefs.GetFilePath(kSomeDirectory).value()); | 143 EXPECT_EQ(some_path.value(), prefs.GetFilePath(kSomeDirectory).value()); |
| 142 | 144 |
| 143 // Serialize and compare to expected output. | 145 // Serialize and compare to expected output. |
| 146 // SavePersistentPrefs uses ImportantFileWriter which needs a file thread. |
| 147 MessageLoop message_loop; |
| 148 ChromeThread file_thread(ChromeThread::FILE, &message_loop); |
| 144 FilePath output_file = test_dir_.AppendASCII("write.json"); | 149 FilePath output_file = test_dir_.AppendASCII("write.json"); |
| 145 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); | 150 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
| 146 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 151 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 147 ASSERT_TRUE(prefs.SavePersistentPrefs()); | 152 ASSERT_TRUE(prefs.SavePersistentPrefs()); |
| 153 MessageLoop::current()->RunAllPending(); |
| 148 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 154 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
| 149 ASSERT_TRUE(file_util::Delete(output_file, false)); | 155 ASSERT_TRUE(file_util::Delete(output_file, false)); |
| 150 } | 156 } |
| 151 | 157 |
| 152 TEST_F(PrefServiceTest, Overlay) { | 158 TEST_F(PrefServiceTest, Overlay) { |
| 153 const std::string transient = | 159 const std::string transient = |
| 154 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\"," | 160 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\"," |
| 155 "\"dictionary\":{\"value\":\"transient\"}," | 161 "\"dictionary\":{\"value\":\"transient\"}," |
| 156 "\"list\":[\"transient\"]}"; | 162 "\"list\":[\"transient\"]}"; |
| 157 | 163 |
| 158 std::wstring persistent_string(L"persistent"); | 164 std::wstring persistent_string(L"persistent"); |
| 159 std::wstring transient_string(L"transient"); | 165 std::wstring transient_string(L"transient"); |
| 160 | 166 |
| 161 FilePath persistent_file = data_dir_.AppendASCII("overlay.json"); | 167 FilePath persistent_file = data_dir_.AppendASCII("overlay.json"); |
| 162 PrefService prefs(persistent_file, NULL); | 168 PrefService prefs(persistent_file); |
| 163 EXPECT_TRUE(prefs.ReloadPersistentPrefs()); | 169 EXPECT_TRUE(prefs.ReloadPersistentPrefs()); |
| 164 | 170 |
| 165 Value* transient_value; | 171 Value* transient_value; |
| 166 { | 172 { |
| 167 JSONStringValueSerializer serializer(transient); | 173 JSONStringValueSerializer serializer(transient); |
| 168 transient_value = serializer.Deserialize(NULL); | 174 transient_value = serializer.Deserialize(NULL); |
| 169 ASSERT_TRUE(transient_value); | 175 ASSERT_TRUE(transient_value); |
| 170 } | 176 } |
| 171 prefs.transient()->Set(transient_string, transient_value); | 177 prefs.transient()->Set(transient_string, transient_value); |
| 172 | 178 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 std::wstring result_string; | 285 std::wstring result_string; |
| 280 ASSERT_TRUE(member_value->GetAsString(&result_string)); | 286 ASSERT_TRUE(member_value->GetAsString(&result_string)); |
| 281 ASSERT_EQ(transient_string, result_string); | 287 ASSERT_EQ(transient_string, result_string); |
| 282 } | 288 } |
| 283 } | 289 } |
| 284 | 290 |
| 285 TEST_F(PrefServiceTest, Observers) { | 291 TEST_F(PrefServiceTest, Observers) { |
| 286 FilePath input_file = data_dir_.AppendASCII("read.json"); | 292 FilePath input_file = data_dir_.AppendASCII("read.json"); |
| 287 EXPECT_TRUE(file_util::PathExists(input_file)); | 293 EXPECT_TRUE(file_util::PathExists(input_file)); |
| 288 | 294 |
| 289 PrefService prefs(input_file, NULL); | 295 PrefService prefs(input_file); |
| 290 | 296 |
| 291 EXPECT_TRUE(prefs.ReloadPersistentPrefs()); | 297 EXPECT_TRUE(prefs.ReloadPersistentPrefs()); |
| 292 | 298 |
| 293 const wchar_t pref_name[] = L"homepage"; | 299 const wchar_t pref_name[] = L"homepage"; |
| 294 prefs.RegisterStringPref(pref_name, L""); | 300 prefs.RegisterStringPref(pref_name, L""); |
| 295 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name)); | 301 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name)); |
| 296 | 302 |
| 297 const std::wstring new_pref_value(L"http://www.google.com/"); | 303 const std::wstring new_pref_value(L"http://www.google.com/"); |
| 298 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 304 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 299 prefs.AddPrefObserver(pref_name, &obs); | 305 prefs.AddPrefObserver(pref_name, &obs); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 322 EXPECT_FALSE(obs.observer_fired()); | 328 EXPECT_FALSE(obs.observer_fired()); |
| 323 EXPECT_TRUE(obs2.observer_fired()); | 329 EXPECT_TRUE(obs2.observer_fired()); |
| 324 | 330 |
| 325 // Ok, clean up. | 331 // Ok, clean up. |
| 326 prefs.RemovePrefObserver(pref_name, &obs2); | 332 prefs.RemovePrefObserver(pref_name, &obs2); |
| 327 } | 333 } |
| 328 | 334 |
| 329 // TODO(port): port this test to POSIX. | 335 // TODO(port): port this test to POSIX. |
| 330 #if defined(OS_WIN) | 336 #if defined(OS_WIN) |
| 331 TEST_F(PrefServiceTest, LocalizedPrefs) { | 337 TEST_F(PrefServiceTest, LocalizedPrefs) { |
| 332 PrefService prefs(FilePath(), NULL); | 338 PrefService prefs((FilePath())); |
| 333 const wchar_t kBoolean[] = L"boolean"; | 339 const wchar_t kBoolean[] = L"boolean"; |
| 334 const wchar_t kInteger[] = L"integer"; | 340 const wchar_t kInteger[] = L"integer"; |
| 335 const wchar_t kString[] = L"string"; | 341 const wchar_t kString[] = L"string"; |
| 336 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); | 342 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); |
| 337 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); | 343 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); |
| 338 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); | 344 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); |
| 339 | 345 |
| 340 // The locale default should take preference over the user default. | 346 // The locale default should take preference over the user default. |
| 341 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); | 347 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); |
| 342 EXPECT_EQ(1, prefs.GetInteger(kInteger)); | 348 EXPECT_EQ(1, prefs.GetInteger(kInteger)); |
| 343 EXPECT_EQ(L"hello", prefs.GetString(kString)); | 349 EXPECT_EQ(L"hello", prefs.GetString(kString)); |
| 344 | 350 |
| 345 prefs.SetBoolean(kBoolean, true); | 351 prefs.SetBoolean(kBoolean, true); |
| 346 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); | 352 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); |
| 347 prefs.SetInteger(kInteger, 5); | 353 prefs.SetInteger(kInteger, 5); |
| 348 EXPECT_EQ(5, prefs.GetInteger(kInteger)); | 354 EXPECT_EQ(5, prefs.GetInteger(kInteger)); |
| 349 prefs.SetString(kString, L"foo"); | 355 prefs.SetString(kString, L"foo"); |
| 350 EXPECT_EQ(L"foo", prefs.GetString(kString)); | 356 EXPECT_EQ(L"foo", prefs.GetString(kString)); |
| 351 } | 357 } |
| 352 #endif | 358 #endif |
| 353 | 359 |
| 354 TEST_F(PrefServiceTest, NoObserverFire) { | 360 TEST_F(PrefServiceTest, NoObserverFire) { |
| 355 PrefService prefs(FilePath(), NULL); | 361 PrefService prefs((FilePath())); |
| 356 | 362 |
| 357 const wchar_t pref_name[] = L"homepage"; | 363 const wchar_t pref_name[] = L"homepage"; |
| 358 prefs.RegisterStringPref(pref_name, L""); | 364 prefs.RegisterStringPref(pref_name, L""); |
| 359 | 365 |
| 360 const std::wstring new_pref_value(L"http://www.google.com/"); | 366 const std::wstring new_pref_value(L"http://www.google.com/"); |
| 361 TestPrefObserver obs(&prefs, pref_name, new_pref_value); | 367 TestPrefObserver obs(&prefs, pref_name, new_pref_value); |
| 362 prefs.AddPrefObserver(pref_name, &obs); | 368 prefs.AddPrefObserver(pref_name, &obs); |
| 363 // This should fire the checks in TestPrefObserver::Observe. | 369 // This should fire the checks in TestPrefObserver::Observe. |
| 364 prefs.SetString(pref_name, new_pref_value); | 370 prefs.SetString(pref_name, new_pref_value); |
| 365 | 371 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 380 // Clearing the pref again should not cause the pref to fire. | 386 // Clearing the pref again should not cause the pref to fire. |
| 381 obs.Reset(L""); | 387 obs.Reset(L""); |
| 382 prefs.ClearPref(pref_name); | 388 prefs.ClearPref(pref_name); |
| 383 EXPECT_FALSE(obs.observer_fired()); | 389 EXPECT_FALSE(obs.observer_fired()); |
| 384 | 390 |
| 385 // Ok, clean up. | 391 // Ok, clean up. |
| 386 prefs.RemovePrefObserver(pref_name, &obs); | 392 prefs.RemovePrefObserver(pref_name, &obs); |
| 387 } | 393 } |
| 388 | 394 |
| 389 TEST_F(PrefServiceTest, HasPrefPath) { | 395 TEST_F(PrefServiceTest, HasPrefPath) { |
| 390 PrefService prefs(FilePath(), NULL); | 396 PrefService prefs((FilePath())); |
| 391 | 397 |
| 392 const wchar_t path[] = L"fake.path"; | 398 const wchar_t path[] = L"fake.path"; |
| 393 | 399 |
| 394 // Shouldn't initially have a path. | 400 // Shouldn't initially have a path. |
| 395 EXPECT_FALSE(prefs.HasPrefPath(path)); | 401 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 396 | 402 |
| 397 // Register the path. This doesn't set a value, so the path still shouldn't | 403 // Register the path. This doesn't set a value, so the path still shouldn't |
| 398 // exist. | 404 // exist. |
| 399 prefs.RegisterStringPref(path, std::wstring()); | 405 prefs.RegisterStringPref(path, std::wstring()); |
| 400 EXPECT_FALSE(prefs.HasPrefPath(path)); | 406 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 401 | 407 |
| 402 // Set a value and make sure we have a path. | 408 // Set a value and make sure we have a path. |
| 403 prefs.SetString(path, L"blah"); | 409 prefs.SetString(path, L"blah"); |
| 404 EXPECT_TRUE(prefs.HasPrefPath(path)); | 410 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 405 } | 411 } |
| OLD | NEW |