/* -*- 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/. */
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; staticint sTotalObservations;
RefPtr<TestObserver> a = new TestObserver(u"A"_ns);
RefPtr<TestObserver> b = new TestObserver(u"B"_ns);
RefPtr<TestObserver> c = new TestObserver(u"C"_ns);
// 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));
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));
}
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);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.