michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: "use strict";
michael@0:
michael@0: const tabs = require("sdk/tabs"); // From addon-kit
michael@0: const windowUtils = require("sdk/deprecated/window-utils");
michael@0: const { getTabForWindow } = require('sdk/tabs/helpers');
michael@0: const app = require("sdk/system/xul-app");
michael@0: const { viewFor } = require("sdk/view/core");
michael@0: const { modelFor } = require("sdk/model/core");
michael@0: const { getTabId, isTab } = require("sdk/tabs/utils");
michael@0: const { defer } = require("sdk/lang/functional");
michael@0:
michael@0: // The primary test tab
michael@0: var primaryTab;
michael@0:
michael@0: // We have an auxiliary tab to test background tabs.
michael@0: var auxTab;
michael@0:
michael@0: // The window for the outer iframe in the primary test page
michael@0: var iframeWin;
michael@0:
michael@0: exports["test GetTabForWindow"] = function(assert, done) {
michael@0:
michael@0: assert.equal(getTabForWindow(windowUtils.activeWindow), null,
michael@0: "getTabForWindow return null on topwindow");
michael@0: assert.equal(getTabForWindow(windowUtils.activeBrowserWindow), null,
michael@0: "getTabForWindow return null on topwindow");
michael@0:
michael@0: let subSubDocument = encodeURIComponent(
michael@0: 'Sub iframe
'+
michael@0: '');
michael@0: let subDocument = encodeURIComponent(
michael@0: 'Iframe
'+
michael@0: '');
michael@0: let url = 'data:text/html;charset=utf-8,' + encodeURIComponent(
michael@0: 'Content
');
michael@0:
michael@0: // Open up a new tab in the background.
michael@0: //
michael@0: // This lets us test whether GetTabForWindow works even when the tab in
michael@0: // question is not active.
michael@0: tabs.open({
michael@0: inBackground: true,
michael@0: url: "about:mozilla",
michael@0: onReady: function(tab) { auxTab = tab; step2(url, assert);},
michael@0: onActivate: function(tab) { step3(assert, done); }
michael@0: });
michael@0: };
michael@0:
michael@0: function step2(url, assert) {
michael@0:
michael@0: tabs.open({
michael@0: url: url,
michael@0: onReady: function(tab) {
michael@0: primaryTab = tab;
michael@0: let window = windowUtils.activeBrowserWindow.content;
michael@0:
michael@0: let matchedTab = getTabForWindow(window);
michael@0: assert.equal(matchedTab, tab,
michael@0: "We are able to find the tab with his content window object");
michael@0:
michael@0: let timer = require("sdk/timers");
michael@0: function waitForFrames() {
michael@0: let iframe = window.document.getElementById("iframe");
michael@0: if (!iframe) {
michael@0: timer.setTimeout(waitForFrames, 100);
michael@0: return;
michael@0: }
michael@0: iframeWin = iframe.contentWindow;
michael@0: let subIframe = iframeWin.document.getElementById("sub-iframe");
michael@0: if (!subIframe) {
michael@0: timer.setTimeout(waitForFrames, 100);
michael@0: return;
michael@0: }
michael@0: let subIframeWin = subIframe.contentWindow;
michael@0: let subSubIframe = subIframeWin.document.getElementById("sub-sub-iframe");
michael@0: if (!subSubIframe) {
michael@0: timer.setTimeout(waitForFrames, 100);
michael@0: return;
michael@0: }
michael@0: let subSubIframeWin = subSubIframe.contentWindow;
michael@0:
michael@0: matchedTab = getTabForWindow(iframeWin);
michael@0: assert.equal(matchedTab, tab,
michael@0: "We are able to find the tab with an iframe window object");
michael@0:
michael@0: matchedTab = getTabForWindow(subIframeWin);
michael@0: assert.equal(matchedTab, tab,
michael@0: "We are able to find the tab with a sub-iframe window object");
michael@0:
michael@0: matchedTab = getTabForWindow(subSubIframeWin);
michael@0: assert.equal(matchedTab, tab,
michael@0: "We are able to find the tab with a sub-sub-iframe window object");
michael@0:
michael@0: // Put our primary tab in the background and test again.
michael@0: // The onActivate listener will take us to step3.
michael@0: auxTab.activate();
michael@0: }
michael@0: waitForFrames();
michael@0: }
michael@0: });
michael@0: }
michael@0:
michael@0: function step3(assert, done) {
michael@0:
michael@0: let matchedTab = getTabForWindow(iframeWin);
michael@0: assert.equal(matchedTab, primaryTab,
michael@0: "We get the correct tab even when it's in the background");
michael@0:
michael@0: primaryTab.close(function () {
michael@0: auxTab.close(function () { done();});
michael@0: });
michael@0: }
michael@0:
michael@0: exports["test behavior on close"] = function(assert, done) {
michael@0:
michael@0: tabs.open({
michael@0: url: "about:mozilla",
michael@0: onReady: function(tab) {
michael@0: assert.equal(tab.url, "about:mozilla", "Tab has the expected url");
michael@0: // if another test ends before closing a tab then index != 1 here
michael@0: assert.ok(tab.index >= 1, "Tab has the expected index, a value greater than 0");
michael@0: tab.close(function () {
michael@0: assert.equal(tab.url, undefined,
michael@0: "After being closed, tab attributes are undefined (url)");
michael@0: assert.equal(tab.index, undefined,
michael@0: "After being closed, tab attributes are undefined (index)");
michael@0: if (app.is("Firefox")) {
michael@0: // Ensure that we can call destroy multiple times without throwing;
michael@0: // Fennec doesn't use this internal utility
michael@0: tab.destroy();
michael@0: tab.destroy();
michael@0: }
michael@0:
michael@0: done();
michael@0: });
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: exports["test viewFor(tab)"] = (assert, done) => {
michael@0: // Note we defer handlers as length collection is updated after
michael@0: // handler is invoked, so if test is finished before counnts are
michael@0: // updated wrong length will show up in followup tests.
michael@0: tabs.once("open", defer(tab => {
michael@0: const view = viewFor(tab);
michael@0: assert.ok(view, "view is returned");
michael@0: assert.equal(getTabId(view), tab.id, "tab has a same id");
michael@0:
michael@0: tab.close(defer(done));
michael@0: }));
michael@0:
michael@0: tabs.open({ url: "about:mozilla" });
michael@0: };
michael@0:
michael@0:
michael@0: exports["test modelFor(xulTab)"] = (assert, done) => {
michael@0: tabs.open({
michael@0: url: "about:mozilla",
michael@0: onReady: tab => {
michael@0: const view = viewFor(tab);
michael@0: assert.ok(view, "view is returned");
michael@0: assert.ok(isTab(view), "view is underlaying tab");
michael@0: assert.equal(getTabId(view), tab.id, "tab has a same id");
michael@0: assert.equal(modelFor(view), tab, "modelFor(view) is SDK tab");
michael@0:
michael@0: tab.close(defer(done));
michael@0: }
michael@0: });
michael@0: };
michael@0:
michael@0: require("test").run(exports);