summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/test_presentation_tcp_sender_default_request.html
blob: 60247ec98678aab5b3f460a64b9ef22b19d8b19e (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
<!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 default request for B2G Presentation API at sender side</title>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1069230">Test default request 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 connection;

function testSetup() {
  return new Promise(function(aResolve, aReject) {
    navigator.presentation.defaultRequest = new PresentationRequest("https://example.com");

    navigator.presentation.defaultRequest.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);
    });

    gScript.addMessageListener('offer-sent', function offerSentHandler() {
      gScript.removeMessageListener('offer-sent', offerSentHandler);
      info("An offer is sent out.");
      gScript.sendAsyncMessage('trigger-incoming-transport');
    });

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

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

    is(navigator.presentation.receiver, undefined, "Sender shouldn't get a presentation receiver instance.");

    navigator.presentation.defaultRequest.onconnectionavailable = function(aEvent) {
      navigator.presentation.defaultRequest.onconnectionavailable = null;
      connection = aEvent.connection;
      ok(connection, "|connectionavailable| event is fired with a connection.");
      ok(connection.id, "Connection ID should be set.");
      is(connection.state, "connecting", "The initial state should be connecting.");
      connection.onconnect = function() {
        connection.onconnect = null;
        is(connection.state, "connected", "Connection should be connected.");
        aResolve();
      };
    };

    // Simulate the UA triggers |start()| of the default request.
    navigator.presentation.defaultRequest.start();
  });
}

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

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

    connection.close();
  });
}

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

  gScript.sendAsyncMessage('teardown');
}

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

  testSetup().
  then(testStartConnection).
  then(testCloseConnection).
  then(teardown);
}

SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({ 'set': [["dom.presentation.enabled", true],
                                    ["dom.presentation.controller.enabled", true],
                                    ["dom.presentation.receiver.enabled", false],
                                    ["dom.presentation.session_transport.data_channel.enable", false]]},
                          runTests);

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