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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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

mercurial