Bug 1526406 - Part 2: Add tests for observers on JS Window Actor Protocols. r=nika

Depends on D21364

Differential Revision: https://phabricator.services.mozilla.com/D21365

--HG--
extra : moz-landing-system : lando
This commit is contained in:
John Dai 2019-03-01 18:24:58 +00:00
Родитель 834792ab94
Коммит 12e61efb81
3 изменённых файлов: 85 добавлений и 9 удалений

Просмотреть файл

@ -13,6 +13,10 @@ let windowActorOptions = {
events: {
"mozshowdropdown": {},
},
observers: [
"test-js-window-actor-child-observer",
],
},
};
@ -141,3 +145,58 @@ add_task(async function test_events() {
});
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function test_observers() {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
await BrowserTestUtils.withNewTab({gBrowser, url: URL}, async browser => {
await ContentTask.spawn(browser, {}, async function() {
const TOPIC = "test-js-window-actor-child-observer";
Services.obs.notifyObservers(content.window, TOPIC, "dataString");
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
let {subject, topic, data} = actorChild.lastObserved;
is(subject.getWindowGlobalChild().getActor("Test"), actorChild, "Should have been recieved on the right actor");
is(topic, TOPIC, "Topic matches");
is(data, "dataString", "Data matches");
});
});
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function test_observers_with_null_data() {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
await BrowserTestUtils.withNewTab({gBrowser, url: URL}, async browser => {
await ContentTask.spawn(browser, {}, async function() {
const TOPIC = "test-js-window-actor-child-observer";
Services.obs.notifyObservers(content.window, TOPIC);
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
let {subject, topic, data} = actorChild.lastObserved;
is(subject.getWindowGlobalChild().getActor("Test"), actorChild, "Should have been recieved on the right actor");
is(topic, TOPIC, "Topic matches");
is(data, null, "Data matches");
});
});
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function test_observers_dont_notify_with_wrong_window() {
ChromeUtils.registerWindowActor("Test", windowActorOptions);
await BrowserTestUtils.withNewTab({gBrowser, url: URL}, async browser => {
await ContentTask.spawn(browser, {}, async function() {
const TOPIC = "test-js-window-actor-child-observer";
Services.obs.notifyObservers(null, TOPIC);
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
is(actorChild.lastObserved, undefined, "Should not receive wrong window's observer notification!");
});
});
ChromeUtils.unregisterWindowActor("Test");
});

Просмотреть файл

@ -15,6 +15,7 @@
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const URL = "about:blank";
let windowActorOptions = {
parent: {
@ -22,6 +23,9 @@
},
child: {
moduleURI: "resource://testing-common/TestChild.jsm",
observers: [
"test-js-window-actor-child-observer",
],
},
};
@ -34,15 +38,6 @@
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function getActor() {
// Test in-process getActor function
ChromeUtils.registerWindowActor("Test", windowActorOptions);
SimpleTest.doesThrow(() =>
ChromeUtils.registerWindowActor("Test", windowActorOptions),
"Should throw if register has duplicate name.");
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function getActor() {
// Test in-process getActor function
ChromeUtils.registerWindowActor("Test", windowActorOptions);
@ -81,5 +76,23 @@
await promise;
ChromeUtils.unregisterWindowActor("Test");
});
add_task(async function observers() {
// Test in-process observers notification
ChromeUtils.registerWindowActor("Test", windowActorOptions);
const TOPIC = "test-js-window-actor-child-observer";
Services.obs.notifyObservers(content.window, TOPIC, "dataString");
let child = content.window.getWindowGlobalChild();
let actorChild = child.getActor("Test");
ok(actorChild, "JSWindowActorChild should have value.");
let {subject, topic, data} = actorChild.lastObserved;
is(subject.getWindowGlobalChild().getActor("Test"), actorChild, "Should have been recieved on the right actor");
is(topic, TOPIC, "Topic matches");
is(data, "dataString", "Data matches");
ChromeUtils.unregisterWindowActor("Test");
});
]]></script>
</window>

Просмотреть файл

@ -27,6 +27,10 @@ class TestChild extends JSWindowActorChild {
this.sendAsyncMessage("event", { type: aEvent.type });
}
observe(subject, topic, data) {
this.lastObserved = {subject, topic, data};
}
show() {
return "TestChild";
}