addon-sdk/source/test/windows/test-firefox-windows.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/windows/test-firefox-windows.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,445 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +'use strict';
     1.8 +
     1.9 +const { Cc, Ci } = require('chrome');
    1.10 +const { setTimeout } = require('sdk/timers');
    1.11 +const { Loader } = require('sdk/test/loader');
    1.12 +const { onFocus, getMostRecentWindow, windows, isBrowser, getWindowTitle } = require('sdk/window/utils');
    1.13 +const { open, close, focus } = require('sdk/window/helpers');
    1.14 +const { browserWindows } = require("sdk/windows");
    1.15 +const tabs = require("sdk/tabs");
    1.16 +const winUtils = require("sdk/deprecated/window-utils");
    1.17 +const { WindowTracker } = winUtils;
    1.18 +const { isPrivate } = require('sdk/private-browsing');
    1.19 +const { isWindowPBSupported } = require('sdk/private-browsing/utils');
    1.20 +const { viewFor } = require("sdk/view/core");
    1.21 +const { defer } = require("sdk/lang/functional");
    1.22 +
    1.23 +// TEST: open & close window
    1.24 +exports.testOpenAndCloseWindow = function(assert, done) {
    1.25 +  assert.equal(browserWindows.length, 1, "Only one window open");
    1.26 +  let title = 'testOpenAndCloseWindow';
    1.27 +
    1.28 +  browserWindows.open({
    1.29 +    url: "data:text/html;charset=utf-8,<title>" + title + "</title>",
    1.30 +    onOpen: function(window) {
    1.31 +      assert.equal(this, browserWindows, "The 'this' object is the windows object.");
    1.32 +      assert.equal(window.tabs.length, 1, "Only one tab open");
    1.33 +      assert.equal(browserWindows.length, 2, "Two windows open");
    1.34 +
    1.35 +      window.tabs.activeTab.once('ready', function onReady(tab) {
    1.36 +        assert.pass(RegExp(title).test(window.title), "URL correctly loaded");
    1.37 +        window.close();
    1.38 +      });
    1.39 +    },
    1.40 +    onClose: function(window) {
    1.41 +      assert.equal(window.tabs.length, 0, "Tabs were cleared");
    1.42 +      assert.equal(browserWindows.length, 1, "Only one window open");
    1.43 +      done();
    1.44 +    }
    1.45 +  });
    1.46 +};
    1.47 +
    1.48 +exports.testAutomaticDestroy = function(assert, done) {
    1.49 +  let windows = browserWindows;
    1.50 +
    1.51 +  // Create a second windows instance that we will unload
    1.52 +  let called = false;
    1.53 +  let loader = Loader(module);
    1.54 +  let windows2 = loader.require("sdk/windows").browserWindows;
    1.55 +
    1.56 +  windows2.on("open", function() {
    1.57 +    called = true;
    1.58 +  });
    1.59 +
    1.60 +  loader.unload();
    1.61 +
    1.62 +  // Fire a windows event and check that this unloaded instance is inactive
    1.63 +  windows.open({
    1.64 +    url: "data:text/html;charset=utf-8,foo",
    1.65 +    onOpen: function(window) {
    1.66 +      setTimeout(function () {
    1.67 +        assert.ok(!called, "Unloaded windows instance is destroyed and inactive");
    1.68 +
    1.69 +        window.close(done);
    1.70 +      });
    1.71 +    }
    1.72 +  });
    1.73 +};
    1.74 +
    1.75 +exports.testWindowTabsObject = function(assert, done) {
    1.76 +  let window, count = 0;
    1.77 +  function runTest() {
    1.78 +    if (++count != 2)
    1.79 +      return;
    1.80 +
    1.81 +    assert.equal(window.tabs.length, 1, "Only 1 tab open");
    1.82 +    assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
    1.83 +
    1.84 +    window.tabs.open({
    1.85 +      url: "data:text/html;charset=utf-8,<title>tab 2</title>",
    1.86 +      inBackground: true,
    1.87 +      onReady: function onReady(newTab) {
    1.88 +        assert.equal(window.tabs.length, 2, "New tab open");
    1.89 +        assert.equal(newTab.title, "tab 2", "Correct new tab title");
    1.90 +        assert.equal(window.tabs.activeTab.title, "tab 1", "Correct active tab");
    1.91 +
    1.92 +        let i = 1;
    1.93 +        for (let tab of window.tabs)
    1.94 +          assert.equal(tab.title, "tab " + i++, "Correct title");
    1.95 +
    1.96 +        window.close();
    1.97 +      }
    1.98 +    });
    1.99 +  }
   1.100 +
   1.101 +  tabs.once("ready", runTest);
   1.102 +
   1.103 +  browserWindows.open({
   1.104 +    url: "data:text/html;charset=utf-8,<title>tab 1</title>",
   1.105 +    onActivate: function onActivate(win) {
   1.106 +      window = win;
   1.107 +      runTest();
   1.108 +    },
   1.109 +    onClose: function onClose(window) {
   1.110 +      assert.equal(window.tabs.length, 0, "No more tabs on closed window");
   1.111 +      done();
   1.112 +    }
   1.113 +  });
   1.114 +};
   1.115 +
   1.116 +exports.testOnOpenOnCloseListeners = function(assert, done) {
   1.117 +  let windows = browserWindows;
   1.118 +
   1.119 +  assert.equal(browserWindows.length, 1, "Only one window open");
   1.120 +
   1.121 +  let received = {
   1.122 +    listener1: false,
   1.123 +    listener2: false,
   1.124 +    listener3: false,
   1.125 +    listener4: false
   1.126 +  }
   1.127 +
   1.128 +  function listener1() {
   1.129 +    assert.equal(this, windows, "The 'this' object is the windows object.");
   1.130 +
   1.131 +    if (received.listener1)
   1.132 +      assert.fail("Event received twice");
   1.133 +    received.listener1 = true;
   1.134 +  }
   1.135 +
   1.136 +  function listener2() {
   1.137 +    if (received.listener2)
   1.138 +      assert.fail("Event received twice");
   1.139 +    received.listener2 = true;
   1.140 +  }
   1.141 +
   1.142 +  function listener3() {
   1.143 +    assert.equal(this, windows, "The 'this' object is the windows object.");
   1.144 +    if (received.listener3)
   1.145 +      assert.fail("Event received twice");
   1.146 +    received.listener3 = true;
   1.147 +  }
   1.148 +
   1.149 +  function listener4() {
   1.150 +    if (received.listener4)
   1.151 +      assert.fail("Event received twice");
   1.152 +    received.listener4 = true;
   1.153 +  }
   1.154 +
   1.155 +  windows.on('open', listener1);
   1.156 +  windows.on('open', listener2);
   1.157 +  windows.on('close', listener3);
   1.158 +  windows.on('close', listener4);
   1.159 +
   1.160 +  windows.open({
   1.161 +    url: "data:text/html;charset=utf-8,foo",
   1.162 +    onOpen: function(window) {
   1.163 +      window.close(function() {
   1.164 +        assert.ok(received.listener1, "onOpen handler called");
   1.165 +        assert.ok(received.listener2, "onOpen handler called");
   1.166 +        assert.ok(received.listener3, "onClose handler called");
   1.167 +        assert.ok(received.listener4, "onClose handler called");
   1.168 +
   1.169 +        windows.removeListener('open', listener1);
   1.170 +        windows.removeListener('open', listener2);
   1.171 +        windows.removeListener('close', listener3);
   1.172 +        windows.removeListener('close', listener4);
   1.173 +
   1.174 +        done();
   1.175 +      });
   1.176 +    }
   1.177 +  });
   1.178 +};
   1.179 +
   1.180 +exports.testActiveWindow = function(assert, done) {
   1.181 +  let windows = browserWindows;
   1.182 +
   1.183 +  // API window objects
   1.184 +  let window2, window3;
   1.185 +
   1.186 +  // Raw window objects
   1.187 +  let rawWindow2, rawWindow3;
   1.188 +
   1.189 +  let testSteps = [
   1.190 +    function() {
   1.191 +      assert.equal(windows.length, 3, "Correct number of browser windows");
   1.192 +
   1.193 +      let count = 0;
   1.194 +      for (let window in windows)
   1.195 +        count++;
   1.196 +      assert.equal(count, 3, "Correct number of windows returned by iterator");
   1.197 +
   1.198 +      assert.equal(windows.activeWindow.title, window3.title, "Correct active window - 3");
   1.199 +
   1.200 +      continueAfterFocus(rawWindow2);
   1.201 +      rawWindow2.focus();
   1.202 +    },
   1.203 +    function() {
   1.204 +      assert.equal(windows.activeWindow.title, window2.title, "Correct active window - 2");
   1.205 +
   1.206 +      continueAfterFocus(rawWindow2);
   1.207 +      window2.activate();
   1.208 +    },
   1.209 +    function() {
   1.210 +      assert.equal(windows.activeWindow.title, window2.title, "Correct active window - 2");
   1.211 +
   1.212 +      continueAfterFocus(rawWindow3);
   1.213 +      window3.activate();
   1.214 +    },
   1.215 +    function() {
   1.216 +      assert.equal(windows.activeWindow.title, window3.title, "Correct active window - 3");
   1.217 +      finishTest();
   1.218 +    }
   1.219 +  ];
   1.220 +
   1.221 +  let newWindow = null;
   1.222 +  let tracker = new WindowTracker({
   1.223 +    onTrack: function(window) {
   1.224 +      newWindow = window;
   1.225 +    }
   1.226 +  });
   1.227 +
   1.228 +  windows.open({
   1.229 +    url: "data:text/html;charset=utf-8,<title>window 2</title>",
   1.230 +    onOpen: function(window) {
   1.231 +      assert.pass('window 2 open');
   1.232 +
   1.233 +      window.tabs.activeTab.on('ready', function() {
   1.234 +        assert.pass('window 2 tab activated');
   1.235 +
   1.236 +        window2 = window;
   1.237 +        assert.ok(newWindow, "A new window was opened");
   1.238 +        rawWindow2 = newWindow;
   1.239 +        newWindow = null;
   1.240 +
   1.241 +        assert.equal(rawWindow2.content.document.title, "window 2", "Got correct raw window 2");
   1.242 +        assert.equal(rawWindow2.document.title, window2.title, "Saw correct title on window 2");
   1.243 +
   1.244 +        windows.open({
   1.245 +          url: "data:text/html;charset=utf-8,<title>window 3</title>",
   1.246 +          onOpen: function(window) {
   1.247 +            assert.pass('window 3 open');
   1.248 +
   1.249 +            window.tabs.activeTab.on('ready', function onReady() {
   1.250 +              assert.pass('window 3 tab activated');
   1.251 +
   1.252 +              window3 = window;
   1.253 +              assert.ok(newWindow, "A new window was opened");
   1.254 +              rawWindow3 = newWindow;
   1.255 +              tracker.unload();
   1.256 +
   1.257 +              assert.equal(rawWindow3.content.document.title, "window 3", "Got correct raw window 3");
   1.258 +              assert.equal(rawWindow3.document.title, window3.title, "Saw correct title on window 3");
   1.259 +
   1.260 +              continueAfterFocus(rawWindow3);
   1.261 +              rawWindow3.focus();
   1.262 +            });
   1.263 +          }
   1.264 +        });
   1.265 +      });
   1.266 +    }
   1.267 +  });
   1.268 +
   1.269 +  function nextStep() {
   1.270 +    if (testSteps.length) {
   1.271 +      setTimeout(testSteps.shift())
   1.272 +    }
   1.273 +  }
   1.274 +
   1.275 +  let continueAfterFocus = function(w) onFocus(w).then(nextStep);
   1.276 +
   1.277 +  function finishTest() {
   1.278 +    // close unactive window first to avoid unnecessary focus changing
   1.279 +    window2.close(function() {
   1.280 +      window3.close(function() {
   1.281 +        assert.equal(rawWindow2.closed, true, 'window 2 is closed');
   1.282 +        assert.equal(rawWindow3.closed, true, 'window 3 is closed');
   1.283 +
   1.284 +        done();
   1.285 +      });
   1.286 +    });
   1.287 +  }
   1.288 +};
   1.289 +
   1.290 +exports.testTrackWindows = function(assert, done) {
   1.291 +  let windows = [];
   1.292 +  let actions = [];
   1.293 +
   1.294 +  let expects = [
   1.295 +    "activate 0", "global activate 0", "deactivate 0", "global deactivate 0",
   1.296 +    "activate 1", "global activate 1", "deactivate 1", "global deactivate 1",
   1.297 +    "activate 2", "global activate 2"
   1.298 +  ];
   1.299 +
   1.300 +  function windowsActivation(window) {
   1.301 +    let index = windows.indexOf(window);
   1.302 +    // only concerned with windows opened for this test
   1.303 +    if (index < 0)
   1.304 +      return;
   1.305 +
   1.306 +    assert.equal(actions.join(), expects.slice(0, index*4 + 1).join(), expects[index*4 + 1]);
   1.307 +    actions.push("global activate " + index)
   1.308 +  }
   1.309 +
   1.310 +  function windowsDeactivation(window) {
   1.311 +    let index = windows.indexOf(window);
   1.312 +    // only concerned with windows opened for this test
   1.313 +    if (index < 0)
   1.314 +      return;
   1.315 +
   1.316 +    assert.equal(actions.join(), expects.slice(0, index*4 + 3).join(), expects[index*4 + 3]);
   1.317 +    actions.push("global deactivate " + index)
   1.318 +  }
   1.319 +
   1.320 +  // listen to global activate events
   1.321 +  browserWindows.on("activate", windowsActivation);
   1.322 +
   1.323 +  // listen to global deactivate events
   1.324 +  browserWindows.on("deactivate", windowsDeactivation);
   1.325 +
   1.326 +
   1.327 +  function openWindow() {
   1.328 +    windows.push(browserWindows.open({
   1.329 +      url: "data:text/html;charset=utf-8,<i>testTrackWindows</i>",
   1.330 +      onActivate: function(window) {
   1.331 +        let index = windows.indexOf(window);
   1.332 +
   1.333 +        // Guard against windows that have already been removed.
   1.334 +        // See bug 874502 comment 32.
   1.335 +        if (index == -1)
   1.336 +          return;
   1.337 +
   1.338 +        assert.equal(actions.join(),
   1.339 +                     expects.slice(0, index*4).join(),
   1.340 +                     "expecting " + expects[index*4]);
   1.341 +        actions.push("activate " + index);
   1.342 +
   1.343 +        if (windows.length < 3) {
   1.344 +          openWindow()
   1.345 +        }
   1.346 +        else {
   1.347 +          (function closeWindows(windows) {
   1.348 +            if (!windows.length) {
   1.349 +              browserWindows.removeListener("activate", windowsActivation);
   1.350 +              browserWindows.removeListener("deactivate", windowsDeactivation);
   1.351 +              return done();
   1.352 +            }
   1.353 +
   1.354 +            return windows.pop().close(function() {
   1.355 +              assert.pass('window was closed');
   1.356 +              closeWindows(windows);
   1.357 +            });
   1.358 +          })(windows)
   1.359 +        }
   1.360 +      },
   1.361 +      onDeactivate: function(window) {
   1.362 +        let index = windows.indexOf(window);
   1.363 +
   1.364 +        // Guard against windows that have already been removed.
   1.365 +        // See bug 874502 comment 32.
   1.366 +        if (index == -1)
   1.367 +          return;
   1.368 +
   1.369 +        assert.equal(actions.join(),
   1.370 +                     expects.slice(0, index*4 + 2).join(),
   1.371 +                     "expecting " + expects[index*4 + 2]);
   1.372 +        actions.push("deactivate " + index)
   1.373 +      }
   1.374 +    }));
   1.375 +  }
   1.376 +  openWindow();
   1.377 +}
   1.378 +
   1.379 +// test that it is not possible to open a private window by default
   1.380 +exports.testWindowOpenPrivateDefault = function(assert, done) {
   1.381 +  browserWindows.open({
   1.382 +    url: 'about:mozilla',
   1.383 +    isPrivate: true,
   1.384 +    onOpen: function(window) {
   1.385 +      let tab = window.tabs[0];
   1.386 +
   1.387 +      tab.once('ready', function() {
   1.388 +        assert.equal(tab.url, 'about:mozilla', 'opened correct tab');
   1.389 +        assert.equal(isPrivate(tab), false, 'tab is not private');
   1.390 +
   1.391 +        window.close(done);
   1.392 +      });
   1.393 +    }
   1.394 +  });
   1.395 +}
   1.396 +
   1.397 +// test that it is not possible to find a private window in
   1.398 +// windows module's iterator
   1.399 +exports.testWindowIteratorPrivateDefault = function(assert, done) {
   1.400 +  assert.equal(browserWindows.length, 1, 'only one window open');
   1.401 +
   1.402 +  open('chrome://browser/content/browser.xul', {
   1.403 +    features: {
   1.404 +      private: true,
   1.405 +      chrome: true
   1.406 +    }
   1.407 +  }).then(focus).then(function(window) {
   1.408 +    // test that there is a private window opened
   1.409 +    assert.equal(isPrivate(window), isWindowPBSupported, 'there is a private window open');
   1.410 +    assert.strictEqual(window, winUtils.activeWindow);
   1.411 +    assert.strictEqual(window, getMostRecentWindow());
   1.412 +
   1.413 +    assert.ok(!isPrivate(browserWindows.activeWindow));
   1.414 +
   1.415 +    assert.equal(browserWindows.length, 1, 'only one window in browserWindows');
   1.416 +    assert.equal(windows().length, 1, 'only one window in windows()');
   1.417 +
   1.418 +    assert.equal(windows(null, { includePrivate: true }).length, 2);
   1.419 +
   1.420 +    // test that all windows in iterator are not private
   1.421 +    for (let window of browserWindows)
   1.422 +      assert.ok(!isPrivate(window), 'no window in browserWindows is private');
   1.423 +
   1.424 +    close(window).then(done);
   1.425 +  });
   1.426 +};
   1.427 +
   1.428 +exports["test getView(window)"] = function(assert, done) {
   1.429 +  browserWindows.once("open", window => {
   1.430 +    const view = viewFor(window);
   1.431 +
   1.432 +    assert.ok(view instanceof Ci.nsIDOMWindow, "view is a window");
   1.433 +    assert.ok(isBrowser(view), "view is a browser window");
   1.434 +    assert.equal(getWindowTitle(view), window.title,
   1.435 +                 "window has a right title");
   1.436 +
   1.437 +    window.close();
   1.438 +    // Defer handler cause window is destroyed after event is dispatched.
   1.439 +    browserWindows.once("close", defer(_ => {
   1.440 +      assert.equal(viewFor(window), null, "window view is gone");
   1.441 +      done();
   1.442 +    }));
   1.443 +  });
   1.444 +
   1.445 +  browserWindows.open({ url: "data:text/html,<title>yo</title>" });
   1.446 +};
   1.447 +
   1.448 +require('sdk/test').run(exports);

mercurial