test_arraybufferinputstream.html 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. -->
  5. <head>
  6. <title>ArrayBuffer stream test</title>
  7. <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  8. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  9. <script type="text/javascript">
  10. function detachArrayBuffer(ab)
  11. {
  12. var w = new Worker("data:application/javascript,");
  13. w.postMessage(ab, [ab]);
  14. }
  15. function test()
  16. {
  17. var ab = new ArrayBuffer(4000);
  18. var ta = new Uint8Array(ab);
  19. ta[0] = 'a'.charCodeAt(0);
  20. ta[1] = 'b'.charCodeAt(0);
  21. const Cc = SpecialPowers.Cc, Ci = SpecialPowers.Ci, Cr = SpecialPowers.Cr;
  22. var abis = Cc["@mozilla.org/io/arraybuffer-input-stream;1"]
  23. .createInstance(Ci.nsIArrayBufferInputStream);
  24. var sis = Cc["@mozilla.org/scriptableinputstream;1"]
  25. .createInstance(Ci.nsIScriptableInputStream);
  26. sis.init(abis);
  27. is(sis.read(1), "", "should read no data from an uninitialized ABIS");
  28. abis.setData(ab, 0, 256 * 1024);
  29. is(sis.read(1), "a", "should read 'a' after init");
  30. detachArrayBuffer(ab);
  31. SpecialPowers.forceGC();
  32. SpecialPowers.forceGC();
  33. try
  34. {
  35. is(sis.read(1), "b", "should read 'b' after detaching buffer");
  36. }
  37. catch (e)
  38. {
  39. ok(false, "reading from stream should have worked");
  40. }
  41. // A regression test for bug 1265076. Previously, overflowing
  42. // the internal buffer from readSegments would cause incorrect
  43. // copying. The constant mirrors the value in
  44. // ArrayBufferInputStream::readSegments.
  45. var size = 8192;
  46. ab = new ArrayBuffer(2 * size);
  47. ta = new Uint8Array(ab);
  48. var i;
  49. for (i = 0; i < size; ++i) {
  50. ta[i] = 'x'.charCodeAt(0);
  51. }
  52. for (i = 0; i < size; ++i) {
  53. ta[size + i] = 'y'.charCodeAt(0);
  54. }
  55. abis = Cc["@mozilla.org/io/arraybuffer-input-stream;1"]
  56. .createInstance(Ci.nsIArrayBufferInputStream);
  57. abis.setData(ab, 0, 2 * size);
  58. sis = Cc["@mozilla.org/scriptableinputstream;1"]
  59. .createInstance(Ci.nsIScriptableInputStream);
  60. sis.init(abis);
  61. var result = sis.read(2 * size);
  62. is(result, "x".repeat(size) + "y".repeat(size), "correctly read the data");
  63. }
  64. test();
  65. </script>
  66. </head>
  67. <body>
  68. <p id="display"></p>
  69. <div id="content" style="display: none"></div>
  70. <pre id="test">
  71. </pre>
  72. </body>
  73. </html>