summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/mochitest/file_presentation_1ua_receiver.html
blob: cf02d2b2c87a594fed13768f8405725fac513503 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!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;
const DATA_ARRAY = [0, 255, 254, 0, 1, 2, 3, 0, 255, 255, 254, 0];
const DATA_ARRAY_BUFFER = new ArrayBuffer(DATA_ARRAY.length);
const TYPED_DATA_ARRAY = new Uint8Array(DATA_ARRAY_BUFFER);
TYPED_DATA_ARRAY.set(DATA_ARRAY);

function is_same_buffer(recv_data, expect_data) {
  let recv_dataview = new Uint8Array(recv_data);
  let expected_dataview = new Uint8Array(expect_data);

  if (recv_dataview.length !== expected_dataview.length) {
    return false;
  }

  for (let i = 0; i < recv_dataview.length; i++) {
    if (recv_dataview[i] != expected_dataview[i]) {
      info('discover byte differenct at ' + i);
      return false;
    }
  }
  return true;
}

function testConnectionAvailable() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testConnectionAvailable ---');
    ok(navigator.presentation, "Receiver: navigator.presentation should be available.");
    ok(navigator.presentation.receiver, "Receiver: navigator.presentation.receiver should be available.");
    is(navigator.presentation.defaultRequest, null, "Receiver: navigator.presentation.defaultRequest should be null.");

    navigator.presentation.receiver.connectionList
    .then((aList) => {
      is(aList.connections.length, 1, "Should get one conncetion.");
      connection = aList.connections[0];
      ok(connection.id, "Connection ID should be set: " + connection.id);
      is(connection.state, "connected", "Connection state at receiver side should be connected.");
      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.onconnect = function() {
      connection.onconnect = null;
      ok(false, "Should not get |onconnect| event.")
      aReject();
    };
    if (connection.state === "connected") {
      connection.onconnect = 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 testIncomingBlobMessage() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testIncomingBlobMessage ---');
    connection.send('testIncomingBlobMessage');
    connection.addEventListener('message', function messageHandler(evt) {
      connection.removeEventListener('message', messageHandler);
      let recvData= String.fromCharCode.apply(null, new Uint8Array(evt.data));
      is(recvData, "Hello World", 'expected same string data');
      aResolve();
    });
  });
}

function testConnectionClosed() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testConnectionClosed ---');
    connection.onclose = function() {
      connection.onclose = null;
      is(connection.state, "closed", "Receiver: Connection should be closed.");
      command('forward-command', JSON.stringify({ name: 'receiver-closed' }));
      aResolve();
    };
    command('forward-command', JSON.stringify({ name: 'ready-to-close' }));
  });
}

function testReconnectConnection() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testReconnectConnection ---');
    window.addEventListener('hashchange', function hashchangeHandler(evt) {
      var message = JSON.parse(decodeURIComponent(window.location.hash.substring(1)));
      if (message.type === 'prepare-for-reconnect') {
        command('forward-command', JSON.stringify({ name: 'ready-to-reconnect' }));
      }
    });
    connection.onconnect = function() {
      connection.onconnect = null;
      ok(true, "The connection is reconnected.")
      aResolve();
    };
  });
}

function testIncomingArrayBuffer() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testIncomingArrayBuffer ---');
    connection.binaryType = "blob";
    connection.send('testIncomingArrayBuffer');
    connection.addEventListener('message', function messageHandler(evt) {
      connection.removeEventListener('message', messageHandler);
      var fileReader = new FileReader();
      fileReader.onload = function() {
        ok(is_same_buffer(DATA_ARRAY_BUFFER, this.result), "expected same buffer data");
        aResolve();
      };
      fileReader.readAsArrayBuffer(evt.data);
    });
  });
}

function testIncomingArrayBufferView() {
  return new Promise(function(aResolve, aReject) {
    info('Receiver: --- testIncomingArrayBufferView ---');
    connection.binaryType = "arraybuffer";
    connection.send('testIncomingArrayBufferView');
    connection.addEventListener('message', function messageHandler(evt) {
      connection.removeEventListener('message', messageHandler);
      ok(is_same_buffer(evt.data, TYPED_DATA_ARRAY), "expected same buffer data");
      aResolve();
    });
  });
}

function runTests() {
  testConnectionAvailable()
  .then(testConnectionReady)
  .then(testIncomingMessage)
  .then(testSendMessage)
  .then(testIncomingBlobMessage)
  .then(testConnectionClosed)
  .then(testReconnectConnection)
  .then(testIncomingArrayBuffer)
  .then(testIncomingArrayBufferView)
  .then(testConnectionClosed);
}

runTests();

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