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

Unified Diff: chrome/common/json_pref_store_unittest.cc

Issue 11027070: Moved JsonPrefStore to use SequencedWorkerPool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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
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 5b5a5f3328bcb11a73eb43f333a9f563d4bb10b7..5c03d542d5c0ce8848d1d650d590baa9860da20d 100644
--- a/chrome/common/json_pref_store_unittest.cc
+++ b/chrome/common/json_pref_store_unittest.cc
@@ -5,12 +5,11 @@
#include "base/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
-#include "base/message_loop.h"
-#include "base/message_loop_proxy.h"
#include "base/path_service.h"
#include "base/scoped_temp_dir.h"
#include "base/string_number_conversions.h"
#include "base/string_util.h"
+#include "base/threading/sequenced_worker_pool.h"
#include "base/threading/thread.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
@@ -37,8 +36,8 @@ class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
class JsonPrefStoreTest : public testing::Test {
protected:
- virtual void SetUp() {
- message_loop_proxy_ = base::MessageLoopProxy::current();
+ virtual void SetUp() OVERRIDE {
+ blocking_pool_ = new base::SequencedWorkerPool(3, "TestBlocking");
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
@@ -47,13 +46,18 @@ class JsonPrefStoreTest : public testing::Test {
ASSERT_TRUE(file_util::PathExists(data_dir_));
}
+ virtual void TearDown() OVERRIDE {
+ blocking_pool_->Shutdown();
+ }
+
// The path to temporary directory used to contain the test operations.
ScopedTempDir temp_dir_;
// The path to the directory where the test data is stored.
FilePath data_dir_;
- // A message loop that we can use as the file thread message loop.
+ // A message loop that we need for timers.
MessageLoop message_loop_;
- scoped_refptr<base::MessageLoopProxy> message_loop_proxy_;
+ // A blocking pool that we can use for file operations.
+ scoped_refptr<base::SequencedWorkerPool> blocking_pool_;
};
// Test fallback behavior for a nonexistent file.
@@ -61,7 +65,8 @@ TEST_F(JsonPrefStoreTest, NonExistentFile) {
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());
+ JsonPrefStore::CreateWithBlockingPool(bogus_input_file,
+ blocking_pool_.get());
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
@@ -73,7 +78,7 @@ TEST_F(JsonPrefStoreTest, InvalidFile) {
FilePath invalid_file = temp_dir_.path().AppendASCII("invalid.json");
ASSERT_TRUE(file_util::CopyFile(invalid_file_original, invalid_file));
scoped_refptr<JsonPrefStore> pref_store =
- new JsonPrefStore(invalid_file, message_loop_proxy_.get());
+ JsonPrefStore::CreateWithBlockingPool(invalid_file, blocking_pool_.get());
EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_JSON_PARSE,
pref_store->ReadPrefs());
EXPECT_FALSE(pref_store->ReadOnly());
@@ -88,7 +93,8 @@ TEST_F(JsonPrefStoreTest, InvalidFile) {
// This function is used to avoid code duplication while testing synchronous and
// asynchronous version of the JsonPrefStore loading.
-void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store,
+void RunBasicJsonPrefStoreTest(base::SequencedWorkerPool* blocking_pool,
+ JsonPrefStore *pref_store,
const FilePath& output_file,
const FilePath& golden_output_file) {
const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
@@ -153,7 +159,7 @@ void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store,
// Serialize and compare to expected output.
ASSERT_TRUE(file_util::PathExists(golden_output_file));
pref_store->CommitPendingWrite();
- MessageLoop::current()->RunAllPending();
+ blocking_pool->FlushForTesting();
EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
ASSERT_TRUE(file_util::Delete(output_file, false));
}
@@ -166,7 +172,7 @@ TEST_F(JsonPrefStoreTest, Basic) {
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());
+ JsonPrefStore::CreateWithBlockingPool(input_file, blocking_pool_.get());
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
ASSERT_FALSE(pref_store->ReadOnly());
@@ -180,7 +186,8 @@ TEST_F(JsonPrefStoreTest, Basic) {
// }
// }
- RunBasicJsonPrefStoreTest(pref_store,
+ RunBasicJsonPrefStoreTest(blocking_pool_.get(),
+ pref_store,
input_file,
data_dir_.AppendASCII("write.golden.json"));
}
@@ -193,21 +200,24 @@ TEST_F(JsonPrefStoreTest, BasicAsync) {
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());
+ JsonPrefStore::CreateWithBlockingPool(input_file, blocking_pool_.get());
- MockPrefStoreObserver mock_observer;
- pref_store->AddObserver(&mock_observer);
+ {
+ MockPrefStoreObserver mock_observer;
+ pref_store->AddObserver(&mock_observer);
- MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
- pref_store->ReadPrefsAsync(mock_error_delegate);
+ 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);
+ EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
+ EXPECT_CALL(*mock_error_delegate,
+ OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
+ blocking_pool_->FlushForTesting();
+ message_loop_.RunAllPending();
+ pref_store->RemoveObserver(&mock_observer);
- ASSERT_FALSE(pref_store->ReadOnly());
+ ASSERT_FALSE(pref_store->ReadOnly());
+ }
// The JSON file looks like this:
// {
@@ -219,7 +229,8 @@ TEST_F(JsonPrefStoreTest, BasicAsync) {
// }
// }
- RunBasicJsonPrefStoreTest(pref_store,
+ RunBasicJsonPrefStoreTest(blocking_pool_.get(),
+ pref_store,
input_file,
data_dir_.AppendASCII("write.golden.json"));
}
@@ -229,7 +240,8 @@ 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());
+ JsonPrefStore::CreateWithBlockingPool(bogus_input_file,
+ blocking_pool_.get());
MockPrefStoreObserver mock_observer;
pref_store->AddObserver(&mock_observer);
@@ -239,6 +251,7 @@ TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
EXPECT_CALL(*mock_error_delegate,
OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
+ blocking_pool_->FlushForTesting();
message_loop_.RunAllPending();
pref_store->RemoveObserver(&mock_observer);
@@ -255,7 +268,7 @@ TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
// Test that the persistent value can be loaded.
ASSERT_TRUE(file_util::PathExists(pref_file));
scoped_refptr<JsonPrefStore> pref_store =
- new JsonPrefStore(pref_file, message_loop_proxy_.get());
+ JsonPrefStore::CreateWithBlockingPool(pref_file, blocking_pool_.get());
ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
ASSERT_FALSE(pref_store->ReadOnly());
@@ -283,7 +296,7 @@ TEST_F(JsonPrefStoreTest, NeedsEmptyValue) {
// Write to file.
pref_store->CommitPendingWrite();
- MessageLoop::current()->RunAllPending();
+ blocking_pool_->FlushForTesting();
// Compare to expected output.
FilePath golden_output_file =

Powered by Google App Engine
This is Rietveld 408576698