summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/file_presentation_reconnect.html
blob: 174ccd3f3ac62adf0df2ccad6231cc72c9ac3f07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test allow-presentation sandboxing flag</title>
<script type="application/javascript;version=1.8">

"use strict";

function is(a, b, msg) {
  window.parent.postMessage((a === b ? "OK " : "KO ") + msg, "*");
}

function ok(a, msg) {
  window.parent.postMessage((a ? "OK " : "KO ") + msg, "*");
}

function info(msg) {
  window.parent.postMessage("INFO " + msg, "*");
}

function command(msg) {
  window.parent.postMessage("COMMAND " + JSON.stringify(msg), "*");
}

function finish() {
  window.parent.postMessage("DONE", "*");
}

var request;
var connection;

function testStartRequest() {
  return new Promise(function(aResolve, aReject) {
    ok(navigator.presentation, "navigator.presentation should be available.");
    request = new PresentationRequest("http://example1.com");

    request.start().then(
      function(aConnection) {
        connection = aConnection;
        ok(connection, "Connection should be available.");
        ok(connection.id, "Connection ID should be set.");
        is(connection.state, "connecting", "The initial state should be connecting.");

        connection.onclose = function() {
          connection.onclose = null;
          command({ name: "notify-connection-closed", id: connection.id });
        };
        connection.onconnect = function() {
          connection.onconnect = null;
          is(connection.state, "connected", "Connection should be connected.");
          aResolve();
        };
      },
      function(aError) {
        ok(false, "Error occurred when establishing a connection: " + aError);
        teardown();
        aReject();
      }
    );
  });
}

function testCloseConnection() {
  return new Promise(function(aResolve, aReject) {
    if (connection.state === "closed") {
      aResolve();
      return;
    }
    connection.onclose = function() {
      connection.onclose = null;
      is(connection.state, "closed", "The connection should be closed.");
      aResolve();
    };

    connection.close();
  });
}

window.addEventListener("message", function onMessage(evt) {
  if (evt.data === "startConnection") {
    testStartRequest().then(
      function () {
        command({ name: "connection-connected", id: connection.id });
      }
    );
  }
  else if (evt.data === "closeConnection") {
    testCloseConnection().then(
      function () {
        command({ name: "connection-closed", id: connection.id });
      }
    );
  }
}, false);

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