PresentationConnection_onmessage_receiving-ua.html (4515B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Receiving 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/#receiving-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="stash.js"></script> 10 11 <script> 12 setup({explicit_timeout: true}); 13 14 const message1 = '1st'; 15 const message2 = '2nd'; 16 const message3 = new Uint8Array([51, 114, 100]); // "3rd" 17 const message4 = new Uint8Array([52, 116, 104]); // "4th" 18 const message5 = new Uint8Array([108, 97, 115, 116]); // "last" 19 20 const toUint8Array = buf => { 21 return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; 22 } 23 24 // compare two ArrayBuffer or Uint8Array 25 const compare = (a, b) => { 26 const p = toUint8Array(a); 27 const q = toUint8Array(b); 28 return !!p && !!q && p.every((item, index) => { return item === q[index]; }); 29 }; 30 31 const stash = new Stash(stashIds.toReceiver, stashIds.toController); 32 33 add_completion_callback((tests, status) => { 34 const log = document.getElementById('log'); 35 stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML })); 36 }); 37 38 navigator.presentation.receiver.connectionList.then(list => { 39 const checkEvent = event => { 40 assert_true(event.isTrusted, 'a trusted event is fired'); 41 assert_true(event instanceof MessageEvent, 'The event uses the MessageEvent interface'); 42 assert_false(event.bubbles, 'the event does not bubble'); 43 assert_false(event.cancelable, 'the event is not cancelable'); 44 }; 45 46 const watchEvent = (obj, watcher, type) => { 47 const watchHandler = new Promise(resolve => { 48 obj['on' + type] = evt => { resolve(evt); }; 49 }); 50 return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { 51 assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); 52 return results[0]; 53 }); 54 }; 55 56 const connection = list.connections[0]; 57 58 promise_test(t => { 59 t.step_timeout(() => { 60 t.force_timeout(); 61 t.done(); 62 }, 3000); 63 64 assert_equals(connection.binaryType, 'arraybuffer', 'the default value of binaryType is "arraybuffer"'); 65 const eventWatcher = new EventWatcher(t, connection, 'message'); 66 return eventWatcher.wait_for('message').then(event => { 67 checkEvent(event); 68 assert_equals(event.data, message1, 'receive a string correctly'); 69 return watchEvent(connection, eventWatcher, 'message'); 70 }).then(event => { 71 checkEvent(event); 72 assert_equals(event.data, message2, 'receive a string correctly'); 73 return watchEvent(connection, eventWatcher, 'message'); 74 }).then(event => { 75 checkEvent(event); 76 assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); 77 assert_true(compare(event.data, message3), 'receive an ArrayBuffer correctly (originally a Blob at a controlling user agent)'); 78 return watchEvent(connection, eventWatcher, 'message'); 79 }).then(event => { 80 checkEvent(event); 81 assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); 82 assert_true(compare(event.data, message4), 'receive an ArrayBuffer correctly (originally an ArrayBuffer at a controlling user agent)'); 83 return watchEvent(connection, eventWatcher, 'message'); 84 }).then(event => { 85 checkEvent(event); 86 assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); 87 assert_true(compare(event.data, message5), 'receive an ArrayBuffer correctly (originally an ArrayBufferView at a controlling user agent)'); 88 89 connection.binaryType = 'blob'; 90 return Promise.all([ 91 stash.send(JSON.stringify({ type: 'blob' })), 92 watchEvent(connection, eventWatcher, 'message') 93 ]).then(results => results[1]); 94 }).then(event => { 95 assert_true(event.data instanceof Blob, 'receive binary data as Blob'); 96 return new Promise((resolve, reject) => { 97 const reader = new FileReader(); 98 reader.onload = resolve; 99 reader.onerror = reject; 100 reader.readAsArrayBuffer(event.data); 101 }); 102 }).then(event => { 103 assert_true(compare(event.target.result, message5), 'receive a Blob correctly'); 104 connection.terminate(); 105 }); 106 }); 107 }); 108 </script>