[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 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 | |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 5 | #include "base/macros.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 6 | #include "components/prefs/default_pref_store.h" |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 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); |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 17 | ~MockPrefStoreObserver() override; |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 18 | |
| 19 | int change_count() { |
| 20 | return change_count_; |
| 21 | } |
| 22 | |
| 23 | // PrefStore::Observer implementation: |
dcheng | 5648818 | 2014-10-21 10:54:51 | [diff] [blame] | 24 | void OnPrefValueChanged(const std::string& key) override; |
| 25 | void OnInitializationCompleted(bool succeeded) override {} |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 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, |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 57 | std::unique_ptr<Value>(new StringValue("foo"))); |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 58 | EXPECT_EQ(0, observer.change_count()); |
| 59 | |
| 60 | // Replacing the default value should send a change notification... |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 61 | pref_store->ReplaceDefaultValue( |
| 62 | kPrefKey, std::unique_ptr<Value>(new StringValue("bar"))); |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 63 | EXPECT_EQ(1, observer.change_count()); |
| 64 | |
| 65 | // But only if the value actually changed. |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 66 | pref_store->ReplaceDefaultValue( |
| 67 | kPrefKey, std::unique_ptr<Value>(new StringValue("bar"))); |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 68 | EXPECT_EQ(1, observer.change_count()); |
| 69 | } |
| 70 | |