<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="../http/tests/resources/permissions-helper.js"></script>
</head>
<body>
<script>

description("Test if various kinds of MIDI messages can be validated.");

shouldBeDefined("navigator.requestMIDIAccess");

window.jsTestIsAsync = true;

PermissionsHelper.setPermission('midi-sysex', 'granted').then(function() {
    return navigator.requestMIDIAccess({sysex: true});
}).then(function(a) {
    output = a.outputs.values().next().value;

    // Note on(off).
    output.send([0xff, 0x90, 0x00, 0x00, 0x90, 0x07, 0x00]);

    // Running status is not allowed in Web MIDI API.
    shouldThrow('output.send([0x00, 0x01])');

    // Unexpected End of Sysex.
    shouldThrow('output.send([0xf7])');

    // Unexpected reserved status bytes.
    shouldThrow('output.send([0xf4])');
    shouldThrow('output.send([0xf5])');
    shouldThrow('output.send([0xf9])');
    shouldThrow('output.send([0xfd])');

    // Incomplete channel messages.
    shouldThrow('output.send([0x80])');
    shouldThrow('output.send([0x80, 0x00])');
    shouldThrow('output.send([0x90])');
    shouldThrow('output.send([0x90, 0x00])');
    shouldThrow('output.send([0xa0])');
    shouldThrow('output.send([0xa0, 0x00])');
    shouldThrow('output.send([0xb0])');
    shouldThrow('output.send([0xb0, 0x00])');
    shouldThrow('output.send([0xc0])');
    shouldThrow('output.send([0xd0])');
    shouldThrow('output.send([0xe0])');
    shouldThrow('output.send([0xe0, 0x00])');

    // Incomplete system messages.
    shouldThrow('output.send([0xf1])');
    shouldThrow('output.send([0xf2])');
    shouldThrow('output.send([0xf2, 0x00])');
    shouldThrow('output.send([0xf3])');

    // Invalid data bytes.
    shouldThrow('output.send([0x80, 0x80, 0x00])');
    shouldThrow('output.send([0x80, 0x00, 0x80])');

    // Complete messages.
    output.send([0x80, 0x00, 0x00]);
    output.send([0x90, 0x00, 0x00]);
    output.send([0xa0, 0x00, 0x00]);
    output.send([0xb0, 0x00, 0x00]);
    output.send([0xc0, 0x00]);
    output.send([0xd0, 0x00]);
    output.send([0xe0, 0x00, 0x00]);

    // Real-Time messages.
    output.send([0xf8]);
    output.send([0xfa]);
    output.send([0xfb]);
    output.send([0xfc]);
    output.send([0xfe]);
    output.send([0xff]);

    // Valid messages with Real-Time messages.
    output.send([0x90, 0xff, 0xff, 0x00, 0xff, 0x01, 0xff, 0x80, 0xff, 0x00, 0xff, 0xff, 0x00, 0xff, 0xff]);

    // Sysex messages.
    output.send([0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7]);
    output.send([0xf0, 0xf8, 0xf7, 0xff]);
    shouldThrow('output.send([0xf0, 0x80, 0xf7])');
    shouldThrow('output.send([0xf0, 0xf0, 0xf7])');
    shouldThrow('output.send([0xf0, 0xff, 0xf7, 0xf7])');

    // Reserved status bytes.
    shouldThrow('output.send([0xf4, 0x80, 0x00, 0x00])');
    shouldThrow('output.send([0x80, 0xf4, 0x00, 0x00])');
    shouldThrow('output.send([0x80, 0x00, 0xf4, 0x00])');
    shouldThrow('output.send([0x80, 0x00, 0x00, 0xf4])');
    shouldThrow('output.send([0xf0, 0xff, 0xf4, 0xf7])');

    // Invalid timestamps.
    shouldThrow('output.send([], NaN)');
    shouldThrow('output.send([], Infinity)');
    shouldThrow('output.send(new Uint8Array(), NaN)');
    shouldThrow('output.send(new Uint8Array(), Infinity)');

    finishJSTest();
}).catch(function () {
    testFailed("requestMIDIAccess() return an error.");
});

</script>
</body>
</html>