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

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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 "use strict";
michael@0 5
michael@0 6 const { Cc, Ci } = require('chrome');
michael@0 7 const { Symbiont } = require('sdk/deprecated/symbiont');
michael@0 8 const self = require('sdk/self');
michael@0 9 const fixtures = require("./fixtures");
michael@0 10 const { close } = require('sdk/window/helpers');
michael@0 11 const app = require("sdk/system/xul-app");
michael@0 12
michael@0 13 function makeWindow() {
michael@0 14 let content =
michael@0 15 '<?xml version="1.0"?>' +
michael@0 16 '<window ' +
michael@0 17 'xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' +
michael@0 18 '<iframe id="content" type="content"/>' +
michael@0 19 '</window>';
michael@0 20 var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
michael@0 21 encodeURIComponent(content);
michael@0 22 var features = ["chrome", "width=10", "height=10"];
michael@0 23
michael@0 24 return Cc["@mozilla.org/embedcomp/window-watcher;1"].
michael@0 25 getService(Ci.nsIWindowWatcher).
michael@0 26 openWindow(null, url, null, features.join(","), null);
michael@0 27 }
michael@0 28
michael@0 29 exports['test:constructing symbiont && validating API'] = function(assert) {
michael@0 30 let contentScript = ["1;", "2;"];
michael@0 31 let contentScriptFile = fixtures.url("test-content-symbiont.js");
michael@0 32
michael@0 33 // We can avoid passing a `frame` argument. Symbiont will create one
michael@0 34 // by using HiddenFrame module
michael@0 35 let contentSymbiont = Symbiont({
michael@0 36 contentScriptFile: contentScriptFile,
michael@0 37 contentScript: contentScript,
michael@0 38 contentScriptWhen: "start"
michael@0 39 });
michael@0 40
michael@0 41 assert.equal(
michael@0 42 contentScriptFile,
michael@0 43 contentSymbiont.contentScriptFile,
michael@0 44 "There is one contentScriptFile, as specified in options."
michael@0 45 );
michael@0 46 assert.equal(
michael@0 47 contentScript.length,
michael@0 48 contentSymbiont.contentScript.length,
michael@0 49 "There are two contentScripts, as specified in options."
michael@0 50 );
michael@0 51 assert.equal(
michael@0 52 contentScript[0],
michael@0 53 contentSymbiont.contentScript[0],
michael@0 54 "There are two contentScripts, as specified in options."
michael@0 55 );
michael@0 56 assert.equal(
michael@0 57 contentScript[1],
michael@0 58 contentSymbiont.contentScript[1],
michael@0 59 "There are two contentScripts, as specified in options."
michael@0 60 )
michael@0 61 assert.equal(
michael@0 62 contentSymbiont.contentScriptWhen,
michael@0 63 "start",
michael@0 64 "contentScriptWhen is as specified in options."
michael@0 65 );
michael@0 66
michael@0 67 contentSymbiont.destroy();
michael@0 68 };
michael@0 69
michael@0 70 exports["test:communication with worker global scope"] = function(assert, done) {
michael@0 71 if (app.is('Fennec')) {
michael@0 72 assert.pass('Test skipped on Fennec');
michael@0 73 done();
michael@0 74 }
michael@0 75
michael@0 76 let window = makeWindow();
michael@0 77 let contentSymbiont;
michael@0 78
michael@0 79 assert.ok(!!window, 'there is a window');
michael@0 80
michael@0 81 function onMessage1(message) {
michael@0 82 assert.equal(message, 1, "Program gets message via onMessage.");
michael@0 83 contentSymbiont.removeListener('message', onMessage1);
michael@0 84 contentSymbiont.on('message', onMessage2);
michael@0 85 contentSymbiont.postMessage(2);
michael@0 86 };
michael@0 87
michael@0 88 function onMessage2(message) {
michael@0 89 if (5 == message) {
michael@0 90 close(window).then(done);
michael@0 91 }
michael@0 92 else {
michael@0 93 assert.equal(message, 3, "Program gets message via onMessage2.");
michael@0 94 contentSymbiont.postMessage(4)
michael@0 95 }
michael@0 96 }
michael@0 97
michael@0 98 window.addEventListener("load", function onLoad() {
michael@0 99 window.removeEventListener("load", onLoad, false);
michael@0 100 let frame = window.document.getElementById("content");
michael@0 101 contentSymbiont = Symbiont({
michael@0 102 frame: frame,
michael@0 103 contentScript: 'new ' + function() {
michael@0 104 self.postMessage(1);
michael@0 105 self.on("message", function onMessage(message) {
michael@0 106 if (message === 2)
michael@0 107 self.postMessage(3);
michael@0 108 if (message === 4)
michael@0 109 self.postMessage(5);
michael@0 110 });
michael@0 111 } + '()',
michael@0 112 contentScriptWhen: 'ready',
michael@0 113 onMessage: onMessage1
michael@0 114 });
michael@0 115
michael@0 116 frame.setAttribute("src", "data:text/html;charset=utf-8,<html><body></body></html>");
michael@0 117 }, false);
michael@0 118 };
michael@0 119
michael@0 120 exports['test:pageWorker'] = function(assert, done) {
michael@0 121 let worker = Symbiont({
michael@0 122 contentURL: 'about:buildconfig',
michael@0 123 contentScript: 'new ' + function WorkerScope() {
michael@0 124 self.on('message', function(data) {
michael@0 125 if (data.valid)
michael@0 126 self.postMessage('bye!');
michael@0 127 })
michael@0 128 self.postMessage(window.location.toString());
michael@0 129 },
michael@0 130 onMessage: function(msg) {
michael@0 131 if (msg == 'bye!') {
michael@0 132 done()
michael@0 133 } else {
michael@0 134 assert.equal(
michael@0 135 worker.contentURL + '',
michael@0 136 msg
michael@0 137 );
michael@0 138 worker.postMessage({ valid: true });
michael@0 139 }
michael@0 140 }
michael@0 141 });
michael@0 142 };
michael@0 143
michael@0 144 exports["test:document element present on 'start'"] = function(assert, done) {
michael@0 145 let xulApp = require("sdk/system/xul-app");
michael@0 146 let worker = Symbiont({
michael@0 147 contentURL: "about:buildconfig",
michael@0 148 contentScript: "self.postMessage(!!document.documentElement)",
michael@0 149 contentScriptWhen: "start",
michael@0 150 onMessage: function(message) {
michael@0 151 if (xulApp.versionInRange(xulApp.platformVersion, "2.0b6", "*"))
michael@0 152 assert.ok(message, "document element present on 'start'");
michael@0 153 else
michael@0 154 assert.pass("document element not necessarily present on 'start'");
michael@0 155 done();
michael@0 156 }
michael@0 157 });
michael@0 158 };
michael@0 159
michael@0 160 require("test").run(exports);

mercurial