summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/test_presentation_tcp_receiver_oop.html
blob: bfbc7947a5d918509b25b4cc4afeda41044eae68 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<!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 (OOP)</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=1069230">Test B2G PresentationConnection API at receiver side (OOP)</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 nonReceiverUrl = SimpleTest.getTestFileURL('file_presentation_non_receiver.html');

var isReceiverFinished = false;
var isNonReceiverFinished = false;

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');

    // Create a receiver OOP iframe.
    var receiverIframe = document.createElement('iframe');
    receiverIframe.setAttribute('remote', 'true');
    receiverIframe.setAttribute('mozbrowser', 'true');
    receiverIframe.setAttribute('mozpresentation', receiverUrl);
    receiverIframe.setAttribute('src', receiverUrl);

    // This event is triggered when the iframe calls "alert".
    receiverIframe.addEventListener('mozbrowsershowmodalprompt', function receiverListener(aEvent) {
      var message = aEvent.detail.message;
      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.");
        receiverIframe.removeEventListener('mozbrowsershowmodalprompt', receiverListener);

        isReceiverFinished = true;

        if (isNonReceiverFinished) {
          teardown();
        }
      }
    }, false);

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

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

    // Create a non-receiver OOP iframe.
    var nonReceiverIframe = document.createElement('iframe');
    nonReceiverIframe.setAttribute('remote', 'true');
    nonReceiverIframe.setAttribute('mozbrowser', 'true');
    nonReceiverIframe.setAttribute('src', nonReceiverUrl);

    // This event is triggered when the iframe calls "alert".
    nonReceiverIframe.addEventListener('mozbrowsershowmodalprompt', function nonReceiverListener(aEvent) {
      var message = aEvent.detail.message;
      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.");
        nonReceiverIframe.removeEventListener('mozbrowsershowmodalprompt', nonReceiverListener);

        isNonReceiverFinished = true;

        if (isReceiverFinished) {
          teardown();
        }
      }
    }, false);

    document.body.appendChild(nonReceiverIframe);

    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('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.");

      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: 'browser', allow: true, context: document},
], function() {
  SpecialPowers.pushPrefEnv({ 'set': [["dom.presentation.enabled", true],
                                      ["dom.presentation.controller.enabled", false],
                                      ["dom.presentation.receiver.enabled", true],
                                      ["dom.presentation.session_transport.data_channel.enable", false],
                                      ["dom.mozBrowserFramesEnabled", true],
                                      ["network.disable.ipc.security", true],
                                      ["dom.ipc.browser_frames.oop_by_default", true]]},
                            runTests);
});

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