test_midi_device_pending.html (4746B)
1 <html> 2 <head> 3 <title>WebMIDI Listener Test</title> 4 <script src="/tests/SimpleTest/SimpleTest.js"></script> 5 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 6 <script type="application/javascript" src="MIDITestUtils.js"></script> 7 </head> 8 9 <body onload="runTests()"> 10 <script class="testbody" type="application/javascript"> 11 SimpleTest.waitForExplicitFinish(); 12 13 async function runTests() { 14 await MIDITestUtils.permissionSetup(true); 15 16 17 var output; 18 var test_ports = []; 19 let access; 20 21 let accessRes; 22 let accessPromise; 23 let portRes; 24 let portPromise; 25 26 function resetPromises() { 27 accessPromise = new Promise((res) => { accessRes = res; }); 28 portPromise = new Promise((res) => { portRes = res; }); 29 } 30 31 function accessStateChangeHandler(event) { 32 var p = event.port; 33 // We'll get an open event for the output control port. Ignore it. 34 if (p.name == MIDITestUtils.outputInfo.name) { 35 return; 36 } 37 accessRes(event); 38 } 39 40 function portStateChangeHandler(event) { 41 var p = event.port; 42 // We'll get an open event for the output control port. Ignore it. 43 if (p.name == MIDITestUtils.outputInfo.name) { 44 return; 45 } 46 portRes(event); 47 } 48 49 // Part 1: Create MIDIAccess object, attach state change listener to list for new connections 50 access = await navigator.requestMIDIAccess({ "sysex": false }); 51 ok(true, "MIDI Access Request successful"); 52 is(access.sysexEnabled, false, "Sysex should be false"); 53 access.addEventListener("statechange", accessStateChangeHandler); 54 55 // Part 2: open test device, make sure it connects, attach event handler to device object 56 output = access.outputs.get(await MIDITestUtils.outputInfo.id); 57 resetPromises(); 58 output.send([0x90, 0x01, 0x00]); 59 let accessEvent = await accessPromise; 60 let testPort = accessEvent.port; 61 test_ports.push(testPort); 62 testPort.addEventListener("statechange", portStateChangeHandler); 63 is(testPort.state, "connected", "Device " + testPort.name + " connected"); 64 65 // Part 3: Listen for port status change on open as both an access event 66 // and a port event. 67 resetPromises(); 68 testPort.open(); 69 accessEvent = await accessPromise; 70 is(testPort.connection, "open", "Connection " + testPort.name + " opened"); 71 let portEvent = await portPromise; 72 is(testPort.connection, "open", "Connection " + testPort.name + " opened"); 73 74 // Part 4: Disconnect port but don't close, check status to make sure we're pending. 75 resetPromises(); 76 output.send([0x90, 0x02, 0x00]); 77 accessEvent = await accessPromise; 78 is(testPort.connection, "pending", "Connection " + testPort.name + " pending"); 79 is(access.inputs.has(testPort.id), false, "port removed from input map while pending"); 80 portEvent = await portPromise; 81 is(testPort.connection, "pending", "Connection " + testPort.name + " pending"); 82 83 // Part 5: Connect ports again, make sure we return to the right status. The events will 84 // fire because the device has been readded to the device maps in the access object. 85 resetPromises(); 86 output.send([0x90, 0x01, 0x00]); 87 accessEvent = await accessPromise; 88 var port = access.inputs.get(testPort.id); 89 is(port, accessEvent.port, "port in map and port in event should be the same"); 90 is(testPort.connection, "pending", "Connection " + testPort.name + " pending"); 91 portEvent = await portPromise; 92 is(testPort.connection, "pending", "Connection " + testPort.name + " pending"); 93 94 // Part 6: Close out everything and clean up. 95 resetPromises(); 96 accessEvent = await accessPromise; 97 is(accessEvent.port.connection, "open", "Connection " + testPort.name + " opened"); 98 portEvent = await portPromise; 99 is(portEvent.port.connection, "open", "Connection " + testPort.name + " opened"); 100 101 /* for (let port of test_ports) { 102 * port.removeEventListener("statechange", checkDevices); 103 * } 104 * access.removeEventListener("statechange", checkDevices);*/ 105 output.send([0x90, 0x02, 0x00]); 106 testPort.removeEventListener("statechange", portStateChangeHandler); 107 access.removeEventListener("statechange", accessStateChangeHandler); 108 access = undefined; 109 output = undefined; 110 testPort = undefined; 111 accessEvent = undefined; 112 portEvent = undefined; 113 SimpleTest.finish(); 114 } 115 </script> 116 </body> 117 </html>