summaryrefslogtreecommitdiffstats
path: root/dom/file/ipc/tests/test_ipcBlob_workers.html
blob: a473948f254acd358c6f650843c4d7435dd1fbc4 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test IPCBlob and Workers</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript">

function test_workerOwner() {
  info("test_workerOwner");

  function workerScript() {
    onmessage = e => {
      e.ports[0].onmessage = event => {
        let reader = new FileReader();
        reader.readAsText(event.data);
        reader.onloadend = () => {
          let status = reader.result == 'hello world';
          postMessage(status);
        }
      }
    }
  }

  let mc = new MessageChannel();
  mc.port1.postMessage(new Blob(['hello world']));

  let workerUrl = URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));
  let worker = new Worker(workerUrl);

  worker.postMessage("", [mc.port2]);
  worker.onmessage = event => {
    ok(event.data, "All is done!");
    next();
  }
}

function test_workerToMainThread() {
  info("test_workerToMainThread");
  function workerScript() {
    onmessage = e => {
      e.ports[0].onmessage = event => {
        postMessage(event.data);
      }
    }
  }

  let mc = new MessageChannel();
  mc.port1.postMessage(new Blob(['hello world']));

  let workerUrl = URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));
  let worker = new Worker(workerUrl);

  worker.postMessage("", [mc.port2]);
  worker.onmessage = event => {
    info("Blob received back, terminate the worker and force GC");
    worker.terminate();
    worker = null;
    SpecialPowers.forceGC();

    var fr = new FileReader();
    fr.readAsText(event.data);
    fr.onloadend = () => {
      is(fr.result, "hello world", "Data matches");
      next();
    }
  }
}

function test_workerOwnerPlusFileReaderSync() {
  info("test_workerOwnerPlusFileReaderSync");

  function workerScript() {
    onmessage = e => {
      e.ports[0].onmessage = event => {
        let reader = new FileReaderSync();
        let status = reader.readAsText(event.data) == 'hello world';
         postMessage(status);
      }
    }
  }

  let mc = new MessageChannel();
  mc.port1.postMessage(new Blob(['hello world']));

  let workerUrl = URL.createObjectURL(new Blob(["(", workerScript.toString(), ")()"]));
  let worker = new Worker(workerUrl);

  worker.postMessage("", [mc.port2]);
  worker.onmessage = event => {
    ok(event.data, "All is done!");
    next();
  }
}

var tests = [
  test_workerOwner,
  test_workerToMainThread,
  test_workerOwnerPlusFileReaderSync,
];

function next() {
  if (!tests.length) {
    SimpleTest.finish();
    return;
  }

  var test = tests.shift();
  test();
}

SimpleTest.waitForExplicitFinish();
next();

</script>
</pre>
</body>
</html>