test_bug336682.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Helper functions for online/offline events tests.
  3. *
  4. * Any copyright is dedicated to the Public Domain.
  5. * http://creativecommons.org/licenses/publicdomain/
  6. */
  7. var gState = 0;
  8. /**
  9. * After all the on/offline handlers run,
  10. * gState is expected to be equal to MAX_STATE.
  11. */
  12. var MAX_STATE;
  13. function trace(text) {
  14. var t = text.replace(/&/g, "&" + "amp;").replace(/</g, "&" + "lt;") + "<br>";
  15. //document.getElementById("display").innerHTML += t;
  16. }
  17. // window.ononline and window.onclick shouldn't work
  18. // Right now, <body ononline=...> sets window.ononline (bug 380618)
  19. // When these start passing, be sure to uncomment the code inside if(0) below.
  20. todo(typeof window.ononline == "undefined",
  21. "window.ononline should be undefined at this point");
  22. todo(typeof window.onoffline == "undefined",
  23. "window.onoffline should be undefined at this point");
  24. if (0) {
  25. window.ononline = function() {
  26. ok(false, "window.ononline shouldn't be called");
  27. }
  28. window.onoffline = function() {
  29. ok(false, "window.onclick shouldn't be called");
  30. }
  31. }
  32. /**
  33. * Returns a handler function for an online/offline event. The returned handler
  34. * ensures the passed event object has expected properties and that the handler
  35. * is called at the right moment (according to the gState variable).
  36. * @param nameTemplate The string identifying the hanlder. '%1' in that
  37. * string will be replaced with the event name.
  38. * @param eventName 'online' or 'offline'
  39. * @param expectedStates an array listing the possible values of gState at the
  40. * moment the handler is called. The handler increases
  41. * gState by one before checking if it's listed in
  42. * expectedStates.
  43. */
  44. function makeHandler(nameTemplate, eventName, expectedStates) {
  45. return function(e) {
  46. var name = nameTemplate.replace(/%1/, eventName);
  47. ++gState;
  48. trace(name + ": gState=" + gState);
  49. ok(expectedStates.indexOf(gState) != -1,
  50. "handlers called in the right order: " + name + " is called, " +
  51. "gState=" + gState + ", expectedStates=" + expectedStates);
  52. ok(e.constructor == Event, "event should be an Event");
  53. ok(e.type == eventName, "event type should be " + eventName);
  54. ok(e.bubbles, "event should bubble");
  55. ok(!e.cancelable, "event should not be cancelable");
  56. ok(e.target == (document instanceof HTMLDocument
  57. ? document.body : document.documentElement),
  58. "the event target should be the body element");
  59. }
  60. }
  61. function doTest() {
  62. var iosvc = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
  63. .getService(SpecialPowers.Ci.nsIIOService2);
  64. iosvc.manageOfflineStatus = false;
  65. iosvc.offline = false;
  66. ok(navigator.onLine, "navigator.onLine should be true, since we've just " +
  67. "set nsIIOService.offline to false");
  68. gState = 0;
  69. trace("setting iosvc.offline = true");
  70. iosvc.offline = true;
  71. trace("done setting iosvc.offline = true");
  72. ok(!navigator.onLine,
  73. "navigator.onLine should be false when iosvc.offline == true");
  74. ok(gState == window.MAX_STATE,
  75. "offline event: all registered handlers should have been invoked, " +
  76. "actual: " + gState);
  77. gState = 0;
  78. trace("setting iosvc.offline = false");
  79. iosvc.offline = false;
  80. trace("done setting iosvc.offline = false");
  81. ok(navigator.onLine,
  82. "navigator.onLine should be true when iosvc.offline == false");
  83. ok(gState == window.MAX_STATE,
  84. "online event: all registered handlers should have been invoked, " +
  85. "actual: " + gState);
  86. }