Chromium Code Reviews| 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/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" | |
| 11 #include "base/scoped_temp_dir.h" | |
| 9 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 13 #include "base/values.h" |
| 11 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 14 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 12 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 15 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 13 #include "chrome/browser/prefs/browser_prefs.h" | 16 #include "chrome/browser/prefs/browser_prefs.h" |
| 14 #include "chrome/browser/prefs/command_line_pref_store.h" | 17 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 15 #include "chrome/browser/prefs/pref_change_registrar.h" | 18 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 16 #include "chrome/browser/prefs/pref_observer_mock.h" | 19 #include "chrome/browser/prefs/pref_observer_mock.h" |
| 17 #include "chrome/browser/prefs/pref_service_mock_builder.h" | 20 #include "chrome/browser/prefs/pref_service_mock_builder.h" |
| 18 #include "chrome/browser/prefs/pref_value_store.h" | 21 #include "chrome/browser/prefs/pref_value_store.h" |
| 22 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 19 #include "chrome/browser/prefs/testing_pref_store.h" | 23 #include "chrome/browser/prefs/testing_pref_store.h" |
| 20 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
| 21 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/json_pref_store.h" | |
| 22 #include "chrome/common/pref_names.h" | 27 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 28 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 24 #include "chrome/test/base/testing_pref_service.h" | 29 #include "chrome/test/base/testing_pref_service.h" |
| 25 #include "chrome/test/base/testing_profile.h" | 30 #include "chrome/test/base/testing_profile.h" |
| 26 #include "content/test/test_browser_thread.h" | 31 #include "content/test/test_browser_thread.h" |
| 27 #include "content/test/web_contents_tester.h" | 32 #include "content/test/web_contents_tester.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 30 #include "ui/base/test/data/resource.h" | 35 #include "ui/base/test/data/resource.h" |
| 31 #include "webkit/glue/webpreferences.h" | 36 #include "webkit/glue/webpreferences.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); | 180 pref = prefs.FindPreference(prefs::kCloudPrintProxyEnabled); |
| 176 ASSERT_TRUE(pref); | 181 ASSERT_TRUE(pref); |
| 177 value = pref->GetValue(); | 182 value = pref->GetValue(); |
| 178 ASSERT_TRUE(value); | 183 ASSERT_TRUE(value); |
| 179 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | 184 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); |
| 180 actual_bool_value = false; | 185 actual_bool_value = false; |
| 181 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); | 186 EXPECT_TRUE(value->GetAsBoolean(&actual_bool_value)); |
| 182 EXPECT_TRUE(actual_bool_value); | 187 EXPECT_TRUE(actual_bool_value); |
| 183 } | 188 } |
| 184 | 189 |
| 190 class PrefServiceUserFilePrefsTest : public testing::Test { | |
| 191 protected: | |
| 192 virtual void SetUp() { | |
| 193 message_loop_proxy_ = base::MessageLoopProxy::current(); | |
| 194 | |
| 195 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 196 | |
| 197 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | |
| 198 data_dir_ = data_dir_.AppendASCII("pref_service"); | |
| 199 ASSERT_TRUE(file_util::PathExists(data_dir_)); | |
| 200 } | |
| 201 | |
| 202 void ClearListValue(PrefService* prefs, const char* key) { | |
| 203 ListPrefUpdate updater(prefs, key); | |
| 204 updater.Get()->Clear(); | |
|
battre
2012/04/11 21:55:51
nit, optional: you can simplify this to updater->C
xiyuan
2012/04/11 22:36:46
Done.
| |
| 205 } | |
| 206 | |
| 207 void ClearDictionaryValue(PrefService* prefs, const char* key) { | |
| 208 DictionaryPrefUpdate updater(prefs, key); | |
| 209 updater.Get()->Clear(); | |
| 210 } | |
| 211 | |
| 212 // The path to temporary directory used to contain the test operations. | |
| 213 ScopedTempDir temp_dir_; | |
| 214 // The path to the directory where the test data is stored. | |
| 215 FilePath data_dir_; | |
| 216 // A message loop that we can use as the file thread message loop. | |
| 217 MessageLoop message_loop_; | |
| 218 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
| 219 }; | |
| 220 | |
| 221 // Verifies that ListValue and DictionaryValue pref with non emtpy default | |
| 222 // preserves its empty value. | |
| 223 TEST_F(PrefServiceUserFilePrefsTest, PreserveEmptyValue) { | |
| 224 FilePath pref_file = temp_dir_.path().AppendASCII("write.json"); | |
| 225 | |
| 226 ASSERT_TRUE(file_util::CopyFile( | |
| 227 data_dir_.AppendASCII("read.need_empty_value.json"), | |
|
battre
2012/04/11 21:55:51
nit: indentation
xiyuan
2012/04/11 22:36:46
Done.
| |
| 228 pref_file)); | |
| 229 | |
| 230 PrefServiceMockBuilder builder; | |
| 231 builder.WithUserFilePrefs(pref_file, message_loop_proxy_.get()); | |
|
battre
2012/04/11 21:55:51
optional: do you want to inline message_loop_proxy
xiyuan
2012/04/11 22:36:46
Done.
| |
| 232 scoped_ptr<PrefService> prefs(builder.Create()); | |
| 233 | |
| 234 // Register testing prefs. | |
| 235 prefs->RegisterListPref("list", | |
| 236 PrefService::UNSYNCABLE_PREF); | |
| 237 prefs->RegisterDictionaryPref("dict", | |
| 238 PrefService::UNSYNCABLE_PREF); | |
| 239 | |
| 240 base::ListValue* non_empty_list = new base::ListValue; | |
| 241 non_empty_list->Append(base::Value::CreateStringValue("test")); | |
| 242 prefs->RegisterListPref("list_needs_empty_value", | |
| 243 non_empty_list, | |
| 244 PrefService::UNSYNCABLE_PREF); | |
| 245 | |
| 246 base::DictionaryValue* non_empty_dict = new base::DictionaryValue; | |
| 247 non_empty_dict->SetString("dummy", "whatever"); | |
| 248 prefs->RegisterDictionaryPref("dict_needs_empty_value", | |
| 249 non_empty_dict, | |
| 250 PrefService::UNSYNCABLE_PREF); | |
| 251 | |
| 252 // Set all testing prefs to empty. | |
| 253 ClearListValue(prefs.get(), "list"); | |
| 254 ClearListValue(prefs.get(), "list_needs_empty_value"); | |
| 255 ClearDictionaryValue(prefs.get(), "dict"); | |
| 256 ClearDictionaryValue(prefs.get(), "dict_needs_empty_value"); | |
| 257 | |
| 258 // Write to file. | |
| 259 prefs->CommitPendingWrite(); | |
| 260 MessageLoop::current()->RunAllPending(); | |
| 261 | |
| 262 // Compare to expected output. | |
| 263 FilePath golden_output_file = | |
| 264 data_dir_.AppendASCII("write.golden.need_empty_value.json"); | |
| 265 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | |
| 266 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); | |
| 267 ASSERT_TRUE(file_util::Delete(pref_file, false)); | |
|
battre
2012/04/11 21:55:51
nit: necessary? should be deleted by ScopedTempDir
xiyuan
2012/04/11 22:36:46
Removed.
| |
| 268 } | |
| 269 | |
| 185 class PrefServiceSetValueTest : public testing::Test { | 270 class PrefServiceSetValueTest : public testing::Test { |
| 186 protected: | 271 protected: |
| 187 static const char kName[]; | 272 static const char kName[]; |
| 188 static const char kValue[]; | 273 static const char kValue[]; |
| 189 | 274 |
| 190 TestingPrefService prefs_; | 275 TestingPrefService prefs_; |
| 191 PrefObserverMock observer_; | 276 PrefObserverMock observer_; |
| 192 }; | 277 }; |
| 193 | 278 |
| 194 const char PrefServiceSetValueTest::kName[] = "name"; | 279 const char PrefServiceSetValueTest::kName[] = "name"; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 319 #if defined(OS_MACOSX) | 404 #if defined(OS_MACOSX) |
| 320 const char kDefaultFont[] = "Times"; | 405 const char kDefaultFont[] = "Times"; |
| 321 #elif defined(OS_CHROMEOS) | 406 #elif defined(OS_CHROMEOS) |
| 322 const char kDefaultFont[] = "Tinos"; | 407 const char kDefaultFont[] = "Tinos"; |
| 323 #else | 408 #else |
| 324 const char kDefaultFont[] = "Times New Roman"; | 409 const char kDefaultFont[] = "Times New Roman"; |
| 325 #endif | 410 #endif |
| 326 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), webkit_prefs.standard_font_family); | 411 EXPECT_EQ(ASCIIToUTF16(kDefaultFont), webkit_prefs.standard_font_family); |
| 327 EXPECT_TRUE(webkit_prefs.javascript_enabled); | 412 EXPECT_TRUE(webkit_prefs.javascript_enabled); |
| 328 } | 413 } |
| OLD | NEW |