summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/test_presentation_dc_sender.html
blob: 97e252e8406990ae8572e80807e9a7b9b8551cfd (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<!DOCTYPE HTML>
<html>
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
  <meta charset="utf-8">
  <title>Test for B2G Presentation API at sender side</title>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" src="PresentationSessionFrameScript.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1148307">Test for B2G Presentation API at sender side</a>
<script type="application/javascript;version=1.8">

'use strict';

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('PresentationSessionChromeScript.js'));
var frameScript = SpecialPowers.isMainProcess() ? gScript : contentScript;
var request;
var connection;

function testSetup() {
  return new Promise(function(aResolve, aReject) {
    request = new PresentationRequest("http://example.com/");

    request.getAvailability().then(
      function(aAvailability) {
        is(aAvailability.value, false, "Sender: should have no available device after setup");
        aAvailability.onchange = function() {
          aAvailability.onchange = null;
          ok(aAvailability.value, "Device should be available.");
          aResolve();
        }

        gScript.sendAsyncMessage('trigger-device-add');
      },
      function(aError) {
        ok(false, "Error occurred when getting availability: " + aError);
        teardown();
        aReject();
      }
    );
  });
}

function testStartConnection() {
  return new Promise(function(aResolve, aReject) {
    gScript.addMessageListener('device-prompt', function devicePromptHandler() {
      gScript.removeMessageListener('device-prompt', devicePromptHandler);
      info("Device prompt is triggered.");
      gScript.sendAsyncMessage('trigger-device-prompt-select');
    });

    gScript.addMessageListener('control-channel-established', function controlChannelEstablishedHandler() {
      gScript.removeMessageListener('control-channel-established', controlChannelEstablishedHandler);
      info("A control channel is established.");
      gScript.sendAsyncMessage('trigger-control-channel-open');
    });

    gScript.addMessageListener('control-channel-opened', function controlChannelOpenedHandler(aReason) {
      gScript.removeMessageListener('control-channel-opened', controlChannelOpenedHandler);
      info("The control channel is opened.");
    });

    gScript.addMessageListener('control-channel-closed', function controlChannelClosedHandler(aReason) {
      gScript.removeMessageListener('control-channel-closed', controlChannelClosedHandler);
      info("The control channel is closed. " + aReason);
    });

    frameScript.addMessageListener('check-navigator', function checknavigatorHandler(aSuccess) {
      frameScript.removeMessageListener('check-navigator', checknavigatorHandler);
      ok(aSuccess, "buildDataChannel get correct window object");
    });

    gScript.addMessageListener('offer-sent', function offerSentHandler(aIsValid) {
      gScript.removeMessageListener('offer-sent', offerSentHandler);
      ok(aIsValid, "A valid offer is sent out.");
      gScript.sendAsyncMessage('trigger-incoming-answer');
    });

    gScript.addMessageListener('answer-received', function answerReceivedHandler() {
      gScript.removeMessageListener('answer-received', answerReceivedHandler);
      info("An answer is received.");
    });

    frameScript.addMessageListener('data-transport-initialized', function dataTransportInitializedHandler() {
      frameScript.removeMessageListener('data-transport-initialized', dataTransportInitializedHandler);
      info("Data transport channel is initialized.");
    });

    frameScript.addMessageListener('data-transport-notification-enabled', function dataTransportNotificationEnabledHandler() {
      frameScript.removeMessageListener('data-transport-notification-enabled', dataTransportNotificationEnabledHandler);
      info("Data notification is enabled for data transport channel.");
    });

    var connectionFromEvent;
    request.onconnectionavailable = function(aEvent) {
      request.onconnectionavailable = null;
      connectionFromEvent = aEvent.connection;
      ok(connectionFromEvent, "|connectionavailable| event is fired with a connection.");

      if (connection) {
        is(connection, connectionFromEvent, "The connection from promise and the one from |connectionavailable| event should be the same.");
      }
    };

    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.");

        if (connectionFromEvent) {
          is(connection, connectionFromEvent, "The connection from promise and the one from |connectionavailable| event should be the same.");
        }
        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 testSend() {
  return new Promise(function(aResolve, aReject) {
    const outgoingMessage = "test outgoing message";

    frameScript.addMessageListener('message-sent', function messageSentHandler(aMessage) {
      frameScript.removeMessageListener('message-sent', messageSentHandler);
      is(aMessage, outgoingMessage, "The message is sent out.");
      aResolve();
    });

    connection.send(outgoingMessage);
  });
}

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();
    });

    frameScript.sendAsyncMessage('trigger-incoming-message', incomingMessage);
  });
}

