OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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. |
| 4 |
| 5 #include "base/macros.h" |
| 6 #include "base/prefs/default_pref_store.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 using base::StringValue; |
| 10 using base::Value; |
| 11 |
| 12 namespace { |
| 13 |
| 14 class MockPrefStoreObserver : public PrefStore::Observer { |
| 15 public: |
| 16 explicit MockPrefStoreObserver(DefaultPrefStore* pref_store); |
| 17 ~MockPrefStoreObserver() override; |
| 18 |
| 19 int change_count() { |
| 20 return change_count_; |
| 21 } |
| 22 |
| 23 // PrefStore::Observer implementation: |
| 24 void OnPrefValueChanged(const std::string& key) override; |
| 25 void OnInitializationCompleted(bool succeeded) override {} |
| 26 |
| 27 private: |
| 28 DefaultPrefStore* pref_store_; |
| 29 |
| 30 int change_count_; |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(MockPrefStoreObserver); |
| 33 }; |
| 34 |
| 35 MockPrefStoreObserver::MockPrefStoreObserver(DefaultPrefStore* pref_store) |
| 36 : pref_store_(pref_store), change_count_(0) { |
| 37 pref_store_->AddObserver(this); |
| 38 } |
| 39 |
| 40 MockPrefStoreObserver::~MockPrefStoreObserver() { |
| 41 pref_store_->RemoveObserver(this); |
| 42 } |
| 43 |
| 44 void MockPrefStoreObserver::OnPrefValueChanged(const std::string& key) { |
| 45 change_count_++; |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 TEST(DefaultPrefStoreTest, NotifyPrefValueChanged) { |
| 51 scoped_refptr<DefaultPrefStore> pref_store(new DefaultPrefStore); |
| 52 MockPrefStoreObserver observer(pref_store.get()); |
| 53 std::string kPrefKey("pref_key"); |
| 54 |
| 55 // Setting a default value shouldn't send a change notification. |
| 56 pref_store->SetDefaultValue(kPrefKey, |
| 57 scoped_ptr<Value>(new StringValue("foo"))); |
| 58 EXPECT_EQ(0, observer.change_count()); |
| 59 |
| 60 // Replacing the default value should send a change notification... |
| 61 pref_store->ReplaceDefaultValue(kPrefKey, |
| 62 scoped_ptr<Value>(new StringValue("bar"))); |
| 63 EXPECT_EQ(1, observer.change_count()); |
| 64 |
| 65 // But only if the value actually changed. |
| 66 pref_store->ReplaceDefaultValue(kPrefKey, |
| 67 scoped_ptr<Value>(new StringValue("bar"))); |
| 68 EXPECT_EQ(1, observer.change_count()); |
| 69 } |
| 70 |
OLD | NEW |