dom/presentation/tests/mochitest/test_presentation_dc_receiver.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>
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<head>
  <meta charset="utf-8">
  <title>Test for B2G PresentationConnection API at receiver side</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1148307">Test for B2G PresentationConnection API at receiver side</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<script type="application/javascript">

'use strict';

var gScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL('PresentationSessionChromeScript.js'));
var receiverUrl = SimpleTest.getTestFileURL('file_presentation_receiver.html');

var obs = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
          .getService(SpecialPowers.Ci.nsIObserverService);

function setup() {
  return new Promise(function(aResolve, aReject) {
    gScript.sendAsyncMessage('trigger-device-add');

    var iframe = document.createElement('iframe');
    iframe.setAttribute('src', receiverUrl);

    // This event is triggered when the iframe calls "postMessage".
    window.addEventListener('message', function listener(aEvent) {
      var message = aEvent.data;
      if (/^OK /.exec(message)) {
        ok(true, "Message from iframe: " + message);
      } else if (/^KO /.exec(message)) {
        ok(false, "Message from iframe: " + message);
      } else if (/^INFO /.exec(message)) {
        info("Message from iframe: " + message);
      } else if (/^COMMAND /.exec(message)) {
        var command = JSON.parse(message.replace(/^COMMAND /, ''));
        gScript.sendAsyncMessage(command.name, command.data);
      } else if (/^DONE$/.exec(message)) {
        ok(true, "Messaging from iframe complete.");
        window.removeEventListener('message', listener);

        teardown();
      }
    }, false);

    var promise = new Promise(function(aResolve, aReject) {
      document.body.appendChild(iframe);

      aResolve(iframe);
    });
    obs.notifyObservers(promise, 'setup-request-promise', null);

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

    gScript.addMessageListener('answer-sent', function answerSentHandler(aIsValid) {
      gScript.removeMessageListener('answer-sent', answerSentHandler);
      ok(aIsValid, "A valid answer is sent.");
    });

    gScript.addMessageListener('control-channel-closed', function controlChannelClosedHandler(aReason) {
      gScript.removeMessageListener('control-channel-closed', controlChannelClosedHandler);
      is(aReason, SpecialPowers.Cr.NS_OK, "The control channel is closed normally.");
    });

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

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

    gScript.addMessageListener('data-transport-closed', function dataTransportClosedHandler(aReason) {
      gScript.removeMessageListener('data-transport-closed', dataTransportClosedHandler);
      is(aReason, SpecialPowers.Cr.NS_OK, "The data transport should be closed normally.");
    });

    aResolve();
  });
}

function testIncomingSessionRequest() {
  return new Promise(function(aResolve, aReject) {
    gScript.addMessageListener('receiver-launching', function launchReceiverHandler(aSessionId) {
      gScript.removeMessageListener('receiver-launching', launchReceiverHandler);
      info("Trying to launch receiver page.");

      ok(navigator.presentation, "navigator.presentation should be available in in-process pages.");
      ok(!navigator.presentation.receiver, "Non-receiving in-process pages shouldn't get a presentation receiver instance.");
      aResolve();
    });

    gScript.sendAsyncMessage('trigger-incoming-session-request', receiverUrl);
  });
}

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

  gScript.sendAsyncMessage('teardown');
}

function runTests() {
  setup().
  then(testIncomingSessionRequest);
}

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

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