test_transferable.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <!DOCTYPE HTML>
  6. <html>
  7. <!--
  8. Tests of DOM Worker transferable objects
  9. -->
  10. <head>
  11. <title>Test for DOM Worker transferable objects</title>
  12. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  13. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  14. </head>
  15. <body>
  16. <p id="display"></p>
  17. <div id="content" style="display: none">
  18. </div>
  19. <pre id="test">
  20. <script class="testbody" language="javascript">
  21. function test1(sizes) {
  22. if (!sizes.length) {
  23. runTests();
  24. return;
  25. }
  26. var size = sizes.pop();
  27. var worker = new Worker("transferable_worker.js");
  28. worker.onmessage = function(event) {
  29. ok(event.data.status, event.data.event);
  30. if (!event.data.status) {
  31. runTests();
  32. return;
  33. }
  34. if ("notEmpty" in event.data && "byteLength" in event.data.notEmpty) {
  35. ok(event.data.notEmpty.byteLength != 0,
  36. "P: NotEmpty object received: " + event.data.notEmpty.byteLength);
  37. }
  38. if (!event.data.last)
  39. return;
  40. test1(sizes);
  41. }
  42. worker.onerror = function(event) {
  43. ok(false, "No errors!");
  44. }
  45. try {
  46. worker.postMessage(42, true);
  47. ok(false, "P: PostMessage - Exception for wrong type");
  48. } catch(e) {
  49. ok(true, "P: PostMessage - Exception for wrong type");
  50. }
  51. try {
  52. ab = new ArrayBuffer(size);
  53. worker.postMessage(42,[ab, ab]);
  54. ok(false, "P: PostMessage - Exception for duplicate");
  55. } catch(e) {
  56. ok(true, "P: PostMessage - Exception for duplicate");
  57. }
  58. var ab = new ArrayBuffer(size);
  59. ok(ab.byteLength == size, "P: The size is: " + size + " == " + ab.byteLength);
  60. worker.postMessage({ data: 0, timeout: 0, ab: ab, cb: ab, size: size }, [ab]);
  61. ok(ab.byteLength == 0, "P: PostMessage - The size is: 0 == " + ab.byteLength)
  62. }
  63. function test2() {
  64. var worker = new Worker("transferable_worker.js");
  65. worker.onmessage = function(event) {
  66. ok(event.data.status, event.data.event);
  67. if (!event.data.status) {
  68. runTests();
  69. return;
  70. }
  71. if ("notEmpty" in event.data && "byteLength" in event.data.notEmpty) {
  72. ok(event.data.notEmpty.byteLength != 0,
  73. "P: NotEmpty object received: " + event.data.notEmpty.byteLength);
  74. }
  75. if (event.data.last) {
  76. runTests();
  77. }
  78. }
  79. worker.onerror = function(event) {
  80. ok(false, "No errors!");
  81. }
  82. var f = new Float32Array([0,1,2,3]);
  83. ok(f.byteLength != 0, "P: The size is: " + f.byteLength + " is not 0");
  84. worker.postMessage({ event: "P: postMessage with Float32Array", status: true,
  85. size: 4, notEmpty: f, bc: [ f, f, { dd: f } ] }, [f.buffer]);
  86. ok(f.byteLength == 0, "P: The size is: " + f.byteLength + " is 0");
  87. }
  88. var tests = [
  89. function() { test1([1024 * 1024 * 32, 128, 4]); },
  90. test2
  91. ];
  92. function runTests() {
  93. if (!tests.length) {
  94. SimpleTest.finish();
  95. return;
  96. }
  97. var test = tests.shift();
  98. test();
  99. }
  100. SimpleTest.waitForExplicitFinish();
  101. runTests();
  102. </script>
  103. </pre>
  104. </body>
  105. </html>