MIDITestUtils.js (2717B)
1 var MIDITestUtils = { 2 permissionSetup: allow => { 3 let permPromiseRes; 4 let permPromise = new Promise(res => { 5 permPromiseRes = res; 6 }); 7 SpecialPowers.pushPrefEnv( 8 { 9 set: [ 10 ["dom.webmidi.enabled", true], 11 ["midi.testing", true], 12 ["midi.prompt.testing", true], 13 ["media.navigator.permission.disabled", allow], 14 ], 15 }, 16 () => { 17 permPromiseRes(); 18 } 19 ); 20 return permPromise; 21 }, 22 // This list needs to stay synced with the ports in 23 // dom/midi/TestMIDIPlatformService. 24 inputInfo: { 25 get id() { 26 return MIDITestUtils.stableId(this); 27 }, 28 name: "Test Control MIDI Device Input Port", 29 manufacturer: "Test Manufacturer", 30 version: "1.0.0", 31 }, 32 outputInfo: { 33 get id() { 34 return MIDITestUtils.stableId(this); 35 }, 36 name: "Test Control MIDI Device Output Port", 37 manufacturer: "Test Manufacturer", 38 version: "1.0.0", 39 }, 40 stateTestInputInfo: { 41 get id() { 42 return MIDITestUtils.stableId(this); 43 }, 44 name: "Test State MIDI Device Input Port", 45 manufacturer: "Test Manufacturer", 46 version: "1.0.0", 47 }, 48 stateTestOutputInfo: { 49 get id() { 50 return MIDITestUtils.stableId(this); 51 }, 52 name: "Test State MIDI Device Output Port", 53 manufacturer: "Test Manufacturer", 54 version: "1.0.0", 55 }, 56 alwaysClosedTestOutputInfo: { 57 get id() { 58 return MIDITestUtils.stableId(this); 59 }, 60 name: "Always Closed MIDI Device Output Port", 61 manufacturer: "Test Manufacturer", 62 version: "1.0.0", 63 }, 64 checkPacket: (expected, actual) => { 65 if (expected.length != actual.length) { 66 ok(false, "Packet " + expected + " length not same as packet " + actual); 67 } 68 for (var i = 0; i < expected.length; ++i) { 69 is(expected[i], actual[i], "Packet value " + expected[i] + " matches."); 70 } 71 }, 72 stableId: async info => { 73 // This computes the stable ID of a MIDI port according to the logic we 74 // use in the Web MIDI implementation. See MIDIPortChild::GenerateStableId() 75 // and nsContentUtils::AnonymizeId(). 76 const id = info.name + info.manufacturer + info.version; 77 const encoder = new TextEncoder(); 78 const data = encoder.encode(id); 79 const keyBytes = encoder.encode(self.origin); 80 const key = await crypto.subtle.importKey( 81 "raw", 82 keyBytes, 83 { name: "HMAC", hash: "SHA-256" }, 84 false, 85 ["sign"] 86 ); 87 const result = new Uint8Array(await crypto.subtle.sign("HMAC", key, data)); 88 let resultString = ""; 89 for (let i = 0; i < result.length; i++) { 90 resultString += String.fromCharCode(result[i]); 91 } 92 return btoa(resultString); 93 }, 94 };