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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     5 "use strict";
     7 const { Loader } = require("sdk/test/loader");
     8 const { getMostRecentBrowserWindow, getInnerId } = require("sdk/window/utils");
     9 const { openTab, closeTab, getBrowserForTab } = require("sdk/tabs/utils");
    10 const { defer } = require("sdk/core/promise");
    11 const { curry, identity, partial } = require("sdk/lang/functional");
    13 let when = curry(function(options, tab) {
    14   let type = options.type || options;
    15   let capture = options.capture || false;
    16   let target = getBrowserForTab(tab);
    17   let { promise, resolve } = defer();
    19   target.addEventListener(type, function handler(event) {
    20     if (!event.target.defaultView.frameElement) {
    21       target.removeEventListener(type, handler, capture);
    22       resolve(tab);
    23     }
    24   }, capture);
    26   return promise;
    27 });
    29 let use = function(value) function() value;
    32 let open = curry(function(url, window) openTab(window, url));
    33 let close = function(tab) {
    34   let promise = when("pagehide", tab);
    35   closeTab(tab);
    36   return promise;
    37 }
    39 exports["test multiple tabs"] = function(assert, done) {
    40   let loader = Loader(module);
    41   let { events } = loader.require("sdk/content/events");
    42   let { on, off } = loader.require("sdk/event/core");
    43   let actual = [];
    45   on(events, "data", handler);
    46   function handler ({type, target, timeStamp}) {
    47     eventFilter(type, target, () => {
    48       actual.push(type + " -> " + target.URL)
    49     });
    50   }
    52   let window = getMostRecentBrowserWindow();
    53   let firstTab = open("data:text/html,first-tab", window);
    55   when("pageshow", firstTab).
    56     then(close).
    57     then(use(window)).
    58     then(open("data:text/html,second-tab")).
    59     then(when("pageshow")).
    60     then(close).
    61     then(function() {
    62       assert.deepEqual(actual, [
    63         "document-element-inserted -> data:text/html,first-tab",
    64         "DOMContentLoaded -> data:text/html,first-tab",
    65         "load -> data:text/html,first-tab",
    66         "pageshow -> data:text/html,first-tab",
    67         "document-element-inserted -> data:text/html,second-tab",
    68         "DOMContentLoaded -> data:text/html,second-tab",
    69         "load -> data:text/html,second-tab",
    70         "pageshow -> data:text/html,second-tab"
    71       ], "all events dispatche as expeced")
    72     }, function(reason) {
    73       assert.fail(Error(reason));
    74     }).then(function() {
    75       loader.unload();
    76       off(events, "data", handler);
    77       done();
    78     });
    79 };
    81 exports["test nested frames"] = function(assert, done) {
    82   let loader = Loader(module);
    83   let { events } = loader.require("sdk/content/events");
    84   let { on, off } = loader.require("sdk/event/core");
    85   let actual = [];
    86   on(events, "data", handler);
    87   function handler ({type, target, timeStamp}) {
    88     eventFilter(type, target, () => {
    89       actual.push(type + " -> " + target.URL)
    90     });
    91   }
    93   let window =  getMostRecentBrowserWindow();
    94   let uri = encodeURI("data:text/html,<iframe src='data:text/html,iframe'>");
    95   let tab = open(uri, window);
    97   when("pageshow", tab).
    98     then(close).
    99     then(function() {
   100       assert.deepEqual(actual, [
   101         "document-element-inserted -> " + uri,
   102         "DOMContentLoaded -> " + uri,
   103         "document-element-inserted -> data:text/html,iframe",
   104         "DOMContentLoaded -> data:text/html,iframe",
   105         "load -> data:text/html,iframe",
   106         "pageshow -> data:text/html,iframe",
   107         "load -> " + uri,
   108         "pageshow -> " + uri
   109       ], "events where dispatched")
   110     }, function(reason) {
   111       assert.fail(Error(reason))
   112     }).then(function() {
   113       loader.unload();
   114       off(events, "data", handler);
   115       done();
   116     });
   117 };
   119 // ignore *-document-global-created events that are not very consistent.
   120 // only allow data uris that we create to ignore unwanted events, e.g.,
   121 // about:blank, http:// requests from Fennec's `about:`home page that displays
   122 // add-ons a user could install, local `searchplugins`, other chrome uris
   123 // Calls callback if passes filter
   124 function eventFilter (type, target, callback) {
   125   if (target.URL.startsWith("data:text/html,") &&
   126     type !== "chrome-document-global-created" &&
   127     type !== "content-document-global-created")
   129     callback();
   130 }
   131 require("test").run(exports);

mercurial