test-ui-sidebar-private-browsing.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. module.metadata = {
  6. 'engines': {
  7. 'Firefox': '*'
  8. }
  9. };
  10. const { Loader } = require('sdk/test/loader');
  11. const { show, hide } = require('sdk/ui/sidebar/actions');
  12. const { isShowing } = require('sdk/ui/sidebar/utils');
  13. const { getMostRecentBrowserWindow, isWindowPrivate } = require('sdk/window/utils');
  14. const { open, close, focus, promise: windowPromise } = require('sdk/window/helpers');
  15. const { setTimeout } = require('sdk/timers');
  16. const { isPrivate } = require('sdk/private-browsing');
  17. const { data } = require('sdk/self');
  18. const { URL } = require('sdk/url');
  19. const { BUILTIN_SIDEBAR_MENUITEMS, isSidebarShowing,
  20. getSidebarMenuitems, getExtraSidebarMenuitems, makeID, simulateCommand,
  21. simulateClick, isChecked } = require('./sidebar/utils');
  22. exports.testSideBarIsNotInNewPrivateWindows = function(assert, done) {
  23. const { Sidebar } = require('sdk/ui/sidebar');
  24. let testName = 'testSideBarIsNotInNewPrivateWindows';
  25. let sidebar = Sidebar({
  26. id: testName,
  27. title: testName,
  28. url: 'data:text/html;charset=utf-8,'+testName
  29. });
  30. let startWindow = getMostRecentBrowserWindow();
  31. let ele = startWindow.document.getElementById(makeID(testName));
  32. assert.ok(ele, 'sidebar element was added');
  33. open(null, { features: { private: true } }).then(function(window) {
  34. let ele = window.document.getElementById(makeID(testName));
  35. assert.ok(isPrivate(window), 'the new window is private');
  36. assert.equal(ele, null, 'sidebar element was not added');
  37. sidebar.destroy();
  38. assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
  39. assert.ok(!startWindow.document.getElementById(makeID(testName)), 'sidebar id DNE');
  40. close(window).then(done, assert.fail);
  41. })
  42. }
  43. exports.testSidebarIsNotOpenInNewPrivateWindow = function(assert, done) {
  44. const { Sidebar } = require('sdk/ui/sidebar');
  45. let testName = 'testSidebarIsNotOpenInNewPrivateWindow';
  46. let window = getMostRecentBrowserWindow();
  47. let sidebar = Sidebar({
  48. id: testName,
  49. title: testName,
  50. url: 'data:text/html;charset=utf-8,'+testName
  51. });
  52. sidebar.on('show', function() {
  53. assert.equal(isPrivate(window), false, 'the new window is not private');
  54. assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
  55. assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
  56. let window2 = window.OpenBrowserWindow({private: true});
  57. windowPromise(window2, 'load').then(focus).then(function() {
  58. // TODO: find better alt to setTimeout...
  59. setTimeout(function() {
  60. assert.equal(isPrivate(window2), true, 'the new window is private');
  61. assert.equal(isSidebarShowing(window), true, 'the sidebar is showing in old window still');
  62. assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing in the new private window');
  63. assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
  64. sidebar.destroy();
  65. close(window2).then(done);
  66. }, 500);
  67. })
  68. });
  69. sidebar.show();
  70. }
  71. // TEST: edge case where web panel is destroyed while loading
  72. exports.testDestroyEdgeCaseBugWithPrivateWindow = function(assert, done) {
  73. const { Sidebar } = require('sdk/ui/sidebar');
  74. let testName = 'testDestroyEdgeCaseBug';
  75. let window = getMostRecentBrowserWindow();
  76. let sidebar = Sidebar({
  77. id: testName,
  78. title: testName,
  79. url: 'data:text/html;charset=utf-8,'+testName
  80. });
  81. // NOTE: purposely not listening to show event b/c the event happens
  82. // between now and then.
  83. sidebar.show();
  84. assert.equal(isPrivate(window), false, 'the new window is not private');
  85. assert.equal(isSidebarShowing(window), true, 'the sidebar is showing');
  86. //assert.equal(isShowing(sidebar), true, 'the sidebar is showing');
  87. open(null, { features: { private: true } }).then(focus).then(function(window2) {
  88. assert.equal(isPrivate(window2), true, 'the new window is private');
  89. assert.equal(isSidebarShowing(window2), false, 'the sidebar is not showing');
  90. assert.equal(isShowing(sidebar), false, 'the sidebar is not showing');
  91. sidebar.destroy();
  92. assert.pass('destroying the sidebar');
  93. close(window2).then(function() {
  94. let loader = Loader(module);
  95. assert.equal(isPrivate(window), false, 'the current window is not private');
  96. let sidebar = loader.require('sdk/ui/sidebar').Sidebar({
  97. id: testName,
  98. title: testName,
  99. url: 'data:text/html;charset=utf-8,'+ testName,
  100. onShow: function() {
  101. assert.pass('onShow works for Sidebar');
  102. loader.unload();
  103. let sidebarMI = getSidebarMenuitems();
  104. for (let mi of sidebarMI) {
  105. assert.ok(BUILTIN_SIDEBAR_MENUITEMS.indexOf(mi.getAttribute('id')) >= 0, 'the menuitem is for a built-in sidebar')
  106. assert.ok(!isChecked(mi), 'no sidebar menuitem is checked');
  107. }
  108. assert.ok(!window.document.getElementById(makeID(testName)), 'sidebar id DNE');
  109. assert.equal(isSidebarShowing(window), false, 'the sidebar is not showing');
  110. done();
  111. }
  112. })
  113. sidebar.show();
  114. assert.pass('showing the sidebar');
  115. });
  116. });
  117. }
  118. exports.testShowInPrivateWindow = function(assert, done) {
  119. const { Sidebar } = require('sdk/ui/sidebar');
  120. let testName = 'testShowInPrivateWindow';
  121. let window = getMostRecentBrowserWindow();
  122. let { document } = window;
  123. let url = 'data:text/html;charset=utf-8,'+testName;
  124. let sidebar1 = Sidebar({
  125. id: testName,
  126. title: testName,
  127. url: url
  128. });
  129. assert.equal(sidebar1.url, url, 'url getter works');
  130. assert.equal(isShowing(sidebar1), false, 'the sidebar is not showing');
  131. assert.ok(!isChecked(document.getElementById(makeID(sidebar1.id))),
  132. 'the menuitem is not checked');
  133. assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
  134. windowPromise(window.OpenBrowserWindow({ private: true }), 'load').then(function(window) {
  135. let { document } = window;
  136. assert.equal(isWindowPrivate(window), true, 'new window is private');
  137. assert.equal(isPrivate(window), true, 'new window is private');
  138. sidebar1.show().then(
  139. function bad() {
  140. assert.fail('a successful show should not happen here..');
  141. },
  142. function good() {
  143. assert.equal(isShowing(sidebar1), false, 'the sidebar is still not showing');
  144. assert.equal(document.getElementById(makeID(sidebar1.id)),
  145. null,
  146. 'the menuitem dne on the private window');
  147. assert.equal(isSidebarShowing(window), false, 'the new window sidebar is not showing');
  148. sidebar1.destroy();
  149. close(window).then(done);
  150. });
  151. }, assert.fail);
  152. }
  153. // If the module doesn't support the app we're being run in, require() will
  154. // throw. In that case, remove all tests above from exports, and add one dummy
  155. // test that passes.
  156. try {
  157. require('sdk/ui/sidebar');
  158. }
  159. catch (err) {
  160. if (!/^Unsupported Application/.test(err.message))
  161. throw err;
  162. module.exports = {
  163. 'test Unsupported Application': assert => assert.pass(err.message)
  164. }
  165. }
  166. require('sdk/test').run(exports);