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

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: always call OnInit.. Created 9 years, 8 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 "content/browser/browser_thread.h"
Mattias Nissler (ping if slow) 2011/04/26 09:04:26 Are we allowed to include content/browser from chr
altimofeev 2011/04/27 10:32:08 Good point. If I understand correctly, you suggest
21 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
Mattias Nissler (ping if slow) 2011/04/26 09:04:26 newline
altimofeev 2011/04/27 10:32:08 Done.
23 namespace {
24
25 class MockPrefStoreObserver: public PrefStore::Observer {
26 public:
Mattias Nissler (ping if slow) 2011/04/26 09:04:26 indentation
altimofeev 2011/04/27 10:32:08 Done.
27 MOCK_METHOD1(OnPrefValueChanged, void (const std::string&));
28 MOCK_METHOD0(OnInitializationCompleted, void ());
29 };
30
31 } // namespace
21 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");
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 case when there is no file.
Mattias Nissler (ping if slow) 2011/04/26 09:04:26 remove "case"
altimofeev 2011/04/27 10:32:08 Done.
176 TEST_F(JsonPrefStoreTest, AsyncNonExistingFile) {
177 BrowserThread ui_thread(BrowserThread::UI, &message_loop_);
178 BrowserThread file_thread(BrowserThread::FILE, &message_loop_);
179
180 FilePath bogus_input_file = data_dir_.AppendASCII("read.txt");
181 ASSERT_FALSE(file_util::PathExists(bogus_input_file));
182 scoped_refptr<JsonPrefStore> pref_store =
183 new JsonPrefStore(bogus_input_file, message_loop_proxy_.get());
184 MockPrefStoreObserver mock_observer;
185 pref_store->AddObserver(&mock_observer);
186
187 PersistentPrefStore::PrefReadError error;
188 bool is_fatal;
189 pref_store->GetErrors(&error, &is_fatal);
190 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, error);
191 EXPECT_FALSE(is_fatal);
192
193 pref_store->ReadPrefsAsync();
194
195 EXPECT_CALL(mock_observer, OnInitializationCompleted()).Times(1);
196 message_loop_.RunAllPending();
197 pref_store->RemoveObserver(&mock_observer);
198
199 pref_store->GetErrors(&error, &is_fatal);
200
201 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE, error);
202 EXPECT_FALSE(is_fatal);
203 EXPECT_FALSE(pref_store->ReadOnly());
204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698