Fire an error if a pref used in the UI is missing once all prefs are fetched.
[chromium-blink-merge.git] / chrome / browser / prefs / tracked / segregated_pref_store_unittest.cc
blob27b45d77ac027c65ac569159638d6f92c4092d06
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 "chrome/browser/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 "chrome/browser/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(
73 default_store_,
74 selected_store_,
75 selected_pref_names);
77 segregated_store_->AddObserver(&observer_);
80 void TearDown() override { segregated_store_->RemoveObserver(&observer_); }
82 protected:
83 scoped_ptr<PersistentPrefStore::ReadErrorDelegate> GetReadErrorDelegate() {
84 EXPECT_TRUE(read_error_delegate_);
85 return read_error_delegate_.Pass();
88 PrefStoreObserverMock observer_;
90 scoped_refptr<TestingPrefStore> default_store_;
91 scoped_refptr<TestingPrefStore> selected_store_;
92 scoped_refptr<SegregatedPrefStore> segregated_store_;
94 MockReadErrorDelegate::Data read_error_delegate_data_;
96 private:
97 scoped_ptr<MockReadErrorDelegate> read_error_delegate_;
100 TEST_F(SegregatedPrefStoreTest, StoreValues) {
101 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
102 segregated_store_->ReadPrefs());
104 // Properly stores new values.
105 segregated_store_->SetValue(kSelectedPref, new base::StringValue(kValue1));
106 segregated_store_->SetValue(kUnselectedPref, new base::StringValue(kValue2));
108 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
109 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
110 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
111 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
113 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
114 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
116 ASSERT_FALSE(selected_store_->committed());
117 ASSERT_FALSE(default_store_->committed());
119 segregated_store_->CommitPendingWrite();
121 ASSERT_TRUE(selected_store_->committed());
122 ASSERT_TRUE(default_store_->committed());
125 TEST_F(SegregatedPrefStoreTest, ReadValues) {
126 selected_store_->SetValue(kSelectedPref, new base::StringValue(kValue1));
127 default_store_->SetValue(kUnselectedPref,
128 new base::StringValue(kValue2));
130 // Works properly with values that are already there.
131 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
132 segregated_store_->ReadPrefs());
133 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
134 segregated_store_->GetReadError());
136 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
137 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
138 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
139 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
141 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
142 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
145 TEST_F(SegregatedPrefStoreTest, Observer) {
146 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
147 segregated_store_->ReadPrefs());
148 EXPECT_TRUE(observer_.initialized);
149 EXPECT_TRUE(observer_.initialization_success);
150 EXPECT_TRUE(observer_.changed_keys.empty());
151 segregated_store_->SetValue(kSelectedPref, new base::StringValue(kValue1));
152 observer_.VerifyAndResetChangedKey(kSelectedPref);
153 segregated_store_->SetValue(kUnselectedPref, new base::StringValue(kValue2));
154 observer_.VerifyAndResetChangedKey(kUnselectedPref);
157 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileError) {
158 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
159 // to PREF_READ_ERROR_NONE.
160 selected_store_->set_read_error(
161 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
162 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
163 segregated_store_->ReadPrefs());
164 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
165 segregated_store_->GetReadError());
168 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadError) {
169 selected_store_->set_read_error(
170 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
171 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
172 segregated_store_->ReadPrefs());
173 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
174 segregated_store_->GetReadError());
177 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileErrorAsync) {
178 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
179 // to PREF_READ_ERROR_NONE.
180 selected_store_->set_read_error(
181 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
183 default_store_->SetBlockAsyncRead(true);
185 EXPECT_FALSE(read_error_delegate_data_.invoked);
187 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
189 EXPECT_FALSE(read_error_delegate_data_.invoked);
191 default_store_->SetBlockAsyncRead(false);
193 // ReadErrorDelegate is not invoked for ERROR_NONE.
194 EXPECT_FALSE(read_error_delegate_data_.invoked);
195 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
196 segregated_store_->GetReadError());
197 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
198 segregated_store_->GetReadError());
201 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadNoFileError) {
202 default_store_->set_read_error(
203 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
204 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
205 segregated_store_->ReadPrefs());
206 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
207 segregated_store_->GetReadError());
210 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadError) {
211 default_store_->set_read_error(
212 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
213 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
214 segregated_store_->ReadPrefs());
215 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
216 segregated_store_->GetReadError());
219 TEST_F(SegregatedPrefStoreTest, BothPrefReadError) {
220 default_store_->set_read_error(
221 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
222 selected_store_->set_read_error(
223 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
224 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
225 segregated_store_->ReadPrefs());
226 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
227 segregated_store_->GetReadError());
230 TEST_F(SegregatedPrefStoreTest, BothPrefReadErrorAsync) {
231 default_store_->set_read_error(
232 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
233 selected_store_->set_read_error(
234 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
236 selected_store_->SetBlockAsyncRead(true);
238 EXPECT_FALSE(read_error_delegate_data_.invoked);
240 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
242 EXPECT_FALSE(read_error_delegate_data_.invoked);
244 selected_store_->SetBlockAsyncRead(false);
246 EXPECT_TRUE(read_error_delegate_data_.invoked);
247 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
248 segregated_store_->GetReadError());
249 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
250 segregated_store_->GetReadError());
253 TEST_F(SegregatedPrefStoreTest, IsInitializationComplete) {
254 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
255 segregated_store_->ReadPrefs();
256 EXPECT_TRUE(segregated_store_->IsInitializationComplete());
259 TEST_F(SegregatedPrefStoreTest, IsInitializationCompleteAsync) {
260 selected_store_->SetBlockAsyncRead(true);
261 default_store_->SetBlockAsyncRead(true);
262 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
263 segregated_store_->ReadPrefsAsync(NULL);
264 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
265 selected_store_->SetBlockAsyncRead(false);
266 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
267 default_store_->SetBlockAsyncRead(false);
268 EXPECT_TRUE(segregated_store_->IsInitializationComplete());