dom/presentation/tests/mochitest/test_presentation_sender_disconnect.html
author Sean Lin <selin@mozilla.com>
Fri, 04 Sep 2015 15:54:34 +0800
changeset 294539 780bdbf0c0a9ac2ab5d18965f480760f6d5defa8
parent 294538 cd93c40bb34f1a169127ee8482bba020d872c2c2
child 294911 967ff76a23a3dd43b5e03034783225649b504ed9
permissions -rw-r--r--
Bug 1201805 - [Presentation WebAPI] Fix collaboration issues with control channel. Part 2 - Adjust the timing to send offer. r=smaug

<!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 session disconnection of 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>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1069230">Test for session disconnection of 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 presentation;
var session;

function testSetup() {
  return new Promise(function(aResolve, aReject) {
    presentation.onavailablechange = function(aIsAvailable) {
      presentation.onavailablechange = null;
      ok(aIsAvailable, "Device should be available.");
      aResolve();
    };

    gScript.sendAsyncMessage('trigger-device-add');
  });
}

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

    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.");
      gScript.sendAsyncMessage('trigger-incoming-transport');
    });

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

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

    presentation.startSession("http://example.com").then(
      function(aSession) {
        session = aSession;
        ok(session, "Session should be availlable.");
        ok(session.id, "Session ID should be set.");
        is(session.state, "connected", "Session state at sender side should be connected by default.");
        aResolve();
      },
      function(aError) {
        ok(false, "Error occurred when starting session: " + aError);
        teardown();
        aReject();
      }
    );
  });
}

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

    session.onstatechange = function() {
      session.onstatechange = null;
      is(session.state, "disconnected", "Session should be disconnected.");
      aResolve();
    };

    gScript.sendAsyncMessage('trigger-data-transport-close', SpecialPowers.Cr.NS_ERROR_FAILURE);
  });
}

function testCloseSession() {
  return new Promise(function(aResolve, aReject) {
    session.onstatechange = function() {
      session.onstatechange = null;
      is(session.state, "terminated", "Session should be terminated.");
      aResolve();
    };

    session.close();
  });
}

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

  gScript.sendAsyncMessage('teardown');
}

function runTests() {
  ok(navigator.presentation, "navigator.presentation should be available.");
  presentation = navigator.presentation;

  testSetup().
  then(testStartSession).
  then(testSessionDisconnection).
  then(testCloseSession).
  then(teardown);
}

SimpleTest.expectAssertions(0, 5);
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.ignore_webidl_scope_checks", true],
                                      ["dom.presentation.test.enabled", true],
                                      ["dom.presentation.test.stage", 0]]},
                            runTests);
});

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