summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/test/addons/e10s-remote/main.js
blob: cea27af9b298fe7282199a5afd2d1bd8987ed615 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

const LOCAL_URI = "about:robots";
const REMOTE_URI = "data:text/html;charset=utf-8,remote";

const { Cu } = require('chrome');
const { Loader } = require('sdk/test/loader');
const { getTabs, openTab, closeTab, setTabURL, getBrowserForTab, getURI } = require('sdk/tabs/utils');
const { getMostRecentBrowserWindow } = require('sdk/window/utils');
const { cleanUI } = require("sdk/test/utils");
const { setTimeout } = require("sdk/timers");
const { promiseEvent, promiseDOMEvent, promiseEventOnItemAndContainer,
        waitForProcesses, getChildFrameCount, isE10S } = require("./utils");
const { after } = require('sdk/test/utils');
const { processID } = require('sdk/system/runtime');

const { set } = require('sdk/preferences/service');
// The hidden preload browser messes up our frame counts
set('browser.newtab.preload', false);

function promiseTabFrameAttach(frames) {
  return new Promise(resolve => {
    let listener = function(frame, ...args) {
      if (!frame.isTab)
        return;
      frames.off("attach", listener);
      resolve([frame, ...args]);
    }

    frames.on("attach", listener);
  });
}

// Check that we see a process stop and start
exports["test process restart"] = function*(assert) {
  if (!isE10S) {
    assert.pass("Skipping test in non-e10s mode");
    return;
  }

  let window = getMostRecentBrowserWindow();

  let tabs = getTabs(window);
  assert.equal(tabs.length, 1, "Should have just the one tab to start with");
  let tab = tabs[0];
  let browser = getBrowserForTab(tab);

  let loader = new Loader(module);
  let { processes, frames } = yield waitForProcesses(loader);

  let remoteProcess = Array.filter(processes, p => p.isRemote)[0];
  let localProcess = Array.filter(processes, p => !p.isRemote)[0];
  let remoteFrame = Array.filter(frames, f => f.process == remoteProcess)[0];

  // Switch the remote tab to a local URI which should kill the remote process

  let frameDetach = promiseEventOnItemAndContainer(assert, remoteFrame, frames, 'detach');
  let frameAttach = promiseTabFrameAttach(frames);
  let processDetach = promiseEventOnItemAndContainer(assert, remoteProcess, processes, 'detach');
  let browserLoad = promiseDOMEvent(browser, "load", true);
  setTabURL(tab, LOCAL_URI);
  // The load should kill the remote frame
  yield frameDetach;
  // And create a new frame in the local process
  let [newFrame] = yield frameAttach;
  assert.equal(newFrame.process, localProcess, "New frame should be in the local process");
  // And kill the process
  yield processDetach;
  yield browserLoad;

  frameDetach = promiseEventOnItemAndContainer(assert, newFrame, frames, 'detach');
  let processAttach = promiseEvent(processes, 'attach');
  frameAttach = promiseTabFrameAttach(frames);
  browserLoad = promiseDOMEvent(browser, "load", true);
  setTabURL(tab, REMOTE_URI);
  // The load should kill the remote frame
  yield frameDetach;
  // And create a new remote process
  [remoteProcess] = yield processAttach;
  assert.ok(remoteProcess.isRemote, "Process should be remote");
  // And create a new frame in the remote process
  [newFrame] = yield frameAttach;
  assert.equal(newFrame.process, remoteProcess, "New frame should be in the remote process");
  yield browserLoad;

  browserLoad = promiseDOMEvent(browser, "load", true);
  setTabURL(tab, "about:blank");
  yield browserLoad;

  loader.unload();
};

