dom/presentation/tests/mochitest/file_presentation_receiver_establish_connection_error.html
author Ryan VanderMeulen <ryanvm@gmail.com>
Thu, 19 May 2016 10:32:02 -0400
changeset 298168 e42d3d42dafa739d8a99f89d72300e9e74fd37be
parent 298141 11db59507360d0d72aa72301d064c8fd2b0e926e
child 298644 46845866285d8248352856a5e6a42e1a3afe6e30
permissions -rw-r--r--
Backed out 3 changesets (bug 1234128) for Android crashtest failures. Backed out changeset 11db59507360 (bug 1234128) Backed out changeset 8a0848fb59ac (bug 1234128) Backed out changeset 7cf300dda85a (bug 1234128) CLOSED TREE

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test for connection establishing errors of B2G Presentation API at receiver side</title>
</head>
<body>
<div id="content"></div>
<script type="application/javascript;version=1.7">

"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 connection;

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

    navigator.presentation.receiver.getConnection().then(
      function(aConnection) {
        connection = aConnection;

        ok(connection.id, "Connection ID should be set: " + connection.id);
        is(connection.state, "closed", "Connection state at receiver side should be closed by default.");
        aResolve();
      },
      function(aError) {
        ok(false, "Error occurred when getting the connection: " + aError);
        finish();
        aReject();
      }
    );
  });
}

function testUnexpectedControlChannelClose() {
  return new Promise(function(aResolve, aReject) {
    connection.onstatechange = function() {
      connection.onstatechange = null;
      is(connection.state, "terminated", "Connection state should become terminated.");
      aResolve();
    };

    // Trigger the control channel to be closed with error code.
    command({ name: 'trigger-control-channel-close', data: 0x80004004 /* NS_ERROR_ABORT */ });
  });
}

testConnectionAvailable().
then(testUnexpectedControlChannelClose).
then(finish);

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