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

Unified Diff: base/prefs/leveldb_pref_store_unittest.cc

Issue 25428002: LevelDBPrefStore Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: latest Created 6 years, 10 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/leveldb_pref_store.cc ('k') | base/prefs/migration_pref_store.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/prefs/leveldb_pref_store_unittest.cc
diff --git a/base/prefs/leveldb_pref_store_unittest.cc b/base/prefs/leveldb_pref_store_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1698ffd8fe6b231bd01e22a8bba0d53a20296c5d
--- /dev/null
+++ b/base/prefs/leveldb_pref_store_unittest.cc
@@ -0,0 +1,243 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/prefs/leveldb_pref_store.h"
+
+#include "base/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/path_service.h"
+#include "base/prefs/pref_filter.h"
+#include "base/run_loop.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/threading/sequenced_worker_pool.h"
+#include "base/threading/thread.h"
+#include "base/values.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+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 LevelDBPrefStoreTest : public testing::Test {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+
+ ASSERT_TRUE(PathService::Get(base::DIR_TEST_DATA, &data_dir_));
+ data_dir_ = data_dir_.AppendASCII("prefs");
+ ASSERT_TRUE(PathExists(data_dir_));
+ }
+
+ void Open() {
+ pref_store_ = new LevelDBPrefStore(
+ temp_dir_.path(), message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+ DCHECK_EQ(LevelDBPrefStore::PREF_READ_ERROR_NONE, pref_store_->ReadPrefs());
+ }
+
+ void CloseAndReopen() {
+ pref_store_ = NULL;
+ Open();
+ }
+
+ // The path to temporary directory used to contain the test operations.
+ base::ScopedTempDir temp_dir_;
+ // The path to the directory where the test data is stored in the source tree.
+ base::FilePath data_dir_;
+ // A message loop that we can use as the file thread message loop.
+ MessageLoop message_loop_;
+
+ scoped_refptr<LevelDBPrefStore> pref_store_;
+};
+
+TEST_F(LevelDBPrefStoreTest, PutAndGet) {
+ Open();
+ const std::string key = "some.key";
+ base::Value* value = new FundamentalValue(5);
+ pref_store_->SetValue(key, value);
+ RunLoop().RunUntilIdle();
+ const base::Value* actual_value;
+ EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
+ EXPECT_TRUE(value->Equals(actual_value));
+}
+
+TEST_F(LevelDBPrefStoreTest, PutAndGetPersistent) {
+ Open();
+ const std::string key = "some.key";
+ base::Value* value = new FundamentalValue(5);
+ pref_store_->SetValue(key, value);
+ RunLoop().RunUntilIdle();
+
+ CloseAndReopen();
+ const base::Value* actual_value = NULL;
+ value = new FundamentalValue(5);
+ EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
+ EXPECT_TRUE(base::Value::Equals(value, actual_value));
+}
+
+TEST_F(LevelDBPrefStoreTest, BasicObserver) {
+ scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
+ temp_dir_.path(), message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+ MockPrefStoreObserver mock_observer;
+ pref_store->AddObserver(&mock_observer);
+ EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
+ EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
+
+ const std::string key = "some.key";
+ base::Value* value = new FundamentalValue(5);
+ EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
+ pref_store->SetValue(key, value);
+
+ pref_store->RemoveObserver(&mock_observer);
+}
+
+TEST_F(LevelDBPrefStoreTest, SetValueSilently) {
+ Open();
+
+ MockPrefStoreObserver mock_observer;
+ pref_store_->AddObserver(&mock_observer);
+ const std::string key = "some.key";
+ base::Value* value = new FundamentalValue(30);
+ EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(0);
+ pref_store_->SetValueSilently(key, value);
+ RunLoop().RunUntilIdle();
+ pref_store_->RemoveObserver(&mock_observer);
+
+ CloseAndReopen();
+ value = new FundamentalValue(30);
+ const base::Value* actual_value = NULL;
+ EXPECT_TRUE(pref_store_->GetValue(key, &actual_value));
+ EXPECT_TRUE(base::Value::Equals(value, actual_value));
+}
+
+TEST_F(LevelDBPrefStoreTest, GetMutableValue) {
+ Open();
+
+ const std::string key = "some.key";
+ base::DictionaryValue* orig_value = new DictionaryValue;
+ orig_value->SetInteger("key2", 25);
+ pref_store_->SetValue(key, orig_value);
+ base::Value* actual_value;
+
+ EXPECT_TRUE(pref_store_->GetMutableValue(key, &actual_value));
+ EXPECT_TRUE(orig_value->Equals(actual_value));
+ base::DictionaryValue* dict_value =
+ static_cast<base::DictionaryValue*>(actual_value);
+ dict_value->SetInteger("key2", 30);
+ pref_store_->ReportValueChanged(key);
+ RunLoop().RunUntilIdle();
+
+ // Ensure the new value is stored in memory.
+ const base::Value* retrieved_value;
+ EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
+ const base::Value* inner_value;
+ EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
+ ->Get("key2", &inner_value));
+ const base::FundamentalValue* inner_fundamental_value =
+ static_cast<const base::FundamentalValue*>(inner_value);
+ int inner_integer;
+ EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
+ EXPECT_EQ(30, inner_integer);
+
+ // Ensure the new value is persisted to disk.
+ CloseAndReopen();
+ EXPECT_TRUE(pref_store_->GetValue(key, &retrieved_value));
+ EXPECT_TRUE(static_cast<const base::DictionaryValue*>(retrieved_value)
+ ->Get("key2", &inner_value));
+ inner_fundamental_value =
+ static_cast<const base::FundamentalValue*>(inner_value);
+ EXPECT_TRUE(inner_fundamental_value->GetAsInteger(&inner_integer));
+ EXPECT_EQ(30, inner_integer);
+}
+
+TEST_F(LevelDBPrefStoreTest, Remove) {
+ Open();
+ const std::string key = "some.key";
+ base::Value* orig_value = new FundamentalValue(5);
+ pref_store_->SetValue(key, orig_value);
+ MockPrefStoreObserver mock_observer;
+ pref_store_->AddObserver(&mock_observer);
+ EXPECT_CALL(mock_observer, OnPrefValueChanged(key)).Times(1);
+ pref_store_->RemoveValue(key);
+ pref_store_->RemoveObserver(&mock_observer);
+ RunLoop().RunUntilIdle();
+
+ CloseAndReopen();
+ const base::Value* retrieved_value;
+ EXPECT_FALSE(pref_store_->GetValue(key, &retrieved_value));
+}
+
+TEST_F(LevelDBPrefStoreTest, OpenAsync) {
+ pref_store_ = new LevelDBPrefStore(
+ temp_dir_.path(), message_loop_.message_loop_proxy().get(),
+ scoped_ptr<PrefFilter>());
+ MockReadErrorDelegate* delegate = new MockReadErrorDelegate;
+ pref_store_->ReadPrefsAsync(delegate);
+ EXPECT_CALL(*delegate,
+ OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(0);
+ MockPrefStoreObserver mock_observer;
+ pref_store_->AddObserver(&mock_observer);
+ EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
+ RunLoop().RunUntilIdle();
+ pref_store_->RemoveObserver(&mock_observer);
+}
+
+// Test fallback behavior for an invalid file.
+//TEST_F(LevelDBPrefStoreTest, InvalidFile) {
+// base::FilePath invalid_file_original = data_dir_.AppendASCII("invalid.json");
+// base::FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
+// ASSERT_TRUE(base::CopyFile(invalid_file_original, invalid_file));
+// scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
+// invalid_file, message_loop_.message_loop_proxy().get());
+// EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
+// pref_store->ReadPrefs());
+// EXPECT_FALSE(pref_store->ReadOnly());
+//
+// // The file should have been moved aside.
+// EXPECT_FALSE(PathExists(invalid_file));
+// base::FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
+// EXPECT_TRUE(PathExists(moved_aside));
+// EXPECT_TRUE(TextContentsEqual(invalid_file_original, moved_aside));
+//}
+//
+//// Tests asynchronous reading of the file when there is no file.
+//TEST_F(LevelDBPrefStoreTest, AsyncNonExistingFile) {
+// base::FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
+// ASSERT_FALSE(PathExists(bogus_input_file));
+// scoped_refptr<LevelDBPrefStore> pref_store = new LevelDBPrefStore(
+// bogus_input_file, message_loop_.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);
+// RunLoop().RunUntilIdle();
+// pref_store->RemoveObserver(&mock_observer);
+//
+// EXPECT_FALSE(pref_store->ReadOnly());
+//}
+
+} // namespace base
« no previous file with comments | « base/prefs/leveldb_pref_store.cc ('k') | base/prefs/migration_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698