dom/presentation/tests/mochitest/file_presentation_1ua_receiver_oop.html
author Ryan VanderMeulen <ryanvm@gmail.com>
Thu, 19 May 2016 10:32:02 -0400
changeset 298168 e42d3d42dafa739d8a99f89d72300e9e74fd37be
parent 294720 94ec70bf8c2297109611568159521fd8dfb3a98c
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>
<!-- vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: -->
<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) {
  if (a === b) {
    alert('OK ' + msg);
  } else {
    alert('KO ' + msg + ' | reason: ' + a + ' != ' + b);
  }
}

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

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

function command(name, data) {
  alert('COMMAND ' + JSON.stringify({name: name, data: data}));
}

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

var connection;

function testConnectionAvailable() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testConnectionAvailable ---');
    ok(navigator.presentation, "Receiver: navigator.presentation should be available.");
    // FIXME Sometimes navigator.presentation.receiver is initialized lately.
    // See bug 1234128 - navigator.presentation.receiver is null in 1-UA use case.
    // https://bugzilla.mozilla.org/show_bug.cgi?id=1234128
    while (!navigator.presentation.receiver) {
      info('Receiver: navigator.presentation.receiver is null, see Bug 1234128');
    }
    ok(navigator.presentation.receiver, "Receiver: navigator.presentation.receiver should be available.");
    navigator.presentation.receiver.getConnection()
    .then((aConnection) => {
      connection = aConnection;
      ok(connection.id, "Receiver: Connection ID should be set: " + connection.id);
      is(connection.state, "closed", "Connection state at receiver side should be closed by default.");
      aResolve();
    })
    .catch((aError) => {
      ok(false, "Receiver: Error occurred when getting the connection: " + aError);
      finish();
      aReject();
    });
  });
}

function testConnectionReady() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testConnectionReady ---');
    connection.onstatechange = function() {
      connection.onstatechange = null;
      is(connection.state, "connected", "Receiver: Connection state should become connected.");
      aResolve();
    };
    if (connection.state === "connected") {
      connection.onstatechange = null;
      is(connection.state, "connected", "Receiver: Connection state should become connected.");
      aResolve();
    }
  });
}

function testIncomingMessage() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testIncomingMessage ---');
    connection.addEventListener('message', function messageHandler(evt) {
      connection.removeEventListener('message', messageHandler);
      let msg = evt.data;
      is(msg, 'msg-sender-to-receiver', 'Receiver: Receiver should receive message from sender.');
      command('forward-command', JSON.stringify({ name: 'message-from-sender-received' }));
      aResolve();
    });
    command('forward-command', JSON.stringify({ name: 'trigger-message-from-sender' }));
  });
}

function testSendMessage() {
  return new Promise(function(aResolve, aReject) {
    window.addEventListener('hashchange', function hashchangeHandler(evt) {
      var message = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
      if (message.type === 'trigger-message-from-receiver') {
        info('Receiver: --- testSendMessage ---');
        connection.send('msg-receiver-to-sender');
      }
      if (message.type === 'message-from-receiver-received') {
        window.removeEventListener('hashchange', hashchangeHandler);
        aResolve();
      }
    });
  });
}

function testTerminateConnection() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testTerminateConnection ---');
    connection.onstatechange = function() {
      connection.onstatechange = null;
      is(connection.state, "terminated", "Receiver: Connection should be terminated.");
      aResolve();
    };
    connection.terminate();
  });
}

function runTests() {
  testConnectionAvailable()
  .then(testConnectionReady)
  .then(testIncomingMessage)
  .then(testSendMessage)
  .then(testTerminateConnection)
  .then(finish);
}

runTests();

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