Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / webmidi / state-check-utils.js
bloba8ab71f66cb8fb2f8b0dafb28193db10a7b32d63
1 function checkStateTransition(options) {
2     debug("Check state transition for " + options.method + " on " +
3           options.initialconnection + " state.");
4     debug("- check initial state.");
5     window.port = options.port;
6     shouldBeEqualToString("port.connection", options.initialconnection);
7     var checkHandler = function(e) {
8         window.eventport = e.port;
9         testPassed("handler is called with port " + eventport + ".");
10         if (options.initialconnection == options.finalconnection) {
11             testFailed("onstatechange handler should not be called here.");
12         }
13         shouldBeEqualToString("eventport.id", options.port.id);
14         shouldBeEqualToString("eventport.connection", options.finalconnection);
15     };
16     port.onstatechange = function(e) {
17         debug("- check port handler.");
18         checkHandler(e);
19     };
20     access.onstatechange = function(e) {
21         debug("- check access handler.");
22         checkHandler(e);
23     };
24     if (options.method == "send") {
25         port.send([]);
26     }
27     if (options.method == "setonmidimessage") {
28         port.onmidimessage = function() {};
29     }
30     if (options.method == "addeventlistener") {
31         port.addEventListener("midimessage", function() {});
32     }
33     if (options.method == "send" || options.method == "setonmidimessage" ||
34         options.method == "addeventlistener") {
35         // Following tests expect an implicit open finishes synchronously.
36         // But it will be asynchronous in the future.
37         debug("- check final state.");
38         shouldBeEqualToString("port.connection", options.finalconnection);
39         return Promise.resolve();
40     }
41     // |method| is expected to be "open" or "close".
42     return port[options.method]().then(function(p) {
43         window.callbackport = p;
44         debug("- check callback arguments.");
45         testPassed("callback is called with port " + callbackport + ".");
46         shouldBeEqualToString("callbackport.id", options.port.id);
47         shouldBeEqualToString("callbackport.connection", options.finalconnection);
48         debug("- check final state.");
49         shouldBeEqualToString("port.connection", options.finalconnection);
50     }, function(e) {
51         testFailed("error callback should not be called here.");
52         throw e;
53     });