summaryrefslogtreecommitdiffstats
path: root/chrome/browser/pref_notifier_unittest.cc
blob: a6c2da94870c709faedf35b3bb2b17bcbcae30a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/pref_notifier.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/pref_value_store.h"
#include "chrome/common/notification_observer.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/notification_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"


namespace {

const char kChangedPref[] = "changed_pref";
const char kUnchangedPref[] = "unchanged_pref";

bool DetailsAreChangedPref(const Details<std::string>& details) {
  std::string* string_in = Details<std::string>(details).ptr();
  return strcmp(string_in->c_str(), kChangedPref) == 0;
}

// Test PrefNotifier that allows tracking of observers and notifications.
class MockPrefNotifier : public PrefNotifier {
 public:
  MockPrefNotifier(PrefService* prefs, PrefValueStore* value_store)
      : PrefNotifier(prefs, value_store) {}

  virtual ~MockPrefNotifier() {}

  MOCK_METHOD1(FireObservers, void(const char* path));

  void RealFireObservers(const char* path) {
    PrefNotifier::FireObservers(path);
  }

  size_t CountObserver(const char* path, NotificationObserver* obs) {
    PrefObserverMap::const_iterator observer_iterator =
        pref_observers()->find(path);
    if (observer_iterator == pref_observers()->end())
      return false;

    NotificationObserverList* observer_list = observer_iterator->second;
    NotificationObserverList::Iterator it(*observer_list);
    NotificationObserver* existing_obs;
    size_t count = 0;
    while ((existing_obs = it.GetNext()) != NULL) {
      if (existing_obs == obs)
        count++;
    }

    return count;
  }
};

// Mock PrefValueStore that has no PrefStores and injects a simpler
// PrefHasChanged().
class MockPrefValueStore : public PrefValueStore {
 public:
  MockPrefValueStore() : PrefValueStore(NULL, NULL, NULL, NULL, NULL) {}

  virtual ~MockPrefValueStore() {}

  // This mock version returns true if the pref path starts with "changed".
  virtual bool PrefHasChanged(const char* path, const Value* old_value) {
    std::string path_str(path);
    if (path_str.compare(0, 7, "changed") == 0)
      return true;
    return false;
  }
};

// Mock PrefService that allows the PrefNotifier to be injected.
class MockPrefService : public PrefService {
 public:
  explicit MockPrefService(PrefValueStore* pref_value_store)
      : PrefService(pref_value_store) {}

  void SetPrefNotifier(PrefNotifier* notifier) {
    pref_notifier_.reset(notifier);
  }
};

// Mock PrefObserver that verifies notifications.
class MockPrefObserver : public NotificationObserver {
 public:
  virtual ~MockPrefObserver() {}

  MOCK_METHOD3(Observe, void(NotificationType type,
                             const NotificationSource& source,
                             const NotificationDetails& details));
};

// Test fixture class.
class PrefNotifierTest : public testing::Test {
 protected:
  virtual void SetUp() {
    value_store_ = new MockPrefValueStore;
    pref_service_.reset(new MockPrefService(value_store_));
    notifier_ = new MockPrefNotifier(pref_service_.get(), value_store_);
    pref_service_->SetPrefNotifier(notifier_);
  }

  // The PrefService takes ownership of the PrefValueStore and PrefNotifier.
  PrefValueStore* value_store_;
  MockPrefNotifier* notifier_;
  scoped_ptr<MockPrefService> pref_service_;

  MockPrefObserver obs1_;
  MockPrefObserver obs2_;
};


TEST_F(PrefNotifierTest, OnPreferenceSet) {
  EXPECT_CALL(*notifier_, FireObservers(kChangedPref));
  EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);
  notifier_->OnPreferenceSet(kChangedPref, NULL);
  notifier_->OnPreferenceSet(kUnchangedPref, NULL);
}

TEST_F(PrefNotifierTest, OnUserPreferenceSet) {
  EXPECT_CALL(*notifier_, FireObservers(kChangedPref));
  EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);
  notifier_->OnUserPreferenceSet(kChangedPref, NULL);
  notifier_->OnUserPreferenceSet(kUnchangedPref, NULL);
}

