summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/file_presentation_receiver.html
blob: 46a330b5fcc809f919e8cd7a9d8087872074a521 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for B2G PresentationReceiver at receiver side</title>
</head>
<body>
<div id="content"></div>
<script type="application/javascript;version=1.7">

"use strict";

function is(a, b, msg) {
  alert((a === b ? 'OK ' : 'KO ') + msg);
}

function ok(a, msg) {
  alert((a ? 'OK ' : 'KO ') + msg);
}

function info(msg) {
  alert('INFO ' + msg);
}

function command(msg) {
  alert('COMMAND ' + JSON.stringify(msg));
}

function finish() {
  alert('DONE');
}

var connection;

function testConnectionAvailable() {
  return new Promise(function(aResolve, aReject) {
    ok(navigator.presentation, "navigator.presentation should be available in receiving pages.");
    ok(navigator.presentation.receiver, "navigator.presentation.receiver should be available in receiving pages.");

    navigator.presentation.receiver.connectionList.then(
      function(aList) {
        is(aList.connections.length, 1, "Should get one conncetion.");
        connection = aList.connections[0];
        ok(connection.id, "Connection ID should be set: " + connection.id);
        is(connection.state, "connected", "Connection state at receiver side should be connected.");
        aResolve();
      },
      function(aError) {
        ok(false, "Error occurred when getting the connection list: " + aError);
        finish();
        aReject();
      }
    );
    command({ name: 'trigger-incoming-offer' });
  });
}

function testDefaultRequestIsUndefined() {
  return new Promise(function(aResolve, aReject) {
    is(navigator.presentation.defaultRequest, undefined, "navigator.presentation.defaultRequest should not be available in receiving UA");
    aResolve();
  });
}

function testConnectionAvailableSameOriginInnerIframe() {
  return new Promise(function(aResolve, aReject) {
    var iframe = document.createElement('iframe');
    iframe.setAttribute('src', './file_presentation_receiver_inner_iframe.html');
    document.body.appendChild(iframe);

    aResolve();
  });
}

function testConnectionUnavailableDiffOriginInnerIframe() {
  return new Promise(function(aResolve, aReject) {
    var iframe = document.createElement('iframe');
    iframe.setAttribute('src', 'http://example.com/tests/dom/presentation/tests/mochitest/file_presentation_non_receiver_inner_iframe.html');
    document.body.appendChild(iframe);

    aResolve();
  });
}

function testConnectionListSameObject() {
  return new Promise(function(aResolve, aReject) {
    is(navigator.presentation.receiver.connectionList, navigator.presentation.receiver.connectionList, "The promise should be the same object.");
    var promise = navigator.presentation.receiver.connectionList.then(
      function(aList) {
        is(connection, aList.connections[0], "The connection from list and the one from |connectionavailable| event should be the same.");
        aResolve();
      },
      function(aError) {
        ok(false, "Error occurred when getting the connection list: " + aError);
        finish();
        aReject();
      }
    );
  });
}

function testIncomingMessage() {
  return new Promise(function(aResolve, aReject) {
    const incomingMessage = "test incoming message";

    connection.addEventListener('message', function messageHandler(aEvent) {
      connection.removeEventListener('message', messageHandler);
      is(aEvent.data, incomingMessage, "An incoming message should be received.");
      aResolve();
    });

    command({ name: 'trigger-incoming-message',
              data: incomingMessage });
  });
}

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

    connection.close();
  });
}

testConnectionAvailable().
then(testDefaultRequestIsUndefined).
then(testConnectionAvailableSameOriginInnerIframe).
then(testConnectionUnavailableDiffOriginInnerIframe).
then(testConnectionListSameObject).
then(testIncomingMessage).
then(testCloseConnection).
then(finish);

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