test-page-mod.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. const { getMostRecentBrowserWindow } = require('sdk/window/utils');
  2. const { PageMod } = require("sdk/page-mod");
  3. const { getActiveTab, setTabURL, openTab, closeTab } = require('sdk/tabs/utils');
  4. const xulApp = require('sdk/system/xul-app');
  5. const windowHelpers = require('sdk/window/helpers');
  6. const { defer } = require("sdk/core/promise");
  7. const { isPrivate } = require('sdk/private-browsing');
  8. const { isTabPBSupported, isWindowPBSupported } = require('sdk/private-browsing/utils');
  9. function openWebpage(url, enablePrivate) {
  10. let { promise, resolve, reject } = defer();
  11. if (xulApp.is("Fennec")) {
  12. let chromeWindow = getMostRecentBrowserWindow();
  13. let rawTab = openTab(chromeWindow, url, {
  14. isPrivate: enablePrivate
  15. });
  16. resolve(function() {
  17. closeTab(rawTab)
  18. });
  19. return promise;
  20. }
  21. else {
  22. windowHelpers.open("", {
  23. features: {
  24. toolbar: true,
  25. private: enablePrivate
  26. }
  27. }).then(function(chromeWindow) {
  28. if (isPrivate(chromeWindow) !== !!enablePrivate)
  29. reject(new Error("Window should have Private set to " + !!enablePrivate));
  30. let tab = getActiveTab(chromeWindow);
  31. setTabURL(tab, url);
  32. resolve(function(){
  33. windowHelpers.close(chromeWindow);
  34. });
  35. });
  36. return promise;
  37. }
  38. }
  39. exports["test page-mod on private tab"] = function (assert, done) {
  40. // Only set private mode when explicitely supported.
  41. // (fennec 19 has some intermediate PB support where isTabPBSupported
  42. // will be false, but isPrivate(worker.tab) will be true if we open a private
  43. // tab)
  44. let setPrivate = isTabPBSupported || isWindowPBSupported;
  45. let id = Date.now().toString(36);
  46. let frameUri = "data:text/html;charset=utf-8," + id;
  47. let uri = "data:text/html;charset=utf-8," +
  48. encodeURIComponent(id + "<iframe src='" + frameUri + "'><iframe>");
  49. let count = 0;
  50. openWebpage(uri, setPrivate).then(function(close) {
  51. PageMod({
  52. include: [uri, frameUri],
  53. onAttach: function(worker) {
  54. assert.ok(worker.tab.url == uri || worker.tab.url == frameUri,
  55. "Got a worker attached to the private window tab");
  56. if (setPrivate) {
  57. assert.ok(isPrivate(worker), "The worker is really private");
  58. assert.ok(isPrivate(worker.tab), "The document is really private");
  59. }
  60. else {
  61. assert.ok(!isPrivate(worker),
  62. "private browsing isn't supported, " +
  63. "so that the worker isn't private");
  64. assert.ok(!isPrivate(worker.tab),
  65. "private browsing isn't supported, " +
  66. "so that the document isn't private");
  67. }
  68. if (++count == 2) {
  69. this.destroy();
  70. close();
  71. done();
  72. }
  73. }
  74. });
  75. }).then(null, assert.fail);
  76. };
  77. exports["test page-mod on non-private tab"] = function (assert, done) {
  78. let id = Date.now().toString(36);
  79. let url = "data:text/html;charset=utf-8," + encodeURIComponent(id);
  80. openWebpage(url, false).then(function(close){
  81. PageMod({
  82. include: url,
  83. onAttach: function(worker) {
  84. assert.equal(worker.tab.url, url,
  85. "Got a worker attached to the private window tab");
  86. assert.ok(!isPrivate(worker), "The worker is really non-private");
  87. assert.ok(!isPrivate(worker.tab), "The document is really non-private");
  88. this.destroy();
  89. close();
  90. done();
  91. }
  92. });
  93. }).then(null, assert.fail);
  94. }