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

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: 2010->2011 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
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/persistent_pref_store.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 EXPECT_FALSE(pref_store->ReadOnly()); 79 EXPECT_FALSE(pref_store->ReadOnly());
64 80
65 // The file should have been moved aside. 81 // The file should have been moved aside.
66 EXPECT_FALSE(file_util::PathExists(invalid_file)); 82 EXPECT_FALSE(file_util::PathExists(invalid_file));
67 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad"); 83 FilePath moved_aside = temp_dir_.path().AppendASCII("invalid.bad");
68 EXPECT_TRUE(file_util::PathExists(moved_aside)); 84 EXPECT_TRUE(file_util::PathExists(moved_aside));
69 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original, 85 EXPECT_TRUE(file_util::TextContentsEqual(invalid_file_original,
70 moved_aside)); 86 moved_aside));
71 } 87 }
72 88
73 TEST_F(JsonPrefStoreTest, Basic) { 89 // This function is used to avoid code duplication while testing synchronous and
74 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"), 90 // asynchronous version of the JsonPrefStore loading.
75 temp_dir_.path().AppendASCII("write.json"))); 91 void RunBasicJsonPrefStoreTest(JsonPrefStore *pref_store,
76 92 const FilePath& output_file,
77 // Test that the persistent value can be loaded. 93 const FilePath& golden_output_file) {
78 FilePath input_file = temp_dir_.path().AppendASCII("write.json");
79 ASSERT_TRUE(file_util::PathExists(input_file));
80 scoped_refptr<JsonPrefStore> pref_store =
81 new JsonPrefStore(input_file, message_loop_proxy_.get());
82 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
83 ASSERT_FALSE(pref_store->ReadOnly());
84
85 // The JSON file looks like this:
86 // {
87 // "homepage": "http://www.cnn.com",
88 // "some_directory": "/usr/local/",
89 // "tabs": {
90 // "new_windows_in_tabs": true,
91 // "max_tabs": 20
92 // }
93 // }
94
95 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs"; 94 const char kNewWindowsInTabs[] = "tabs.new_windows_in_tabs";
96 const char kMaxTabs[] = "tabs.max_tabs"; 95 const char kMaxTabs[] = "tabs.max_tabs";
97 const char kLongIntPref[] = "long_int.pref"; 96 const char kLongIntPref[] = "long_int.pref";
98 97
99 std::string cnn("http://www.cnn.com"); 98 std::string cnn("http://www.cnn.com");
100 99
101 const Value* actual; 100 const Value* actual;
102 EXPECT_EQ(PrefStore::READ_OK, 101 EXPECT_EQ(PrefStore::READ_OK,
103 pref_store->GetValue(prefs::kHomePage, &actual)); 102 pref_store->GetValue(prefs::kHomePage, &actual));
104 std::string string_value; 103 std::string string_value;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 pref_store->SetValue(kLongIntPref, 144 pref_store->SetValue(kLongIntPref,
146 Value::CreateStringValue( 145 Value::CreateStringValue(
147 base::Int64ToString(214748364842LL))); 146 base::Int64ToString(214748364842LL)));
148 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual)); 147 EXPECT_EQ(PrefStore::READ_OK, pref_store->GetValue(kLongIntPref, &actual));
149 EXPECT_TRUE(actual->GetAsString(&string_value)); 148 EXPECT_TRUE(actual->GetAsString(&string_value));
150 int64 value; 149 int64 value;
151 base::StringToInt64(string_value, &value); 150 base::StringToInt64(string_value, &value);
152 EXPECT_EQ(214748364842LL, value); 151 EXPECT_EQ(214748364842LL, value);
153 152
154 // Serialize and compare to expected output. 153 // Serialize and compare to expected output.
155 FilePath output_file = input_file;
156 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json");
157 ASSERT_TRUE(file_util::PathExists(golden_output_file)); 154 ASSERT_TRUE(file_util::PathExists(golden_output_file));
158 ASSERT_TRUE(pref_store->WritePrefs()); 155 ASSERT_TRUE(pref_store->WritePrefs());
159 MessageLoop::current()->RunAllPending(); 156 MessageLoop::current()->RunAllPending();
160 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); 157 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file));
161 ASSERT_TRUE(file_util::Delete(output_file, false)); 158 ASSERT_TRUE(file_util::Delete(output_file, false));
162 } 159 }
160
161 TEST_F(JsonPrefStoreTest, Basic) {
162 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
163 temp_dir_.path().AppendASCII("write.json")));
164
165 // Test that the persistent value can be loaded.
166 FilePath input_file = temp_dir_.path().AppendASCII("write.json");
167 ASSERT_TRUE(file_util::PathExists(input_file));
168 scoped_refptr<JsonPrefStore> pref_store =
169 new JsonPrefStore(input_file, message_loop_proxy_.get());
170 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, pref_store->ReadPrefs());
171 ASSERT_FALSE(pref_store->ReadOnly());
172
173 // The JSON file looks like this:
174 // {
175 // "homepage": "http://www.cnn.com",
176 // "some_directory": "/usr/local/",
177 // "tabs": {
178 // "new_windows_in_tabs": true,
179 // "max_tabs": 20
180 // }
181 // }
182
183 RunBasicJsonPrefStoreTest(pref_store,
184 input_file,
185 data_dir_.AppendASCII("write.golden.json"));
186 }
187
188 TEST_F(JsonPrefStoreTest, BasicAsync) {
189 ASSERT_TRUE(file_util::CopyFile(data_dir_.AppendASCII("read.json"),
190 temp_dir_.path().AppendASCII("write.json")));
191
192 // Test that the persistent value can be loaded.
193 FilePath input_file = temp_dir_.path().AppendASCII("write.json");
194 ASSERT_TRUE(file_util::PathExists(input_file));
195 scoped_refptr<JsonPrefStore> pref_store =
196 new JsonPrefStore(input_file, message_loop_proxy_.get());
197
198 MockPrefStoreObserver mock_observer;
199 pref_store->AddObserver(&mock_observer);
200
201 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
202 pref_store->ReadPrefsAsync(mock_error_delegate);
203
204 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
205 EXPECT_CALL(*mock_error_delegate,
206 OnError(PersistentPrefStore::PREF_READ_ERROR_NONE)).Times(0);
207 message_loop_.RunAllPending();
208 pref_store->RemoveObserver(&mock_observer);
209
210 ASSERT_FALSE(pref_store->ReadOnly());
211
212 // The JSON file looks like this:
213 // {
214 // "homepage": "http://www.cnn.com",
215 // "some_directory": "/usr/local/",
216 // "tabs": {
217 // "new_windows_in_tabs": true,
218 // "max_tabs": 20
219 // }
220 // }
221
222 RunBasicJsonPrefStoreTest(pref_store,
223 input_file,
224 data_dir_.AppendASCII("write.golden.json"));
225 }
226
227 // Tests asynchronous reading of the file when there is no file.
228 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
229 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
230 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
231 scoped_refptr<JsonPrefStore> pref_store =
232 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get());
233 MockPrefStoreObserver mock_observer;
234 pref_store->AddObserver(&mock_observer);
235
236 MockReadErrorDelegate *mock_error_delegate = new MockReadErrorDelegate;
237 pref_store->ReadPrefsAsync(mock_error_delegate);
238
239 EXPECT_CALL(mock_observer, OnInitializationCompleted(true)).Times(1);
240 EXPECT_CALL(*mock_error_delegate,
241 OnError(PersistentPrefStore::PREF_READ_ERROR_NO_FILE)).Times(1);
242 message_loop_.RunAllPending();
243 pref_store->RemoveObserver(&mock_observer);
244
245 EXPECT_FALSE(pref_store->ReadOnly());
246 }
OLDNEW
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/persistent_pref_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698