TEST_F(PrefNotifierTest, AddAndRemovePrefObservers) {
  const char pref_name[] = "homepage";
  const char pref_name2[] = "proxy";

  notifier_->AddPrefObserver(pref_name, &obs1_);
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  // Re-adding the same observer for the same pref doesn't change anything.
  // Skip this in debug mode, since it hits a DCHECK and death tests aren't
  // thread-safe.
#if defined(NDEBUG)
  notifier_->AddPrefObserver(pref_name, &obs1_);
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
#endif  // NDEBUG

  // Ensure that we can add the same observer to a different pref.
  notifier_->AddPrefObserver(pref_name2, &obs1_);
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  // Ensure that we can add another observer to the same pref.
  notifier_->AddPrefObserver(pref_name, &obs2_);
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  // Ensure that we can remove all observers, and that removing a non-existent
  // observer is harmless.
  notifier_->RemovePrefObserver(pref_name, &obs1_);
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  notifier_->RemovePrefObserver(pref_name, &obs2_);
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  notifier_->RemovePrefObserver(pref_name, &obs1_);
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(1u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));

  notifier_->RemovePrefObserver(pref_name2, &obs1_);
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs1_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name, &obs2_));
  ASSERT_EQ(0u, notifier_->CountObserver(pref_name2, &obs2_));
}

TEST_F(PrefNotifierTest, FireObservers) {
  using testing::_;
  using testing::Field;
  using testing::Invoke;
  using testing::Mock;
  using testing::Truly;

  // Tell googlemock to pass calls to the mock's "back door" too.
  ON_CALL(*notifier_, FireObservers(_))
      .WillByDefault(Invoke(notifier_, &MockPrefNotifier::RealFireObservers));
  EXPECT_CALL(*notifier_, FireObservers(kChangedPref)).Times(4);
  EXPECT_CALL(*notifier_, FireObservers(kUnchangedPref)).Times(0);

  notifier_->AddPrefObserver(kChangedPref, &obs1_);
  notifier_->AddPrefObserver(kUnchangedPref, &obs1_);

  EXPECT_CALL(obs1_, Observe(
      Field(&NotificationType::value, NotificationType::PREF_CHANGED),
      Source<PrefService>(pref_service_.get()),
      Truly(DetailsAreChangedPref)));
  EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
  notifier_->OnPreferenceSet(kChangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
  notifier_->OnPreferenceSet(kUnchangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  notifier_->AddPrefObserver(kChangedPref, &obs2_);
  notifier_->AddPrefObserver(kUnchangedPref, &obs2_);

  EXPECT_CALL(obs1_, Observe(
      Field(&NotificationType::value, NotificationType::PREF_CHANGED),
      Source<PrefService>(pref_service_.get()),
      Truly(DetailsAreChangedPref)));
  EXPECT_CALL(obs2_, Observe(
      Field(&NotificationType::value, NotificationType::PREF_CHANGED),
      Source<PrefService>(pref_service_.get()),
      Truly(DetailsAreChangedPref)));
  notifier_->OnPreferenceSet(kChangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
  notifier_->OnPreferenceSet(kUnchangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  // Make sure removing an observer from one pref doesn't affect anything else.
  notifier_->RemovePrefObserver(kChangedPref, &obs1_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(
      Field(&NotificationType::value, NotificationType::PREF_CHANGED),
      Source<PrefService>(pref_service_.get()),
      Truly(DetailsAreChangedPref)));
  notifier_->OnPreferenceSet(kChangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
  notifier_->OnPreferenceSet(kUnchangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  // Make sure removing an observer entirely doesn't affect anything else.
  notifier_->RemovePrefObserver(kUnchangedPref, &obs1_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(
      Field(&NotificationType::value, NotificationType::PREF_CHANGED),
      Source<PrefService>(pref_service_.get()),
      Truly(DetailsAreChangedPref)));
  notifier_->OnPreferenceSet(kChangedPref, NULL);
  Mock::VerifyAndClearExpectations(&obs1_);
  Mock::VerifyAndClearExpectations(&obs2_);

  EXPECT_CALL(obs1_, Observe(_, _, _)).Times(0);
  EXPECT_CALL(obs2_, Observe(_, _, _)).Times(0);
  notifier_->OnPreferenceSet(kUnchangedPref, NULL);

  notifier_->RemovePrefObserver(kChangedPref, &obs2_);
  notifier_->RemovePrefObserver(kUnchangedPref, &obs2_);
}

}  // namespace