windows.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { pb, pbUtils } = require('./helper');
  6. const { onFocus, openDialog, open } = require('sdk/window/utils');
  7. const { open: openPromise, close, focus, promise } = require('sdk/window/helpers');
  8. const { isPrivate } = require('sdk/private-browsing');
  9. const { browserWindows: windows } = require('sdk/windows');
  10. const { defer } = require('sdk/core/promise');
  11. const tabs = require('sdk/tabs');
  12. // test openDialog() from window/utils with private option
  13. // test isActive state in pwpb case
  14. // test isPrivate on ChromeWindow
  15. exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
  16. let win = openDialog({
  17. private: true
  18. });
  19. promise(win, 'DOMContentLoaded').then(function onload() {
  20. assert.equal(pbUtils.getMode(win),
  21. true, 'Newly opened window is in PB mode');
  22. assert.ok(isPrivate(win), 'isPrivate(window) is true');
  23. assert.equal(pb.isActive, false, 'PB mode is not active');
  24. close(win).then(function() {
  25. assert.equal(pb.isActive, false, 'PB mode is not active');
  26. done();
  27. });
  28. });
  29. }
  30. // test open() from window/utils with private feature
  31. // test isActive state in pwpb case
  32. // test isPrivate on ChromeWindow
  33. exports.testPerWindowPrivateBrowsingGetter = function(assert, done) {
  34. let win = open('chrome://browser/content/browser.xul', {
  35. features: {
  36. private: true
  37. }
  38. });
  39. promise(win, 'DOMContentLoaded').then(function onload() {
  40. assert.equal(pbUtils.getMode(win),
  41. true, 'Newly opened window is in PB mode');
  42. assert.ok(isPrivate(win), 'isPrivate(window) is true');
  43. assert.equal(pb.isActive, false, 'PB mode is not active');
  44. close(win).then(function() {
  45. assert.equal(pb.isActive, false, 'PB mode is not active');
  46. done();
  47. });
  48. });
  49. }
  50. exports.testIsPrivateOnWindowOpen = function(assert, done) {
  51. windows.open({
  52. isPrivate: true,
  53. onOpen: function(window) {
  54. assert.equal(isPrivate(window), false, 'isPrivate for a window is true when it should be');
  55. assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
  56. window.close(done);
  57. }
  58. });
  59. }
  60. exports.testIsPrivateOnWindowOpenFromPrivate = function(assert, done) {
  61. // open a private window
  62. openPromise(null, {
  63. features: {
  64. private: true,
  65. chrome: true,
  66. titlebar: true,
  67. toolbar: true
  68. }
  69. }).then(focus).then(function(window) {
  70. let { promise, resolve } = defer();
  71. assert.equal(isPrivate(window), true, 'the only open window is private');
  72. windows.open({
  73. url: 'about:blank',
  74. onOpen: function(w) {
  75. assert.equal(isPrivate(w), false, 'new test window is not private');
  76. w.close(function() resolve(window));
  77. }
  78. });
  79. return promise;
  80. }).then(close).
  81. then(done, assert.fail);
  82. };
  83. exports.testOpenTabWithPrivateWindow = function(assert, done) {
  84. function start() {
  85. openPromise(null, {
  86. features: {
  87. private: true,
  88. toolbar: true
  89. }
  90. }).then(focus).then(function(window) {
  91. let { promise, resolve } = defer();
  92. assert.equal(isPrivate(window), true, 'the focused window is private');
  93. tabs.open({
  94. url: 'about:blank',
  95. onOpen: function(tab) {
  96. assert.equal(isPrivate(tab), false, 'the opened tab is not private');
  97. // not closing this tab on purpose.. for now...
  98. // we keep this tab open because we closed all windows
  99. // and must keep a non-private window open at end of this test for next ones.
  100. resolve(window);
  101. }
  102. });
  103. return promise;
  104. }).then(close).then(done, assert.fail);
  105. }
  106. (function closeWindows() {
  107. if (windows.length > 0) {
  108. return windows.activeWindow.close(closeWindows);
  109. }
  110. assert.pass('all pre test windows have been closed');
  111. return start();
  112. })()
  113. };
  114. exports.testIsPrivateOnWindowOff = function(assert, done) {
  115. windows.open({
  116. onOpen: function(window) {
  117. assert.equal(isPrivate(window), false, 'isPrivate for a window is false when it should be');
  118. assert.equal(isPrivate(window.tabs[0]), false, 'isPrivate for a tab is false when it should be');
  119. window.close(done);
  120. }
  121. })
  122. }