123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- 'use strict';
- const { Cc, Ci } = require('chrome');
- const { setTimeout } = require('sdk/timers');
- const { Loader } = require('sdk/test/loader');
- const { onFocus, getMostRecentWindow, windows, isBrowser, getWindowTitle } = require('sdk/window/utils');
- const { open, close, focus } = require('sdk/window/helpers');
- const { browserWindows } = require("sdk/windows");
- const tabs = require("sdk/tabs");
- const winUtils = require("sdk/deprecated/window-utils");
- const { WindowTracker } = winUtils;
- const { isPrivate } = require('sdk/private-browsing');
- const { isWindowPBSupported } = require('sdk/private-browsing/utils');
- const { viewFor } = require("sdk/view/core");
- const { defer } = require("sdk/lang/functional");
- // TEST: open & close window
- exports.testOpenAndCloseWindow = function(assert, done) {
- assert.equal(browserWindows.length, 1, "Only one window open");
- let title = 'testOpenAndCloseWindow';
- browserWindows.open({
- url: "data:text/html;charset=utf-8,<title>" + title + "</title>",
- onOpen: function(window) {
- assert.equal(this, browserWindows, "The 'this' object is the windows object.");
- assert.equal(window.tabs.length, 1, "Only one tab open");
- assert.equal(browserWindows.length, 2, "Two windows open");
- window.tabs.activeTab.once('ready', function onReady(tab) {
- assert.pass(RegExp(title).test(window.title), "URL correctly loaded");
- window.close();
- });
- },
- onClose: function(window) {
- assert.equal(window.tabs.length, 0, "Tabs were cleared");
- assert.equal(browserWindows.length, 1, "Only one window open");
- done();
- }
- });
- };
- exports.testAutomaticDestroy = function(assert, done) {
- let windows = browserWindows;
- // Create a second windows instance that we will unload
- let called = false;
- let loader = Loader(module);
- let windows2 = loader.require("sdk/windows").browserWindows;
- windows2.on("open", function() {
- called = true;
- });
- loader.unload();
- // Fire a windows event and check that this unloaded instance is inactive
- windows.open({
- url: "data:text/html;charset=utf-8,foo",
- onOpen: function(window) {
- setTimeout(function () {
- assert.ok(!called, "Unloaded windows instance is destroyed and inactive");
- window.close(done);
- });
- }
- });
- };
- exports.testWindowTabsObject = function(assert, done) {
- let window, count = 0;
- function runTest() {
- if (++count != 2)
- return;
- assert.equal(window.tabs.length, 1, "Only 1 tab open");
- assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
- window.tabs.open({
- url: "data:text/html;charset=utf-8,<title>tab 2</title>",
- inBackground: true,
- onReady: function onReady(newTab) {
- assert.equal(window.tabs.length, 2, "New tab open");
- assert.equal(newTab.title, "tab 2", "Correct new tab title");
- assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
- let i = 1;
- for (let tab of window.tabs)
- assert.equal(tab.title, "tab " + i++, "Correct title");
- window.close();
- }
- });
- }
- tabs.once("ready", runTest);
- browserWindows.open({
- url: "data:text/html;charset=utf-8,<title>tab 1</title>",
- onActivate: function onActivate(win) {
- window = win;
- runTest();
- },
- onClose: function onClose(window) {
- assert.equal(window.tabs.length, 0, "No more tabs on closed window");
- done();
- }
- });
- };
- exports.testOnOpenOnCloseListeners = function(assert, done) {
- let windows = browserWindows;
- assert.equal(browserWindows.length, 1, "Only one window open");
- let received = {
- listener1: false,
- listener2: false,
- listener3: false,
- listener4: false
- }
- function listener1() {
- assert.equal(this, windows, "The 'this' object is the windows object.");
- if (received.listener1)
- assert.fail("Event received twice");
- received.listener1 = true;
- }
- function listener2() {
- if (received.listener2)
- assert.fail("Event received twice");
- received.listener2 = true;
- }
- function listener3() {
- assert.equal(this, windows, "The 'this' object is the windows object.");
- if (received.listener3)
- assert.fail("Event received twice");
- received.listener3 = true;
- }
- function listener4() {
- if (received.listener4)
- assert.fail("Event received twice");
- received.listener4 = true;
- }
- windows.on('open', listener1);
- windows.on('open', listener2);
- windows.on('close', listener3);
- windows.on('close', listener4);
- windows.open({
- url: "data:text/html;charset=utf-8,foo",
- onOpen: function(window) {
- window.close(function() {
- assert.ok(received.listener1, "onOpen handler called");
- assert.ok(received.listener2, "onOpen handler called");
- assert.ok(received.listener3, "onClose handler called");
- assert.ok(received.listener4, "onClose handler called");
- windows.removeListener('open', listener1);
- windows.removeListener('open', listener2);
- windows.removeListener('close', listener3);
- windows.removeListener('close', listener4);
- done();
- });
- }
- });
- };
- exports.testActiveWindow = function(assert, done) {
- let windows = browserWindows;
- // API window objects
- let window2, window3;
- // Raw window objects
- let rawWindow2, rawWindow3;
- let testSteps = [
- function() {
- assert.equal(windows.length, 3, "Correct number of browser windows");
- let count = 0;
- for (let window in windows)
- count++;
- assert.equal(count, 3, "Correct number of windows returned by iterator");
- assert.equal(windows.activeWindow.title, window3.title, "Correct active window - 3");
- continueAfterFocus(rawWindow2);
- rawWindow2.focus();
- },
- function() {
- assert.equal(windows.activeWindow.title, window2.title, "Correct active window - 2");
- continueAfterFocus(rawWindow2);
- window2.activate();
- },
- function() {
- assert.equal(windows.activeWindow.title, window2.title, "Correct active window - 2");
- continueAfterFocus(rawWindow3);
- window3.activate();
- },
- function() {
- assert.equal(windows.activeWindow.title, window3.title, "Correct active window - 3");
- finishTest();
- }
- ];
- let newWindow = null;
- let tracker = new WindowTracker({
- onTrack: function(window) {
- newWindow = window;
- }
- });
- windows.open({
- url: "data:text/html;charset=utf-8,<title>window 2</title>",
- onOpen: function(window) {
- assert.pass('window 2 open');
- window.tabs.activeTab.on('ready', function() {
- assert.pass('window 2 tab activated');
- window2 = window;
- assert.ok(newWindow, "A new window was opened");
- rawWindow2 = newWindow;
- newWindow = null;
- assert.equal(rawWindow2.content.document.title, "window 2", "Got correct raw window 2");
- assert.equal(rawWindow2.document.title, window2.title, "Saw correct title on window 2");
- windows.open({
- url: "data:text/html;charset=utf-8,<title>window 3</title>",
- onOpen: function(window) {
- assert.pass('window 3 open');
- window.tabs.activeTab.on('ready', function onReady() {
- assert.pass('window 3 tab activated');
- window3 = window;
- assert.ok(newWindow, "A new window was opened");
- rawWindow3 = newWindow;
- tracker.unload();
- assert.equal(rawWindow3.content.document.title, "window 3", "Got correct raw window 3");
- assert.equal(rawWindow3.document.title, window3.title, "Saw correct title on window 3");
- continueAfterFocus(rawWindow3);
- rawWindow3.focus();
- });
- }
- });
- });
- }
- });
- function nextStep() {
- if (testSteps.length) {
- setTimeout(testSteps.shift())
- }
- }
- let continueAfterFocus = function(w) onFocus(w).then(nextStep);
- function finishTest() {
- // close unactive window first to avoid unnecessary focus changing
- window2.close(function() {
- window3.close(function() {
- assert.equal(rawWindow2.closed, true, 'window 2 is closed');
- assert.equal(rawWindow3.closed, true, 'window 3 is closed');
- done();
- });
- });
- }
- };
- exports.testTrackWindows = function(assert, done) {
- let windows = [];
- let actions = [];
- let expects = [
- "activate 0", "global activate 0", "deactivate 0", "global deactivate 0",
- "activate 1", "global activate 1", "deactivate 1", "global deactivate 1",
- "activate 2", "global activate 2"
- ];
- function windowsActivation(window) {
- let index = windows.indexOf(window);
- // only concerned with windows opened for this test
- if (index < 0)
- return;
- assert.equal(actions.join(), expects.slice(0, index*4 + 1).join(), expects[index*4 + 1]);
- actions.push("global activate " + index)
- }
- function windowsDeactivation(window) {
- let index = windows.indexOf(window);
- // only concerned with windows opened for this test
- if (index < 0)
- return;
- assert.equal(actions.join(), expects.slice(0, index*4 + 3).join(), expects[index*4 + 3]);
- actions.push("global deactivate " + index)
- }
- // listen to global activate events
- browserWindows.on("activate", windowsActivation);
- // listen to global deactivate events
- browserWindows.on("deactivate", windowsDeactivation);
- function openWindow() {
- windows.push(browserWindows.open({
- url: "data:text/html;charset=utf-8,<i>testTrackWindows</i>",
- onActivate: function(window) {
- let index = windows.indexOf(window);
- // Guard against windows that have already been removed.
- // See bug 874502 comment 32.
- if (index == -1)
- return;
- assert.equal(actions.join(),
- expects.slice(0, index*4).join(),
- "expecting " + expects[index*4]);
- actions.push("activate " + index);
- if (windows.length < 3) {
- openWindow()
- }
- else {
- (function closeWindows(windows) {
- if (!windows.length) {
- browserWindows.removeListener("activate", windowsActivation);
- browserWindows.removeListener("deactivate", windowsDeactivation);
- return done();
- }
- return windows.pop().close(function() {
- assert.pass('window was closed');
- closeWindows(windows);
- });
- })(windows)
- }
- },
- onDeactivate: function(window) {
- let index = windows.indexOf(window);
- // Guard against windows that have already been removed.
- // See bug 874502 comment 32.
- if (index == -1)
- return;
- assert.equal(actions.join(),
- expects.slice(0, index*4 + 2).join(),
- "expecting " + expects[index*4 + 2]);
- actions.push("deactivate " + index)
- }
- }));
- }
- openWindow();
- }
- // test that it is not possible to open a private window by default
- exports.testWindowOpenPrivateDefault = function(assert, done) {
- browserWindows.open({
- url: 'about:mozilla',
- isPrivate: true,
- onOpen: function(window) {
- let tab = window.tabs[0];
- tab.once('ready', function() {
- assert.equal(tab.url, 'about:mozilla', 'opened correct tab');
- assert.equal(isPrivate(tab), false, 'tab is not private');
- window.close(done);
- });
- }
- });
- }
- // test that it is not possible to find a private window in
- // windows module's iterator
- exports.testWindowIteratorPrivateDefault = function(assert, done) {
- assert.equal(browserWindows.length, 1, 'only one window open');
- open('chrome://browser/content/browser.xul', {
- features: {
- private: true,
- chrome: true
- }
- }).then(focus).then(function(window) {
- // test that there is a private window opened
- assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open');
- assert.strictEqual(window, winUtils.activeWindow);
- assert.strictEqual(window, getMostRecentWindow());
- assert.ok(!isPrivate(browserWindows.activeWindow));
- assert.equal(browserWindows.length, 1, 'only one window in browserWindows');
- assert.equal(windows().length, 1, 'only one window in windows()');
- assert.equal(windows(null, { includePrivate: true }).length, 2);
- // test that all windows in iterator are not private
- for (let window of browserWindows)
- assert.ok(!isPrivate(window), 'no window in browserWindows is private');
- close(window).then(done);
- });
- };
- exports["test getView(window)"] = function(assert, done) {
- browserWindows.once("open", window => {
- const view = viewFor(window);
- assert.ok(view instanceof Ci.nsIDOMWindow, "view is a window");
- assert.ok(isBrowser(view), "view is a browser window");
- assert.equal(getWindowTitle(view), window.title,
- "window has a right title");
- window.close();
- // Defer handler cause window is destroyed after event is dispatched.
- browserWindows.once("close", defer(_ => {
- assert.equal(viewFor(window), null, "window view is gone");
- done();
- }));
- });
- browserWindows.open({ url: "data:text/html,<title>yo</title>" });
- };
- require('sdk/test').run(exports);
|