PresentationConnection_send-manual.https.html (5668B)
1 <!DOCTYPE html> 2 3 <meta charset="utf-8"> 4 <title>Sending a message through PresentationConnection</title> 5 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 6 <link rel="help" href="http://w3c.github.io/presentation-api/#sending-a-message-through-presentationconnection"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="common.js"></script> 10 <script src="support/stash.js"></script> 11 12 <p id="notice"> 13 Click the button below and select the available presentation display, to start the manual test. The test passes if a "PASS" result appears.<br> 14 <button id="presentBtn">Start Presentation Test</button> 15 </p> 16 17 <script> 18 setup({explicit_timeout: true}); 19 20 const presentBtn = document.getElementById('presentBtn'); 21 22 const message1 = '1st'; 23 const message2 = '2nd'; 24 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 25 const message4 = new Uint8Array([52, 116, 104]); // "4th" 26 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 27 28 const toUint8Array = buf => { 29 return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; 30 } 31 32 // convert ArrayBuffer or Uint8Array into string 33 const toText = buf => { 34 const arr = toUint8Array(buf); 35 return !buf ? null : arr.reduce((result, item) => { 36 return result + String.fromCharCode(item); 37 }, ''); 38 } 39 40 promise_test(t => { 41 const clickWatcher = new EventWatcher(t, presentBtn, 'click'); 42 const stash = new Stash(stashIds.toController, stashIds.toReceiver); 43 const request = new PresentationRequest(presentationUrls); 44 let connection, watcher; 45 46 t.add_cleanup(() => { 47 if (connection) { 48 connection.onconnect = () => { connection.terminate(); }; 49 if (connection.state === 'closed') 50 request.reconnect(connection.id); 51 else 52 connection.terminate(); 53 } 54 const notice = document.getElementById('notice'); 55 notice.parentNode.removeChild(notice); 56 stash.stop(); 57 }); 58 59 return clickWatcher.wait_for('click').then(() => { 60 presentBtn.disabled = true; 61 62 return request.start(); 63 }).then(c => { 64 connection = c; 65 66 // send data in "connecting" state (throws an exception) 67 assert_equals(connection.state, 'connecting', 'the initial state of the presentation connection is "connecting"'); 68 assert_throws_dom('InvalidStateError', () => { 69 connection.send(''); 70 }, 'an InvalidStateError is thrown if the state is "connecting"'); 71 72 // enable timeout again, cause no user action is needed from here. 73 t.step_timeout(() => { 74 t.force_timeout(); 75 t.done(); 76 }, 10000); 77 78 watcher = new EventWatcher(t, connection, ['connect', 'close', 'terminate']); 79 return watcher.wait_for('connect'); 80 }).then(() => { 81 return stash.init(); 82 }).then(() => { 83 return Promise.all([ stash.send('send'), stash.receive() ]); 84 }).then(results => { 85 // send messages 86 connection.send(message1); // string 87 connection.send(message2); // string 88 connection.send(new Blob([message3])); // Blob 89 connection.send(message4.buffer); // ArrayBuffer 90 connection.send(message5); // ArrayBufferView 91 return stash.receive(); 92 }).then(stash => { 93 // verify messages 94 const results = JSON.parse(stash); 95 assert_true(!!results[0] && results[0].type === 'text' && results[0].data === message1, 'send a string correctly'); 96 assert_true(!!results[1] && results[1].type === 'text' && results[1].data === message2, 'send a string correctly'); 97 assert_true(!!results[2] && results[2].type === 'binary' && results[2].data === toText(message3), 'send a Blob correctly'); 98 assert_true(!!results[3] && results[3].type === 'binary' && results[3].data === toText(message4), 'send a ArrayBuffer correctly'); 99 assert_true(!!results[4] && results[4].type === 'binary' && results[4].data === toText(message5), 'send a ArrayBufferView correctly'); 100 101 // send data in "closed" state (throws an exception) 102 connection.close(); 103 return watcher.wait_for('close'); 104 }).then(() => { 105 assert_equals(connection.state, 'closed', 'the state is set to "closed" when the presentation connection is closed'); 106 assert_throws_dom('InvalidStateError', () => { 107 connection.send(''); 108 }, 'an InvalidStateError is thrown if the state is "closed"'); 109 110 // reconnect and terminate the connection 111 return request.reconnect(connection.id); 112 }).then(() => { 113 return watcher.wait_for('connect'); 114 }).then(() => { 115 // send data in "terminated" state (throws an exception) 116 connection.terminate(); 117 return watcher.wait_for('terminate'); 118 }).then(() => { 119 assert_equals(connection.state, 'terminated', 'the state is set to "terminated" when the presentation connection is terminated'); 120 assert_throws_dom('InvalidStateError', () => { 121 connection.send(''); 122 }, 'an InvalidStateError is thrown if the state is "terminated"'); 123 }); 124 }); 125 </script>