Index: chrome/common/json_pref_store_unittest.cc |
diff --git a/chrome/common/json_pref_store_unittest.cc b/chrome/common/json_pref_store_unittest.cc |
index e7f34f520331b8fb37e80bd874a1874da1ae3da0..0a3a6638970d16a31555c9d14b9bb02c09067414 100644 |
--- a/chrome/common/json_pref_store_unittest.cc |
+++ b/chrome/common/json_pref_store_unittest.cc |
@@ -5,6 +5,7 @@ |
#include "base/file_util.h" |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
+#include "base/memory/scoped_temp_dir.h" |
#include "base/message_loop.h" |
#include "base/message_loop_proxy.h" |
#include "base/path_service.h" |
@@ -13,12 +14,27 @@ |
#include "base/threading/thread.h" |
#include "base/utf_string_conversions.h" |
#include "base/values.h" |
-#include "base/memory/scoped_temp_dir.h" |
-#include "chrome/common/json_pref_store.h" |
#include "chrome/common/chrome_paths.h" |
+#include "chrome/common/json_pref_store.h" |
#include "chrome/common/pref_names.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
+namespace { |
+ |
+class MockPrefStoreObserver : public PrefStore::Observer { |
+ public: |
+ MOCK_METHOD1(OnPrefValueChanged, void (const std::string&)); |
+ MOCK_METHOD1(OnInitializationCompleted, void (bool)); |
+}; |
+ |
+class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
+ public: |
+ MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError)); |
+}; |
+ |
+} // namespace |
+ |
class JsonPrefStoreTest : public testing::Test { |
protected: |
virtual void SetUp() { |
@@ -70,28 +86,11 @@ TEST_F(JsonPrefStoreTest, InvalidFile) { |
moved_aside)); |
} |
-TEST_F(JsonPrefStoreTest, Basic) { |
- ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
- temp_dir_.path().AppendASCII("write.json"))); |
- |
- // Test that the persistent value can be loaded. |
- FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
- ASSERT_TRUE(file_util::PathExists(input_file)); |
- scoped_refptr<JsonPrefStore> pref_store = |
- new JsonPrefStore(input_file, message_loop_proxy_.get()); |
- ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
- ASSERT_FALSE(pref_store->ReadOnly()); |
- |
- // The JSON file looks like this: |
- // { |
- // "homepage": "http://www.cnn.com", |
- // "some_directory": "/usr/local/", |
- // "tabs": { |
- // "new_windows_in_tabs": true, |
- // "max_tabs": 20 |
- // } |
- // } |
- |
+// This function is used to avoid code duplication while testing synchronios and |
Mattias Nissler (ping if slow)
2011/05/07 00:24:14
s/synchronios/synchronous/
altimofeev
2011/05/10 11:10:22
Done.
|
+// asynchronous version of the JsonPrefStore loading. |
+void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store, |
+ const FilePath& output_file, |
+ const FilePath& golden_output_file) { |
const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; |
const char kMaxTabs[] = "tabs.max_tabs"; |
const char kLongIntPref[] = "long_int.pref"; |
@@ -152,11 +151,96 @@ TEST_F(JsonPrefStoreTest, Basic) { |
EXPECT_EQ(214748364842LL, value); |
// Serialize and compare to expected output. |
- FilePath output_file = input_file; |
- FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
ASSERT_TRUE(pref_store->WritePrefs()); |
MessageLoop::current()->RunAllPending(); |
EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
ASSERT_TRUE(file_util::Delete(output_file, false)); |
} |
+ |
+TEST_F(JsonPrefStoreTest, Basic) { |
+ ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
+ temp_dir_.path().AppendASCII("write.json"))); |
+ |
+ // Test that the persistent value can be loaded. |
+ FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
+ ASSERT_TRUE(file_util::PathExists(input_file)); |
+ scoped_refptr<JsonPrefStore> pref_store = |
+ new JsonPrefStore(input_file, message_loop_proxy_.get()); |
+ ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs()); |
+ ASSERT_FALSE(pref_store->ReadOnly()); |
+ |
+ // 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, |
+ input_file, |
+ data_dir_.AppendASCII("write.golden.json")); |
+} |
+ |
+TEST_F(JsonPrefStoreTest, BasicAsync) { |
+ ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), |
+ temp_dir_.path().AppendASCII("write.json"))); |
+ |
+ // Test that the persistent value can be loaded. |
+ FilePath input_file = temp_dir_.path().AppendASCII("write.json"); |
+ ASSERT_TRUE(file_util::PathExists(input_file)); |
+ scoped_refptr<JsonPrefStore> pref_store = |
+ new JsonPrefStore(input_file, message_loop_proxy_.get()); |
+ |
+ 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); |
+ message_loop_.RunAllPending(); |
+ pref_store->RemoveObserver(&mock_observer); |
+ |
+ ASSERT_FALSE(pref_store->ReadOnly()); |
+ |
+ // 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, |
+ input_file, |
+ data_dir_.AppendASCII("write.golden.json")); |
+} |
+ |
+// Tests asynchronous reading of the file when there is no file. |
+TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
+ FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
+ ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
+ scoped_refptr<JsonPrefStore> pref_store = |
+ new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
+ 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_NO_FILE)).Times(1); |
+ message_loop_.RunAllPending(); |
+ pref_store->RemoveObserver(&mock_observer); |
+ |
+ EXPECT_FALSE(pref_store->ReadOnly()); |
+} |