[sql] Remove _HAS_EXCEPTIONS=0 from build info.
[chromium-blink-merge.git] / chrome / browser / prefs / tracked / segregated_pref_store_unittest.cc
blob04e07084db8d7025ec42cecf47ccba6b09ae0b17
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,
106 make_scoped_ptr(new base::StringValue(kValue1)),
107 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
108 segregated_store_->SetValue(kUnselectedPref,
109 make_scoped_ptr(new base::StringValue(kValue2)),
110 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
112 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
113 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
114 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
115 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
117 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
118 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
120 ASSERT_FALSE(selected_store_->committed());
121 ASSERT_FALSE(default_store_->committed());
123 segregated_store_->CommitPendingWrite();
125 ASSERT_TRUE(selected_store_->committed());
126 ASSERT_TRUE(default_store_->committed());
129 TEST_F(SegregatedPrefStoreTest, ReadValues) {
130 selected_store_->SetValue(kSelectedPref,
131 make_scoped_ptr(new base::StringValue(kValue1)),
132 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
133 default_store_->SetValue(kUnselectedPref,
134 make_scoped_ptr(new base::StringValue(kValue2)),
135 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
137 // Works properly with values that are already there.
138 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
139 segregated_store_->ReadPrefs());
140 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
141 segregated_store_->GetReadError());
143 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL));
144 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL));
145 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL));
146 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL));
148 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL));
149 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL));
152 TEST_F(SegregatedPrefStoreTest, Observer) {
153 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
154 segregated_store_->ReadPrefs());
155 EXPECT_TRUE(observer_.initialized);
156 EXPECT_TRUE(observer_.initialization_success);
157 EXPECT_TRUE(observer_.changed_keys.empty());
158 segregated_store_->SetValue(kSelectedPref,
159 make_scoped_ptr(new base::StringValue(kValue1)),
160 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
161 observer_.VerifyAndResetChangedKey(kSelectedPref);
162 segregated_store_->SetValue(kUnselectedPref,
163 make_scoped_ptr(new base::StringValue(kValue2)),
164 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
165 observer_.VerifyAndResetChangedKey(kUnselectedPref);
168 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileError) {
169 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
170 // to PREF_READ_ERROR_NONE.
171 selected_store_->set_read_error(
172 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
173 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
174 segregated_store_->ReadPrefs());
175 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
176 segregated_store_->GetReadError());
179 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadError) {
180 selected_store_->set_read_error(
181 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
182 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
183 segregated_store_->ReadPrefs());
184 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
185 segregated_store_->GetReadError());
188 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileErrorAsync) {
189 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted
190 // to PREF_READ_ERROR_NONE.
191 selected_store_->set_read_error(
192 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
194 default_store_->SetBlockAsyncRead(true);
196 EXPECT_FALSE(read_error_delegate_data_.invoked);
198 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
200 EXPECT_FALSE(read_error_delegate_data_.invoked);
202 default_store_->SetBlockAsyncRead(false);
204 // ReadErrorDelegate is not invoked for ERROR_NONE.
205 EXPECT_FALSE(read_error_delegate_data_.invoked);
206 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
207 segregated_store_->GetReadError());
208 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE,
209 segregated_store_->GetReadError());
212 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadNoFileError) {
213 default_store_->set_read_error(
214 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
215 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
216 segregated_store_->ReadPrefs());
217 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
218 segregated_store_->GetReadError());
221 TEST_F(SegregatedPrefStoreTest, UnselectedPrefReadError) {
222 default_store_->set_read_error(
223 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
224 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
225 segregated_store_->ReadPrefs());
226 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED,
227 segregated_store_->GetReadError());
230 TEST_F(SegregatedPrefStoreTest, BothPrefReadError) {
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);
235 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
236 segregated_store_->ReadPrefs());
237 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
238 segregated_store_->GetReadError());
241 TEST_F(SegregatedPrefStoreTest, BothPrefReadErrorAsync) {
242 default_store_->set_read_error(
243 PersistentPrefStore::PREF_READ_ERROR_NO_FILE);
244 selected_store_->set_read_error(
245 PersistentPrefStore::PREF_READ_ERROR_ACCESS_DENIED);
247 selected_store_->SetBlockAsyncRead(true);
249 EXPECT_FALSE(read_error_delegate_data_.invoked);
251 segregated_store_->ReadPrefsAsync(GetReadErrorDelegate().release());
253 EXPECT_FALSE(read_error_delegate_data_.invoked);
255 selected_store_->SetBlockAsyncRead(false);
257 EXPECT_TRUE(read_error_delegate_data_.invoked);
258 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
259 segregated_store_->GetReadError());
260 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NO_FILE,
261 segregated_store_->GetReadError());
264 TEST_F(SegregatedPrefStoreTest, IsInitializationComplete) {
265 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
266 segregated_store_->ReadPrefs();
267 EXPECT_TRUE(segregated_store_->IsInitializationComplete());
270 TEST_F(SegregatedPrefStoreTest, IsInitializationCompleteAsync) {
271 selected_store_->SetBlockAsyncRead(true);
272 default_store_->SetBlockAsyncRead(true);
273 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
274 segregated_store_->ReadPrefsAsync(NULL);
275 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
276 selected_store_->SetBlockAsyncRead(false);
277 EXPECT_FALSE(segregated_store_->IsInitializationComplete());
278 default_store_->SetBlockAsyncRead(false);
279 EXPECT_TRUE(segregated_store_->IsInitializationComplete());