addon-sdk/source/test/test-content-events.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/test/test-content-events.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,131 @@
     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 +
     1.8 +"use strict";
     1.9 +
    1.10 +const { Loader } = require("sdk/test/loader");
    1.11 +const { getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
    1.12 +const { openTab, closeTab, getBrowserForTab } = require("sdk/tabs/utils");
    1.13 +const { defer } = require("sdk/core/promise");
    1.14 +const { curry, identity, partial } = require("sdk/lang/functional");
    1.15 +
    1.16 +let when = curry(function(options, tab) {
    1.17 +  let type = options.type || options;
    1.18 +  let capture = options.capture || false;
    1.19 +  let target = getBrowserForTab(tab);
    1.20 +  let { promise, resolve } = defer();
    1.21 +
    1.22 +  target.addEventListener(type, function handler(event) {
    1.23 +    if (!event.target.defaultView.frameElement) {
    1.24 +      target.removeEventListener(type, handler, capture);
    1.25 +      resolve(tab);
    1.26 +    }
    1.27 +  }, capture);
    1.28 +
    1.29 +  return promise;
    1.30 +});
    1.31 +
    1.32 +let use = function(value) function() value;
    1.33 +
    1.34 +
    1.35 +let open = curry(function(url, window) openTab(window, url));
    1.36 +let close = function(tab) {
    1.37 +  let promise = when("pagehide", tab);
    1.38 +  closeTab(tab);
    1.39 +  return promise;
    1.40 +}
    1.41 +
    1.42 +exports["test multiple tabs"] = function(assert, done) {
    1.43 +  let loader = Loader(module);
    1.44 +  let { events } = loader.require("sdk/content/events");
    1.45 +  let { on, off } = loader.require("sdk/event/core");
    1.46 +  let actual = [];
    1.47 +
    1.48 +  on(events, "data", handler);
    1.49 +  function handler ({type, target, timeStamp}) {
    1.50 +    eventFilter(type, target, () => {
    1.51 +      actual.push(type + " -> " + target.URL)
    1.52 +    });
    1.53 +  }
    1.54 +
    1.55 +  let window = getMostRecentBrowserWindow();
    1.56 +  let firstTab = open("data:text/html,first-tab", window);
    1.57 +
    1.58 +  when("pageshow", firstTab).
    1.59 +    then(close).
    1.60 +    then(use(window)).
    1.61 +    then(open("data:text/html,second-tab")).
    1.62 +    then(when("pageshow")).
    1.63 +    then(close).
    1.64 +    then(function() {
    1.65 +      assert.deepEqual(actual, [
    1.66 +        "document-element-inserted -> data:text/html,first-tab",
    1.67 +        "DOMContentLoaded -> data:text/html,first-tab",
    1.68 +        "load -> data:text/html,first-tab",
    1.69 +        "pageshow -> data:text/html,first-tab",
    1.70 +        "document-element-inserted -> data:text/html,second-tab",
    1.71 +        "DOMContentLoaded -> data:text/html,second-tab",
    1.72 +        "load -> data:text/html,second-tab",
    1.73 +        "pageshow -> data:text/html,second-tab"
    1.74 +      ], "all events dispatche as expeced")
    1.75 +    }, function(reason) {
    1.76 +      assert.fail(Error(reason));
    1.77 +    }).then(function() {
    1.78 +      loader.unload();
    1.79 +      off(events, "data", handler);
    1.80 +      done();
    1.81 +    });
    1.82 +};
    1.83 +
    1.84 +exports["test nested frames"] = function(assert, done) {
    1.85 +  let loader = Loader(module);
    1.86 +  let { events } = loader.require("sdk/content/events");
    1.87 +  let { on, off } = loader.require("sdk/event/core");
    1.88 +  let actual = [];
    1.89 +  on(events, "data", handler);
    1.90 +  function handler ({type, target, timeStamp}) {
    1.91 +    eventFilter(type, target, () => {
    1.92 +      actual.push(type + " -> " + target.URL)
    1.93 +    });
    1.94 +  }
    1.95 +
    1.96 +  let window =  getMostRecentBrowserWindow();
    1.97 +  let uri = encodeURI("data:text/html,<iframe src='data:text/html,iframe'>");
    1.98 +  let tab = open(uri, window);
    1.99 +
   1.100 +  when("pageshow", tab).
   1.101 +    then(close).
   1.102 +    then(function() {
   1.103 +      assert.deepEqual(actual, [
   1.104 +        "document-element-inserted -> " + uri,
   1.105 +        "DOMContentLoaded -> " + uri,
   1.106 +        "document-element-inserted -> data:text/html,iframe",
   1.107 +        "DOMContentLoaded -> data:text/html,iframe",
   1.108 +        "load -> data:text/html,iframe",
   1.109 +        "pageshow -> data:text/html,iframe",
   1.110 +        "load -> " + uri,
   1.111 +        "pageshow -> " + uri
   1.112 +      ], "events where dispatched")
   1.113 +    }, function(reason) {
   1.114 +      assert.fail(Error(reason))
   1.115 +    }).then(function() {
   1.116 +      loader.unload();
   1.117 +      off(events, "data", handler);
   1.118 +      done();
   1.119 +    });
   1.120 +};
   1.121 +
   1.122 +// ignore *-document-global-created events that are not very consistent.
   1.123 +// only allow data uris that we create to ignore unwanted events, e.g.,
   1.124 +// about:blank, http:// requests from Fennec's `about:`home page that displays
   1.125 +// add-ons a user could install, local `searchplugins`, other chrome uris
   1.126 +// Calls callback if passes filter
   1.127 +function eventFilter (type, target, callback) {
   1.128 +  if (target.URL.startsWith("data:text/html,") &&
   1.129 +    type !== "chrome-document-global-created" &&
   1.130 +    type !== "content-document-global-created")
   1.131 +  
   1.132 +    callback();
   1.133 +}
   1.134 +require("test").run(exports);

mercurial