<html>

<head>
  <title>WebMIDI Send Test</title>
  <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
  <script type="application/javascript" src="MIDITestUtils.js"></script>
</head>

<body onload="runTests()">
  <script class="testbody" type="application/javascript">
    SimpleTest.waitForExplicitFinish();

    async function runTests() {
      await MIDITestUtils.permissionSetup(true);
      const access = await navigator.requestMIDIAccess({ sysex: true });
      const output = access.outputs.get(await MIDITestUtils.stateTestOutputInfo.id);


      // Note on(off).
      output.send([0xff, 0x90, 0x00, 0x00, 0x90, 0x07, 0x00]);

      try {
        output.send([0x00, 0x01])
      } catch (ex) {
        ok(true, "Caught exception");
      }

      // Running status is not allowed in Web MIDI API.
      SimpleTest.doesThrow(() => output.send([0x00, 0x01]), "Running status is not allowed in Web MIDI API.");

      // Unexpected End of Sysex.
      SimpleTest.doesThrow(() => output.send([0xf7]), "Unexpected End of Sysex.");

      // Unexpected reserved status bytes.
      SimpleTest.doesThrow(() => output.send([0xf4]), "Unexpected reserved status byte 0xf4.");
      SimpleTest.doesThrow(() => output.send([0xf5]), "Unexpected reserved status byte 0xf5.");
      SimpleTest.doesThrow(() => output.send([0xf9]), "Unexpected reserved status byte 0xf9.");
      SimpleTest.doesThrow(() => output.send([0xfd]), "Unexpected reserved status byte 0xfd.");

      // Incomplete channel messages.
      SimpleTest.doesThrow(() => output.send([0x80]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0x80, 0x00]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0x90]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0x90, 0x00]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xa0]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xa0, 0x00]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xb0]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xb0, 0x00]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xc0]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xd0]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xe0]), "Incomplete channel message.");
      SimpleTest.doesThrow(() => output.send([0xe0, 0x00]), "Incomplete channel message.");

      // Incomplete system messages.
      SimpleTest.doesThrow(() => output.send([0xf1]), "Incomplete system message.");
      SimpleTest.doesThrow(() => output.send([0xf2]), "Incomplete system message.");
      SimpleTest.doesThrow(() => output.send([0xf2, 0x00]), "Incomplete system message.");
      SimpleTest.doesThrow(() => output.send([0xf3]), "Incomplete system message.");

      // Invalid data bytes.
      SimpleTest.doesThrow(() => output.send([0x80, 0x80, 0x00]), "Incomplete system message.");
      SimpleTest.doesThrow(() => output.send([0x80, 0x00, 0x80]), "Incomplete system message.");

      // Complete messages.
      output.send([0x80, 0x00, 0x00]);
      output.send([0x90, 0x00, 0x00]);
      output.send([0xa0, 0x00, 0x00]);
      output.send([0xb0, 0x00, 0x00]);
      output.send([0xc0, 0x00]);
      output.send([0xd0, 0x00]);
      output.send([0xe0, 0x00, 0x00]);

      // Real-Time messages.
      output.send([0xf8]);
      output.send([0xfa]);
      output.send([0xfb]);
      output.send([0xfc]);
      output.send([0xfe]);
      output.send([0xff]);

      // Valid messages with Real-Time messages.
      output.send([0x90, 0xff, 0xff, 0x00, 0xff, 0x01, 0xff, 0x80, 0xff, 0x00,
        0xff, 0xff, 0x00, 0xff, 0xff]);

      // Sysex messages.
      output.send([0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7]);
      output.send([0xf0, 0xf8, 0xf7, 0xff]);
      SimpleTest.doesThrow(() => output.send([0xf0, 0x80, 0xf7]), "Invalid sysex message.");
      SimpleTest.doesThrow(() => output.send([0xf0, 0xf0, 0xf7]), "Double begin sysex message.");
      SimpleTest.doesThrow(() => output.send([0xf0, 0xff, 0xf7, 0xf7]), "Double end sysex message.");

      // Reserved status bytes.
      SimpleTest.doesThrow(() => output.send([0xf4, 0x80, 0x00, 0x00]), "Reserved status byte.");
      SimpleTest.doesThrow(() => output.send([0x80, 0xf4, 0x00, 0x00]), "Reserved status byte.");
      SimpleTest.doesThrow(() => output.send([0x80, 0x00, 0xf4, 0x00]), "Reserved status byte.");
      SimpleTest.doesThrow(() => output.send([0x80, 0x00, 0x00, 0xf4]), "Reserved status byte.");
      SimpleTest.doesThrow(() => output.send([0xf0, 0xff, 0xf4, 0xf7]), "Reserved status byte.");

      // Invalid timestamps.
      SimpleTest.doesThrow(() => output.send([], NaN), "NaN timestamp.");
      SimpleTest.doesThrow(() => output.send([], Infinity), "Infinity timestamp.");
      SimpleTest.doesThrow(() => output.send(new Uint8Array(), NaN), "NaN timestamp.");
      SimpleTest.doesThrow(() => output.send(new Uint8Array(), Infinity), "Infinity timestamp.");

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

</html>
