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

161 lines
4.7 KiB
JavaScript
Raw Normal View History

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const { Cc, Ci } = require('chrome');
const { Symbiont } = require('sdk/content/symbiont');
const self = require('sdk/self');
Bug 935220: Uplift Add-on SDK to Firefox. r=me https://github.com/mozilla/addon-sdk/compare/firefox27...50697e6182cb4f79f9605b7cd1d1f5084cfaf03f --HG-- rename : addon-sdk/source/data/index.html => addon-sdk/source/test/addons/addon-page/data/index.html rename : addon-sdk/source/test/favicon-helpers.js => addon-sdk/source/test/addons/places/favicon-helpers.js rename : addon-sdk/source/test/places-helper.js => addon-sdk/source/test/addons/places/places-helper.js rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/addons/symbiont/data/test-trusted-document.html rename : addon-sdk/source/data/index.html => addon-sdk/source/test/fixtures/index.html rename : addon-sdk/source/data/mofo_logo.SVG => addon-sdk/source/test/fixtures/mofo_logo.SVG rename : addon-sdk/source/data/moz_favicon.ico => addon-sdk/source/test/fixtures/moz_favicon.ico rename : addon-sdk/source/data/pagemod-css-include-file.css => addon-sdk/source/test/fixtures/pagemod-css-include-file.css rename : addon-sdk/source/data/test-content-symbiont.js => addon-sdk/source/test/fixtures/test-content-symbiont.js rename : addon-sdk/source/data/test-context-menu.js => addon-sdk/source/test/fixtures/test-context-menu.js rename : addon-sdk/source/data/test-iframe-postmessage.html => addon-sdk/source/test/fixtures/test-iframe-postmessage.html rename : addon-sdk/source/data/test-iframe.html => addon-sdk/source/test/fixtures/test-iframe.html rename : addon-sdk/source/data/test-iframe.js => addon-sdk/source/test/fixtures/test-iframe.js rename : addon-sdk/source/data/test-message-manager.js => addon-sdk/source/test/fixtures/test-message-manager.js rename : addon-sdk/source/data/test-net-url.txt => addon-sdk/source/test/fixtures/test-net-url.txt rename : addon-sdk/source/data/test-page-mod.html => addon-sdk/source/test/fixtures/test-page-mod.html rename : addon-sdk/source/data/test-page-worker.html => addon-sdk/source/test/fixtures/test-page-worker.html rename : addon-sdk/source/data/test-page-worker.js => addon-sdk/source/test/fixtures/test-page-worker.js rename : addon-sdk/source/data/test-sidebar-addon-global.html => addon-sdk/source/test/fixtures/test-sidebar-addon-global.html rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/fixtures/test-trusted-document.html rename : addon-sdk/source/data/test.html => addon-sdk/source/test/fixtures/test.html rename : addon-sdk/source/data/testLocalXhr.json => addon-sdk/source/test/fixtures/testLocalXhr.json
2013-11-05 13:51:58 -08:00
const fixtures = require("./fixtures");
const { close } = require('sdk/window/helpers');
const app = require("sdk/system/xul-app");
function makeWindow() {
let content =
'<?xml version="1.0"?>' +
'<window ' +
'xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">' +
'<iframe id="content" type="content"/>' +
'</window>';
var url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," +
encodeURIComponent(content);
var features = ["chrome", "width=10", "height=10"];
return Cc["@mozilla.org/embedcomp/window-watcher;1"].
getService(Ci.nsIWindowWatcher).
openWindow(null, url, null, features.join(","), null);
}
exports['test:constructing symbiont && validating API'] = function(assert) {
let contentScript = ["1;", "2;"];
Bug 935220: Uplift Add-on SDK to Firefox. r=me https://github.com/mozilla/addon-sdk/compare/firefox27...50697e6182cb4f79f9605b7cd1d1f5084cfaf03f --HG-- rename : addon-sdk/source/data/index.html => addon-sdk/source/test/addons/addon-page/data/index.html rename : addon-sdk/source/test/favicon-helpers.js => addon-sdk/source/test/addons/places/favicon-helpers.js rename : addon-sdk/source/test/places-helper.js => addon-sdk/source/test/addons/places/places-helper.js rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/addons/symbiont/data/test-trusted-document.html rename : addon-sdk/source/data/index.html => addon-sdk/source/test/fixtures/index.html rename : addon-sdk/source/data/mofo_logo.SVG => addon-sdk/source/test/fixtures/mofo_logo.SVG rename : addon-sdk/source/data/moz_favicon.ico => addon-sdk/source/test/fixtures/moz_favicon.ico rename : addon-sdk/source/data/pagemod-css-include-file.css => addon-sdk/source/test/fixtures/pagemod-css-include-file.css rename : addon-sdk/source/data/test-content-symbiont.js => addon-sdk/source/test/fixtures/test-content-symbiont.js rename : addon-sdk/source/data/test-context-menu.js => addon-sdk/source/test/fixtures/test-context-menu.js rename : addon-sdk/source/data/test-iframe-postmessage.html => addon-sdk/source/test/fixtures/test-iframe-postmessage.html rename : addon-sdk/source/data/test-iframe.html => addon-sdk/source/test/fixtures/test-iframe.html rename : addon-sdk/source/data/test-iframe.js => addon-sdk/source/test/fixtures/test-iframe.js rename : addon-sdk/source/data/test-message-manager.js => addon-sdk/source/test/fixtures/test-message-manager.js rename : addon-sdk/source/data/test-net-url.txt => addon-sdk/source/test/fixtures/test-net-url.txt rename : addon-sdk/source/data/test-page-mod.html => addon-sdk/source/test/fixtures/test-page-mod.html rename : addon-sdk/source/data/test-page-worker.html => addon-sdk/source/test/fixtures/test-page-worker.html rename : addon-sdk/source/data/test-page-worker.js => addon-sdk/source/test/fixtures/test-page-worker.js rename : addon-sdk/source/data/test-sidebar-addon-global.html => addon-sdk/source/test/fixtures/test-sidebar-addon-global.html rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/fixtures/test-trusted-document.html rename : addon-sdk/source/data/test.html => addon-sdk/source/test/fixtures/test.html rename : addon-sdk/source/data/testLocalXhr.json => addon-sdk/source/test/fixtures/testLocalXhr.json
2013-11-05 13:51:58 -08:00
let contentScriptFile = fixtures.url("test-content-symbiont.js");
// We can avoid passing a `frame` argument. Symbiont will create one
// by using HiddenFrame module
let contentSymbiont = Symbiont({
contentScriptFile: contentScriptFile,
contentScript: contentScript,
contentScriptWhen: "start"
});
assert.equal(
contentScriptFile,
contentSymbiont.contentScriptFile,
"There is one contentScriptFile, as specified in options."
);
assert.equal(
contentScript.length,
contentSymbiont.contentScript.length,
"There are two contentScripts, as specified in options."
);
assert.equal(
contentScript[0],
contentSymbiont.contentScript[0],
"There are two contentScripts, as specified in options."
);
assert.equal(
contentScript[1],
contentSymbiont.contentScript[1],
"There are two contentScripts, as specified in options."
)
assert.equal(
contentSymbiont.contentScriptWhen,
"start",
"contentScriptWhen is as specified in options."
);
contentSymbiont.destroy();
};
exports["test:communication with worker global scope"] = function(assert, done) {
if (app.is('Fennec')) {
assert.pass('Test skipped on Fennec');
done();
}
let window = makeWindow();
let contentSymbiont;
Bug 935220: Uplift Add-on SDK to Firefox. r=me https://github.com/mozilla/addon-sdk/compare/firefox27...50697e6182cb4f79f9605b7cd1d1f5084cfaf03f --HG-- rename : addon-sdk/source/data/index.html => addon-sdk/source/test/addons/addon-page/data/index.html rename : addon-sdk/source/test/favicon-helpers.js => addon-sdk/source/test/addons/places/favicon-helpers.js rename : addon-sdk/source/test/places-helper.js => addon-sdk/source/test/addons/places/places-helper.js rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/addons/symbiont/data/test-trusted-document.html rename : addon-sdk/source/data/index.html => addon-sdk/source/test/fixtures/index.html rename : addon-sdk/source/data/mofo_logo.SVG => addon-sdk/source/test/fixtures/mofo_logo.SVG rename : addon-sdk/source/data/moz_favicon.ico => addon-sdk/source/test/fixtures/moz_favicon.ico rename : addon-sdk/source/data/pagemod-css-include-file.css => addon-sdk/source/test/fixtures/pagemod-css-include-file.css rename : addon-sdk/source/data/test-content-symbiont.js => addon-sdk/source/test/fixtures/test-content-symbiont.js rename : addon-sdk/source/data/test-context-menu.js => addon-sdk/source/test/fixtures/test-context-menu.js rename : addon-sdk/source/data/test-iframe-postmessage.html => addon-sdk/source/test/fixtures/test-iframe-postmessage.html rename : addon-sdk/source/data/test-iframe.html => addon-sdk/source/test/fixtures/test-iframe.html rename : addon-sdk/source/data/test-iframe.js => addon-sdk/source/test/fixtures/test-iframe.js rename : addon-sdk/source/data/test-message-manager.js => addon-sdk/source/test/fixtures/test-message-manager.js rename : addon-sdk/source/data/test-net-url.txt => addon-sdk/source/test/fixtures/test-net-url.txt rename : addon-sdk/source/data/test-page-mod.html => addon-sdk/source/test/fixtures/test-page-mod.html rename : addon-sdk/source/data/test-page-worker.html => addon-sdk/source/test/fixtures/test-page-worker.html rename : addon-sdk/source/data/test-page-worker.js => addon-sdk/source/test/fixtures/test-page-worker.js rename : addon-sdk/source/data/test-sidebar-addon-global.html => addon-sdk/source/test/fixtures/test-sidebar-addon-global.html rename : addon-sdk/source/data/test-trusted-document.html => addon-sdk/source/test/fixtures/test-trusted-document.html rename : addon-sdk/source/data/test.html => addon-sdk/source/test/fixtures/test.html rename : addon-sdk/source/data/testLocalXhr.json => addon-sdk/source/test/fixtures/testLocalXhr.json
2013-11-05 13:51:58 -08:00
console.log(window)
function onMessage1(message) {
assert.equal(message, 1, "Program gets message via onMessage.");
contentSymbiont.removeListener('message', onMessage1);
contentSymbiont.on('message', onMessage2);
contentSymbiont.postMessage(2);
};
function onMessage2(message) {
if (5 == message) {
close(window).then(done);
}
else {
assert.equal(message, 3, "Program gets message via onMessage2.");
contentSymbiont.postMessage(4)
}
}
window.addEventListener("load", function onLoad() {
window.removeEventListener("load", onLoad, false);
let frame = window.document.getElementById("content");
contentSymbiont = Symbiont({
frame: frame,
contentScript: 'new ' + function() {
self.postMessage(1);
self.on("message", function onMessage(message) {
if (message === 2)
self.postMessage(3);
if (message === 4)
self.postMessage(5);
});
} + '()',
contentScriptWhen: 'ready',
onMessage: onMessage1
});
frame.setAttribute("src", "data:text/html;charset=utf-8,<html><body></body></html>");
}, false);
};
exports['test:pageWorker'] = function(assert, done) {
let worker = Symbiont({
contentURL: 'about:buildconfig',
contentScript: 'new ' + function WorkerScope() {
self.on('message', function(data) {
if (data.valid)
self.postMessage('bye!');
})
self.postMessage(window.location.toString());
},
onMessage: function(msg) {
if (msg == 'bye!') {
done()
} else {
assert.equal(
worker.contentURL + '',
msg
);
worker.postMessage({ valid: true });
}
}
});
};
exports["test:document element present on 'start'"] = function(assert, done) {
let xulApp = require("sdk/system/xul-app");
let worker = Symbiont({
contentURL: "about:buildconfig",
contentScript: "self.postMessage(!!document.documentElement)",
contentScriptWhen: "start",
onMessage: function(message) {
if (xulApp.versionInRange(xulApp.platformVersion, "2.0b6", "*"))
assert.ok(message, "document element present on 'start'");
else
assert.pass("document element not necessarily present on 'start'");
done();
}
});
};
require("test").run(exports);