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> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "components/prefs/persistent_pref_store.h" | 17 #include "components/prefs/persistent_pref_store.h" |
18 #include "components/prefs/pref_store_observer_mock.h" | 18 #include "components/prefs/pref_store_observer_mock.h" |
19 #include "components/prefs/testing_pref_store.h" | 19 #include "components/prefs/testing_pref_store.h" |
20 #include "components/user_prefs/tracked/segregated_pref_store.h" | 20 #include "components/user_prefs/tracked/segregated_pref_store.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 const char kSelectedPref[] = "selected_pref"; | 25 const char kSelectedPref[] = "selected_pref"; |
26 const char kUnselectedPref[] = "unselected_pref"; | 26 const char kUnselectedPref[] = "unselected_pref"; |
27 const char kSharedPref[] = "shared_pref"; | |
28 | 27 |
29 const char kValue1[] = "value1"; | 28 const char kValue1[] = "value1"; |
30 const char kValue2[] = "value2"; | 29 const char kValue2[] = "value2"; |
31 | 30 |
32 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
33 public: | 32 public: |
34 struct Data { | 33 struct Data { |
35 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in) | 34 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in) |
36 : invoked(invoked_in), read_error(read_error_in) {} | 35 : invoked(invoked_in), read_error(read_error_in) {} |
37 | 36 |
(...skipping 26 matching lines...) Expand all Loading... |
64 PersistentPrefStore::PREF_READ_ERROR_NONE), | 63 PersistentPrefStore::PREF_READ_ERROR_NONE), |
65 read_error_delegate_( | 64 read_error_delegate_( |
66 new MockReadErrorDelegate(&read_error_delegate_data_)) {} | 65 new MockReadErrorDelegate(&read_error_delegate_data_)) {} |
67 | 66 |
68 void SetUp() override { | 67 void SetUp() override { |
69 selected_store_ = new TestingPrefStore; | 68 selected_store_ = new TestingPrefStore; |
70 default_store_ = new TestingPrefStore; | 69 default_store_ = new TestingPrefStore; |
71 | 70 |
72 std::set<std::string> selected_pref_names; | 71 std::set<std::string> selected_pref_names; |
73 selected_pref_names.insert(kSelectedPref); | 72 selected_pref_names.insert(kSelectedPref); |
74 selected_pref_names.insert(kSharedPref); | |
75 | 73 |
76 segregated_store_ = new SegregatedPrefStore(default_store_, selected_store_, | 74 segregated_store_ = new SegregatedPrefStore(default_store_, selected_store_, |
77 selected_pref_names); | 75 selected_pref_names); |
78 | 76 |
79 segregated_store_->AddObserver(&observer_); | 77 segregated_store_->AddObserver(&observer_); |
80 } | 78 } |
81 | 79 |
82 void TearDown() override { segregated_store_->RemoveObserver(&observer_); } | 80 void TearDown() override { segregated_store_->RemoveObserver(&observer_); } |
83 | 81 |
84 protected: | 82 protected: |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 selected_store_->SetBlockAsyncRead(true); | 267 selected_store_->SetBlockAsyncRead(true); |
270 default_store_->SetBlockAsyncRead(true); | 268 default_store_->SetBlockAsyncRead(true); |
271 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 269 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
272 segregated_store_->ReadPrefsAsync(NULL); | 270 segregated_store_->ReadPrefsAsync(NULL); |
273 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 271 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
274 selected_store_->SetBlockAsyncRead(false); | 272 selected_store_->SetBlockAsyncRead(false); |
275 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 273 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
276 default_store_->SetBlockAsyncRead(false); | 274 default_store_->SetBlockAsyncRead(false); |
277 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); | 275 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); |
278 } | 276 } |
279 | |
280 TEST_F(SegregatedPrefStoreTest, GetValues) { | |
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. | |
283 selected_store_->SetValue(kSelectedPref, | |
284 base::MakeUnique<base::StringValue>(kValue1), | |
285 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
286 default_store_->SetValue(kUnselectedPref, | |
287 base::MakeUnique<base::StringValue>(kValue2), | |
288 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
289 selected_store_->SetValue(kSharedPref, | |
290 base::MakeUnique<base::StringValue>(kValue1), | |
291 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS); | |
292 | |
293 auto values = segregated_store_->GetValues(); | |
294 const base::Value* value = nullptr; | |
295 // Check that a selected preference is returned. | |
296 ASSERT_TRUE(values->Get(kSelectedPref, &value)); | |
297 EXPECT_TRUE(base::FundamentalValue(kValue1).Equals(value)); | |
298 | |
299 // Check that a a default preference is returned. | |
300 ASSERT_TRUE(values->Get(kUnselectedPref, &value)); | |
301 EXPECT_TRUE(base::FundamentalValue(kValue2).Equals(value)); | |
302 | |
303 // Check that the selected preference is preferred. | |
304 ASSERT_TRUE(values->Get(kSharedPref, &value)); | |
305 EXPECT_TRUE(base::FundamentalValue(kValue1).Equals(value)); | |
306 } | |
OLD | NEW |