summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/window_swapFrameLoaders.xul
blob: 3deae2e63c772855f9de1c2f7469ac58839a2ad1 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1242644
Test swapFrameLoaders with different frame types and remoteness
-->
<window title="Mozilla Bug 1242644"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>

  <script type="application/javascript"><![CDATA[
  ["SimpleTest", "SpecialPowers", "info", "is", "ok"].forEach(key => {
    window[key] = window.opener[key];
  })
  const { interfaces: Ci } = Components;

  const NS = {
    xul: "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
    html: "http://www.w3.org/1999/xhtml",
  }

  const TAG = {
    xul: "browser",
    html: "iframe", // mozbrowser
  }

  const SCENARIOS = [
    ["xul", "xul"],
    ["xul", "html"],
    ["html", "xul"],
    ["html", "html"],
    ["xul", "xul", "remote"],
    ["xul", "html", "remote"],
    ["html", "xul", "remote"],
    ["html", "html", "remote"],
  ];

  const HEIGHTS = [
    200,
    400
  ];

  function frameScript() {
    addEventListener("load", function onLoad() {
      sendAsyncMessage("test:load");
    }, true);
  }

  // Watch for loads in new frames
  window.messageManager.loadFrameScript(`data:,(${frameScript})();`, true);

  function once(target, eventName, useCapture = false) {
    info("Waiting for event: '" + eventName + "' on " + target + ".");

    return new Promise(resolve => {
      for (let [add, remove] of [
        ["addEventListener", "removeEventListener"],
        ["addMessageListener", "removeMessageListener"],
      ]) {
        if ((add in target) && (remove in target)) {
          target[add](eventName, function onEvent(...aArgs) {
            info("Got event: '" + eventName + "' on " + target + ".");
            target[remove](eventName, onEvent, useCapture);
            resolve(aArgs);
          }, useCapture);
          break;
        }
      }
    });
  }

  function* addFrame(type, remote, height) {
    let frame = document.createElementNS(NS[type], TAG[type]);
    frame.setAttribute("remote", remote);
    if (remote && type == "xul") {
      frame.setAttribute("style", "-moz-binding: none;");
    }
    if (type == "html") {
      frame.setAttribute("mozbrowser", "true");
      frame.setAttribute("noisolation", "true");
      frame.setAttribute("allowfullscreen", "true");
    } else if (type == "xul") {
      frame.setAttribute("type", "content");
    }
    let src = `data:text/html,<!doctype html>` +
              `<body style="height:${height}px"/>`;
    frame.setAttribute("src", src);
    document.documentElement.appendChild(frame);
    let mm = frame.frameLoader.messageManager;
    yield once(mm, "test:load");
    return frame;
  }

  add_task(function*() {
    yield new Promise(resolve => {
      SpecialPowers.pushPrefEnv(
        { "set": [["dom.mozBrowserFramesEnabled", true],
                  ["network.disable.ipc.security", true]] },
        resolve);
    });
  });

  add_task(function*() {
    for (let scenario of SCENARIOS) {
      let [ typeA, typeB, remote ] = scenario;
      remote = !!remote;
      let heightA = HEIGHTS[0];
      info(`Adding frame A, type ${typeA}, remote ${remote}, height ${heightA}`);
      let frameA = yield addFrame(typeA, remote, heightA);

      let heightB = HEIGHTS[1];
      info(`Adding frame B, type ${typeB}, remote ${remote}, height ${heightB}`);
      let frameB = yield addFrame(typeB, remote, heightB);

      let frameScriptFactory = function(name) {
        return `function() {
          addMessageListener("ping", function() {
            sendAsyncMessage("pong", "${name}");
          });
          addMessageListener("check-browser-api", function() {
            let exists = "api" in this;
            sendAsyncMessage("check-browser-api", {
              exists,
              running: exists && !this.api._shuttingDown,
            });
          });
        }`;
      }

      // Load frame script into each frame
      {
        let mmA = frameA.frameLoader.messageManager;
        let mmB = frameB.frameLoader.messageManager;

        mmA.loadFrameScript("data:,(" + frameScriptFactory("A") + ")()", false);
        mmB.loadFrameScript("data:,(" + frameScriptFactory("B") + ")()", false);
      }

      // Ping before swap
      {
        let mmA = frameA.frameLoader.messageManager;
        let mmB = frameB.frameLoader.messageManager;

        let inflightA = once(mmA, "pong");
        let inflightB = once(mmB, "pong");

        info("Ping message manager for frame A");
        mmA.sendAsyncMessage("ping");
        let [ { data: pongA } ] = yield inflightA;
        is(pongA, "A", "Frame A message manager gets reply A before swap");

        info("Ping message manager for frame B");
        mmB.sendAsyncMessage("ping");
        let [ { data: pongB } ] = yield inflightB;
        is(pongB, "B", "Frame B message manager gets reply B before swap");
      }

      // Check height before swap
      {
        if (frameA.getContentDimensions) {
          let { height } = yield frameA.getContentDimensions();
          is(height, heightA, "Frame A's content height is 200px before swap");
        }
        if (frameB.getContentDimensions) {
          let { height } = yield frameB.getContentDimensions();
          is(height, heightB, "Frame B's content height is 400px before swap");
        }
      }

      // Ping after swap using message managers acquired before
      {
        let mmA = frameA.frameLoader.messageManager;
        let mmB = frameB.frameLoader.messageManager;

        info("swapFrameLoaders");
        frameA.swapFrameLoaders(frameB);

        let inflightA = once(mmA, "pong");
        let inflightB = once(mmB, "pong");

        info("Ping message manager for frame A");
        mmA.sendAsyncMessage("ping");
        let [ { data: pongA } ] = yield inflightA;
        is(pongA, "B", "Frame A message manager acquired before swap gets reply B after swap");

        info("Ping message manager for frame B");
        mmB.sendAsyncMessage("ping");
        let [ { data: pongB } ] = yield inflightB;
        is(pongB, "A", "Frame B message manager acquired before swap gets reply A after swap");
      }

      // Check height after swap
      {
        if (frameA.getContentDimensions) {
          let { height } = yield frameA.getContentDimensions();
          is(height, heightB, "Frame A's content height is 400px after swap");
        }
        if (frameB.getContentDimensions) {
          let { height } = yield frameB.getContentDimensions();
          is(height, heightA, "Frame B's content height is 200px after swap");
        }
      }

      // Ping after swap using message managers acquired after
      {
        let mmA = frameA.frameLoader.messageManager;
        let mmB = frameB.frameLoader.messageManager;

        let inflightA = once(mmA, "pong");
        let inflightB = once(mmB, "pong");

        info("Ping message manager for frame A");
        mmA.sendAsyncMessage("ping");
        let [ { data: pongA } ] = yield inflightA;
        is(pongA, "B", "Frame A message manager acquired after swap gets reply B after swap");

        info("Ping message manager for frame B");
        mmB.sendAsyncMessage("ping");
        let [ { data: pongB } ] = yield inflightB;
        is(pongB, "A", "Frame B message manager acquired after swap gets reply A after swap");
      }

      // Verify browser API frame scripts destroyed if swapped out of browser frame
      if (frameA.hasAttribute("mozbrowser") != frameB.hasAttribute("mozbrowser")) {
        let mmA = frameA.frameLoader.messageManager;
        let mmB = frameB.frameLoader.messageManager;

        let inflightA = once(mmA, "check-browser-api");
        let inflightB = once(mmB, "check-browser-api");

        info("Check browser API for frame A");
        mmA.sendAsyncMessage("check-browser-api");
        let [ { data: apiA } ] = yield inflightA;
        if (frameA.hasAttribute("mozbrowser")) {
          ok(apiA.exists && apiA.running, "Frame A browser API exists and is running");
        } else {
          ok(apiA.exists && !apiA.running, "Frame A browser API did exist but is now destroyed");
        }

        info("Check browser API for frame B");
        mmB.sendAsyncMessage("check-browser-api");
        let [ { data: apiB } ] = yield inflightB;
        if (frameB.hasAttribute("mozbrowser")) {
          ok(apiB.exists && apiB.running, "Frame B browser API exists and is running");
        } else {
          ok(apiB.exists && !apiB.running, "Frame B browser API did exist but is now destroyed");
        }
      } else {
        info("Frames have matching mozbrowser state, skipping browser API destruction check");
      }

      frameA.remove();
      frameB.remove();
    }
  });
  ]]></script>
</window>