OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "components/user_prefs/tracked/segregated_pref_store.h" | 5 #include "components/user_prefs/tracked/segregated_pref_store.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 private: | 99 private: |
100 std::unique_ptr<MockReadErrorDelegate> read_error_delegate_; | 100 std::unique_ptr<MockReadErrorDelegate> read_error_delegate_; |
101 }; | 101 }; |
102 | 102 |
103 TEST_F(SegregatedPrefStoreTest, StoreValues) { | 103 TEST_F(SegregatedPrefStoreTest, StoreValues) { |
104 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 104 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
105 segregated_store_->ReadPrefs()); | 105 segregated_store_->ReadPrefs()); |
106 | 106 |
107 // Properly stores new values. | 107 // Properly stores new values. |
108 segregated_store_->SetValue(kSelectedPref, | 108 segregated_store_->SetValue(kSelectedPref, |
109 base::MakeUnique<base::StringValue>(kValue1), | 109 base::MakeUnique<base::Value>(kValue1), |
110 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 110 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
111 segregated_store_->SetValue(kUnselectedPref, | 111 segregated_store_->SetValue(kUnselectedPref, |
112 base::MakeUnique<base::StringValue>(kValue2), | 112 base::MakeUnique<base::Value>(kValue2), |
113 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 113 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
114 | 114 |
115 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); | 115 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); |
116 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); | 116 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); |
117 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); | 117 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); |
118 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); | 118 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); |
119 | 119 |
120 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); | 120 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); |
121 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); | 121 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
122 | 122 |
123 ASSERT_FALSE(selected_store_->committed()); | 123 ASSERT_FALSE(selected_store_->committed()); |
124 ASSERT_FALSE(default_store_->committed()); | 124 ASSERT_FALSE(default_store_->committed()); |
125 | 125 |
126 segregated_store_->CommitPendingWrite(); | 126 segregated_store_->CommitPendingWrite(); |
127 | 127 |
128 ASSERT_TRUE(selected_store_->committed()); | 128 ASSERT_TRUE(selected_store_->committed()); |
129 ASSERT_TRUE(default_store_->committed()); | 129 ASSERT_TRUE(default_store_->committed()); |
130 } | 130 } |
131 | 131 |
132 TEST_F(SegregatedPrefStoreTest, ReadValues) { | 132 TEST_F(SegregatedPrefStoreTest, ReadValues) { |
133 selected_store_->SetValue(kSelectedPref, | 133 selected_store_->SetValue(kSelectedPref, |
134 base::MakeUnique<base::StringValue>(kValue1), | 134 base::MakeUnique<base::Value>(kValue1), |
135 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 135 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
136 default_store_->SetValue(kUnselectedPref, | 136 default_store_->SetValue(kUnselectedPref, |
137 base::MakeUnique<base::StringValue>(kValue2), | 137 base::MakeUnique<base::Value>(kValue2), |
138 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 138 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
139 | 139 |
140 // Works properly with values that are already there. | 140 // Works properly with values that are already there. |
141 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 141 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
142 segregated_store_->ReadPrefs()); | 142 segregated_store_->ReadPrefs()); |
143 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 143 ASSERT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
144 segregated_store_->GetReadError()); | 144 segregated_store_->GetReadError()); |
145 | 145 |
146 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); | 146 ASSERT_TRUE(selected_store_->GetValue(kSelectedPref, NULL)); |
147 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); | 147 ASSERT_FALSE(selected_store_->GetValue(kUnselectedPref, NULL)); |
148 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); | 148 ASSERT_FALSE(default_store_->GetValue(kSelectedPref, NULL)); |
149 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); | 149 ASSERT_TRUE(default_store_->GetValue(kUnselectedPref, NULL)); |
150 | 150 |
151 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); | 151 ASSERT_TRUE(segregated_store_->GetValue(kSelectedPref, NULL)); |
152 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); | 152 ASSERT_TRUE(segregated_store_->GetValue(kUnselectedPref, NULL)); |
153 } | 153 } |
154 | 154 |
155 TEST_F(SegregatedPrefStoreTest, Observer) { | 155 TEST_F(SegregatedPrefStoreTest, Observer) { |
156 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 156 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
157 segregated_store_->ReadPrefs()); | 157 segregated_store_->ReadPrefs()); |
158 EXPECT_TRUE(observer_.initialized); | 158 EXPECT_TRUE(observer_.initialized); |
159 EXPECT_TRUE(observer_.initialization_success); | 159 EXPECT_TRUE(observer_.initialization_success); |
160 EXPECT_TRUE(observer_.changed_keys.empty()); | 160 EXPECT_TRUE(observer_.changed_keys.empty()); |
161 segregated_store_->SetValue(kSelectedPref, | 161 segregated_store_->SetValue(kSelectedPref, |
162 base::MakeUnique<base::StringValue>(kValue1), | 162 base::MakeUnique<base::Value>(kValue1), |
163 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 163 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
164 observer_.VerifyAndResetChangedKey(kSelectedPref); | 164 observer_.VerifyAndResetChangedKey(kSelectedPref); |
165 segregated_store_->SetValue(kUnselectedPref, | 165 segregated_store_->SetValue(kUnselectedPref, |
166 base::MakeUnique<base::StringValue>(kValue2), | 166 base::MakeUnique<base::Value>(kValue2), |
167 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 167 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
168 observer_.VerifyAndResetChangedKey(kUnselectedPref); | 168 observer_.VerifyAndResetChangedKey(kUnselectedPref); |
169 } | 169 } |
170 | 170 |
171 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileError) { | 171 TEST_F(SegregatedPrefStoreTest, SelectedPrefReadNoFileError) { |
172 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted | 172 // PREF_READ_ERROR_NO_FILE for the selected prefs file is silently converted |
173 // to PREF_READ_ERROR_NONE. | 173 // to PREF_READ_ERROR_NONE. |
174 selected_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE); | 174 selected_store_->set_read_error(PersistentPrefStore::PREF_READ_ERROR_NO_FILE); |
175 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, | 175 EXPECT_EQ(PersistentPrefStore::PREF_READ_ERROR_NONE, |
176 segregated_store_->ReadPrefs()); | 176 segregated_store_->ReadPrefs()); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 selected_store_->SetBlockAsyncRead(false); | 274 selected_store_->SetBlockAsyncRead(false); |
275 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 275 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
276 default_store_->SetBlockAsyncRead(false); | 276 default_store_->SetBlockAsyncRead(false); |
277 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); | 277 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); |
278 } | 278 } |
279 | 279 |
280 TEST_F(SegregatedPrefStoreTest, GetValues) { | 280 TEST_F(SegregatedPrefStoreTest, GetValues) { |
281 // To check merge behavior, create selected and default stores so each has a | 281 // To check merge behavior, create selected and default stores so each has a |
282 // key the other doesn't have and they have one key in common. | 282 // key the other doesn't have and they have one key in common. |
283 selected_store_->SetValue(kSelectedPref, | 283 selected_store_->SetValue(kSelectedPref, |
284 base::MakeUnique<base::StringValue>(kValue1), | 284 base::MakeUnique<base::Value>(kValue1), |
285 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 285 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
286 default_store_->SetValue(kUnselectedPref, | 286 default_store_->SetValue(kUnselectedPref, |
287 base::MakeUnique<base::StringValue>(kValue2), | 287 base::MakeUnique<base::Value>(kValue2), |
288 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 288 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
289 selected_store_->SetValue(kSharedPref, | 289 selected_store_->SetValue(kSharedPref, base::MakeUnique<base::Value>(kValue1), |
290 base::MakeUnique<base::StringValue>(kValue1), | |
291 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | 290 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); |
292 | 291 |
293 auto values = segregated_store_->GetValues(); | 292 auto values = segregated_store_->GetValues(); |
294 const base::Value* value = nullptr; | 293 const base::Value* value = nullptr; |
295 // Check that a selected preference is returned. | 294 // Check that a selected preference is returned. |
296 ASSERT_TRUE(values->Get(kSelectedPref, &value)); | 295 ASSERT_TRUE(values->Get(kSelectedPref, &value)); |
297 EXPECT_TRUE(base::Value(kValue1).Equals(value)); | 296 EXPECT_TRUE(base::Value(kValue1).Equals(value)); |
298 | 297 |
299 // Check that a a default preference is returned. | 298 // Check that a a default preference is returned. |
300 ASSERT_TRUE(values->Get(kUnselectedPref, &value)); | 299 ASSERT_TRUE(values->Get(kUnselectedPref, &value)); |
301 EXPECT_TRUE(base::Value(kValue2).Equals(value)); | 300 EXPECT_TRUE(base::Value(kValue2).Equals(value)); |
302 | 301 |
303 // Check that the selected preference is preferred. | 302 // Check that the selected preference is preferred. |
304 ASSERT_TRUE(values->Get(kSharedPref, &value)); | 303 ASSERT_TRUE(values->Get(kSharedPref, &value)); |
305 EXPECT_TRUE(base::Value(kValue1).Equals(value)); | 304 EXPECT_TRUE(base::Value(kValue1).Equals(value)); |
306 } | 305 } |
OLD | NEW |