function testCloseConnection() {
  return new Promise(function(aResolve, aReject) {
    frameScript.addMessageListener('data-transport-closed', function dataTransportClosedHandler(aReason) {
      frameScript.removeMessageListener('data-transport-closed', dataTransportClosedHandler);
      info("The data transport is closed. " + aReason);
    });

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

    connection.close();
  });
}

function testReconnect() {
  return new Promise(function(aResolve, aReject) {
    info('--- testReconnect ---');
    gScript.addMessageListener('control-channel-established', function controlChannelEstablished() {
      gScript.removeMessageListener('control-channel-established', controlChannelEstablished);
      gScript.sendAsyncMessage("trigger-control-channel-open");
    });

    gScript.addMessageListener('start-reconnect', function startReconnectHandler(url) {
      gScript.removeMessageListener('start-reconnect', startReconnectHandler);
      is(url, "http://example.com/", "URLs should be the same.")
      gScript.sendAsyncMessage('trigger-reconnected-acked', url);
    });

    gScript.addMessageListener('offer-sent', function offerSentHandler(aIsValid) {
      gScript.removeMessageListener('offer-sent', offerSentHandler);
      ok(aIsValid, "A valid offer is sent out.");
      gScript.sendAsyncMessage('trigger-incoming-answer');
    });

    gScript.addMessageListener('answer-received', function answerReceivedHandler() {
      gScript.removeMessageListener('answer-received', answerReceivedHandler);
      info("An answer is received.");
    });

    frameScript.addMessageListener('check-navigator', function checknavigatorHandler(aSuccess) {
      frameScript.removeMessageListener('check-navigator', checknavigatorHandler);
      ok(aSuccess, "buildDataChannel get correct window object");
    });

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

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

function teardown() {
  gScript.addMessageListener('teardown-complete', function teardownCompleteHandler() {
    gScript.removeMessageListener('teardown-complete', teardownCompleteHandler);
    gScript.destroy();
    info('teardown-complete');
    SimpleTest.finish();
  });

  gScript.sendAsyncMessage('teardown');
}

function testConstructRequestError() {
  return Promise.all([
    // XXX: Bug 1305204 - uncomment when bug 1275746 is fixed again.
    // new Promise(function(aResolve, aReject) {
    //   try {
    //     request = new PresentationRequest("\\\\\\");
    //   }
    //   catch(e) {
    //     is(e.name, "SyntaxError", "Expect to get SyntaxError.");
    //     aResolve();
    //   }
    // }),
    new Promise(function(aResolve, aReject) {
      try {
        request = new PresentationRequest([]);
      }
      catch(e) {
        is(e.name, "NotSupportedError", "Expect to get NotSupportedError.");
        aResolve();
      }
    }),
  ]);
}

function runTests() {
  ok(window.PresentationRequest, "PresentationRequest should be available.");

  testSetup().
  then(testStartConnection).
  then(testSend).
  then(testIncomingMessage).
  then(testCloseConnection).
  then(testReconnect).
  then(testCloseConnection).
  then(testConstructRequestError).
  then(teardown);
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPermissions([
  {type: 'presentation-device-manage', allow: false, context: document},
], function() {
  SpecialPowers.pushPrefEnv({ 'set': [["dom.presentation.enabled", true],
                                      ["dom.presentation.controller.enabled", true],
                                      ["dom.presentation.session_transport.data_channel.enable", true]]},
                            runTests);
});

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