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

Side by Side Diff: chrome/common/json_pref_store_unittest.cc

Issue 6894020: Adds async interface method to PersistentPrefStore. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 9 years, 7 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/file_util.h" 5 #include "base/file_util.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/scoped_temp_dir.h"
8 #include "base/message_loop.h" 9 #include "base/message_loop.h"
9 #include "base/message_loop_proxy.h" 10 #include "base/message_loop_proxy.h"
10 #include "base/path_service.h" 11 #include "base/path_service.h"
11 #include "base/string_number_conversions.h" 12 #include "base/string_number_conversions.h"
12 #include "base/string_util.h" 13 #include "base/string_util.h"
13 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
14 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "base/memory/scoped_temp_dir.h" 17 #include "chrome/common/chrome_paths.h"
17 #include "chrome/common/json_pref_store.h" 18 #include "chrome/common/json_pref_store.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
20 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 22
23 namespace {
24
25 class MockPrefStoreObserver : public PrefStore::Observer {
26 public:
27 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
28 MOCK_METHOD1(OnInitializationCompleted, void (bool));
29 };
30
31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
32 public:
33 MOCK_METHOD1(OnError, void(PersistentPrefStore::PrefReadError));
34 };
35
36 } // namespace
37
22 class JsonPrefStoreTest : public testing::Test { 38 class JsonPrefStoreTest : public testing::Test {
23 protected: 39 protected:
24 virtual void SetUp() { 40 virtual void SetUp() {
25 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); 41 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread();
26 42
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
28 44
29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); 45 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_));
30 data_dir_ = data_dir_.AppendASCII("pref_service"); 46 data_dir_ = data_dir_.AppendASCII("pref_service");
31 ASSERT_TRUE(file_util::PathExists(data_dir_)); 47 ASSERT_TRUE(file_util::PathExists(data_dir_));
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 169
154 // Serialize and compare to expected output. 170 // Serialize and compare to expected output.
155 FilePath output_file = input_file; 171 FilePath output_file = input_file;
156 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); 172 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json");
157 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 173 ASSERT_TRUE(file_util::PathExists(golden_output_file));
158 ASSERT_TRUE(pref_store->WritePrefs()); 174 ASSERT_TRUE(pref_store->WritePrefs());
159 MessageLoop::current()->RunAllPending(); 175 MessageLoop::current()->RunAllPending();
160 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 176 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
161 ASSERT_TRUE(file_util::Delete(output_file, false)); 177 ASSERT_TRUE(file_util::Delete(output_file, false));
162 } 178 }
179
180 // Tests asynchronous reading of the file when there is no file.
Mattias Nissler (ping if slow) 2011/05/04 23:33:08 Can you please also add a test case for the positi
altimofeev 2011/05/05 12:59:28 Done.
181 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
182 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
183 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
184 scoped_refptr<JsonPrefStore> pref_store =
185 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get());
186 MockPrefStoreObserver mock_observer;
187 pref_store->AddObserver(&mock_observer);
188
189 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
190 pref_store->ReadPrefsAsync(mock_error_delegate);
191
192 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
193 EXPECT_CALL(*mock_error_delegate,
194 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
195 message_loop_.RunAllPending();
196 pref_store->RemoveObserver(&mock_observer);
197
198 EXPECT_FALSE(pref_store->ReadOnly());
199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698