test-tab.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. const tabs = require("sdk/tabs"); // From addon-kit
  5. const windowUtils = require("sdk/deprecated/window-utils");
  6. const { getTabForWindow } = require('sdk/tabs/helpers');
  7. const app = require("sdk/system/xul-app");
  8. const { viewFor } = require("sdk/view/core");
  9. const { getTabId } = require("sdk/tabs/utils");
  10. const { defer } = require("sdk/lang/functional");
  11. // The primary test tab
  12. var primaryTab;
  13. // We have an auxiliary tab to test background tabs.
  14. var auxTab;
  15. // The window for the outer iframe in the primary test page
  16. var iframeWin;
  17. exports["test GetTabForWindow"] = function(assert, done) {
  18. assert.equal(getTabForWindow(windowUtils.activeWindow), null,
  19. "getTabForWindow return null on topwindow");
  20. assert.equal(getTabForWindow(windowUtils.activeBrowserWindow), null,
  21. "getTabForWindow return null on topwindow");
  22. let subSubDocument = encodeURIComponent(
  23. 'Sub iframe<br/>'+
  24. '<iframe id="sub-sub-iframe" src="data:text/html;charset=utf-8,SubSubIframe" />');
  25. let subDocument = encodeURIComponent(
  26. 'Iframe<br/>'+
  27. '<iframe id="sub-iframe" src="data:text/html;charset=utf-8,'+subSubDocument+'" />');
  28. let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(
  29. 'Content<br/><iframe id="iframe" src="data:text/html;charset=utf-8,'+subDocument+'" />');
  30. // Open up a new tab in the background.
  31. //
  32. // This lets us test whether GetTabForWindow works even when the tab in
  33. // question is not active.
  34. tabs.open({
  35. inBackground: true,
  36. url: "about:mozilla",
  37. onReady: function(tab) { auxTab = tab; step2(url, assert);},
  38. onActivate: function(tab) { step3(assert, done); }
  39. });
  40. };
  41. function step2(url, assert) {
  42. tabs.open({
  43. url: url,
  44. onReady: function(tab) {
  45. primaryTab = tab;
  46. let window = windowUtils.activeBrowserWindow.content;
  47. let matchedTab = getTabForWindow(window);
  48. assert.equal(matchedTab, tab,
  49. "We are able to find the tab with his content window object");
  50. let timer = require("sdk/timers");
  51. function waitForFrames() {
  52. let iframe = window.document.getElementById("iframe");
  53. if (!iframe) {
  54. timer.setTimeout(waitForFrames, 100);
  55. return;
  56. }
  57. iframeWin = iframe.contentWindow;
  58. let subIframe = iframeWin.document.getElementById("sub-iframe");
  59. if (!subIframe) {
  60. timer.setTimeout(waitForFrames, 100);
  61. return;
  62. }
  63. let subIframeWin = subIframe.contentWindow;
  64. let subSubIframe = subIframeWin.document.getElementById("sub-sub-iframe");
  65. if (!subSubIframe) {
  66. timer.setTimeout(waitForFrames, 100);
  67. return;
  68. }
  69. let subSubIframeWin = subSubIframe.contentWindow;
  70. matchedTab = getTabForWindow(iframeWin);
  71. assert.equal(matchedTab, tab,
  72. "We are able to find the tab with an iframe window object");
  73. matchedTab = getTabForWindow(subIframeWin);
  74. assert.equal(matchedTab, tab,
  75. "We are able to find the tab with a sub-iframe window object");
  76. matchedTab = getTabForWindow(subSubIframeWin);
  77. assert.equal(matchedTab, tab,
  78. "We are able to find the tab with a sub-sub-iframe window object");
  79. // Put our primary tab in the background and test again.
  80. // The onActivate listener will take us to step3.
  81. auxTab.activate();
  82. }
  83. waitForFrames();
  84. }
  85. });
  86. }
  87. function step3(assert, done) {
  88. let matchedTab = getTabForWindow(iframeWin);
  89. assert.equal(matchedTab, primaryTab,
  90. "We get the correct tab even when it's in the background");
  91. primaryTab.close(function () {
  92. auxTab.close(function () { done();});
  93. });
  94. }
  95. exports["test behavior on close"] = function(assert, done) {
  96. tabs.open({
  97. url: "about:mozilla",
  98. onReady: function(tab) {
  99. assert.equal(tab.url, "about:mozilla", "Tab has the expected url");
  100. // if another test ends before closing a tab then index != 1 here
  101. assert.ok(tab.index >= 1, "Tab has the expected index, a value greater than 0");
  102. tab.close(function () {
  103. assert.equal(tab.url, undefined,
  104. "After being closed, tab attributes are undefined (url)");
  105. assert.equal(tab.index, undefined,
  106. "After being closed, tab attributes are undefined (index)");
  107. if (app.is("Firefox")) {
  108. // Ensure that we can call destroy multiple times without throwing;
  109. // Fennec doesn't use this internal utility
  110. tab.destroy();
  111. tab.destroy();
  112. }
  113. done();
  114. });
  115. }
  116. });
  117. };
  118. exports["test viewFor(tab)"] = (assert, done) => {
  119. // Note we defer handlers as length collection is updated after
  120. // handler is invoked, so if test is finished before counnts are
  121. // updated wrong length will show up in followup tests.
  122. tabs.once("open", defer(tab => {
  123. const view = viewFor(tab);
  124. assert.ok(view, "view is returned");
  125. assert.equal(getTabId(view), tab.id, "tab has a same id");
  126. tab.close(defer(done));
  127. }));
  128. tabs.open({ url: "about:mozilla" });
  129. }
  130. require("test").run(exports);