blob: af6a074857501e2432d0b9742f3eb48809bfaf42 [file] [log] [blame]
[email protected]f8f8b672013-03-07 09:12:581// 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
avi9ef8bb02015-12-24 05:29:365#include "base/macros.h"
brettwf00b9b42016-02-01 22:11:386#include "components/prefs/default_pref_store.h"
[email protected]f8f8b672013-03-07 09:12:587#include "testing/gtest/include/gtest/gtest.h"
8
9using base::StringValue;
10using base::Value;
11
12namespace {
13
14class MockPrefStoreObserver : public PrefStore::Observer {
15 public:
16 explicit MockPrefStoreObserver(DefaultPrefStore* pref_store);
dcheng56488182014-10-21 10:54:5117 ~MockPrefStoreObserver() override;
[email protected]f8f8b672013-03-07 09:12:5818
19 int change_count() {
20 return change_count_;
21 }
22
23 // PrefStore::Observer implementation:
dcheng56488182014-10-21 10:54:5124 void OnPrefValueChanged(const std::string& key) override;
25 void OnInitializationCompleted(bool succeeded) override {}
[email protected]f8f8b672013-03-07 09:12:5826
27 private:
28 DefaultPrefStore* pref_store_;
29
30 int change_count_;
31
32 DISALLOW_COPY_AND_ASSIGN(MockPrefStoreObserver);
33};
34
35MockPrefStoreObserver::MockPrefStoreObserver(DefaultPrefStore* pref_store)
36 : pref_store_(pref_store), change_count_(0) {
37 pref_store_->AddObserver(this);
38}
39
40MockPrefStoreObserver::~MockPrefStoreObserver() {
41 pref_store_->RemoveObserver(this);
42}
43
44void MockPrefStoreObserver::OnPrefValueChanged(const std::string& key) {
45 change_count_++;
46}
47
48} // namespace
49
50TEST(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,
dcheng5f043bc2016-04-22 19:09:0657 std::unique_ptr<Value>(new StringValue("foo")));
[email protected]f8f8b672013-03-07 09:12:5858 EXPECT_EQ(0, observer.change_count());
59
60 // Replacing the default value should send a change notification...
dcheng5f043bc2016-04-22 19:09:0661 pref_store->ReplaceDefaultValue(
62 kPrefKey, std::unique_ptr<Value>(new StringValue("bar")));
[email protected]f8f8b672013-03-07 09:12:5863 EXPECT_EQ(1, observer.change_count());
64
65 // But only if the value actually changed.
dcheng5f043bc2016-04-22 19:09:0666 pref_store->ReplaceDefaultValue(
67 kPrefKey, std::unique_ptr<Value>(new StringValue("bar")));
[email protected]f8f8b672013-03-07 09:12:5868 EXPECT_EQ(1, observer.change_count());
69}
70