Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / user_prefs / tracked / segregated_pref_store_unittest.cc
blobdc458a5582c44b72fdc92a70bd76e03183c18eb8
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/user_prefs/tracked/segregated_pref_store.h"
7 #include <set>
8 #include <string>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/persistent_pref_store.h"
15 #include "base/prefs/pref_store_observer_mock.h"
16 #include "base/prefs/testing_pref_store.h"
17 #include "base/values.h"
18 #include "components/user_prefs/tracked/segregated_pref_store.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace {
23 const char kSelectedPref[] = "selected_pref";
24 const char kUnselectedPref[] = "unselected_pref";
26 const char kValue1[] = "value1";
27 const char kValue2[] = "value2";
29 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate {
30 public:
31 struct Data {
32 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in)
33 : invoked(invoked_in), read_error(read_error_in) {}
35 bool invoked;
36 PersistentPrefStore::PrefReadError read_error;
39 explicit MockReadErrorDelegate(Data* data) : data_(data) {
40 DCHECK(data_);
41 EXPECT_FALSE(data_->invoked);
44 // PersistentPrefStore::ReadErrorDelegate implementation
45 void OnError(PersistentPrefStore::PrefReadError read_error) override {
46 EXPECT_FALSE(data_->invoked);
47 data_->invoked = true;
48 data_->read_error = read_error;
51 private:
52 Data* data_;
55 } // namespace
57 class SegregatedPrefStoreTest : public testing::Test {
58 public:
59 SegregatedPrefStoreTest()
60 : read_error_delegate_data_(false,
61 PersistentPrefStore::PREF_READ_ERROR_NONE),
62 read_error_delegate_(
63 new MockReadErrorDelegate(&read_error_delegate_data_)) {}
65 void SetUp() override {
66 selected_store_ = new TestingPrefStore;
67 default_store_ = new TestingPrefStore;
69 std::set<std::string> selected_pref_names;
70 selected_pref_names.insert(kSelectedPref);
72 segregated_store_ = new SegregatedPrefStore(default_store_, selected_store_,
73 selected_pref_names);
75 segregated_store_->AddObserver(&observer_);
78 void TearDown() override { segregated_store_->RemoveObserver(&observer_); }
80 protected:
81 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> GetReadErrorDelegate() {
82 EXPECT_TRUE(read_error_delegate_);
83 return read_error_delegate_.Pass();
86 PrefStoreObserverMock observer_;
88 scoped_refptr<TestingPrefStore> default_store_;
89 scoped_refptr<TestingPrefStore> selected_store_;
90 scoped_refptr<SegregatedPrefStore> segregated_store_;
92 MockReadErrorDelegate::Data read_error_delegate_data_;
94 private:
95 scoped_ptr<MockReadErrorDelegate> read_error_delegate_;
98 TEST_F(SegregatedPrefStoreTest, StoreValues) {
99 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
100 segregated_store_->ReadPrefs());
102 // Properly stores new values.
103 segregated_store_->SetValue(kSelectedPref,
104 make_scoped_ptr(new base::StringValue(kValue1)),
105 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
106 segregated_store_->SetValue(kUnselectedPref,
107 make_scoped_ptr(new base::StringValue(kValue2)),
108 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
110 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
111 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
112 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
113 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
115 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
116 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
118 ASSERT_FALSE(selected_store_->committed());
119 ASSERT_FALSE(default_store_->committed());
121 segregated_store_->CommitPendingWrite();
123 ASSERT_TRUE(selected_store_->committed());
124 ASSERT_TRUE(default_store_->committed());
127 TEST_F(SegregatedPrefStoreTest, ReadValues) {
128 selected_store_->SetValue(kSelectedPref,
129 make_scoped_ptr(new base::StringValue(kValue1)),
130 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
131 default_store_->SetValue(kUnselectedPref,
132 make_scoped_ptr(new base::StringValue(kValue2)),
133 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
135 // Works properly with values that are already there.
136 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
137 segregated_store_->ReadPrefs());
138 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
139 segregated_store_->GetReadError());
141 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
142 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
143 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
144 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
146 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
147 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
150 TEST_F(SegregatedPrefStoreTest, Observer) {
151 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
152 segregated_store_->ReadPrefs());
153 EXPECT_TRUE(observer_.initialized);
154 EXPECT_TRUE(observer_.initialization_success);
155 EXPECT_TRUE(observer_.changed_keys.empty());
156 segregated_store_->SetValue(kSelectedPref,
157 make_scoped_ptr(new base::StringValue(kValue1)),
158 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
159 observer_.VerifyAndResetChangedKey(kSelectedPref);
160 segregated_store_->SetValue(kUnselectedPref,
161 make_scoped_ptr(new base::StringValue(kValue2)),
162 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
163 observer_.VerifyAndResetChangedKey(kUnselectedPref);
166 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileError) {
167 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
168 // to PREF_READ_ERROR_NONE.
169 selected_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
170 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
171 segregated_store_->ReadPrefs());
172 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
173 segregated_store_->GetReadError());
176 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadError) {
177 selected_store_->set_read_error(
178 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
179 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
180 segregated_store_->ReadPrefs());
181 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
182 segregated_store_->GetReadError());
185 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileErrorAsync) {
186 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
187 // to PREF_READ_ERROR_NONE.
188 selected_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
190 default_store_->SetBlockAsyncRead(true);
192 EXPECT_FALSE(read_error_delegate_data_.invoked);
194 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
196 EXPECT_FALSE(read_error_delegate_data_.invoked);
198 default_store_->SetBlockAsyncRead(false);
200 // ReadErrorDelegate is not invoked for ERROR_NONE.
201 EXPECT_FALSE(read_error_delegate_data_.invoked);
202 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
203 segregated_store_->GetReadError());
204 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
205 segregated_store_->GetReadError());
208 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadNoFileError) {
209 default_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
210 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
211 segregated_store_->ReadPrefs());
212 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
213 segregated_store_->GetReadError());
216 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadError) {
217 default_store_->set_read_error(
218 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
219 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
220 segregated_store_->ReadPrefs());
221 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
222 segregated_store_->GetReadError());
225 TEST_F(SegregatedPrefStoreTest, BothPrefReadError) {
226 default_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
227 selected_store_->set_read_error(
228 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
229 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
230 segregated_store_->ReadPrefs());
231 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
232 segregated_store_->GetReadError());
235 TEST_F(SegregatedPrefStoreTest, BothPrefReadErrorAsync) {
236 default_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
237 selected_store_->set_read_error(
238 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
240 selected_store_->SetBlockAsyncRead(true);
242 EXPECT_FALSE(read_error_delegate_data_.invoked);
244 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
246 EXPECT_FALSE(read_error_delegate_data_.invoked);
248 selected_store_->SetBlockAsyncRead(false);
250 EXPECT_TRUE(read_error_delegate_data_.invoked);
251 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
252 segregated_store_->GetReadError());
253 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
254 segregated_store_->GetReadError());
257 TEST_F(SegregatedPrefStoreTest, IsInitializationComplete) {
258 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
259 segregated_store_->ReadPrefs();
260 EXPECT_TRUE(segregated_store_->IsInitializationComplete());
263 TEST_F(SegregatedPrefStoreTest, IsInitializationCompleteAsync) {
264 selected_store_->SetBlockAsyncRead(true);
265 default_store_->SetBlockAsyncRead(true);
266 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
267 segregated_store_->ReadPrefsAsync(NULL);
268 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
269 selected_store_->SetBlockAsyncRead(false);
270 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
271 default_store_->SetBlockAsyncRead(false);
272 EXPECT_TRUE(segregated_store_->IsInitializationComplete());