summaryrefslogtreecommitdiffstats
path: root/dom/midi/tests/test_midi_device_connect_disconnect.html
blob: 338d1de55d16c2dd35f1de29bcd5def0c5003eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<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 output;

       let midi_access;
       try {
         midi_access = await navigator.requestMIDIAccess({ "sysex": false });
         ok(true, "MIDI Access Request successful");
       } catch (e) {
         ok(false, "MIDI Access Request failed!");
         SimpleTest.finish();
         return;
       }
       is(midi_access.sysexEnabled, false, "Sysex should be false");
       output = midi_access.outputs.get(await MIDITestUtils.outputInfo.id);
       let statePromiseRes;
       let statePromise = new Promise((res) => { statePromiseRes = res; });
       await output.open();
       let stateChangeHandler = (event) => {
         if (event.port == output) {
           return;
         }
         statePromiseRes(event.port);
       };
       midi_access.addEventListener("statechange", stateChangeHandler);
       // Send command to connect new port.
       output.send([0x90, 0x01, 0x00]);
       let p = await statePromise;
       is(p.state, "connected", "Device " + p.name + " connected");

       // Rebuild our promise, we'll need to await another one.
       statePromise = new Promise((res) => { statePromiseRes = res; });
       output.send([0x90, 0x02, 0x00]);
       p = await statePromise;
       is(p.state, "disconnected", "Device " + p.name + " disconnected");
       midi_access.removeEventListener("statechange", stateChangeHandler);
       SimpleTest.finish();
     }
    </script>
  </body>
</html>