Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: chrome/common/pref_service_unittest.cc

Issue 83001: ImportantFileWriter (Closed)
Patch Set: share more code Created 11 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/common/pref_service.cc ('k') | chrome/test/reliability/page_load_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 new_pref_value_ = new_pref_value; 70 new_pref_value_ = new_pref_value;
71 } 71 }
72 72
73 private: 73 private:
74 bool observer_fired_; 74 bool observer_fired_;
75 const PrefService* prefs_; 75 const PrefService* prefs_;
76 const std::wstring pref_name_; 76 const std::wstring pref_name_;
77 std::wstring new_pref_value_; 77 std::wstring new_pref_value_;
78 }; 78 };
79 79
80 // This test is disabled. See issue 8339.
81 TEST_F(PrefServiceTest, Basic) { 80 TEST_F(PrefServiceTest, Basic) {
82 PrefService prefs; 81 {
82 // Test that it fails on nonexistent file.
83 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
84 PrefService prefs(bogus_input_file, NULL);
85 EXPECT_FALSE(prefs.ReloadPersistentPrefs());
86 }
83 87
84 // Test that it fails on nonexistent file. 88 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
85 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); 89 test_dir_.AppendASCII("write.json")));
86 EXPECT_FALSE(prefs.LoadPersistentPrefs(bogus_input_file));
87 90
88 // Test that the persistent value can be loaded. 91 // Test that the persistent value can be loaded.
89 FilePath input_file = data_dir_.AppendASCII("read.json"); 92 FilePath input_file = test_dir_.AppendASCII("write.json");
90 ASSERT_TRUE(file_util::PathExists(input_file)); 93 ASSERT_TRUE(file_util::PathExists(input_file));
91 ASSERT_TRUE(prefs.LoadPersistentPrefs(input_file)); 94 PrefService prefs(input_file, NULL);
95 ASSERT_TRUE(prefs.ReloadPersistentPrefs());
92 96
93 // Register test prefs. 97 // Register test prefs.
94 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs"; 98 const wchar_t kNewWindowsInTabs[] = L"tabs.new_windows_in_tabs";
95 const wchar_t kMaxTabs[] = L"tabs.max_tabs"; 99 const wchar_t kMaxTabs[] = L"tabs.max_tabs";
96 const wchar_t kLongIntPref[] = L"long_int.pref"; 100 const wchar_t kLongIntPref[] = L"long_int.pref";
97 prefs.RegisterStringPref(prefs::kHomePage, L""); 101 prefs.RegisterStringPref(prefs::kHomePage, L"");
98 prefs.RegisterBooleanPref(kNewWindowsInTabs, false); 102 prefs.RegisterBooleanPref(kNewWindowsInTabs, false);
99 prefs.RegisterIntegerPref(kMaxTabs, 0); 103 prefs.RegisterIntegerPref(kMaxTabs, 0);
100 prefs.RegisterStringPref(kLongIntPref, L"2147483648"); 104 prefs.RegisterStringPref(kLongIntPref, L"2147483648");
101 105
(...skipping 29 matching lines...) Expand all
131 prefs.SetInt64(kLongIntPref, 214748364842LL); 135 prefs.SetInt64(kLongIntPref, 214748364842LL);
132 EXPECT_EQ(214748364842LL, prefs.GetInt64(kLongIntPref)); 136 EXPECT_EQ(214748364842LL, prefs.GetInt64(kLongIntPref));
133 137
134 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")), 138 EXPECT_EQ(FilePath::StringType(FILE_PATH_LITERAL("/usr/local/")),
135 prefs.GetFilePath(kSomeDirectory).value()); 139 prefs.GetFilePath(kSomeDirectory).value());
136 prefs.SetFilePath(kSomeDirectory, some_path); 140 prefs.SetFilePath(kSomeDirectory, some_path);
137 EXPECT_EQ(some_path.value(), prefs.GetFilePath(kSomeDirectory).value()); 141 EXPECT_EQ(some_path.value(), prefs.GetFilePath(kSomeDirectory).value());
138 142
139 // Serialize and compare to expected output. 143 // Serialize and compare to expected output.
140 FilePath output_file = test_dir_.AppendASCII("write.json"); 144 FilePath output_file = test_dir_.AppendASCII("write.json");
141 prefs.pref_filename_ = output_file;
142 ASSERT_TRUE(prefs.SavePersistentPrefs(NULL));
143 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); 145 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json");
144 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 146 ASSERT_TRUE(file_util::PathExists(golden_output_file));
145 ASSERT_TRUE(file_util::ContentsEqual(golden_output_file, output_file)); 147 ASSERT_TRUE(prefs.SavePersistentPrefs());
148 EXPECT_TRUE(file_util::ContentsEqual(golden_output_file, output_file));
149 ASSERT_TRUE(file_util::Delete(output_file, false));
146 } 150 }
147 151
148 TEST_F(PrefServiceTest, Overlay) { 152 TEST_F(PrefServiceTest, Overlay) {
149 const std::string transient = 153 const std::string transient =
150 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\"," 154 "{\"bool\":true, \"int\":2, \"real\":2.0, \"string\":\"transient\","
151 "\"dictionary\":{\"value\":\"transient\"}," 155 "\"dictionary\":{\"value\":\"transient\"},"
152 "\"list\":[\"transient\"]}"; 156 "\"list\":[\"transient\"]}";
153 157
154 std::wstring persistent_string(L"persistent"); 158 std::wstring persistent_string(L"persistent");
155 std::wstring transient_string(L"transient"); 159 std::wstring transient_string(L"transient");
156 160
157 PrefService prefs;
158
159 FilePath persistent_file = data_dir_.AppendASCII("overlay.json"); 161 FilePath persistent_file = data_dir_.AppendASCII("overlay.json");
160 EXPECT_TRUE(prefs.LoadPersistentPrefs(persistent_file)); 162 PrefService prefs(persistent_file, NULL);
163 EXPECT_TRUE(prefs.ReloadPersistentPrefs());
161 164
162 Value* transient_value; 165 Value* transient_value;
163 { 166 {
164 JSONStringValueSerializer serializer(transient); 167 JSONStringValueSerializer serializer(transient);
165 transient_value = serializer.Deserialize(NULL); 168 transient_value = serializer.Deserialize(NULL);
166 ASSERT_TRUE(transient_value); 169 ASSERT_TRUE(transient_value);
167 } 170 }
168 prefs.transient()->Set(transient_string, transient_value); 171 prefs.transient()->Set(transient_string, transient_value);
169 172
170 Value* both_transient_value; 173 Value* both_transient_value;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 Value* member_value; 276 Value* member_value;
274 ASSERT_TRUE(result_value->Get(0, &member_value)); 277 ASSERT_TRUE(result_value->Get(0, &member_value));
275 ASSERT_TRUE(member_value); 278 ASSERT_TRUE(member_value);
276 std::wstring result_string; 279 std::wstring result_string;
277 ASSERT_TRUE(member_value->GetAsString(&result_string)); 280 ASSERT_TRUE(member_value->GetAsString(&result_string));
278 ASSERT_EQ(transient_string, result_string); 281 ASSERT_EQ(transient_string, result_string);
279 } 282 }
280 } 283 }
281 284
282 TEST_F(PrefServiceTest, Observers) { 285 TEST_F(PrefServiceTest, Observers) {
283 PrefService prefs;
284
285 FilePath input_file = data_dir_.AppendASCII("read.json"); 286 FilePath input_file = data_dir_.AppendASCII("read.json");
286 EXPECT_TRUE(file_util::PathExists(input_file)); 287 EXPECT_TRUE(file_util::PathExists(input_file));
287 EXPECT_TRUE(prefs.LoadPersistentPrefs(input_file)); 288
289 PrefService prefs(input_file, NULL);
290
291 EXPECT_TRUE(prefs.ReloadPersistentPrefs());
288 292
289 const wchar_t pref_name[] = L"homepage"; 293 const wchar_t pref_name[] = L"homepage";
290 prefs.RegisterStringPref(pref_name, L""); 294 prefs.RegisterStringPref(pref_name, L"");
291 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name)); 295 EXPECT_EQ(std::wstring(L"http://www.cnn.com"), prefs.GetString(pref_name));
292 296
293 const std::wstring new_pref_value(L"http://www.google.com/"); 297 const std::wstring new_pref_value(L"http://www.google.com/");
294 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 298 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
295 prefs.AddPrefObserver(pref_name, &obs); 299 prefs.AddPrefObserver(pref_name, &obs);
296 // This should fire the checks in TestPrefObserver::Observe. 300 // This should fire the checks in TestPrefObserver::Observe.
297 prefs.SetString(pref_name, new_pref_value); 301 prefs.SetString(pref_name, new_pref_value);
(...skipping 20 matching lines...) Expand all
318 EXPECT_FALSE(obs.observer_fired()); 322 EXPECT_FALSE(obs.observer_fired());
319 EXPECT_TRUE(obs2.observer_fired()); 323 EXPECT_TRUE(obs2.observer_fired());
320 324
321 // Ok, clean up. 325 // Ok, clean up.
322 prefs.RemovePrefObserver(pref_name, &obs2); 326 prefs.RemovePrefObserver(pref_name, &obs2);
323 } 327 }
324 328
325 // TODO(port): port this test to POSIX. 329 // TODO(port): port this test to POSIX.
326 #if defined(OS_WIN) 330 #if defined(OS_WIN)
327 TEST_F(PrefServiceTest, LocalizedPrefs) { 331 TEST_F(PrefServiceTest, LocalizedPrefs) {
328 PrefService prefs; 332 PrefService prefs(FilePath(), NULL);
329 const wchar_t kBoolean[] = L"boolean"; 333 const wchar_t kBoolean[] = L"boolean";
330 const wchar_t kInteger[] = L"integer"; 334 const wchar_t kInteger[] = L"integer";
331 const wchar_t kString[] = L"string"; 335 const wchar_t kString[] = L"string";
332 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL); 336 prefs.RegisterLocalizedBooleanPref(kBoolean, IDS_LOCALE_BOOL);
333 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT); 337 prefs.RegisterLocalizedIntegerPref(kInteger, IDS_LOCALE_INT);
334 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING); 338 prefs.RegisterLocalizedStringPref(kString, IDS_LOCALE_STRING);
335 339
336 // The locale default should take preference over the user default. 340 // The locale default should take preference over the user default.
337 EXPECT_FALSE(prefs.GetBoolean(kBoolean)); 341 EXPECT_FALSE(prefs.GetBoolean(kBoolean));
338 EXPECT_EQ(1, prefs.GetInteger(kInteger)); 342 EXPECT_EQ(1, prefs.GetInteger(kInteger));
339 EXPECT_EQ(L"hello", prefs.GetString(kString)); 343 EXPECT_EQ(L"hello", prefs.GetString(kString));
340 344
341 prefs.SetBoolean(kBoolean, true); 345 prefs.SetBoolean(kBoolean, true);
342 EXPECT_TRUE(prefs.GetBoolean(kBoolean)); 346 EXPECT_TRUE(prefs.GetBoolean(kBoolean));
343 prefs.SetInteger(kInteger, 5); 347 prefs.SetInteger(kInteger, 5);
344 EXPECT_EQ(5, prefs.GetInteger(kInteger)); 348 EXPECT_EQ(5, prefs.GetInteger(kInteger));
345 prefs.SetString(kString, L"foo"); 349 prefs.SetString(kString, L"foo");
346 EXPECT_EQ(L"foo", prefs.GetString(kString)); 350 EXPECT_EQ(L"foo", prefs.GetString(kString));
347 } 351 }
348 #endif 352 #endif
349 353
350 TEST_F(PrefServiceTest, NoObserverFire) { 354 TEST_F(PrefServiceTest, NoObserverFire) {
351 PrefService prefs; 355 PrefService prefs(FilePath(), NULL);
352 356
353 const wchar_t pref_name[] = L"homepage"; 357 const wchar_t pref_name[] = L"homepage";
354 prefs.RegisterStringPref(pref_name, L""); 358 prefs.RegisterStringPref(pref_name, L"");
355 359
356 const std::wstring new_pref_value(L"http://www.google.com/"); 360 const std::wstring new_pref_value(L"http://www.google.com/");
357 TestPrefObserver obs(&prefs, pref_name, new_pref_value); 361 TestPrefObserver obs(&prefs, pref_name, new_pref_value);
358 prefs.AddPrefObserver(pref_name, &obs); 362 prefs.AddPrefObserver(pref_name, &obs);
359 // This should fire the checks in TestPrefObserver::Observe. 363 // This should fire the checks in TestPrefObserver::Observe.
360 prefs.SetString(pref_name, new_pref_value); 364 prefs.SetString(pref_name, new_pref_value);
361 365
(...skipping 14 matching lines...) Expand all
376 // Clearing the pref again should not cause the pref to fire. 380 // Clearing the pref again should not cause the pref to fire.
377 obs.Reset(L""); 381 obs.Reset(L"");
378 prefs.ClearPref(pref_name); 382 prefs.ClearPref(pref_name);
379 EXPECT_FALSE(obs.observer_fired()); 383 EXPECT_FALSE(obs.observer_fired());
380 384
381 // Ok, clean up. 385 // Ok, clean up.
382 prefs.RemovePrefObserver(pref_name, &obs); 386 prefs.RemovePrefObserver(pref_name, &obs);
383 } 387 }
384 388
385 TEST_F(PrefServiceTest, HasPrefPath) { 389 TEST_F(PrefServiceTest, HasPrefPath) {
386 PrefService prefs; 390 PrefService prefs(FilePath(), NULL);
387 391
388 const wchar_t path[] = L"fake.path"; 392 const wchar_t path[] = L"fake.path";
389 393
390 // Shouldn't initially have a path. 394 // Shouldn't initially have a path.
391 EXPECT_FALSE(prefs.HasPrefPath(path)); 395 EXPECT_FALSE(prefs.HasPrefPath(path));
392 396
393 // Register the path. This doesn't set a value, so the path still shouldn't 397 // Register the path. This doesn't set a value, so the path still shouldn't
394 // exist. 398 // exist.
395 prefs.RegisterStringPref(path, std::wstring()); 399 prefs.RegisterStringPref(path, std::wstring());
396 EXPECT_FALSE(prefs.HasPrefPath(path)); 400 EXPECT_FALSE(prefs.HasPrefPath(path));
397 401
398 // Set a value and make sure we have a path. 402 // Set a value and make sure we have a path.
399 prefs.persistent_->SetString(path, L"blah"); 403 prefs.SetString(path, L"blah");
400 EXPECT_TRUE(prefs.HasPrefPath(path)); 404 EXPECT_TRUE(prefs.HasPrefPath(path));
401 } 405 }
OLDNEW
« no previous file with comments | « chrome/common/pref_service.cc ('k') | chrome/test/reliability/page_load_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698