<!--
  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 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, cb: ab, 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>
