summaryrefslogtreecommitdiffstats
path: root/xpcom/tests/gtest/TestObserverService.cpp
blob: 4126815f1fc118a977b38334148cfde8dcd084c6 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsISupports.h"
#include "nsIObserverService.h"
#include "nsIObserver.h"
#include "nsISimpleEnumerator.h"
#include "nsComponentManagerUtils.h"

#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsWeakReference.h"

#include "mozilla/gtest/MozAssertions.h"
#include "mozilla/RefPtr.h"

#include "gtest/gtest.h"

static void testResult(nsresult rv) {
  EXPECT_TRUE(NS_SUCCEEDED(rv)) << "0x" << std::hex << (int)rv;
}

class TestObserver final : public nsIObserver, public nsSupportsWeakReference {
 public:
  explicit TestObserver(const nsAString& name)
      : mName(name), mObservations(0) {}
  NS_DECL_ISUPPORTS
  NS_DECL_NSIOBSERVER

  nsString mName;
  int mObservations;
  static int sTotalObservations;

  nsString mExpectedData;

 private:
  ~TestObserver() = default;
};

NS_IMPL_ISUPPORTS(TestObserver, nsIObserver, nsISupportsWeakReference)

int TestObserver::sTotalObservations;

NS_IMETHODIMP
TestObserver::Observe(nsISupports* aSubject, const char* aTopic,
                      const char16_t* someData) {
  mObservations++;
  sTotalObservations++;

  if (!mExpectedData.IsEmpty()) {
    EXPECT_TRUE(mExpectedData.Equals(someData));
  }

  return NS_OK;
}

static nsISupports* ToSupports(TestObserver* aObs) {
  return static_cast<nsIObserver*>(aObs);
}

static void TestExpectedCount(nsIObserverService* svc, const char* topic,
                              size_t expected) {
  nsCOMPtr<nsISimpleEnumerator> e;
  nsresult rv = svc->EnumerateObservers(topic, getter_AddRefs(e));
  testResult(rv);
  EXPECT_TRUE(e);

  bool hasMore = false;
  rv = e->HasMoreElements(&hasMore);
  testResult(rv);

  if (expected == 0) {
    EXPECT_FALSE(hasMore);
    return;
  }

  size_t count = 0;
  while (hasMore) {
    count++;

    // Grab the element.
    nsCOMPtr<nsISupports> supports;
    e->GetNext(getter_AddRefs(supports));
    ASSERT_TRUE(supports);

    // Move on.
    rv = e->HasMoreElements(&hasMore);
    testResult(rv);
  }

  EXPECT_EQ(count, expected);
}

TEST(ObserverService, Creation)
{
  nsresult rv;
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1", &rv);

  ASSERT_EQ(rv, NS_OK);
  ASSERT_TRUE(svc);
}

TEST(ObserverService, AddObserver)
{
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  // Add a strong ref.
  RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
  nsresult rv = svc->AddObserver(a, "Foo", false);
  testResult(rv);

  // Add a few weak ref.
  RefPtr<TestObserver> b = new TestObserver(u"B"_ns);
  rv = svc->AddObserver(b, "Bar", true);
  testResult(rv);
}

TEST(ObserverService, RemoveObserver)
{
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
  RefPtr<TestObserver> b = new TestObserver(u"B"_ns);
  RefPtr<TestObserver> c = new TestObserver(u"C"_ns);

  svc->AddObserver(a, "Foo", false);
  svc->AddObserver(b, "Foo", true);

  // Remove from non-existent topic.
  nsresult rv = svc->RemoveObserver(a, "Bar");
  ASSERT_NS_FAILED(rv);

  // Remove a.
  testResult(svc->RemoveObserver(a, "Foo"));

  // Remove b.
  testResult(svc->RemoveObserver(b, "Foo"));

  // Attempt to remove c.
  rv = svc->RemoveObserver(c, "Foo");
  ASSERT_NS_FAILED(rv);
}

TEST(ObserverService, EnumerateEmpty)
{
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  // Try with no observers.
  TestExpectedCount(svc, "A", 0);

  // Now add an observer and enumerate an unobserved topic.
  RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
  testResult(svc->AddObserver(a, "Foo", false));

  TestExpectedCount(svc, "A", 0);
}

