home *** CD-ROM | disk | FTP | other *** search
- function G_ObserverWrapper(topic, observeFunction) {
- this.debugZone = "observer";
- this.topic_ = topic;
- this.observeFunction_ = observeFunction;
- }
- G_ObserverWrapper.prototype.QueryInterface = function(iid) {
- if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver))
- return this;
- throw Components.results.NS_ERROR_NO_INTERFACE;
- }
- G_ObserverWrapper.prototype.observe = function(subject, topic, data) {
- if (topic == this.topic_)
- this.observeFunction_(subject, topic, data);
- }
- function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) {
- this.debugZone = "observerserviceobserver";
- this.topic_ = topic;
- this.observeFunction_ = observeFunction;
- this.onlyOnce_ = !!opt_onlyOnce;
- this.observer_ = new G_ObserverWrapper(this.topic_,
- BindToObject(this.observe_, this));
- this.observerService_ = Cc["@mozilla.org/observer-service;1"]
- .getService(Ci.nsIObserverService);
- this.observerService_.addObserver(this.observer_, this.topic_, false);
- }
- G_ObserverServiceObserver.prototype.unregister = function() {
- this.observerService_.removeObserver(this.observer_, this.topic_);
- }
- G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) {
- this.observeFunction_(subject, topic, data);
- if (this.onlyOnce_)
- this.unregister();
- }
- function TEST_G_Observer() {
- if (G_GDEBUG) {
- var z = "observer UNITTEST";
- G_debugService.enableZone(z);
- G_Debug(z, "Starting");
- var regularObserverRan = 0;
- var observerServiceObserverRan = 0;
- function regularObserver() {
- regularObserverRan++;
- };
- function observerServiceObserver() {
- observerServiceObserverRan++;
- };
- var service = Cc["@mozilla.org/observer-service;1"]
- .getService(Ci.nsIObserverService);
- var topic = "google-observer-test";
- var o1 = new G_ObserverWrapper(topic, regularObserver);
- service.addObserver(o1, topic, false);
- new G_ObserverServiceObserver(topic,
- observerServiceObserver, true /* once */);
- service.notifyObservers(null, topic, null);
- service.notifyObservers(null, topic, null);
- G_Assert(z, regularObserverRan == 2, "Regular observer broken");
- G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken");
- service.removeObserver(o1, topic);
- G_Debug(z, "PASSED");
- }
- }
-