PresentationConnection_send-manual.https.html (3859B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Sending a message through PresentationConnection</title> 4 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 5 <link rel="help" href="http://w3c.github.io/presentation-api/#sending-a-message-through-presentationconnection"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="common.js"></script> 9 <script src="support/stash.js"></script> 10 11 <p>Click the button below and select the available presentation display, to start the manual test.</p> 12 <button id="presentBtn">Start Presentation Test</button> 13 14 <script> 15 setup({explicit_timeout: true}); 16 17 const message1 = '1st'; 18 const message2 = '2nd'; 19 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 20 const message4 = new Uint8Array([52, 116, 104]); // "4th" 21 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 22 23 const toUint8Array = buf => { 24 return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; 25 } 26 27 // compare two ArrayBuffer or Uint8Array 28 const compare = (a, b) => { 29 const p = toUint8Array(a); 30 const q = toUint8Array(b); 31 return !!p && !!q && p.every((item, index) => { return item === q[index]; }); 32 }; 33 34 let connection; 35 36 const presentBtn = document.getElementById('presentBtn'); 37 presentBtn.onclick = () => { 38 presentBtn.disabled = true; 39 const stash = new Stash(stashIds.toController, stashIds.toReceiver); 40 const request = new PresentationRequest('support/PresentationConnection_send_receiving-ua.html'); 41 42 let eventWatcher; 43 44 promise_test(t => { 45 t.add_cleanup(() => { 46 connection.onconnect = () => { connection.terminate(); }; 47 if (connection.state === 'closed') 48 request.reconnect(connection.id); 49 else 50 connection.terminate(); 51 }); 52 53 return request.start().then(c => { 54 // enable timeout again, cause no user action is needed from here. 55 t.step_timeout(() => { 56 t.force_timeout(); 57 t.done(); 58 }, 3000); 59 60 connection = c; 61 eventWatcher = new EventWatcher(t, connection, 'connect'); 62 return eventWatcher.wait_for('connect'); 63 }).then(() => { 64 return stash.init(); 65 }).then(() => { 66 stash.send({ type: 'ok' }); 67 eventWatcher = new EventWatcher(t, connection, 'message'); 68 return eventWatcher.wait_for('message'); 69 }).then(evt => { 70 assert_true(typeof evt.data === 'string' && evt.data === message1, 'send a string correctly'); 71 return eventWatcher.wait_for('message'); 72 }).then(evt => { 73 assert_true(typeof evt.data === 'string' && evt.data === message2, 'send a string correctly'); 74 return eventWatcher.wait_for('message'); 75 }).then(evt => { 76 assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message3), 'send a Blob correctly'); 77 return eventWatcher.wait_for('message'); 78 }).then(evt => { 79 assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message4), 'send an ArrayBuffer correctly'); 80 return eventWatcher.wait_for('message'); 81 }).then(evt => { 82 assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message5), 'send an ArrayBufferView correctly'); 83 return stash.send({ type: 'ok' }); 84 }).then(() => { 85 return stash.receive(); 86 }).then(data => { 87 const result = JSON.parse(data); 88 if (!result.type || result.type !== 'error') 89 assert_unreached('an InvalidStateError is thrown if the state is "closed"'); 90 else 91 assert_throws_dom('InvalidStateError', () => { 92 throw new DOMException(result.message, result.name); 93 }, 'an InvalidStateError is thrown if the state is "closed"'); 94 }); 95 }); 96 }; 97 </script>