OLD | NEW |
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_METHOD0(OnInitializationCompleted, void ()); |
| 29 }; |
| 30 |
| 31 } // namespace |
| 32 |
22 class JsonPrefStoreTest : public testing::Test { | 33 class JsonPrefStoreTest : public testing::Test { |
23 protected: | 34 protected: |
24 virtual void SetUp() { | 35 virtual void SetUp() { |
25 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); | 36 message_loop_proxy_ = base::MessageLoopProxy::CreateForCurrentThread(); |
26 | 37 |
27 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 38 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
28 | 39 |
29 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); | 40 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_dir_)); |
30 data_dir_ = data_dir_.AppendASCII("pref_service"); | 41 data_dir_ = data_dir_.AppendASCII("pref_service"); |
31 ASSERT_TRUE(file_util::PathExists(data_dir_)); | 42 ASSERT_TRUE(file_util::PathExists(data_dir_)); |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 164 |
154 // Serialize and compare to expected output. | 165 // Serialize and compare to expected output. |
155 FilePath output_file = input_file; | 166 FilePath output_file = input_file; |
156 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); | 167 FilePath golden_output_file = data_dir_.AppendASCII("write.golden.json"); |
157 ASSERT_TRUE(file_util::PathExists(golden_output_file)); | 168 ASSERT_TRUE(file_util::PathExists(golden_output_file)); |
158 ASSERT_TRUE(pref_store->WritePrefs()); | 169 ASSERT_TRUE(pref_store->WritePrefs()); |
159 MessageLoop::current()->RunAllPending(); | 170 MessageLoop::current()->RunAllPending(); |
160 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); | 171 EXPECT_TRUE(file_util::TextContentsEqual(golden_output_file, output_file)); |
161 ASSERT_TRUE(file_util::Delete(output_file, false)); | 172 ASSERT_TRUE(file_util::Delete(output_file, false)); |
162 } | 173 } |
| 174 |
| 175 // Tests asynchronous reading of the file when there is no file. |
| 176 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) { |
| 177 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt"); |
| 178 ASSERT_FALSE(file_util::PathExists(bogus_input_file)); |
| 179 scoped_refptr<JsonPrefStore> pref_store = |
| 180 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get()); |
| 181 MockPrefStoreObserver mock_observer; |
| 182 pref_store->AddObserver(&mock_observer); |
| 183 |
| 184 PersistentPrefStore::PrefReadError error; |
| 185 bool is_fatal; |
| 186 pref_store->GetErrors(&error, &is_fatal); |
| 187 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, error); |
| 188 EXPECT_FALSE(is_fatal); |
| 189 |
| 190 pref_store->ReadPrefsAsync(); |
| 191 |
| 192 EXPECT_CALL(mock_observer, OnInitializationCompleted()).Times(1); |
| 193 message_loop_.RunAllPending(); |
| 194 pref_store->RemoveObserver(&mock_observer); |
| 195 |
| 196 pref_store->GetErrors(&error, &is_fatal); |
| 197 |
| 198 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error); |
| 199 EXPECT_FALSE(is_fatal); |
| 200 EXPECT_FALSE(pref_store->ReadOnly()); |
| 201 } |
OLD | NEW |