test-sidebar.js 7.9 KB

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