test-private-browsing.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 { Ci } = require('chrome');
  6. const { isPrivateBrowsingSupported } = require('sdk/self');
  7. const tabs = require('sdk/tabs');
  8. const { browserWindows: windows } = require('sdk/windows');
  9. const { isPrivate } = require('sdk/private-browsing');
  10. const { getOwnerWindow } = require('sdk/private-browsing/window/utils');
  11. const { is } = require('sdk/system/xul-app');
  12. const { isWindowPBSupported, isTabPBSupported } = require('sdk/private-browsing/utils');
  13. const TAB_URL = 'data:text/html;charset=utf-8,TEST-TAB';
  14. exports.testIsPrivateBrowsingTrue = function(assert) {
  15. assert.ok(isPrivateBrowsingSupported,
  16. 'isPrivateBrowsingSupported property is true');
  17. };
  18. // test tab.open with isPrivate: true
  19. // test isPrivate on a tab
  20. // test getOwnerWindow on windows and tabs
  21. exports.testGetOwnerWindow = function(assert, done) {
  22. let window = windows.activeWindow;
  23. let chromeWindow = getOwnerWindow(window);
  24. assert.ok(chromeWindow instanceof Ci.nsIDOMWindow, 'associated window is found');
  25. tabs.open({
  26. url: 'about:blank',
  27. isPrivate: true,
  28. onOpen: function(tab) {
  29. // test that getOwnerWindow works as expected
  30. if (is('Fennec')) {
  31. assert.notStrictEqual(chromeWindow, getOwnerWindow(tab));
  32. assert.ok(getOwnerWindow(tab) instanceof Ci.nsIDOMWindow);
  33. }
  34. else {
  35. if (isWindowPBSupported) {
  36. assert.notStrictEqual(chromeWindow,
  37. getOwnerWindow(tab),
  38. 'associated window is not the same for window and window\'s tab');
  39. }
  40. else {
  41. assert.strictEqual(chromeWindow,
  42. getOwnerWindow(tab),
  43. 'associated window is the same for window and window\'s tab');
  44. }
  45. }
  46. let pbSupported = isTabPBSupported || isWindowPBSupported;
  47. // test that the tab is private if it should be
  48. assert.equal(isPrivate(tab), pbSupported);
  49. assert.equal(isPrivate(getOwnerWindow(tab)), pbSupported);
  50. tab.close(function() done());
  51. }
  52. });
  53. };
  54. // test that it is possible to open a private tab
  55. exports.testTabOpenPrivate = function(assert, done) {
  56. tabs.open({
  57. url: TAB_URL,
  58. isPrivate: true,
  59. onReady: function(tab) {
  60. assert.equal(tab.url, TAB_URL, 'opened correct tab');
  61. assert.equal(isPrivate(tab), (isWindowPBSupported || isTabPBSupported));
  62. tab.close(function() {
  63. done();
  64. });
  65. }
  66. });
  67. }
  68. // test that it is possible to open a non private tab
  69. exports.testTabOpenPrivateDefault = function(assert, done) {
  70. tabs.open({
  71. url: TAB_URL,
  72. onReady: function(tab) {
  73. assert.equal(tab.url, TAB_URL, 'opened correct tab');
  74. assert.equal(isPrivate(tab), false);
  75. tab.close(function() {
  76. done();
  77. });
  78. }
  79. });
  80. }
  81. // test that it is possible to open a non private tab in explicit case
  82. exports.testTabOpenPrivateOffExplicit = function(assert, done) {
  83. tabs.open({
  84. url: TAB_URL,
  85. isPrivate: false,
  86. onReady: function(tab) {
  87. assert.equal(tab.url, TAB_URL, 'opened correct tab');
  88. assert.equal(isPrivate(tab), false);
  89. tab.close(function() {
  90. done();
  91. });
  92. }
  93. });
  94. }
  95. // test windows.open with isPrivate: true
  96. // test isPrivate on a window
  97. if (!is('Fennec')) {
  98. // test that it is possible to open a private window
  99. exports.testWindowOpenPrivate = function(assert, done) {
  100. windows.open({
  101. url: TAB_URL,
  102. isPrivate: true,
  103. onOpen: function(window) {
  104. let tab = window.tabs[0];
  105. tab.once('ready', function() {
  106. assert.equal(tab.url, TAB_URL, 'opened correct tab');
  107. assert.equal(isPrivate(tab), isWindowPBSupported, 'tab is private');
  108. window.close(function() {
  109. done();
  110. });
  111. });
  112. }
  113. });
  114. };
  115. exports.testIsPrivateOnWindowOn = function(assert, done) {
  116. windows.open({
  117. isPrivate: true,
  118. onOpen: function(window) {
  119. assert.equal(isPrivate(window), isWindowPBSupported, 'isPrivate for a window is true when it should be');
  120. assert.equal(isPrivate(window.tabs[0]), isWindowPBSupported, 'isPrivate for a tab is false when it should be');
  121. window.close(done);
  122. }
  123. });
  124. };
  125. exports.testIsPrivateOnWindowOffImplicit = function(assert, done) {
  126. windows.open({
  127. onOpen: function(window) {
  128. assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
  129. assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
  130. window.close(done);
  131. }
  132. })
  133. }
  134. exports.testIsPrivateOnWindowOffExplicit = function(assert, done) {
  135. windows.open({
  136. isPrivate: false,
  137. onOpen: function(window) {
  138. assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
  139. assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
  140. window.close(done);
  141. }
  142. })
  143. }
  144. }