home *** CD-ROM | disk | FTP | other *** search
/ Minami 83 / MINAMI83.iso / Extra / DivXInstaller.exe / $PLUGINSDIR / GoogleToolbarFirefox.msi / xpi / amulet-jslib / firefox / observer.js < prev    next >
Text File  |  2006-08-07  |  2KB  |  62 lines

  1. function G_ObserverWrapper(topic, observeFunction) {
  2. this.debugZone = "observer";
  3. this.topic_ = topic;
  4. this.observeFunction_ = observeFunction;
  5. }
  6. G_ObserverWrapper.prototype.QueryInterface = function(iid) {
  7. if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver))
  8. return this;
  9. throw Components.results.NS_ERROR_NO_INTERFACE;
  10. }
  11. G_ObserverWrapper.prototype.observe = function(subject, topic, data) {
  12. if (topic == this.topic_)
  13. this.observeFunction_(subject, topic, data);
  14. }
  15. function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) {
  16. this.debugZone = "observerserviceobserver";
  17. this.topic_ = topic;
  18. this.observeFunction_ = observeFunction;
  19. this.onlyOnce_ = !!opt_onlyOnce;
  20. this.observer_ = new G_ObserverWrapper(this.topic_,
  21. BindToObject(this.observe_, this));
  22. this.observerService_ = Cc["@mozilla.org/observer-service;1"]
  23. .getService(Ci.nsIObserverService);
  24. this.observerService_.addObserver(this.observer_, this.topic_, false);
  25. }
  26. G_ObserverServiceObserver.prototype.unregister = function() {
  27. this.observerService_.removeObserver(this.observer_, this.topic_);
  28. }
  29. G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) {
  30. this.observeFunction_(subject, topic, data);
  31. if (this.onlyOnce_)
  32. this.unregister();
  33. }
  34. function TEST_G_Observer() {
  35. if (G_GDEBUG) {
  36. var z = "observer UNITTEST";
  37. G_debugService.enableZone(z);
  38. G_Debug(z, "Starting");
  39. var regularObserverRan = 0;
  40. var observerServiceObserverRan = 0;
  41. function regularObserver() {
  42. regularObserverRan++;
  43. };
  44. function observerServiceObserver() {
  45. observerServiceObserverRan++;
  46. };
  47. var service = Cc["@mozilla.org/observer-service;1"]
  48. .getService(Ci.nsIObserverService);
  49. var topic = "google-observer-test";
  50. var o1 = new G_ObserverWrapper(topic, regularObserver);
  51. service.addObserver(o1, topic, false);
  52. new G_ObserverServiceObserver(topic,
  53. observerServiceObserver, true /* once */);
  54. service.notifyObservers(null, topic, null);
  55. service.notifyObservers(null, topic, null);
  56. G_Assert(z, regularObserverRan == 2, "Regular observer broken");
  57. G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken");
  58. service.removeObserver(o1, topic);
  59. G_Debug(z, "PASSED");
  60. }
  61. }
  62.