gecko-dev/dom/ipc/tests/browser_JSWindowActor.js

144 строки
5.4 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const URL = "about:blank";
let windowActorOptions = {
parent: {
moduleURI: "resource://testing-common/TestParent.jsm",
},
child: {
moduleURI: "resource://testing-common/TestChild.jsm",
events: {
"mozshowdropdown": {},
},
},
};
add_task(function test_registerWindowActor() {
ok(ChromeUtils, "Should be able to get the ChromeUtils interface");
ChromeUtils.registerWindowActor("Test", windowActorOptions);
SimpleTest.doesThrow(() =>
ChromeUtils.registerWindowActor("Test", windowActorOptions),
"Should throw if register has duplicate name.");
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function test_getActor() {
await BrowserTestUtils.withNewTab({gBrowser, url: URL},
async function(browser) {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
let parent = browser.browsingContext.currentWindowGlobal;
ok(parent, "WindowGlobalParent should have value.");
let actorParent = parent.getActor("Test");
is(actorParent.show(), "TestParent", "actor show should have vaule.");
is(actorParent.manager, parent, "manager should match WindowGlobalParent.");
await ContentTask.spawn(
browser, {}, async function() {
let child = content.window.getWindowGlobalChild();
ok(child, "WindowGlobalChild should have value.");
is(child.isInProcess, false, "Actor should be loaded in the content process.");
let actorChild = child.getActor("Test");
is(actorChild.show(), "TestChild", "actor show should have vaule.");
is(actorChild.manager, child, "manager should match WindowGlobalChild.");
});
ChromeUtils.unregisterWindowActor("Test");
});
});
add_task(async function test_asyncMessage() {
await BrowserTestUtils.withNewTab({gBrowser, url: URL},
async function(browser) {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
let parent = browser.browsingContext.currentWindowGlobal;
let actorParent = parent.getActor("Test");
ok(actorParent, "JSWindowActorParent should have value.");
await ContentTask.spawn(
browser, {}, async function() {
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
let promise = new Promise(resolve => {
actorChild.sendAsyncMessage("init", {});
actorChild.done = (data) => resolve(data);
}).then(data => {
ok(data.initial, "Initial should be true.");
ok(data.toParent, "ToParent should be true.");
ok(data.toChild, "ToChild should be true.");
});
await promise;
});
ChromeUtils.unregisterWindowActor("Test");
});
});
add_task(async function test_asyncMessage_without_both_side_actor() {
await BrowserTestUtils.withNewTab({gBrowser, url: URL},
async function(browser) {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
// If we don't create a parent actor, make sure the parent actor
// gets created by having sent the message.
await ContentTask.spawn(
browser, {}, async function() {
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
let promise = new Promise(resolve => {
actorChild.sendAsyncMessage("init", {});
actorChild.done = (data) => resolve(data);
}).then(data => {
ok(data.initial, "Initial should be true.");
ok(data.toParent, "ToParent should be true.");
ok(data.toChild, "ToChild should be true.");
});
await promise;
});
ChromeUtils.unregisterWindowActor("Test");
});
});
add_task(async function test_events() {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
await BrowserTestUtils.withNewTab({gBrowser, url: URL}, async browser => {
// Add a select element to the DOM of the loaded document.
await ContentTask.spawn(browser, {}, async function() {
content.document.body.innerHTML += `
<select id="testSelect">
<option>A</option>
<option>B</option>
</select>`;
});
// Wait for the observer notification.
let observePromise = new Promise(resolve => {
const TOPIC = "test-js-window-actor-parent-event";
Services.obs.addObserver(function obs(subject, topic, data) {
is(topic, TOPIC, "topic matches");
Services.obs.removeObserver(obs, TOPIC);
resolve({subject, data});
}, TOPIC);
});
// Click on the select to show the dropdown.
await BrowserTestUtils.synthesizeMouseAtCenter("#testSelect", {}, browser);
// Wait for the observer notification to fire, and inspect the results.
let {subject, data} = await observePromise;
is(data, "mozshowdropdown");
let parent = browser.browsingContext.currentWindowGlobal;
let actorParent = parent.getActor("Test");
ok(actorParent, "JSWindowActorParent should have value.");
is(subject.wrappedJSObject, actorParent, "Should have been recieved on the right actor");
});
ChromeUtils.unregisterWindowActor("Test");
});