summaryrefslogtreecommitdiffstats
path: root/dom/ipc/tests/JSWindowActor/browser_destroy_callbacks.js
blob: 74cbae94150ccbec4ee29d55f0ee75971c46cf75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

declTest("destroy actor by iframe remove", {
  allFrames: true,

  async test(browser) {
    await SpecialPowers.spawn(browser, [], async function () {
      // Create and append an iframe into the window's document.
      let frame = content.document.createElement("iframe");
      frame.id = "frame";
      content.document.body.appendChild(frame);
      await ContentTaskUtils.waitForEvent(frame, "load");
      is(content.window.frames.length, 1, "There should be an iframe.");
      let child = frame.contentWindow.windowGlobalChild;
      let actorChild = child.getActor("TestWindow");
      ok(actorChild, "JSWindowActorChild should have value.");

      {
        let error = actorChild.uninitializedGetterError;
        const prop = "contentWindow";
        Assert.ok(
          error,
          `Should get error accessing '${prop}' before actor initialization`
        );
        if (error) {
          Assert.equal(
            error.name,
            "InvalidStateError",
            "Error should be an InvalidStateError"
          );
          Assert.equal(
            error.message,
            `JSWindowActorChild.${prop} getter: Cannot access property '${prop}' before actor is initialized`,
            "Error should have informative message"
          );
        }
      }

      let didDestroyPromise = new Promise(resolve => {
        const TOPIC = "test-js-window-actor-diddestroy";
        Services.obs.addObserver(function obs(subject, topic, data) {
          ok(data, "didDestroyCallback data should be true.");
          is(subject, actorChild, "Should have this value");

          Services.obs.removeObserver(obs, TOPIC);
          // Make a trip through the event loop to ensure that the
          // actor's manager has been cleared before running remaining
          // checks.
          Services.tm.dispatchToMainThread(resolve);
        }, TOPIC);
      });

      info("Remove frame");
      content.document.getElementById("frame").remove();
      await didDestroyPromise;

      Assert.throws(
        () => child.getActor("TestWindow"),
        /InvalidStateError/,
        "Should throw if frame destroy."
      );

      for (let prop of [
        "document",
        "browsingContext",
        "docShell",
        "contentWindow",
      ]) {
        let error;
        try {
          void actorChild[prop];
        } catch (e) {
          error = e;
        }
        Assert.ok(
          error,
          `Should get error accessing '${prop}' after actor destruction`
        );
        if (error) {
          Assert.equal(
            error.name,
            "InvalidStateError",
            "Error should be an InvalidStateError"
          );
          Assert.equal(
            error.message,
            `JSWindowActorChild.${prop} getter: Cannot access property '${prop}' after actor 'TestWindow' has been destroyed`,
            "Error should have informative message"
          );
        }
      }
    });
  },
});

declTest("destroy actor by page navigates", {
  allFrames: true,

  async test(browser) {
    info("creating an in-process frame");
    await SpecialPowers.spawn(browser, [URL], async function (url) {
      let frame = content.document.createElement("iframe");
      frame.src = url;
      content.document.body.appendChild(frame);
    });

    info("navigating page");
    await SpecialPowers.spawn(browser, [TEST_URL], async function (url) {
      let frame = content.document.querySelector("iframe");
      frame.contentWindow.location = url;
      let child = frame.contentWindow.windowGlobalChild;
      let actorChild = child.getActor("TestWindow");
      ok(actorChild, "JSWindowActorChild should have value.");

      let didDestroyPromise = new Promise(resolve => {
        const TOPIC = "test-js-window-actor-diddestroy";
        Services.obs.addObserver(function obs(subject, topic, data) {
          ok(data, "didDestroyCallback data should be true.");
          is(subject, actorChild, "Should have this value");

          Services.obs.removeObserver(obs, TOPIC);
          resolve();
        }, TOPIC);
      });

      await Promise.all([
        didDestroyPromise,
        ContentTaskUtils.waitForEvent(frame, "load"),
      ]);

      Assert.throws(
        () => child.getActor("TestWindow"),
        /InvalidStateError/,
        "Should throw if frame destroy."
      );
    });
  },
});

declTest("destroy actor by tab being closed", {
  allFrames: true,

  async test(browser) {
    info("creating a new tab");
    let newTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, URL);
    let newTabBrowser = newTab.linkedBrowser;

    let parent =
      newTabBrowser.browsingContext.currentWindowGlobal.getActor("TestWindow");
    ok(parent, "JSWindowActorParent should have value.");

    // We can't depend on `SpecialPowers.spawn` to resolve our promise, as the
    // frame message manager will be being shut down at the same time. Instead
    // send messages over the per-process message manager which should still be
    // active.
    let didDestroyPromise = new Promise(resolve => {
      Services.ppmm.addMessageListener(
        "test-jswindowactor-diddestroy",
        function onmessage(msg) {
          Services.ppmm.removeMessageListener(
            "test-jswindowactor-diddestroy",
            onmessage
          );
          resolve();
        }
      );
    });

    info("setting up destroy listeners");
    await SpecialPowers.spawn(newTabBrowser, [], () => {
      let child = content.windowGlobalChild;
      let actorChild = child.getActor("TestWindow");
      ok(actorChild, "JSWindowActorChild should have value.");

      Services.obs.addObserver(function obs(subject, topic, data) {
        if (subject != actorChild) {
          return;
        }
        dump("DidDestroy called\n");
        Services.obs.removeObserver(obs, "test-js-window-actor-diddestroy");
        Services.cpmm.sendAsyncMessage("test-jswindowactor-diddestroy", data);
      }, "test-js-window-actor-diddestroy");
    });

    info("removing new tab");
    await BrowserTestUtils.removeTab(newTab);
    info("waiting for destroy callbacks to fire");
    await didDestroyPromise;
    info("got didDestroy callback");
  },
});