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

Unified Diff: base/prefs/json_pref_store_unittest.cc

Issue 347793002: Expand the JsonPrefStore's interface to allow for an alternate filename to be specified. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Dont even attempt to delete the alternate file in unexpected situations. Created 6 years, 6 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/prefs/json_pref_store.cc ('k') | chrome/browser/prefs/profile_pref_store_manager.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/prefs/json_pref_store_unittest.cc
diff --git a/base/prefs/json_pref_store_unittest.cc b/base/prefs/json_pref_store_unittest.cc
index 4c9c847708f5f2d3f9845759bfffb7598b68e832..2e59d097e33866294784afab50bcd24c241553f5 100644
--- a/base/prefs/json_pref_store_unittest.cc
+++ b/base/prefs/json_pref_store_unittest.cc
@@ -44,7 +44,7 @@ class InterceptingPrefFilter : public PrefFilter {
bool has_intercepted_prefs() const { return intercepted_prefs_ != NULL; }
- // Finalize an intercepted read, handing |intercept_prefs_| back to its
+ // Finalize an intercepted read, handing |intercepted_prefs_| back to its
// JsonPrefStore.
void ReleasePrefs();
@@ -122,6 +122,23 @@ TEST_F(JsonPrefStoreTest, NonExistentFile) {
EXPECT_FALSE(pref_store->ReadOnly());
}
+// Test fallback behavior for a nonexistent file and alternate file.
+TEST_F(JsonPrefStoreTest, NonExistentFileAndAlternateFile) {
+ base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
+ base::FilePath bogus_alternate_input_file =
+ data_dir_.AppendASCII("read_alternate.txt");
+ ASSERT_FALSE(PathExists(bogus_input_file));
+ ASSERT_FALSE(PathExists(bogus_alternate_input_file));
+ scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
+ bogus_input_file,
+ bogus_alternate_input_file,
+ message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
+ pref_store->ReadPrefs());
+ EXPECT_FALSE(pref_store->ReadOnly());
+}
+
// Test fallback behavior for an invalid file.
TEST_F(JsonPrefStoreTest, InvalidFile) {
base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
@@ -469,4 +486,189 @@ TEST_F(JsonPrefStoreTest, ReadAsyncWithInterceptor) {
pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
}
+TEST_F(JsonPrefStoreTest, AlternateFile) {
+ ASSERT_TRUE(
+ base::CopyFile(data_dir_.AppendASCII("read.json"),
+ temp_dir_.path().AppendASCII("alternate.json")));
+
+ // Test that the alternate file is moved to the main file and read as-is from
+ // there.
+ base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
+ base::FilePath alternate_input_file =
+ temp_dir_.path().AppendASCII("alternate.json");
+ ASSERT_FALSE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+ scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
+ input_file,
+ alternate_input_file,
+ message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+
+ ASSERT_FALSE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_FALSE(PathExists(alternate_input_file));
+
+ EXPECT_FALSE(pref_store->ReadOnly());
+ EXPECT_TRUE(pref_store->IsInitializationComplete());
+
+ // The JSON file looks like this:
+ // {
+ // "homepage": "http://www.cnn.com",
+ // "some_directory": "/usr/local/",
+ // "tabs": {
+ // "new_windows_in_tabs": true,
+ // "max_tabs": 20
+ // }
+ // }
+
+ RunBasicJsonPrefStoreTest(
+ pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
+}
+
+TEST_F(JsonPrefStoreTest, AlternateFileIgnoredWhenMainFileExists) {
+ ASSERT_TRUE(
+ base::CopyFile(data_dir_.AppendASCII("read.json"),
+ temp_dir_.path().AppendASCII("write.json")));
+ ASSERT_TRUE(
+ base::CopyFile(data_dir_.AppendASCII("invalid.json"),
+ temp_dir_.path().AppendASCII("alternate.json")));
+
+ // Test that the alternate file is ignored and that the read occurs from the
+ // existing main file. There is no attempt at even deleting the alternate
+ // file as this scenario should never happen in normal user-data-dirs.
+ base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
+ base::FilePath alternate_input_file =
+ temp_dir_.path().AppendASCII("alternate.json");
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+ scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
+ input_file,
+ alternate_input_file,
+ message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+
+ EXPECT_FALSE(pref_store->ReadOnly());
+ EXPECT_TRUE(pref_store->IsInitializationComplete());
+
+ // The JSON file looks like this:
+ // {
+ // "homepage": "http://www.cnn.com",
+ // "some_directory": "/usr/local/",
+ // "tabs": {
+ // "new_windows_in_tabs": true,
+ // "max_tabs": 20
+ // }
+ // }
+
+ RunBasicJsonPrefStoreTest(
+ pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
+}
+
+TEST_F(JsonPrefStoreTest, AlternateFileDNE) {
+ ASSERT_TRUE(
+ base::CopyFile(data_dir_.AppendASCII("read.json"),
+ temp_dir_.path().AppendASCII("write.json")));
+
+ // Test that the basic read works fine when an alternate file is specified but
+ // does not exist.
+ base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
+ base::FilePath alternate_input_file =
+ temp_dir_.path().AppendASCII("alternate.json");
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_FALSE(PathExists(alternate_input_file));
+ scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
+ input_file,
+ alternate_input_file,
+ message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_FALSE(PathExists(alternate_input_file));
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_FALSE(PathExists(alternate_input_file));
+
+ EXPECT_FALSE(pref_store->ReadOnly());
+ EXPECT_TRUE(pref_store->IsInitializationComplete());
+
+ // The JSON file looks like this:
+ // {
+ // "homepage": "http://www.cnn.com",
+ // "some_directory": "/usr/local/",
+ // "tabs": {
+ // "new_windows_in_tabs": true,
+ // "max_tabs": 20
+ // }
+ // }
+
+ RunBasicJsonPrefStoreTest(
+ pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
+}
+
+TEST_F(JsonPrefStoreTest, BasicAsyncWithAlternateFile) {
+ ASSERT_TRUE(
+ base::CopyFile(data_dir_.AppendASCII("read.json"),
+ temp_dir_.path().AppendASCII("alternate.json")));
+
+ // Test that the alternate file is moved to the main file and read as-is from
+ // there even when the read is made asynchronously.
+ base::FilePath input_file = temp_dir_.path().AppendASCII("write.json");
+ base::FilePath alternate_input_file =
+ temp_dir_.path().AppendASCII("alternate.json");
+ ASSERT_FALSE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+ scoped_refptr<JsonPrefStore> pref_store = new JsonPrefStore(
+ input_file,
+ alternate_input_file,
+ message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+
+ ASSERT_FALSE(PathExists(input_file));
+ ASSERT_TRUE(PathExists(alternate_input_file));
+
+ {
+ MockPrefStoreObserver mock_observer;
+ pref_store->AddObserver(&mock_observer);
+
+ MockReadErrorDelegate* mock_error_delegate = new MockReadErrorDelegate;
+ pref_store->ReadPrefsAsync(mock_error_delegate);
+
+ EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
+ EXPECT_CALL(*mock_error_delegate,
+ OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
+ RunLoop().RunUntilIdle();
+ pref_store->RemoveObserver(&mock_observer);
+
+ EXPECT_FALSE(pref_store->ReadOnly());
+ EXPECT_TRUE(pref_store->IsInitializationComplete());
+ }
+
+ ASSERT_TRUE(PathExists(input_file));
+ ASSERT_FALSE(PathExists(alternate_input_file));
+
+ // The JSON file looks like this:
+ // {
+ // "homepage": "http://www.cnn.com",
+ // "some_directory": "/usr/local/",
+ // "tabs": {
+ // "new_windows_in_tabs": true,
+ // "max_tabs": 20
+ // }
+ // }
+
+ RunBasicJsonPrefStoreTest(
+ pref_store.get(), input_file, data_dir_.AppendASCII("write.golden.json"));
+}
+
} // namespace base
« no previous file with comments | « base/prefs/json_pref_store.cc ('k') | chrome/browser/prefs/profile_pref_store_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698