| 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"; |
| 27 | 28 |
| 28 const char kValue1[] = "value1"; | 29 const char kValue1[] = "value1"; |
| 29 const char kValue2[] = "value2"; | 30 const char kValue2[] = "value2"; |
| 30 | 31 |
| 31 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { | 32 class MockReadErrorDelegate : public PersistentPrefStore::ReadErrorDelegate { |
| 32 public: | 33 public: |
| 33 struct Data { | 34 struct Data { |
| 34 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in) | 35 Data(bool invoked_in, PersistentPrefStore::PrefReadError read_error_in) |
| 35 : invoked(invoked_in), read_error(read_error_in) {} | 36 : invoked(invoked_in), read_error(read_error_in) {} |
| 36 | 37 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 63 PersistentPrefStore::PREF_READ_ERROR_NONE), | 64 PersistentPrefStore::PREF_READ_ERROR_NONE), |
| 64 read_error_delegate_( | 65 read_error_delegate_( |
| 65 new MockReadErrorDelegate(&read_error_delegate_data_)) {} | 66 new MockReadErrorDelegate(&read_error_delegate_data_)) {} |
| 66 | 67 |
| 67 void SetUp() override { | 68 void SetUp() override { |
| 68 selected_store_ = new TestingPrefStore; | 69 selected_store_ = new TestingPrefStore; |
| 69 default_store_ = new TestingPrefStore; | 70 default_store_ = new TestingPrefStore; |
| 70 | 71 |
| 71 std::set<std::string> selected_pref_names; | 72 std::set<std::string> selected_pref_names; |
| 72 selected_pref_names.insert(kSelectedPref); | 73 selected_pref_names.insert(kSelectedPref); |
| 74 selected_pref_names.insert(kSharedPref); |
| 73 | 75 |
| 74 segregated_store_ = new SegregatedPrefStore(default_store_, selected_store_, | 76 segregated_store_ = new SegregatedPrefStore(default_store_, selected_store_, |
| 75 selected_pref_names); | 77 selected_pref_names); |
| 76 | 78 |
| 77 segregated_store_->AddObserver(&observer_); | 79 segregated_store_->AddObserver(&observer_); |
| 78 } | 80 } |
| 79 | 81 |
| 80 void TearDown() override { segregated_store_->RemoveObserver(&observer_); } | 82 void TearDown() override { segregated_store_->RemoveObserver(&observer_); } |
| 81 | 83 |
| 82 protected: | 84 protected: |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 selected_store_->SetBlockAsyncRead(true); | 269 selected_store_->SetBlockAsyncRead(true); |
| 268 default_store_->SetBlockAsyncRead(true); | 270 default_store_->SetBlockAsyncRead(true); |
| 269 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 271 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 270 segregated_store_->ReadPrefsAsync(NULL); | 272 segregated_store_->ReadPrefsAsync(NULL); |
| 271 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 273 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 272 selected_store_->SetBlockAsyncRead(false); | 274 selected_store_->SetBlockAsyncRead(false); |
| 273 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); | 275 EXPECT_FALSE(segregated_store_->IsInitializationComplete()); |
| 274 default_store_->SetBlockAsyncRead(false); | 276 default_store_->SetBlockAsyncRead(false); |
| 275 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); | 277 EXPECT_TRUE(segregated_store_->IsInitializationComplete()); |
| 276 } | 278 } |
| 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 |