<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1688616
-->
<head>
  <meta charset="utf-8">
  <title>Test for large ArrayBuffers and views</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1688616">Mozilla Bug 1688616</a>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
  <script type="application/javascript">
    /* global TestFunctions */

    SimpleTest.waitForExplicitFinish();
    SpecialPowers.pushPrefEnv({set: [["dom.expose_test_interfaces", true]]}, go);

    function checkThrowsTooLarge(f) {
      let ex;
      try{
        f();
        ok(false, "Should have thrown!");
      } catch (e) {
        ex = e;
      }
      ok(ex.toString().includes("larger than 2 GB"), "Got exception: " + ex);
    }

    function go() {
      let test = new TestFunctions();

      const gb = 1 * 1024 * 1024 * 1024;
      let ab = new ArrayBuffer(5 * gb);
      checkThrowsTooLarge(() => test.testNotAllowShared(ab));
      checkThrowsTooLarge(() => test.testAllowShared(ab));
      checkThrowsTooLarge(() => test.testDictWithAllowShared({arrayBuffer: ab}));
      checkThrowsTooLarge(() => test.testUnionOfBuffferSource(ab));
      checkThrowsTooLarge(() => { test.arrayBuffer = ab; });
      checkThrowsTooLarge(() => { test.allowSharedArrayBuffer = ab; });
      checkThrowsTooLarge(() => { test.sequenceOfArrayBuffer = [ab]; });
      checkThrowsTooLarge(() => { test.sequenceOfAllowSharedArrayBuffer = [ab]; });

      let ta = new Int8Array(ab, 0, 3 * gb);
      checkThrowsTooLarge(() => test.testNotAllowShared(ta));
      checkThrowsTooLarge(() => test.testAllowShared(ta));
      checkThrowsTooLarge(() => test.testDictWithAllowShared({arrayBufferView: ta}));
      checkThrowsTooLarge(() => test.testUnionOfBuffferSource(ta));
      checkThrowsTooLarge(() => { test.arrayBufferView = ta; });
      checkThrowsTooLarge(() => { test.allowSharedArrayBufferView = ta; });
      checkThrowsTooLarge(() => { test.sequenceOfArrayBufferView = [ta]; });
      checkThrowsTooLarge(() => { test.sequenceOfAllowSharedArrayBufferView = [ta]; });

      let dv = new DataView(ab);
      checkThrowsTooLarge(() => test.testNotAllowShared(dv));
      checkThrowsTooLarge(() => test.testAllowShared(dv));
      checkThrowsTooLarge(() => test.testDictWithAllowShared({arrayBufferView: dv}));
      checkThrowsTooLarge(() => test.testUnionOfBuffferSource(dv));
      checkThrowsTooLarge(() => { test.arrayBufferView = dv; });
      checkThrowsTooLarge(() => { test.allowSharedArrayBufferView = dv; });
      checkThrowsTooLarge(() => { test.sequenceOfArrayBufferView = [dv]; });
      checkThrowsTooLarge(() => { test.sequenceOfAllowSharedArrayBufferView = [dv]; });

      if (this.SharedArrayBuffer) {
        let sab = new SharedArrayBuffer(5 * gb);
        checkThrowsTooLarge(() => test.testAllowShared(sab));
        checkThrowsTooLarge(() => test.testDictWithAllowShared({allowSharedArrayBuffer: sab}));
        checkThrowsTooLarge(() => test.testUnionOfAllowSharedBuffferSource(sab));
        checkThrowsTooLarge(() => { test.allowSharedArrayBuffer = sab; });
        checkThrowsTooLarge(() => { test.sequenceOfAllowSharedArrayBuffer = [sab]; });

        let sta = new Int8Array(sab);
        checkThrowsTooLarge(() => test.testAllowShared(sta));
        checkThrowsTooLarge(() => test.testDictWithAllowShared({allowSharedArrayBufferView: sta}));
        checkThrowsTooLarge(() => test.testUnionOfAllowSharedBuffferSource(sta));
        checkThrowsTooLarge(() => { test.allowSharedArrayBufferView = sta; });
        checkThrowsTooLarge(() => { test.sequenceOfAllowSharedArrayBufferView = [sta]; });
      }

      // Small views on large buffers are fine.
      let ta2 = new Int8Array(ab, 4 * gb);
      is(ta2.byteLength, 1 * gb, "Small view on large ArrayBuffer");
      test.testNotAllowShared(ta2);
      test.arrayBufferView = ta2;

      SimpleTest.finish();
    }
  </script>
</body>
</html>
