PresentationConnection_onclose_receiving-ua.html (4224B)
1 <!DOCTYPE html> 2 3 <meta charset="utf-8"> 4 <title>Closing a PresentationConnection</title> 5 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> 6 <link rel="help" href="https://w3c.github.io/presentation-api/#closing-a-presentationconnection"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="../common.js"></script> 10 <script src="stash.js"></script> 11 12 <script> 13 const stash = new Stash(stashIds.toReceiver, stashIds.toController); 14 15 add_completion_callback((tests, status) => { 16 const log = document.getElementById('log'); 17 stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML })); 18 }); 19 20 const checkCloseEvent = (evt, connection, reason) => { 21 assert_true(evt instanceof PresentationConnectionCloseEvent, 'An event using PresentationConnectionCloseEvent is fired.'); 22 assert_true(evt.isTrusted, 'The event is a trusted event.'); 23 assert_false(evt.bubbles, 'The event does not bubbles.'); 24 assert_false(evt.cancelable, 'The event is not cancelable.'); 25 assert_equals(evt.type, 'close', 'The event name is "close".'); 26 assert_equals(evt.target, connection, 'event.target is the presentation connection.'); 27 assert_equals(connection.state, 'closed', 'State of the presentation connection is "closed".'); 28 assert_equals(evt.reason, reason, 'The reason for closing the presentation connection is "' + reason + '".'); 29 }; 30 31 const watchEvent = (obj, watcher, type) => { 32 const watchHandler = new Promise(resolve => { 33 obj['on' + type] = evt => { resolve(evt); }; 34 }); 35 return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { 36 assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); 37 return results[0]; 38 }); 39 }; 40 41 promise_test(t => { 42 return navigator.presentation.receiver.connectionList.then(list => { 43 let connection = list.connections[0]; 44 assert_equals(list.connections.length, 1, 'A presentation connection list is populated with a first presentation connection.'); 45 46 // Step 1: close the presentation connection in "connected" state 47 connection.close(); 48 let watchClose = new EventWatcher(t, connection, 'close'); 49 const watchNewConnection = new EventWatcher(t, list, 'connectionavailable'); 50 return watchEvent(connection, watchClose, 'close').then(evt => { 51 checkCloseEvent(evt, connection, 'closed'); 52 53 // Step 2: check a connection closed by the controlling user agent 54 stash.send(JSON.stringify({ type: 'ok' })); 55 return watchNewConnection.wait_for('connectionavailable'); 56 }).then(evt => { 57 connection = evt.connection; 58 watchClose = new EventWatcher(t, connection, 'close'); 59 return watchEvent(connection, watchClose, 'close'); 60 }).then(evt => { 61 checkCloseEvent(evt, connection, 'closed'); 62 63 // Step 3: try to close the presentation connection in "closed" state (nothing should happen) 64 connection.close(); 65 return Promise.race([ 66 new Promise(resolve => { t.step_timeout(resolve, 1000); }), 67 watchEvent(connection, watchClose, 'close').then(() => { 68 assert_unreached('Invoking PresentationConnection.close() in the "closed" state causes nothing.'); }) 69 ]); 70 }).then(() => { 71 72 // Step 4: check a connection closed due to aborting the nested browsing context 73 stash.send(JSON.stringify({ type: 'ok' })); 74 return watchNewConnection.wait_for('connectionavailable'); 75 }).then(evt => { 76 connection = evt.connection; 77 stash.send(JSON.stringify({ type: 'ok' })); 78 watchClose = new EventWatcher(t, connection, 'close'); 79 return Promise.race([ 80 watchEvent(connection, watchClose, 'close'), 81 new Promise(resolve => { t.step_timeout(() => { resolve(null); }, 3000); }) 82 ]); 83 }).then(evt => { 84 assert_true(!!evt, 'A presentation connection is closed when its controller\'s browsing context is discarded.'); 85 checkCloseEvent(evt, connection, 'wentaway'); 86 }); 87 }); 88 }); 89 </script>