Commit 12e61efb authored by John Dai's avatar John Dai
Browse files

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
parent 834792ab
Loading
Loading
Loading
Loading
+59 −0
Original line number Diff line number Diff line
@@ -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");
});
+22 −9
Original line number Diff line number Diff line
@@ -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>
+4 −0
Original line number Diff line number Diff line
@@ -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";
  }