michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0: "use strict";
michael@0:
michael@0: module.metadata = {
michael@0: "engines": {
michael@0: "Firefox": "*"
michael@0: }
michael@0: };
michael@0:
michael@0: const { Cu } = require("chrome");
michael@0: const { Frame } = require("sdk/ui/frame");
michael@0: const { Toolbar } = require("sdk/ui/toolbar");
michael@0: const { Loader } = require("sdk/test/loader");
michael@0: const { identify } = require("sdk/ui/id");
michael@0: const { setTimeout } = require("sdk/timers");
michael@0: const { getMostRecentBrowserWindow, open } = require("sdk/window/utils");
michael@0: const { ready, loaded, close } = require("sdk/window/helpers");
michael@0: const { defer, all } = require("sdk/core/promise");
michael@0: const { send } = require("sdk/event/utils");
michael@0: const { object } = require("sdk/util/sequence");
michael@0: const { OutputPort } = require("sdk/output/system");
michael@0: const { Task } = Cu.import("resource://gre/modules/Task.jsm", {});
michael@0: const output = new OutputPort({ id: "toolbar-change" });
michael@0:
michael@0: const wait = (toolbar, event, times) => {
michael@0: let { promise, resolve } = defer();
michael@0: if (times) {
michael@0: let resolveArray = [];
michael@0: let counter = 0;
michael@0: toolbar.on(event, function onEvent (e) {
michael@0: resolveArray.push(e);
michael@0: if (++counter === times) {
michael@0: toolbar.off(event, onEvent);
michael@0: resolve(resolveArray);
michael@0: }
michael@0: });
michael@0: }
michael@0: else {
michael@0: toolbar.once(event, resolve);
michael@0: }
michael@0: return promise;
michael@0: };
michael@0:
michael@0: const stateEventsFor = frame =>
michael@0: [wait(frame, "attach"), wait(frame, "ready"),
michael@0: wait(frame, "load"), wait(frame, "detach")];
michael@0:
michael@0:
michael@0: const isAttached = ({id}, window=getMostRecentBrowserWindow()) =>
michael@0: !!window.document.getElementById(id);
michael@0:
michael@0: // Use `Task.spawn` instead of `Task.async` because the returned function does not contain
michael@0: // a length for the test harness to determine whether the test should be executed
michael@0: exports["test frame API"] = function* (assert) {
michael@0: const url = "data:text/html,frame-api";
michael@0: assert.throws(() => new Frame(),
michael@0: /The `options.url`/,
michael@0: "must provide url");
michael@0:
michael@0: assert.throws(() => new Frame({ url: "http://mozilla.org/" }),
michael@0: /The `options.url`/,
michael@0: "options.url must be local url");
michael@0:
michael@0: assert.throws(() => new Frame({url: url, name: "4you" }),
michael@0: /The `option.name` must be a valid/,
michael@0: "can only take valid names");
michael@0:
michael@0: const f1 = new Frame({ url: url });
michael@0:
michael@0: assert.ok(f1.id, "frame has an id");
michael@0: assert.equal(f1.url, void(0), "frame has no url until it's loaded");
michael@0: assert.equal(typeof(f1.postMessage), "function",
michael@0: "frames can postMessages");
michael@0:
michael@0: const p1 = wait(f1, "register");
michael@0:
michael@0: assert.throws(() => new Frame({ url: url }),
michael@0: /Frame with this id already exists/,
michael@0: "can't have two identical frames");
michael@0:
michael@0:
michael@0: const f2 = new Frame({ name: "frame-2", url: url });
michael@0: assert.pass("can create frame with same url but diff name");
michael@0: const p2 = wait(f2, "register");
michael@0:
michael@0: yield p1;
michael@0: assert.pass("frame#1 was registered");
michael@0: assert.equal(f1.url, url, "once registered it get's url");
michael@0:
michael@0: yield p2;
michael@0: assert.pass("frame#2 was registered");
michael@0: assert.equal(f2.url, url, "once registered it get's url");
michael@0:
michael@0: f1.destroy();
michael@0: const f3 = new Frame({ url: url });
michael@0: assert.pass("frame identical to destroyed one can be created");
michael@0:
michael@0: yield wait(f3, "register");
michael@0: assert.equal(f3.url, url, "url is set");
michael@0: f2.destroy();
michael@0: f3.destroy();
michael@0: };
michael@0:
michael@0: exports["test frame in toolbar"] = function* (assert) {
michael@0: const assertEvent = (event, type) => {
michael@0: assert.ok(event, "`" + type + "` event was dispatched");
michael@0: assert.equal(event.type, type, "event.type is: " + type);
michael@0: assert.equal(typeof(event.source), "object",
michael@0: "event.source is an object");
michael@0: assert.equal(typeof(event.source.postMessage), "function",
michael@0: "messages can be posted to event.source");
michael@0: };
michael@0:
michael@0:
michael@0: const url = "data:text/html,toolbar-frame";
michael@0: const f1 = new Frame({ url: url });
michael@0: const t1 = new Toolbar({
michael@0: title: "frame toolbar",
michael@0: items: [f1]
michael@0: });
michael@0:
michael@0: const w1 = getMostRecentBrowserWindow();
michael@0: const [a1, r1, l1] = stateEventsFor(f1);
michael@0:
michael@0: assertEvent((yield a1), "attach");
michael@0: assert.ok(isAttached(f1, w1), "frame is in the window#1");
michael@0: assertEvent((yield r1), "ready");
michael@0: assertEvent((yield l1), "load");
michael@0:
michael@0: const [a2, r2, l2] = stateEventsFor(f1);
michael@0: const w2 = open();
michael@0:
michael@0: assertEvent((yield a2), "attach");
michael@0: assert.ok(isAttached(f1, w2), "frame is in the window#2");
michael@0: assertEvent((yield r2), "ready");
michael@0: assertEvent((yield l2), "load");
michael@0: assert.pass("frame attached to window#2");
michael@0:
michael@0:
michael@0: const d1 = wait(f1, "detach");
michael@0: yield close(w2);
michael@0: assertEvent((yield d1), "detach");
michael@0: assert.pass("frame detached when window is closed");
michael@0:
michael@0: t1.destroy();
michael@0:
michael@0: assertEvent((yield wait(f1, "detach")), "detach");
michael@0: assert.ok(!isAttached(f1, w1), "frame was removed from window#1");
michael@0: assert.pass("toolbar destroy detaches frame");
michael@0: };
michael@0:
michael@0:
michael@0: exports["test host to content messaging"] = function* (assert) {
michael@0: const url = "data:text/html,";
michael@0: const f1 = new Frame({ name: "mailbox", url: url });
michael@0: const t1 = new Toolbar({ title: "mailbox", items: [f1] });
michael@0:
michael@0: const e1 = yield wait(f1, "ready");
michael@0: e1.source.postMessage("ping!", e1.origin);
michael@0:
michael@0: const pong = yield wait(f1, "message");
michael@0: assert.equal(pong.data, "pong!", "received ping back");
michael@0: t1.destroy();
michael@0:
michael@0: yield wait(t1, "detach");
michael@0: };
michael@0:
michael@0:
michael@0: exports["test content to host messaging"] = function* (assert) {
michael@0: const url = "data:text/html,";
michael@0:
michael@0: const f1 = new Frame({ name: "inbox", url: url });
michael@0: const t1 = new Toolbar({ title: "inbox", items: [f1] });
michael@0:
michael@0: const e1 = yield wait(f1, "message");
michael@0: assert.equal(e1.data, "ping!", "received ping from content");
michael@0:
michael@0: e1.source.postMessage("pong!", e1.origin);
michael@0:
michael@0: const e2 = yield wait(f1, "message");
michael@0: assert.equal(e2.data, "end", "received end message");
michael@0:
michael@0: t1.destroy();
michael@0: yield wait(t1, "detach");
michael@0:
michael@0: };
michael@0:
michael@0:
michael@0: exports["test direct messaging"] = function* (assert) {
michael@0: const url = "data:text/html,";
michael@0:
michael@0: const w1 = getMostRecentBrowserWindow();
michael@0: const f1 = new Frame({ url: url, name: "mail-cluster" });
michael@0: const t1 = new Toolbar({ title: "claster", items: [f1] });
michael@0:
michael@0: yield wait(f1, "ready");
michael@0: assert.pass("document loaded in window#1");
michael@0:
michael@0: const w2 = open();
michael@0:
michael@0: yield wait(f1, "ready");
michael@0: assert.pass("document loaded in window#2");
michael@0:
michael@0: let messages = wait(f1, "message", 2);
michael@0: f1.postMessage("inc", f1.origin);
michael@0: f1.postMessage("print", f1.origin);
michael@0:
michael@0: const [e1, e2] = yield messages;
michael@0: assert.deepEqual(e1.data, {n: 1}, "received message from window#1");
michael@0: assert.deepEqual(e2.data, {n: 1}, "received message from window#2");
michael@0:
michael@0: let message = wait(f1, "message");
michael@0: e1.source.postMessage("inc", e1.origin);
michael@0: e1.source.postMessage("print", e1.origin);
michael@0: const e3 = yield message;
michael@0: assert.deepEqual(e3.data, {n: 2}, "state changed in window#1");
michael@0:
michael@0: let message = wait(f1, "message");
michael@0: e2.source.postMessage("print", e2.origin);
michael@0: yield message;
michael@0: assert.deepEqual(e2.data, {n:1}, "window#2 didn't received inc message");
michael@0:
michael@0: yield close(w2);
michael@0: t1.destroy();
michael@0:
michael@0: yield wait(t1, "detach");
michael@0:
michael@0: };
michael@0:
michael@0: require("sdk/test").run(exports);