blob: bc40c4ca5c8b188509bf24b3d992d105103e6682 [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
[email protected]f8f8b672013-03-07 09:12:589using base::Value;
10
11namespace {
12
13class MockPrefStoreObserver : public PrefStore::Observer {
14 public:
15 explicit MockPrefStoreObserver(DefaultPrefStore* pref_store);
dcheng56488182014-10-21 10:54:5116 ~MockPrefStoreObserver() override;
[email protected]f8f8b672013-03-07 09:12:5817
18 int change_count() {
19 return change_count_;
20 }
21
22 // PrefStore::Observer implementation:
dcheng56488182014-10-21 10:54:5123 void OnPrefValueChanged(const std::string& key) override;
24 void OnInitializationCompleted(bool succeeded) override {}
[email protected]f8f8b672013-03-07 09:12:5825
26 private:
27 DefaultPrefStore* pref_store_;
28
29 int change_count_;
30
31 DISALLOW_COPY_AND_ASSIGN(MockPrefStoreObserver);
32};
33
34MockPrefStoreObserver::MockPrefStoreObserver(DefaultPrefStore* pref_store)
35 : pref_store_(pref_store), change_count_(0) {
36 pref_store_->AddObserver(this);
37}
38
39MockPrefStoreObserver::~MockPrefStoreObserver() {
40 pref_store_->RemoveObserver(this);
41}
42
43void MockPrefStoreObserver::OnPrefValueChanged(const std::string& key) {
44 change_count_++;
45}
46
47} // namespace
48
49TEST(DefaultPrefStoreTest, NotifyPrefValueChanged) {
50 scoped_refptr<DefaultPrefStore> pref_store(new DefaultPrefStore);
51 MockPrefStoreObserver observer(pref_store.get());
52 std::string kPrefKey("pref_key");
53
54 // Setting a default value shouldn't send a change notification.
Sylvain Defresne55fe8fa2019-02-05 09:50:5055 pref_store->SetDefaultValue(kPrefKey, Value("foo"));
[email protected]f8f8b672013-03-07 09:12:5856 EXPECT_EQ(0, observer.change_count());
57
58 // Replacing the default value should send a change notification...
Sylvain Defresne55fe8fa2019-02-05 09:50:5059 pref_store->ReplaceDefaultValue(kPrefKey, Value("bar"));
[email protected]f8f8b672013-03-07 09:12:5860 EXPECT_EQ(1, observer.change_count());
61
62 // But only if the value actually changed.
Sylvain Defresne55fe8fa2019-02-05 09:50:5063 pref_store->ReplaceDefaultValue(kPrefKey, Value("bar"));
[email protected]f8f8b672013-03-07 09:12:5864 EXPECT_EQ(1, observer.change_count());
65}
66