PresentationConnection_onclose-manual.https.html (5751B)
1 <!DOCTYPE html> 2 3 <meta charset="utf-8"> 4 <title>Closing a PresentationConnection</title> 5 <link rel="author" title="Intel" href="http://www.intel.com"> 6 <link rel="author" title="He Yue" href="mailto:yue.he@intel.com"> 7 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 8 <link rel="help" href="https://w3c.github.io/presentation-api/#closing-a-presentationconnection"> 9 <script src="/resources/testharness.js"></script> 10 <script src="/resources/testharnessreport.js"></script> 11 <script src="common.js"></script> 12 <script src="support/stash.js"></script> 13 <h2>Description</h2> 14 <p> 15 This test validates that after connection close,<br/> 16 the connection state is set closed,<br/> 17 the onclose EventHandler is triggered. 18 </p> 19 <br/> 20 <p>Click the button below to start the test.</p> 21 <button id="presentBtn" >Start Presentation Test</button> 22 23 <script> 24 setup({explicit_timeout: true}); 25 26 const presentBtn = document.getElementById('presentBtn'); 27 28 promise_test(t => { 29 const clickWatcher = new EventWatcher(t, presentBtn, 'click'); 30 const request = new PresentationRequest(presentationUrls); 31 const stash = new Stash(stashIds.toController, stashIds.toReceiver); 32 let connection, eventWatcher; 33 34 t.add_cleanup(() => { 35 if (connection) { 36 connection.onconnect = () => { connection.terminate(); }; 37 if (connection.state === 'closed') 38 request.reconnect(connection.id); 39 else 40 connection.terminate(); 41 } 42 stash.stop(); 43 }); 44 45 const checkCloseEvent = evt => { 46 assert_true(evt instanceof PresentationConnectionCloseEvent, 'An event using PresentationConnectionCloseEvent is fired.'); 47 assert_true(evt.isTrusted, 'The event is a trusted event.'); 48 assert_false(evt.bubbles, 'The event does not bubbles.'); 49 assert_false(evt.cancelable, 'The event is not cancelable.'); 50 assert_equals(evt.type, 'close', 'The event name is "close".'); 51 assert_equals(evt.target, connection, 'event.target is the presentation connection.'); 52 assert_equals(connection.state, 'closed', 'State of the presentation connection is "closed".'); 53 assert_equals(evt.reason, 'closed', 'The reason for closing the presentation connection is "closed".'); 54 }; 55 56 const watchEvent = (obj, watcher, type) => { 57 const watchHandler = new Promise(resolve => { 58 obj['on' + type] = evt => { resolve(evt); }; 59 }); 60 return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { 61 assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); 62 return results[0]; 63 }); 64 }; 65 66 const waitForEvent = (obj, watcher, type) => { 67 const watchHandler = new Promise(resolve => { 68 obj['on' + type] = evt => { resolve(evt); }; 69 }); 70 return Promise.race([ watchHandler, watcher.wait_for(type) ]); 71 }; 72 73 return Promise.all([ 74 clickWatcher.wait_for('click'), 75 stash.init() 76 ]).then(() => { 77 presentBtn.disabled = true; 78 return request.start(); 79 }).then(c => { 80 // Enable timeout again, cause no user action is needed from here. 81 t.step_timeout(() => { 82 t.force_timeout(); 83 t.done(); 84 }, 10000); 85 86 connection = c; 87 eventWatcher = new EventWatcher(t, connection, ['connect', 'close', 'terminate']); 88 89 // Step 1: close the presentation connection in "connecting" state 90 connection.close(); 91 return Promise.race([ 92 new Promise((_, reject) => { 93 t.step_timeout(() => { reject('The presentation connection in "connecting" state was not closed successfully.'); }, 3000); 94 }), 95 watchEvent(connection, eventWatcher, 'close') 96 ]); 97 }).then(evt => { 98 checkCloseEvent(evt); 99 100 // Step 2: close the presentation connection in "connected" state 101 return request.reconnect(connection.id); 102 }).then(() => { 103 return eventWatcher.wait_for('connect'); 104 }).then(() => { 105 connection.close(); 106 return watchEvent(connection, eventWatcher, 'close'); 107 }).then(evt => { 108 checkCloseEvent(evt); 109 110 // Step 3: check a connection closed by the receiving user agent 111 return request.reconnect(connection.id); 112 }).then(() => { 113 return eventWatcher.wait_for('connect'); 114 }).then(() => { 115 return Promise.all([ stash.send('close'), watchEvent(connection, eventWatcher, 'close') ]); 116 }).then(results => { 117 checkCloseEvent(results[1]); 118 119 // Step 4: close the presentation connection in "closed" state (nothing should happen) 120 const closeWatcher = new EventWatcher(t, connection, 'close'); 121 connection.close(); 122 return Promise.race([ 123 new Promise(resolve => { t.step_timeout(resolve, 1000); }), 124 waitForEvent(connection, closeWatcher, 'close').then(() => { 125 assert_unreached('Invoking PresentationConnection.close() in the "closed" state causes nothing.'); }) 126 ]); 127 }).then(() => { 128 // Step 5: close the presentation connection in "terminated" state (nothing should happen) 129 return request.reconnect(connection.id); 130 }).then(() => { 131 return eventWatcher.wait_for('connect'); 132 }).then(() => { 133 connection.terminate(); 134 return eventWatcher.wait_for('terminate'); 135 }).then(() => { 136 const closeWatcher = new EventWatcher(t, connection, 'close'); 137 connection.close(); 138 return Promise.race([ 139 new Promise(resolve => { t.step_timeout(resolve, 1000); }), 140 waitForEvent(connection, closeWatcher, 'close').then(() => { 141 assert_unreached('Invoking PresentationConnection.close() in the "terminated" state causes nothing.'); }) 142 ]); 143 }); 144 }); 145 </script>