<html>
  <head>
    <title>WebMIDI Listener 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);
       let access = await navigator.requestMIDIAccess({ "sysex": false });
       ok(true, "MIDI Access Request successful");
       is(access.sysexEnabled, false, "Sysex should be false");

       var checkCount = 0;
       var input;
       var output;
       function checkCallbacks(port) {
         if (checkCount < 2) {
           ok(port.connection === "open", "Got port " + port.connection + " for " + port.name);
         } else {
          ok(port.connection === "closed", "Got port " + port.connection + " for " + port.name);
         }

         checkCount++;
         if (checkCount == 4) {
           input.onstatechange = undefined;
           output.onstatechange = undefined;
           SimpleTest.finish();
         }
       }
       function checkReturn(event) {
         ok(true, "Got echo message back");
         MIDITestUtils.checkPacket(event.data, [0x90, 0x00, 0x7f]);
         input.close();
         output.close();
       }

       input = access.inputs.get(await MIDITestUtils.inputInfo.id);
       output = access.outputs.get(await MIDITestUtils.outputInfo.id);
       input.onstatechange = (event) => { checkCallbacks(event.port); };
       output.onstatechange = (event) => { checkCallbacks(event.port); };
       // Ports are closed. Fire rest of tests.
       input.onmidimessage = checkReturn;
       output.send([0x90, 0x00, 0x7F]);
     }
    </script>
  </body>
</html>
