summaryrefslogtreecommitdiffstats
path: root/dom/encoding/test/worker_helper.js
blob: ca34387784616db9e671a56386bc2e438eeb7cc2 (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
/*
 * worker_helper.js
 * bug 764234 tests
 */

// The undefined items here are used in the tests within the worker that this
// runs.
/* eslint-disable no-undef */

function runTestInWorker(files) {
  function workerRun() {
    var tests = [];
    var asserts;
    test = function (func, msg) {
      asserts = [];
      tests.push({ asserts, msg });
    };
    assert_equals = function (result, expected, msg) {
      asserts.push(["assert_equals", result, expected, msg]);
    };
    assert_true = function (condition, msg) {
      asserts.push(["assert_true", condition, msg]);
    };
    assert_unreached = function (condition, msg) {
      asserts.push(["assert_unreached", condition, msg]);
    };
    onmessage = function (event) {
      importScripts.apply(self, event.data);
      runTest();
      postMessage(tests);
    };
  }

  var url = URL.createObjectURL(
    new Blob([runTest.toString(), "\n\n", "(", workerRun.toString(), ")();"])
  );
  var worker = new Worker(url);
  var base = location.toString().replace(/\/[^\/]*$/, "/");
  worker.postMessage(
    files.map(function (f) {
      return base + f;
    })
  );
  worker.onmessage = function (event) {
    URL.revokeObjectURL(url);
    event.data.forEach(function (t) {
      test(function () {
        t.asserts.forEach(function (a) {
          let func = a.shift();
          self[func].apply(self, a);
        });
      }, "worker " + t.msg);
    });
    done();
  };
}