| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 35 #include "ui/base/test/data/resource.h" | 35 #include "ui/base/test/data/resource.h" |
| 36 #include "webkit/glue/webpreferences.h" | 36 #include "webkit/glue/webpreferences.h" |
| 37 | 37 |
| 38 using content::BrowserThread; | 38 using content::BrowserThread; |
| 39 using content::WebContentsTester; | 39 using content::WebContentsTester; |
| 40 using testing::_; | 40 using testing::_; |
| 41 using testing::Mock; | 41 using testing::Mock; |
| 42 | 42 |
| 43 TEST(PrefServiceTest, NoObserverFire) { | 43 TEST(PrefServiceTest, NoObserverFire) { |
| 44 TestingPrefService prefs; | 44 TestingPrefServiceSimple prefs; |
| 45 | 45 |
| 46 const char pref_name[] = "homepage"; | 46 const char pref_name[] = "homepage"; |
| 47 prefs.RegisterStringPref(pref_name, std::string()); | 47 prefs.RegisterStringPref(pref_name, std::string()); |
| 48 | 48 |
| 49 const char new_pref_value[] = "http://www.google.com/"; | 49 const char new_pref_value[] = "http://www.google.com/"; |
| 50 MockPrefChangeCallback obs(&prefs); | 50 MockPrefChangeCallback obs(&prefs); |
| 51 PrefChangeRegistrar registrar; | 51 PrefChangeRegistrar registrar; |
| 52 registrar.Init(&prefs); | 52 registrar.Init(&prefs); |
| 53 registrar.Add(pref_name, obs.GetCallback()); | 53 registrar.Add(pref_name, obs.GetCallback()); |
| 54 | 54 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 70 prefs.ClearPref(pref_name); | 70 prefs.ClearPref(pref_name); |
| 71 Mock::VerifyAndClearExpectations(&obs); | 71 Mock::VerifyAndClearExpectations(&obs); |
| 72 | 72 |
| 73 // Clearing the pref again should not cause the pref to fire. | 73 // Clearing the pref again should not cause the pref to fire. |
| 74 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); | 74 EXPECT_CALL(obs, OnPreferenceChanged(_)).Times(0); |
| 75 prefs.ClearPref(pref_name); | 75 prefs.ClearPref(pref_name); |
| 76 Mock::VerifyAndClearExpectations(&obs); | 76 Mock::VerifyAndClearExpectations(&obs); |
| 77 } | 77 } |
| 78 | 78 |
| 79 TEST(PrefServiceTest, HasPrefPath) { | 79 TEST(PrefServiceTest, HasPrefPath) { |
| 80 TestingPrefService prefs; | 80 TestingPrefServiceSimple prefs; |
| 81 | 81 |
| 82 const char path[] = "fake.path"; | 82 const char path[] = "fake.path"; |
| 83 | 83 |
| 84 // Shouldn't initially have a path. | 84 // Shouldn't initially have a path. |
| 85 EXPECT_FALSE(prefs.HasPrefPath(path)); | 85 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 86 | 86 |
| 87 // Register the path. This doesn't set a value, so the path still shouldn't | 87 // Register the path. This doesn't set a value, so the path still shouldn't |
| 88 // exist. | 88 // exist. |
| 89 prefs.RegisterStringPref(path, std::string()); | 89 prefs.RegisterStringPref(path, std::string()); |
| 90 EXPECT_FALSE(prefs.HasPrefPath(path)); | 90 EXPECT_FALSE(prefs.HasPrefPath(path)); |
| 91 | 91 |
| 92 // Set a value and make sure we have a path. | 92 // Set a value and make sure we have a path. |
| 93 prefs.SetString(path, "blah"); | 93 prefs.SetString(path, "blah"); |
| 94 EXPECT_TRUE(prefs.HasPrefPath(path)); | 94 EXPECT_TRUE(prefs.HasPrefPath(path)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 TEST(PrefServiceTest, Observers) { | 97 TEST(PrefServiceTest, Observers) { |
| 98 const char pref_name[] = "homepage"; | 98 const char pref_name[] = "homepage"; |
| 99 | 99 |
| 100 TestingPrefService prefs; | 100 TestingPrefServiceSimple prefs; |
| 101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); | 101 prefs.SetUserPref(pref_name, Value::CreateStringValue("http://www.cnn.com")); |
| 102 prefs.RegisterStringPref(pref_name, std::string()); | 102 prefs.RegisterStringPref(pref_name, std::string()); |
| 103 | 103 |
| 104 const char new_pref_value[] = "http://www.google.com/"; | 104 const char new_pref_value[] = "http://www.google.com/"; |
| 105 const StringValue expected_new_pref_value(new_pref_value); | 105 const StringValue expected_new_pref_value(new_pref_value); |
| 106 MockPrefChangeCallback obs(&prefs); | 106 MockPrefChangeCallback obs(&prefs); |
| 107 PrefChangeRegistrar registrar; | 107 PrefChangeRegistrar registrar; |
| 108 registrar.Init(&prefs); | 108 registrar.Init(&prefs); |
| 109 registrar.Add(pref_name, obs.GetCallback()); | 109 registrar.Add(pref_name, obs.GetCallback()); |
| 110 | 110 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 // This should only fire the observer in obs2. | 145 // This should only fire the observer in obs2. |
| 146 prefs.SetString(pref_name, new_pref_value); | 146 prefs.SetString(pref_name, new_pref_value); |
| 147 Mock::VerifyAndClearExpectations(&obs); | 147 Mock::VerifyAndClearExpectations(&obs); |
| 148 Mock::VerifyAndClearExpectations(&obs2); | 148 Mock::VerifyAndClearExpectations(&obs2); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Make sure that if a preference changes type, so the wrong type is stored in | 151 // Make sure that if a preference changes type, so the wrong type is stored in |
| 152 // the user pref file, it uses the correct fallback value instead. | 152 // the user pref file, it uses the correct fallback value instead. |
| 153 TEST(PrefServiceTest, GetValueChangedType) { | 153 TEST(PrefServiceTest, GetValueChangedType) { |
| 154 const int kTestValue = 10; | 154 const int kTestValue = 10; |
| 155 TestingPrefService prefs; | 155 TestingPrefServiceSimple prefs; |
| 156 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); | 156 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kTestValue); |
| 157 | 157 |
| 158 // Check falling back to a recommended value. | 158 // Check falling back to a recommended value. |
| 159 prefs.SetUserPref(prefs::kStabilityLaunchCount, | 159 prefs.SetUserPref(prefs::kStabilityLaunchCount, |
| 160 Value::CreateStringValue("not an integer")); | 160 Value::CreateStringValue("not an integer")); |
| 161 const PrefService::Preference* pref = | 161 const PrefService::Preference* pref = |
| 162 prefs.FindPreference(prefs::kStabilityLaunchCount); | 162 prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 163 ASSERT_TRUE(pref); | 163 ASSERT_TRUE(pref); |
| 164 const Value* value = pref->GetValue(); | 164 const Value* value = pref->GetValue(); |
| 165 ASSERT_TRUE(value); | 165 ASSERT_TRUE(value); |
| 166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | 166 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); |
| 167 int actual_int_value = -1; | 167 int actual_int_value = -1; |
| 168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); | 168 EXPECT_TRUE(value->GetAsInteger(&actual_int_value)); |
| 169 EXPECT_EQ(kTestValue, actual_int_value); | 169 EXPECT_EQ(kTestValue, actual_int_value); |
| 170 } | 170 } |
| 171 | 171 |
| 172 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { | 172 TEST(PrefServiceTest, UpdateCommandLinePrefStore) { |
| 173 TestingPrefService prefs; | 173 TestingPrefServiceSimple prefs; |
| 174 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); | 174 prefs.RegisterBooleanPref(prefs::kCloudPrintProxyEnabled, false); |
| 175 | 175 |
| 176 // Check to make sure the value is as expected. | 176 // Check to make sure the value is as expected. |
| 177 const PrefService::Preference* pref = | 177 const PrefService::Preference* pref = |
| 178 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | 178 prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 179 ASSERT_TRUE(pref); | 179 ASSERT_TRUE(pref); |
| 180 const Value* value = pref->GetValue(); | 180 const Value* value = pref->GetValue(); |
| 181 ASSERT_TRUE(value); | 181 ASSERT_TRUE(value); |
| 182 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | 182 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 183 bool actual_bool_value = true; | 183 bool actual_bool_value = true; |
| 184 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 184 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
| 185 EXPECT_FALSE(actual_bool_value); | 185 EXPECT_FALSE(actual_bool_value); |
| 186 | 186 |
| 187 // Change the command line. | 187 // Change the command line. |
| 188 CommandLine cmd_line(CommandLine::NO_PROGRAM); | 188 CommandLine cmd_line(CommandLine::NO_PROGRAM); |
| 189 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy); | 189 cmd_line.AppendSwitch(switches::kEnableCloudPrintProxy); |
| 190 | 190 |
| 191 // Call UpdateCommandLinePrefStore and check to see if the value has changed. | 191 // Call UpdateCommandLinePrefStore and check to see if the value has changed. |
| 192 prefs.UpdateCommandLinePrefStore(&cmd_line); | 192 prefs.UpdateCommandLinePrefStore(new CommandLinePrefStore(&cmd_line)); |
| 193 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | 193 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 194 ASSERT_TRUE(pref); | 194 ASSERT_TRUE(pref); |
| 195 value = pref->GetValue(); | 195 value = pref->GetValue(); |
| 196 ASSERT_TRUE(value); | 196 ASSERT_TRUE(value); |
| 197 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | 197 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 198 actual_bool_value = false; | 198 actual_bool_value = false; |
| 199 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 199 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
| 200 EXPECT_TRUE(actual_bool_value); | 200 EXPECT_TRUE(actual_bool_value); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { | 203 TEST(PrefServiceTest, GetValueAndGetRecommendedValue) { |
| 204 const int kDefaultValue = 5; | 204 const int kDefaultValue = 5; |
| 205 const int kUserValue = 10; | 205 const int kUserValue = 10; |
| 206 const int kRecommendedValue = 15; | 206 const int kRecommendedValue = 15; |
| 207 TestingPrefService prefs; | 207 TestingPrefServiceSimple prefs; |
| 208 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kDefaultValue); | 208 prefs.RegisterIntegerPref(prefs::kStabilityLaunchCount, kDefaultValue); |
| 209 | 209 |
| 210 // Create pref with a default value only. | 210 // Create pref with a default value only. |
| 211 const PrefService::Preference* pref = | 211 const PrefService::Preference* pref = |
| 212 prefs.FindPreference(prefs::kStabilityLaunchCount); | 212 prefs.FindPreference(prefs::kStabilityLaunchCount); |
| 213 ASSERT_TRUE(pref); | 213 ASSERT_TRUE(pref); |
| 214 | 214 |
| 215 // Check that GetValue() returns the default value. | 215 // Check that GetValue() returns the default value. |
| 216 const Value* value = pref->GetValue(); | 216 const Value* value = pref->GetValue(); |
| 217 ASSERT_TRUE(value); | 217 ASSERT_TRUE(value); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 // preserves its empty value. | 312 // preserves its empty value. |
| 313 TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) { | 313 TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) { |
| 314 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | 314 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); |
| 315 | 315 |
| 316 ASSERT_TRUE(file_util::CopyFile( | 316 ASSERT_TRUE(file_util::CopyFile( |
| 317 data_dir_.AppendASCII("read.need_empty_value.json"), | 317 data_dir_.AppendASCII("read.need_empty_value.json"), |
| 318 pref_file)); | 318 pref_file)); |
| 319 | 319 |
| 320 PrefServiceMockBuilder builder; | 320 PrefServiceMockBuilder builder; |
| 321 builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy()); | 321 builder.WithUserFilePrefs(pref_file, message_loop_.message_loop_proxy()); |
| 322 scoped_ptr<PrefService> prefs(builder.Create()); | 322 scoped_ptr<PrefServiceSyncable> prefs(builder.CreateSyncable()); |
| 323 | 323 |
| 324 // Register testing prefs. | 324 // Register testing prefs. |
| 325 prefs->RegisterListPref("list", | 325 prefs->RegisterListPref("list", |
| 326 PrefService::UNSYNCABLE_PREF); | 326 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 327 prefs->RegisterDictionaryPref("dict", | 327 prefs->RegisterDictionaryPref("dict", |
| 328 PrefService::UNSYNCABLE_PREF); | 328 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 329 | 329 |
| 330 base::ListValue* non_empty_list = new base::ListValue; | 330 base::ListValue* non_empty_list = new base::ListValue; |
| 331 non_empty_list->Append(base::Value::CreateStringValue("test")); | 331 non_empty_list->Append(base::Value::CreateStringValue("test")); |
| 332 prefs->RegisterListPref("list_needs_empty_value", | 332 prefs->RegisterListPref("list_needs_empty_value", |
| 333 non_empty_list, | 333 non_empty_list, |
| 334 PrefService::UNSYNCABLE_PREF); | 334 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 335 | 335 |
| 336 base::DictionaryValue* non_empty_dict = new base::DictionaryValue; | 336 base::DictionaryValue* non_empty_dict = new base::DictionaryValue; |
| 337 non_empty_dict->SetString("dummy", "whatever"); | 337 non_empty_dict->SetString("dummy", "whatever"); |
| 338 prefs->RegisterDictionaryPref("dict_needs_empty_value", | 338 prefs->RegisterDictionaryPref("dict_needs_empty_value", |
| 339 non_empty_dict, | 339 non_empty_dict, |
| 340 PrefService::UNSYNCABLE_PREF); | 340 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 341 | 341 |
| 342 // Set all testing prefs to empty. | 342 // Set all testing prefs to empty. |
| 343 ClearListValue(prefs.get(), "list"); | 343 ClearListValue(prefs.get(), "list"); |
| 344 ClearListValue(prefs.get(), "list_needs_empty_value"); | 344 ClearListValue(prefs.get(), "list_needs_empty_value"); |
| 345 ClearDictionaryValue(prefs.get(), "dict"); | 345 ClearDictionaryValue(prefs.get(), "dict"); |
| 346 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value"); | 346 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value"); |
| 347 | 347 |
| 348 // Write to file. | 348 // Write to file. |
| 349 prefs->CommitPendingWrite(); | 349 prefs->CommitPendingWrite(); |
| 350 message_loop_.RunUntilIdle(); | 350 message_loop_.RunUntilIdle(); |
| 351 | 351 |
| 352 // Compare to expected output. | 352 // Compare to expected output. |
| 353 FilePath golden_output_file = | 353 FilePath golden_output_file = |
| 354 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | 354 data_dir_.AppendASCII("write.golden.need_empty_value.json"); |
| 355 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 355 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
| 356 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | 356 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); |
| 357 } | 357 } |
| 358 | 358 |
| 359 class PrefServiceSetValueTest : public testing::Test { | 359 class PrefServiceSetValueTest : public testing::Test { |
| 360 protected: | 360 protected: |
| 361 static const char kName[]; | 361 static const char kName[]; |
| 362 static const char kValue[]; | 362 static const char kValue[]; |
| 363 | 363 |
| 364 PrefServiceSetValueTest() : observer_(&prefs_) {} | 364 PrefServiceSetValueTest() : observer_(&prefs_) {} |
| 365 | 365 |
| 366 TestingPrefService prefs_; | 366 TestingPrefServiceSimple prefs_; |
| 367 MockPrefChangeCallback observer_; | 367 MockPrefChangeCallback observer_; |
| 368 }; | 368 }; |
| 369 | 369 |
| 370 const char PrefServiceSetValueTest::kName[] = "name"; | 370 const char PrefServiceSetValueTest::kName[] = "name"; |
| 371 const char PrefServiceSetValueTest::kValue[] = "value"; | 371 const char PrefServiceSetValueTest::kValue[] = "value"; |
| 372 | 372 |
| 373 TEST_F(PrefServiceSetValueTest, SetStringValue) { | 373 TEST_F(PrefServiceSetValueTest, SetStringValue) { |
| 374 const char default_string[] = "default"; | 374 const char default_string[] = "default"; |
| 375 const StringValue default_value(default_string); | 375 const StringValue default_value(default_string); |
| 376 prefs_.RegisterStringPref(kName, default_string); | 376 prefs_.RegisterStringPref(kName, default_string); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) { | 451 PrefServiceWebKitPrefs() : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 452 } | 452 } |
| 453 | 453 |
| 454 virtual void SetUp() { | 454 virtual void SetUp() { |
| 455 ChromeRenderViewHostTestHarness::SetUp(); | 455 ChromeRenderViewHostTestHarness::SetUp(); |
| 456 | 456 |
| 457 // Supply our own profile so we use the correct profile data. The test | 457 // Supply our own profile so we use the correct profile data. The test |
| 458 // harness is not supposed to overwrite a profile if it's already created. | 458 // harness is not supposed to overwrite a profile if it's already created. |
| 459 | 459 |
| 460 // Set some (WebKit) user preferences. | 460 // Set some (WebKit) user preferences. |
| 461 TestingPrefService* pref_services = profile()->GetTestingPrefService(); | 461 TestingPrefServiceSyncable* pref_services = |
| 462 profile()->GetTestingPrefService(); |
| 462 #if defined(TOOLKIT_GTK) | 463 #if defined(TOOLKIT_GTK) |
| 463 pref_services->SetUserPref(prefs::kUsesSystemTheme, | 464 pref_services->SetUserPref(prefs::kUsesSystemTheme, |
| 464 Value::CreateBooleanValue(false)); | 465 Value::CreateBooleanValue(false)); |
| 465 #endif | 466 #endif |
| 466 pref_services->SetUserPref(prefs::kDefaultCharset, | 467 pref_services->SetUserPref(prefs::kDefaultCharset, |
| 467 Value::CreateStringValue("utf8")); | 468 Value::CreateStringValue("utf8")); |
| 468 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, | 469 pref_services->SetUserPref(prefs::kWebKitDefaultFontSize, |
| 469 Value::CreateIntegerValue(20)); | 470 Value::CreateIntegerValue(20)); |
| 470 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, | 471 pref_services->SetUserPref(prefs::kWebKitTextAreasAreResizable, |
| 471 Value::CreateBooleanValue(false)); | 472 Value::CreateBooleanValue(false)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 496 const char kDefaultFont[] = "Times"; | 497 const char kDefaultFont[] = "Times"; |
| 497 #elif defined(OS_CHROMEOS) | 498 #elif defined(OS_CHROMEOS) |
| 498 const char kDefaultFont[] = "Tinos"; | 499 const char kDefaultFont[] = "Tinos"; |
| 499 #else | 500 #else |
| 500 const char kDefaultFont[] = "Times New Roman"; | 501 const char kDefaultFont[] = "Times New Roman"; |
| 501 #endif | 502 #endif |
| 502 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), | 503 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), |
| 503 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); | 504 webkit_prefs.standard_font_family_map[prefs::kWebKitCommonScript]); |
| 504 EXPECT_TRUE(webkit_prefs.javascript_enabled); | 505 EXPECT_TRUE(webkit_prefs.javascript_enabled); |
| 505 } | 506 } |
| OLD | NEW |