pref_change_registrar_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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 "chrome/browser/prefs/pref_change_registrar.h"
6#include "chrome/common/notification_details.h"
7#include "chrome/common/notification_observer.h"
8#include "chrome/common/notification_source.h"
9#include "chrome/common/pref_names.h"
10#include "chrome/test/testing_pref_service.h"
11#include "testing/gmock/include/gmock/gmock.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14using testing::Mock;
15using testing::Eq;
16
17// A mock provider that allows us to capture pref observer changes.
18class MockPrefService : public TestingPrefService {
19 public:
20  MockPrefService() {}
21  virtual ~MockPrefService() {};
22
23  MOCK_METHOD2(AddPrefObserver, void(const char*, NotificationObserver*));
24  MOCK_METHOD2(RemovePrefObserver, void(const char*, NotificationObserver*));
25};
26
27// A mock observer used as a pref observer
28class MockObserver : public NotificationObserver {
29 public:
30  MOCK_METHOD3(Observe, void(NotificationType, const NotificationSource& source,
31               const NotificationDetails& details));
32};
33
34class PrefChangeRegistrarTest : public testing::Test {
35 public:
36  PrefChangeRegistrarTest() {}
37  virtual ~PrefChangeRegistrarTest() {}
38
39 protected:
40  virtual void SetUp();
41
42  NotificationObserver* observer() const { return observer_.get(); }
43  MockPrefService* service() const { return service_.get(); }
44
45 private:
46  scoped_ptr<MockPrefService> service_;
47  scoped_ptr<MockObserver> observer_;
48};
49
50void PrefChangeRegistrarTest::SetUp() {
51  service_.reset(new MockPrefService());
52  observer_.reset(new MockObserver());
53}
54
55TEST_F(PrefChangeRegistrarTest, AddAndRemove) {
56  PrefChangeRegistrar registrar;
57  registrar.Init(service());
58
59  // Test adding.
60  EXPECT_CALL(*service(),
61              AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
62  EXPECT_CALL(*service(),
63              AddPrefObserver(Eq(std::string("test.pref.2")), observer()));
64  registrar.Add("test.pref.1", observer());
65  registrar.Add("test.pref.2", observer());
66  EXPECT_FALSE(registrar.IsEmpty());
67
68  // Test removing.
69  Mock::VerifyAndClearExpectations(service());
70  EXPECT_CALL(*service(),
71              RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
72  EXPECT_CALL(*service(),
73              RemovePrefObserver(Eq(std::string("test.pref.2")), observer()));
74  registrar.Remove("test.pref.1", observer());
75  registrar.Remove("test.pref.2", observer());
76  EXPECT_TRUE(registrar.IsEmpty());
77
78  // Explicitly check the expectations now to make sure that the Removes
79  // worked (rather than the registrar destructor doing the work).
80  Mock::VerifyAndClearExpectations(service());
81}
82
83TEST_F(PrefChangeRegistrarTest, AutoRemove) {
84  PrefChangeRegistrar registrar;
85  registrar.Init(service());
86
87  // Setup of auto-remove.
88  EXPECT_CALL(*service(),
89              AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
90  registrar.Add("test.pref.1", observer());
91  Mock::VerifyAndClearExpectations(service());
92  EXPECT_FALSE(registrar.IsEmpty());
93
94  // Test auto-removing.
95  EXPECT_CALL(*service(),
96              RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
97}
98
99TEST_F(PrefChangeRegistrarTest, RemoveAll) {
100  PrefChangeRegistrar registrar;
101  registrar.Init(service());
102
103  EXPECT_CALL(*service(),
104              AddPrefObserver(Eq(std::string("test.pref.1")), observer()));
105  EXPECT_CALL(*service(),
106              AddPrefObserver(Eq(std::string("test.pref.2")), observer()));
107  registrar.Add("test.pref.1", observer());
108  registrar.Add("test.pref.2", observer());
109  Mock::VerifyAndClearExpectations(service());
110
111  EXPECT_CALL(*service(),
112              RemovePrefObserver(Eq(std::string("test.pref.1")), observer()));
113  EXPECT_CALL(*service(),
114              RemovePrefObserver(Eq(std::string("test.pref.2")), observer()));
115  registrar.RemoveAll();
116  EXPECT_TRUE(registrar.IsEmpty());
117
118  // Explicitly check the expectations now to make sure that the RemoveAll
119  // worked (rather than the registrar destructor doing the work).
120  Mock::VerifyAndClearExpectations(service());
121}
122