// Test that we find the right number of processes and that messaging between
// them works and none of the streams cross
exports["test process list"] = function*(assert) {
  let loader = new Loader(module);
  let { processes } = loader.require('sdk/remote/parent');

  let processCount = 0;
  processes.forEvery(processes => processCount++);

  yield waitForProcesses(loader);

  let remoteProcesses = Array.filter(processes, process => process.isRemote);
  let localProcesses = Array.filter(processes, process => !process.isRemote);

  assert.equal(localProcesses.length, 1, "Should always be one process");

  if (isE10S) {
    assert.equal(remoteProcesses.length, 1, "Should be one remote process");
  }
  else {
    assert.equal(remoteProcesses.length, 0, "Should be no remote processes");
  }

  assert.equal(processCount, processes.length, "Should have seen all processes");

  processCount = 0;
  processes.forEvery(process => processCount++);

  assert.equal(processCount, processes.length, "forEvery should send existing processes to the listener");

  localProcesses[0].port.on('sdk/test/pong', (process, key) => {
    assert.equal(key, "local", "Should not have seen a pong from the local process with the wrong key");
  });

  if (isE10S) {
    remoteProcesses[0].port.on('sdk/test/pong', (process, key) => {
      assert.equal(key, "remote", "Should not have seen a pong from the remote process with the wrong key");
    });
  }

  let promise = promiseEventOnItemAndContainer(assert, localProcesses[0].port, processes.port, 'sdk/test/pong', localProcesses[0]);
  localProcesses[0].port.emit('sdk/test/ping', "local");

  let reply = yield promise;
  assert.equal(reply[0], "local", "Saw the process reply with the right key");

  if (isE10S) {
    promise = promiseEventOnItemAndContainer(assert, remoteProcesses[0].port, processes.port, 'sdk/test/pong', remoteProcesses[0]);
    remoteProcesses[0].port.emit('sdk/test/ping', "remote");

    reply = yield promise;
    assert.equal(reply[0], "remote", "Saw the process reply with the right key");

    assert.notEqual(localProcesses[0], remoteProcesses[0], "Processes should be different");
  }

  loader.unload();
};

// Test that the frame lists are kept up to date
exports["test frame list"] = function*(assert) {
  function browserFrames(list) {
    return Array.filter(list, b => b.isTab).length;
  }

  let window = getMostRecentBrowserWindow();

  let tabs = getTabs(window);
  assert.equal(tabs.length, 1, "Should have just the one tab to start with");

  let loader = new Loader(module);
  let { processes, frames } = yield waitForProcesses(loader);

  assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
  assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");

  let promise = promiseTabFrameAttach(frames);
  let tab1 = openTab(window, LOCAL_URI);
  let [frame1] = yield promise;
  assert.ok(!!frame1, "Should have seen the new frame");
  assert.ok(!frame1.process.isRemote, "Frame should not be remote");

  assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
  assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");

  promise = promiseTabFrameAttach(frames);
  let tab2 = openTab(window, REMOTE_URI);
  let [frame2] = yield promise;
  assert.ok(!!frame2, "Should have seen the new frame");
  if (isE10S)
    assert.ok(frame2.process.isRemote, "Frame should be remote");
  else
    assert.ok(!frame2.process.isRemote, "Frame should not be remote");

  assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
  assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");

  frames.port.emit('sdk/test/ping')
  yield new Promise(resolve => {
    let count = 0;
    let listener = () => {
      console.log("Saw pong");
      count++;
      if (count == frames.length) {
        frames.port.off('sdk/test/pong', listener);
        resolve();
      }
    };
    frames.port.on('sdk/test/pong', listener);
  });

  let badListener = () => {
    assert.fail("Should not have seen a response through this frame");
  }
  frame1.port.on('sdk/test/pong', badListener);
  frame2.port.emit('sdk/test/ping', 'b');
  let [key] = yield promiseEventOnItemAndContainer(assert, frame2.port, frames.port, 'sdk/test/pong', frame2);
  assert.equal(key, 'b', "Should have seen the right response");
  frame1.port.off('sdk/test/pong', badListener);

  frame2.port.on('sdk/test/pong', badListener);
  frame1.port.emit('sdk/test/ping', 'b');
  [key] = yield promiseEventOnItemAndContainer(assert, frame1.port, frames.port, 'sdk/test/pong', frame1);
  assert.equal(key, 'b', "Should have seen the right response");
  frame2.port.off('sdk/test/pong', badListener);

  promise = promiseEventOnItemAndContainer(assert, frame1, frames, 'detach');
  closeTab(tab1);
  yield promise;

  assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
  assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");

  promise = promiseEventOnItemAndContainer(assert, frame2, frames, 'detach');
  closeTab(tab2);
  yield promise;

  assert.equal(browserFrames(frames), getTabs(window).length, "Should be the right number of browser frames.");
  assert.equal((yield getChildFrameCount(processes)), frames.length, "Child processes should have the right number of frames");

  loader.unload();
};

// Test that multiple loaders get their own loaders in the child and messages
// don't cross. Unload should work
exports["test new loader"] = function*(assert) {
  let loader1 = new Loader(module);
  let { processes: processes1 } = yield waitForProcesses(loader1);

  let loader2 = new Loader(module);
  let { processes: processes2 } = yield waitForProcesses(loader2);

  let process1 = [...processes1][0];
  let process2 = [...processes2][0];

  process1.port.on('sdk/test/pong', (process, key) => {
    assert.equal(key, "a", "Should have seen the right pong");
  });

  process2.port.on('sdk/test/pong', (process, key) => {
    assert.equal(key, "b", "Should have seen the right pong");
  });

  process1.port.emit('sdk/test/ping', 'a');
  yield promiseEvent(process1.port, 'sdk/test/pong');

  process2.port.emit('sdk/test/ping', 'b');
  yield promiseEvent(process2.port, 'sdk/test/pong');

  loader1.unload();

  process2.port.emit('sdk/test/ping', 'b');
  yield promiseEvent(process2.port, 'sdk/test/pong');

  loader2.unload();
};

