summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/test_transferable.html
blob: a3deec12a228d5296d4c6e58fb1ec25a37a3d415 (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
<!--
  Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<!--
Tests of DOM Worker transferable objects
-->
<head>
  <title>Test for DOM Worker transferable objects</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" language="javascript">

  function test1(sizes) {
    if (!sizes.length) {
      runTests();
      return;
    }

    var size = sizes.pop();

    var worker = new Worker("transferable_worker.js");
    worker.onmessage = function(event) {
      ok(event.data.status, event.data.event);
      if (!event.data.status) {
        runTests();
        return;
      }

      if ("notEmpty" in event.data && "byteLength" in event.data.notEmpty) {
        ok(event.data.notEmpty.byteLength != 0,
           "P: NotEmpty object received: " + event.data.notEmpty.byteLength);
      }

      if (!event.data.last)
        return;

      test1(sizes);
    }
    worker.onerror = function(event) {
      ok(false, "No errors!");
    }

    try {
      worker.postMessage(42, true);
      ok(false, "P: PostMessage - Exception for wrong type");
    } catch(e) {
      ok(true, "P: PostMessage - Exception for wrong type");
    }

    try {
      ab = new ArrayBuffer(size);
      worker.postMessage(42,[ab, ab]);
      ok(false, "P: PostMessage - Exception for duplicate");
    } catch(e) {
      ok(true, "P: PostMessage - Exception for duplicate");
    }

    var ab = new ArrayBuffer(size);
    ok(ab.byteLength == size, "P: The size is: " + size + " == " + ab.byteLength);
    worker.postMessage({ data: 0, timeout: 0, ab: ab, cb: ab, size: size }, [ab]);
    ok(ab.byteLength == 0, "P: PostMessage - The size is: 0 == " + ab.byteLength)
  }

  function test2() {
    var worker = new Worker("transferable_worker.js");
    worker.onmessage = function(event) {
      ok(event.data.status, event.data.event);
      if (!event.data.status) {
        runTests();
        return;
      }

      if ("notEmpty" in event.data && "byteLength" in event.data.notEmpty) {
        ok(event.data.notEmpty.byteLength != 0,
           "P: NotEmpty object received: " + event.data.notEmpty.byteLength);
      }

      if (event.data.last) {
        runTests();
      }
    }
    worker.onerror = function(event) {
      ok(false, "No errors!");
    }

    var f = new Float32Array([0,1,2,3]);
    ok(f.byteLength != 0, "P: The size is: " + f.byteLength + " is not 0");
    worker.postMessage({ event: "P: postMessage with Float32Array", status: true,
                         size: 4, notEmpty: f, bc: [ f, f, { dd: f } ] }, [f.buffer]);
    ok(f.byteLength == 0, "P: The size is: " + f.byteLength + " is 0");
  }

  var tests = [
    function() { test1([1024 * 1024 * 32, 128, 4]); },
    test2
  ];
  function runTests() {
    if (!tests.length) {
      SimpleTest.finish();
      return;
    }

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

  SimpleTest.waitForExplicitFinish();
  runTests();

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