browser_webconsole_notifications.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
  2. /* Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/ */
  4. "use strict";
  5. const TEST_URI = "data:text/html;charset=utf-8,<p>Web Console test for " +
  6. "notifications";
  7. add_task(function* () {
  8. yield loadTab(TEST_URI);
  9. let consoleOpened = promise.defer();
  10. let gotEvents = waitForEvents(consoleOpened.promise);
  11. yield openConsole().then(() => {
  12. consoleOpened.resolve();
  13. });
  14. yield gotEvents;
  15. });
  16. function waitForEvents(onConsoleOpened) {
  17. let deferred = promise.defer();
  18. function webConsoleCreated(id) {
  19. Services.obs.removeObserver(observer, "web-console-created");
  20. ok(HUDService.getHudReferenceById(id), "We have a hud reference");
  21. content.wrappedJSObject.console.log("adding a log message");
  22. }
  23. function webConsoleDestroyed(id) {
  24. Services.obs.removeObserver(observer, "web-console-destroyed");
  25. ok(!HUDService.getHudReferenceById(id), "We do not have a hud reference");
  26. executeSoon(deferred.resolve);
  27. }
  28. function webConsoleMessage(id, nodeID) {
  29. Services.obs.removeObserver(observer, "web-console-message-created");
  30. ok(id, "we have a console ID");
  31. is(typeof nodeID, "string", "message node id is a string");
  32. onConsoleOpened.then(closeConsole);
  33. }
  34. let observer = {
  35. QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
  36. observe: function observe(subject, topic, data) {
  37. subject = subject.QueryInterface(Ci.nsISupportsString);
  38. switch (topic) {
  39. case "web-console-created":
  40. webConsoleCreated(subject.data);
  41. break;
  42. case "web-console-destroyed":
  43. webConsoleDestroyed(subject.data);
  44. break;
  45. case "web-console-message-created":
  46. webConsoleMessage(subject, data);
  47. break;
  48. default:
  49. break;
  50. }
  51. },
  52. init: function init() {
  53. Services.obs.addObserver(this, "web-console-created", false);
  54. Services.obs.addObserver(this, "web-console-destroyed", false);
  55. Services.obs.addObserver(this, "web-console-message-created", false);
  56. }
  57. };
  58. observer.init();
  59. return deferred.promise;
  60. }