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

Side by Side Diff: base/prefs/json_pref_store_unittest.cc

Issue 19052005: Move PathIsWritable, DirectoryExists, ContentsEqual, and TextContentsEqual to the base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « base/nix/mime_util_xdg.cc ('k') | base/test/test_launcher.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) 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 "base/prefs/json_pref_store.h" 5 #include "base/prefs/json_pref_store.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 scoped_refptr<JsonPrefStore> pref_store = 74 scoped_refptr<JsonPrefStore> pref_store =
75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get()); 75 new JsonPrefStore(invalid_file, message_loop_.message_loop_proxy().get());
76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE, 76 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
77 pref_store->ReadPrefs()); 77 pref_store->ReadPrefs());
78 EXPECT_FALSE(pref_store->ReadOnly()); 78 EXPECT_FALSE(pref_store->ReadOnly());
79 79
80 // The file should have been moved aside. 80 // The file should have been moved aside.
81 EXPECT_FALSE(PathExists(invalid_file)); 81 EXPECT_FALSE(PathExists(invalid_file));
82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); 82 base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
83 EXPECT_TRUE(PathExists(moved_aside)); 83 EXPECT_TRUE(PathExists(moved_aside));
84 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, 84 EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside));
85 moved_aside));
86 } 85 }
87 86
88 // This function is used to avoid code duplication while testing synchronous and 87 // This function is used to avoid code duplication while testing synchronous and
89 // asynchronous version of the JsonPrefStore loading. 88 // asynchronous version of the JsonPrefStore loading.
90 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store, 89 void RunBasicJsonPrefStoreTest(JsonPrefStore* pref_store,
91 const base::FilePath& output_file, 90 const base::FilePath& output_file,
92 const base::FilePath& golden_output_file) { 91 const base::FilePath& golden_output_file) {
93 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; 92 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
94 const char kMaxTabs[] = "tabs.max_tabs"; 93 const char kMaxTabs[] = "tabs.max_tabs";
95 const char kLongIntPref[] = "long_int.pref"; 94 const char kLongIntPref[] = "long_int.pref";
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual)); 139 EXPECT_TRUE(pref_store->GetValue(kLongIntPref, &actual));
141 EXPECT_TRUE(actual->GetAsString(&string_value)); 140 EXPECT_TRUE(actual->GetAsString(&string_value));
142 int64 value; 141 int64 value;
143 base::StringToInt64(string_value, &value); 142 base::StringToInt64(string_value, &value);
144 EXPECT_EQ(214748364842LL, value); 143 EXPECT_EQ(214748364842LL, value);
145 144
146 // Serialize and compare to expected output. 145 // Serialize and compare to expected output.
147 ASSERT_TRUE(PathExists(golden_output_file)); 146 ASSERT_TRUE(PathExists(golden_output_file));
148 pref_store->CommitPendingWrite(); 147 pref_store->CommitPendingWrite();
149 RunLoop().RunUntilIdle(); 148 RunLoop().RunUntilIdle();
150 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 149 EXPECT_TRUE(TextContentsEqual(golden_output_file, output_file));
151 ASSERT_TRUE(base::Delete(output_file, false)); 150 ASSERT_TRUE(base::Delete(output_file, false));
152 } 151 }
153 152
154 TEST_F(JsonPrefStoreTest, Basic) { 153 TEST_F(JsonPrefStoreTest, Basic) {
155 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"), 154 ASSERT_TRUE(base::CopyFile(data_dir_.AppendASCII("read.json"),
156 temp_dir_.path().AppendASCII("write.json"))); 155 temp_dir_.path().AppendASCII("write.json")));
157 156
158 // Test that the persistent value can be loaded. 157 // Test that the persistent value can be loaded.
159 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json"); 158 base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
160 ASSERT_TRUE(PathExists(input_file)); 159 ASSERT_TRUE(PathExists(input_file));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue); 274 pref_store->SetValue("dict_needs_empty_value", new base::DictionaryValue);
276 275
277 // Write to file. 276 // Write to file.
278 pref_store->CommitPendingWrite(); 277 pref_store->CommitPendingWrite();
279 RunLoop().RunUntilIdle(); 278 RunLoop().RunUntilIdle();
280 279
281 // Compare to expected output. 280 // Compare to expected output.
282 base::FilePath golden_output_file = 281 base::FilePath golden_output_file =
283 data_dir_.AppendASCII("write.golden.need_empty_value.json"); 282 data_dir_.AppendASCII("write.golden.need_empty_value.json");
284 ASSERT_TRUE(PathExists(golden_output_file)); 283 ASSERT_TRUE(PathExists(golden_output_file));
285 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, pref_file)); 284 EXPECT_TRUE(TextContentsEqual(golden_output_file, pref_file));
286 } 285 }
287 286
288 } // namespace base 287 } // namespace base
OLDNEW
« no previous file with comments | « base/nix/mime_util_xdg.cc ('k') | base/test/test_launcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698