global.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. const timer = require("sdk/timers");
  6. const { LoaderWithHookedConsole, deactivate, pb, pbUtils } = require("./helper");
  7. const tabs = require("sdk/tabs");
  8. const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
  9. const { set: setPref } = require("sdk/preferences/service");
  10. const DEPRECATE_PREF = "devtools.errorconsole.deprecation_warnings";
  11. exports["test activate private mode via handler"] = function(assert, done) {
  12. function onReady(tab) {
  13. if (tab.url == "about:robots")
  14. tab.close(function() pb.activate());
  15. }
  16. function cleanup(tab) {
  17. if (tab.url == "about:") {
  18. tabs.removeListener("ready", cleanup);
  19. tab.close(function onClose() {
  20. done();
  21. });
  22. }
  23. }
  24. tabs.on("ready", onReady);
  25. pb.once("start", function onStart() {
  26. assert.pass("private mode was activated");
  27. pb.deactivate();
  28. });
  29. pb.once("stop", function onStop() {
  30. assert.pass("private mode was deactivated");
  31. tabs.removeListener("ready", onReady);
  32. tabs.on("ready", cleanup);
  33. });
  34. tabs.once("open", function onOpen() {
  35. tabs.open("about:robots");
  36. });
  37. tabs.open("about:");
  38. };
  39. // tests that isActive has the same value as the private browsing service
  40. // expects
  41. exports.testGetIsActive = function (assert) {
  42. assert.equal(pb.isActive, false,
  43. "private-browsing.isActive is correct without modifying PB service");
  44. assert.equal(pb.isPrivate(), false,
  45. "private-browsing.sPrivate() is correct without modifying PB service");
  46. pb.once("start", function() {
  47. assert.ok(pb.isActive,
  48. "private-browsing.isActive is correct after modifying PB service");
  49. assert.ok(pb.isPrivate(),
  50. "private-browsing.sPrivate() is correct after modifying PB service");
  51. // Switch back to normal mode.
  52. pb.deactivate();
  53. });
  54. pb.activate();
  55. pb.once("stop", function() {
  56. assert.ok(!pb.isActive,
  57. "private-browsing.isActive is correct after modifying PB service");
  58. assert.ok(!pb.isPrivate(),
  59. "private-browsing.sPrivate() is correct after modifying PB service");
  60. test.done();
  61. });
  62. };
  63. exports.testStart = function(assert, done) {
  64. pb.on("start", function onStart() {
  65. assert.equal(this, pb, "`this` should be private-browsing module");
  66. assert.ok(pbUtils.getMode(),
  67. 'private mode is active when "start" event is emitted');
  68. assert.ok(pb.isActive,
  69. '`isActive` is `true` when "start" event is emitted');
  70. assert.ok(pb.isPrivate(),
  71. '`isPrivate` is `true` when "start" event is emitted');
  72. pb.removeListener("start", onStart);
  73. deactivate(done);
  74. });
  75. pb.activate();
  76. };
  77. exports.testStop = function(assert, done) {
  78. pb.once("stop", function onStop() {
  79. assert.equal(this, pb, "`this` should be private-browsing module");
  80. assert.equal(pbUtils.getMode(), false,
  81. "private mode is disabled when stop event is emitted");
  82. assert.equal(pb.isActive, false,
  83. "`isActive` is `false` when stop event is emitted");
  84. assert.equal(pb.isPrivate(), false,
  85. "`isPrivate()` is `false` when stop event is emitted");
  86. done();
  87. });
  88. pb.activate();
  89. pb.once("start", function() {
  90. pb.deactivate();
  91. });
  92. };
  93. exports.testBothListeners = function(assert, done) {
  94. let stop = false;
  95. let start = false;
  96. function onStop() {
  97. assert.equal(stop, false,
  98. "stop callback must be called only once");
  99. assert.equal(pbUtils.getMode(), false,
  100. "private mode is disabled when stop event is emitted");
  101. assert.equal(pb.isActive, false,
  102. "`isActive` is `false` when stop event is emitted");
  103. assert.equal(pb.isPrivate(), false,
  104. "`isPrivate()` is `false` when stop event is emitted");
  105. pb.on("start", finish);
  106. pb.removeListener("start", onStart);
  107. pb.removeListener("start", onStart2);
  108. pb.activate();
  109. stop = true;
  110. }
  111. function onStart() {
  112. assert.equal(false, start,
  113. "stop callback must be called only once");
  114. assert.ok(pbUtils.getMode(),
  115. "private mode is active when start event is emitted");
  116. assert.ok(pb.isActive,
  117. "`isActive` is `true` when start event is emitted");
  118. assert.ok(pb.isPrivate(),
  119. "`isPrivate()` is `true` when start event is emitted");
  120. pb.on("stop", onStop);
  121. pb.deactivate();
  122. start = true;
  123. }
  124. function onStart2() {
  125. assert.ok(start, "start listener must be called already");
  126. assert.equal(false, stop, "stop callback must not be called yet");
  127. }
  128. function finish() {
  129. assert.ok(pbUtils.getMode(), true,
  130. "private mode is active when start event is emitted");
  131. assert.ok(pb.isActive,
  132. "`isActive` is `true` when start event is emitted");
  133. assert.ok(pb.isPrivate(),
  134. "`isPrivate()` is `true` when start event is emitted");
  135. pb.removeListener("start", finish);
  136. pb.removeListener("stop", onStop);
  137. pb.deactivate();
  138. pb.once("stop", function () {
  139. assert.equal(pbUtils.getMode(), false);
  140. assert.equal(pb.isActive, false);
  141. assert.equal(pb.isPrivate(), false);
  142. done();
  143. });
  144. }
  145. pb.on("start", onStart);
  146. pb.on("start", onStart2);
  147. pb.activate();
  148. };
  149. exports.testAutomaticUnload = function(assert, done) {
  150. setPref(DEPRECATE_PREF, true);
  151. // Create another private browsing instance and unload it
  152. let { loader, errors } = LoaderWithHookedConsole(module);
  153. let pb2 = loader.require("sdk/private-browsing");
  154. let called = false;
  155. pb2.on("start", function onStart() {
  156. called = true;
  157. assert.fail("should not be called:x");
  158. });
  159. loader.unload();
  160. // Then switch to private mode in order to check that the previous instance
  161. // is correctly destroyed
  162. pb.once("start", function onStart() {
  163. timer.setTimeout(function () {
  164. assert.ok(!called,
  165. "First private browsing instance is destroyed and inactive");
  166. // Must reset to normal mode, so that next test starts with it.
  167. deactivate(function() {
  168. assert.ok(errors.length, 0, "should have been 1 deprecation error");
  169. done();
  170. });
  171. }, 0);
  172. });
  173. pb.activate();
  174. };
  175. exports.testUnloadWhileActive = function(assert, done) {
  176. let called = false;
  177. let { loader, errors } = LoaderWithHookedConsole(module);
  178. let pb2 = loader.require("sdk/private-browsing");
  179. let ul = loader.require("sdk/system/unload");
  180. let unloadHappened = false;
  181. ul.when(function() {
  182. unloadHappened = true;
  183. timer.setTimeout(function() {
  184. pb.deactivate();
  185. });
  186. });
  187. pb2.once("start", function() {
  188. loader.unload();
  189. });
  190. pb2.once("stop", function() {
  191. called = true;
  192. assert.ok(unloadHappened, "the unload event should have already occurred.");
  193. assert.fail("stop should not have been fired");
  194. });
  195. pb.once("stop", function() {
  196. assert.ok(!called, "stop was not called on unload");
  197. assert.ok(errors.length, 2, "should have been 2 deprecation errors");
  198. done();
  199. });
  200. pb.activate();
  201. };
  202. exports.testIgnoreWindow = function(assert, done) {
  203. let window = getMostRecentBrowserWindow();
  204. pb.once('start', function() {
  205. assert.ok(isWindowPrivate(window), 'window is private');
  206. assert.ok(!pbUtils.ignoreWindow(window), 'window is not ignored');
  207. pb.once('stop', done);
  208. pb.deactivate();
  209. });
  210. pb.activate();
  211. };