// Test that unloading the loader unloads the child instances
exports["test unload"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let promise = promiseTabFrameAttach(frames);
  let tab = openTab(window, "data:,<html/>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  assert.ok(!!frame, "Should have seen the new frame");

  promise = promiseDOMEvent(browser, 'hashchange');
  frame.port.emit('sdk/test/testunload');
  loader.unload("shutdown");
  yield promise;

  let hash = getURI(tab).replace(/.*#/, "");
  assert.equal(hash, "unloaded:shutdown", "Saw the correct hash change.")

  closeTab(tab);
}

// Test that unloading the loader causes the child to see frame detach events
exports["test frame detach on unload"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let promise = promiseTabFrameAttach(frames);
  let tab = openTab(window, "data:,<html/>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  assert.ok(!!frame, "Should have seen the new frame");

  promise = promiseDOMEvent(browser, 'hashchange');
  frame.port.emit('sdk/test/testdetachonunload');
  loader.unload("shutdown");
  yield promise;

  let hash = getURI(tab).replace(/.*#/, "");
  assert.equal(hash, "unloaded", "Saw the correct hash change.")

  closeTab(tab);
}

// Test that DOM event listener on the frame object works
exports["test frame event listeners"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let promise = promiseTabFrameAttach(frames);
  let tab = openTab(window, "data:text/html,<html></html>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  assert.ok(!!frame, "Should have seen the new frame");

  frame.port.emit('sdk/test/registerframeevent');
  promise = Promise.all([
    promiseEvent(frame.port, 'sdk/test/sawreply'),
    promiseEvent(frame.port, 'sdk/test/eventsent')
  ]);

  frame.port.emit('sdk/test/sendevent');
  yield promise;

  frame.port.emit('sdk/test/unregisterframeevent');
  promise = promiseEvent(frame.port, 'sdk/test/eventsent');
  frame.port.on('sdk/test/sawreply', () => {
    assert.fail("Should not have seen the event listener reply");
  });

  frame.port.emit('sdk/test/sendevent');
  yield promise;

  closeTab(tab);
  loader.unload();
}

// Test that DOM event listener on the frames object works
exports["test frames event listeners"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let promise = promiseTabFrameAttach(frames);
  let tab = openTab(window, "data:text/html,<html></html>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  assert.ok(!!frame, "Should have seen the new frame");

  frame.port.emit('sdk/test/registerframesevent');
  promise = Promise.all([
    promiseEvent(frame.port, 'sdk/test/sawreply'),
    promiseEvent(frame.port, 'sdk/test/eventsent')
  ]);

  frame.port.emit('sdk/test/sendevent');
  yield promise;

  frame.port.emit('sdk/test/unregisterframesevent');
  promise = promiseEvent(frame.port, 'sdk/test/eventsent');
  frame.port.on('sdk/test/sawreply', () => {
    assert.fail("Should not have seen the event listener reply");
  });

  frame.port.emit('sdk/test/sendevent');
  yield promise;

  closeTab(tab);
  loader.unload();
}

// Test that unloading unregisters frame DOM events
exports["test unload removes frame event listeners"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let loader2 = new Loader(module);
  let { frames: frames2 } = yield waitForProcesses(loader2);

  let promise = promiseTabFrameAttach(frames);
  let promise2 = promiseTabFrameAttach(frames2);
  let tab = openTab(window, "data:text/html,<html></html>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  let [frame2] = yield promise2;
  assert.ok(!!frame && !!frame2, "Should have seen the new frame");

  frame.port.emit('sdk/test/registerframeevent');
  promise = Promise.all([
    promiseEvent(frame2.port, 'sdk/test/sawreply'),
    promiseEvent(frame2.port, 'sdk/test/eventsent')
  ]);

  frame2.port.emit('sdk/test/sendevent');
  yield promise;

  loader.unload();

  promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
  frame2.port.on('sdk/test/sawreply', () => {
    assert.fail("Should not have seen the event listener reply");
  });

  frame2.port.emit('sdk/test/sendevent');
  yield promise;

  closeTab(tab);
  loader2.unload();
}

// Test that unloading unregisters frames DOM events
exports["test unload removes frames event listeners"] = function*(assert) {
  let window = getMostRecentBrowserWindow();
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let loader2 = new Loader(module);
  let { frames: frames2 } = yield waitForProcesses(loader2);

  let promise = promiseTabFrameAttach(frames);
  let promise2 = promiseTabFrameAttach(frames2);
  let tab = openTab(window, "data:text/html,<html></html>");
  let browser = getBrowserForTab(tab);
  yield promiseDOMEvent(browser, "load", true);
  let [frame] = yield promise;
  let [frame2] = yield promise2;
  assert.ok(!!frame && !!frame2, "Should have seen the new frame");

  frame.port.emit('sdk/test/registerframesevent');
  promise = Promise.all([
    promiseEvent(frame2.port, 'sdk/test/sawreply'),
    promiseEvent(frame2.port, 'sdk/test/eventsent')
  ]);

  frame2.port.emit('sdk/test/sendevent');
  yield promise;

  loader.unload();

  promise = promiseEvent(frame2.port, 'sdk/test/eventsent');
  frame2.port.on('sdk/test/sawreply', () => {
    assert.fail("Should not have seen the event listener reply");
  });

  frame2.port.emit('sdk/test/sendevent');
  yield promise;

  closeTab(tab);
  loader2.unload();
}

// Check that the child frame has the right properties
exports["test frame properties"] = function*(assert) {
  let loader = new Loader(module);
  let { frames } = yield waitForProcesses(loader);

  let promise = new Promise(resolve => {
    let count = frames.length;
    let listener = (frame, properties) => {
      assert.equal(properties.isTab, frame.isTab,
                   "Child frame should have the same isTab property");

      if (--count == 0) {
        frames.port.off('sdk/test/replyproperties', listener);
        resolve();
      }
    }

    frames.port.on('sdk/test/replyproperties', listener);
  })

  frames.port.emit('sdk/test/checkproperties');
  yield promise;

  loader.unload();
}

// Check that non-remote processes have the same process ID and remote processes
// have different IDs
exports["test processID"] = function*(assert) {
  let loader = new Loader(module);
  let { processes } = yield waitForProcesses(loader);

  for (let process of processes) {
    process.port.emit('sdk/test/getprocessid');
    let [p, ID] = yield promiseEvent(process.port, 'sdk/test/processid');
    if (process.isRemote) {
      assert.notEqual(ID, processID, "Remote processes should have a different process ID");
    }
    else {
      assert.equal(ID, processID, "Remote processes should have the same process ID");
    }
  }

  loader.unload();
}

// Check that sdk/remote/parent and sdk/remote/child can only be loaded in the
// appropriate loaders
exports["test cannot load in wrong loader"] = function*(assert) {
  let loader = new Loader(module);
  let { processes } = yield waitForProcesses(loader);

  try {
    require('sdk/remote/child');
    assert.fail("Should not have been able to load sdk/remote/child");
  }
  catch (e) {
    assert.ok(/Cannot load sdk\/remote\/child in a main process loader/.test(e),
              "Should have seen the right exception.");
  }

  for (let process of processes) {
    processes.port.emit('sdk/test/parentload');
    let [_, isChildLoader, loaded, message] = yield promiseEvent(processes.port, 'sdk/test/parentload');
    assert.ok(isChildLoader, "Process should see itself in a child loader.");
    assert.ok(!loaded, "Process couldn't load sdk/remote/parent.");
    assert.ok(/Cannot load sdk\/remote\/parent in a child loader/.test(message),
              "Should have seen the right exception.");
  }

  loader.unload();
};

exports["test send cpow"] = function*(assert) {
  if (!isE10S) {
    assert.pass("Skipping test in non-e10s mode");
    return;
  }

  let window = getMostRecentBrowserWindow();

  let tabs = getTabs(window);
  assert.equal(tabs.length, 1, "Should have just the one tab to start with");
  let tab = tabs[0];
  let browser = getBrowserForTab(tab);

  assert.ok(Cu.isCrossProcessWrapper(browser.contentWindow),
            "Should have a CPOW for the browser content window");

  let loader = new Loader(module);
  let { processes } = yield waitForProcesses(loader);

  processes.port.emitCPOW('sdk/test/cpow', ['foobar'], { window: browser.contentWindow });
  let [process, arg, id] = yield promiseEvent(processes.port, 'sdk/test/cpow');

  assert.ok(process.isRemote, "Response should come from the remote process");
  assert.equal(arg, "foobar", "Argument should have passed through");
  assert.equal(id, browser.outerWindowID, "Should have got the ID from the child");
};

after(exports, function*(name, assert) {
  yield cleanUI();
});

require('sdk/test/runner').runTestsFromModule(module);