4 <script src="../resources/js-test.js"></script>
8 description("Tests navigator.requestMIDIAccess.");
14 function checkInputMap(inputs) {
15 window.inputs = inputs;
16 debug("for (var input of inputs.values())");
17 for (var input of inputs.values()) {
19 shouldBeEqualToString("input.id", "MockInputID");
20 shouldBeEqualToString("input.manufacturer", "MockInputManufacturer");
21 shouldBeEqualToString("input.name", "MockInputName");
22 shouldBeEqualToString("input.version", "MockInputVersion");
24 debug("for (var input of inputs.keys())");
25 for (var key of inputs.keys()) {
26 window.inputKey = key;
27 shouldBeEqualToString("inputKey", "MockInputID");
29 debug("for (var input of inputs.entries())");
30 for (var entry of inputs.entries()) {
32 shouldBe("entry[0]", "inputKey");
33 shouldBe("entry[1]", "input");
35 debug("for (var input of inputs)");
36 for (var entry of inputs) {
38 shouldBe("entry[0]", "inputKey");
39 shouldBe("entry[1]", "input");
41 shouldBeTrue("inputs.has('MockInputID')");
42 shouldBeFalse("inputs.has('MockOutputID')");
43 shouldBe("inputs.get('MockInputID')", "input");
44 shouldBeUndefined("inputs.get('MockOutputID')");
47 function checkOutputMap(outputs) {
48 window.outputs = outputs;
49 debug("for (var output of outputs.values())");
50 for (var output of outputs.values()) {
51 window.output = output;
52 shouldBeEqualToString("output.id", "MockOutputID");
53 shouldBeEqualToString("output.manufacturer", "MockOutputManufacturer");
54 shouldBeEqualToString("output.name", "MockOutputName");
55 shouldBeEqualToString("output.version", "MockOutputVersion");
57 debug("for (var output of outputs.keys())");
58 for (var key of outputs.keys()) {
59 window.outputKey = key;
60 shouldBeEqualToString("outputKey", "MockOutputID");
62 debug("for (var output of outputs.entries())");
63 for (var entry of outputs.entries()) {
65 shouldBe("entry[0]", "outputKey");
66 shouldBe("entry[1]", "output");
68 debug("for (var output of outputs)");
69 for (var entry of outputs) {
71 shouldBe("entry[0]", "outputKey");
72 shouldBe("entry[1]", "output");
74 shouldBeTrue("outputs.has('MockOutputID')");
75 shouldBeFalse("outputs.has('MockInputID')");
76 shouldBe("outputs.get('MockOutputID')", "output");
77 shouldBeUndefined("outputs.get('MockInputID')");
80 function successCallback1(a) {
83 testPassed("requestMIDIAccess() succeeded with access " + access + ".");
85 // Validate the values of the attributes on the access.
86 shouldBeDefined("access.sysexEnabled");
87 shouldBeFalse("access.sysexEnabled");
88 shouldBe("access.inputs.size", "1");
89 shouldBe("access.outputs.size", "1");
90 checkInputMap(access.inputs);
91 checkOutputMap(access.outputs);
93 window.output = access.outputs.values().next().value;
94 // Test sending of MIDI data with a Uint8Array.
95 var typedArrayData = new Uint8Array([0x90, 0x45, 0x7f]);
96 output.send(typedArrayData);
98 // Test sending of MIDI data with a regular Array.
99 output.send([0x90, 0x45, 0x7f]);
100 testPassed("a note on message is sent without timestamp");
102 // Test sending of MIDI data with a regular Array giving an explicit timestamp.
103 output.send([0x90, 0x45, 0x7f], performance.now());
104 testPassed("a note on message is sent with timestamp");
106 // Test sending of invalid MIDI data.
107 shouldThrow('output.send([0xfff, 0x45, 0x7f])');
109 // Test sending system exclusive messages. These should throw, since we don't have sysex access.
110 shouldThrow('output.send([0xf0, 0x45, 0xf7])');
112 // Now test System Exclusive access - our test mock should not allow this type of access.
113 navigator.requestMIDIAccess({sysex: true}).then(successCallback2, errorCallback2);
116 function errorCallback1(error) {
117 testFailed("requestMIDIAccess() error callback should not be called when requesting basic access.");
121 function successCallback2(access) {
122 testFailed("requestMIDIAccess() was not correctly blocked for System Exclusive access.");
126 function errorCallback2(error) {
127 testPassed("requestMIDIAccess() was correctly blocked for System Exclusive access with error " + error + ".");
131 window.jsTestIsAsync = true;
133 // Test basic access, with no System Exclusive.
134 navigator.requestMIDIAccess().then(successCallback1, errorCallback1);