TEST(ObserverService, Enumerate)
{
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  const size_t kFooCount = 10;
  for (size_t i = 0; i < kFooCount; i++) {
    RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
    testResult(svc->AddObserver(a, "Foo", false));
  }

  const size_t kBarCount = kFooCount / 2;
  for (size_t i = 0; i < kBarCount; i++) {
    RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
    testResult(svc->AddObserver(a, "Bar", false));
  }

  // Enumerate "Foo".
  TestExpectedCount(svc, "Foo", kFooCount);

  // Enumerate "Bar".
  TestExpectedCount(svc, "Bar", kBarCount);
}

TEST(ObserverService, EnumerateWeakRefs)
{
  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  const size_t kFooCount = 10;
  for (size_t i = 0; i < kFooCount; i++) {
    RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
    testResult(svc->AddObserver(a, "Foo", true));
  }

  // All refs are out of scope, expect enumeration to be empty.
  TestExpectedCount(svc, "Foo", 0);

  // Now test a mixture.
  for (size_t i = 0; i < kFooCount; i++) {
    RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
    RefPtr<TestObserver> b = new TestObserver(u"B"_ns);

    // Register a as weak for "Foo".
    testResult(svc->AddObserver(a, "Foo", true));

    // Register b as strong for "Foo".
    testResult(svc->AddObserver(b, "Foo", false));
  }

  // Expect the b instances to stick around.
  TestExpectedCount(svc, "Foo", kFooCount);

  // Now add a couple weak refs, but don't go out of scope.
  RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
  testResult(svc->AddObserver(a, "Foo", true));
  RefPtr<TestObserver> b = new TestObserver(u"B"_ns);
  testResult(svc->AddObserver(b, "Foo", true));

  // Expect all the observers from before and the two new ones.
  TestExpectedCount(svc, "Foo", kFooCount + 2);
}

TEST(ObserverService, TestNotify)
{
  nsCString topicA;
  topicA.Assign("topic-A");
  nsCString topicB;
  topicB.Assign("topic-B");

  nsCOMPtr<nsIObserverService> svc =
      do_CreateInstance("@mozilla.org/observer-service;1");

  RefPtr<TestObserver> aObserver = new TestObserver(u"Observer-A"_ns);
  RefPtr<TestObserver> bObserver = new TestObserver(u"Observer-B"_ns);

  // Add two observers for topicA.
  testResult(svc->AddObserver(aObserver, topicA.get(), false));
  testResult(svc->AddObserver(bObserver, topicA.get(), false));

  // Add one observer for topicB.
  testResult(svc->AddObserver(bObserver, topicB.get(), false));

  // Notify topicA.
  const char16_t* dataA = u"Testing Notify(observer-A, topic-A)";
  aObserver->mExpectedData = dataA;
  bObserver->mExpectedData = dataA;
  nsresult rv =
      svc->NotifyObservers(ToSupports(aObserver), topicA.get(), dataA);
  testResult(rv);
  ASSERT_EQ(aObserver->mObservations, 1);
  ASSERT_EQ(bObserver->mObservations, 1);

  // Notify topicB.
  const char16_t* dataB = u"Testing Notify(observer-B, topic-B)";
  bObserver->mExpectedData = dataB;
  rv = svc->NotifyObservers(ToSupports(bObserver), topicB.get(), dataB);
  testResult(rv);
  ASSERT_EQ(aObserver->mObservations, 1);
  ASSERT_EQ(bObserver->mObservations, 2);

  // Remove one of the topicA observers, make sure it's not notified.
  testResult(svc->RemoveObserver(aObserver, topicA.get()));

  // Notify topicA, only bObserver is expected to be notified.
  bObserver->mExpectedData = dataA;
  rv = svc->NotifyObservers(ToSupports(aObserver), topicA.get(), dataA);
  testResult(rv);
  ASSERT_EQ(aObserver->mObservations, 1);
  ASSERT_EQ(bObserver->mObservations, 3);

  // Remove the other topicA observer, make sure none are notified.
  testResult(svc->RemoveObserver(bObserver, topicA.get()));
  rv = svc->NotifyObservers(ToSupports(aObserver), topicA.get(), dataA);
  testResult(rv);
  ASSERT_EQ(aObserver->mObservations, 1);
  ASSERT_EQ(bObserver->mObservations, 3);
}