PresentationRequest_onconnectionavailable-manual.https.html (4501B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Firing a connectionavailable event at a controlling user agent</title> 4 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> 5 <link rel="help" href="https://w3c.github.io/presentation-api/#starting-a-presentation"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <script src="common.js"></script> 9 <p>Click the button below and select the available presentation display, to start the manual test.</p> 10 <button id="presentBtn">Start Presentation Test</button> 11 12 13 <script> 14 // disable the timeout function for the tests 15 setup({explicit_timeout: true}); 16 17 // ---------- 18 // DOM Object 19 // ---------- 20 const presentBtn = document.getElementById('presentBtn'); 21 22 // -------------------------------------------------------------------------- 23 // Start New PresentationRequest.onconnectionavailable Test (success) - begin 24 // -------------------------------------------------------------------------- 25 promise_test(t => { 26 const clickWatcher = new EventWatcher(t, presentBtn, 'click'); 27 const request = new PresentationRequest(presentationUrls); 28 let connection; 29 30 t.add_cleanup(() => { 31 if (connection) { 32 connection.onconnect = () => { connection.terminate(); }; 33 if (connection.state === 'closed') 34 request.reconnect(connection.id); 35 else 36 connection.terminate(); 37 } 38 }); 39 40 const watchEvent = (obj, watcher, type) => { 41 const watchHandler = new Promise(resolve => { 42 obj['on' + type] = evt => { resolve(evt); }; 43 }); 44 return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { 45 assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); 46 return results[0]; 47 }); 48 }; 49 50 return clickWatcher.wait_for('click').then(() => { 51 presentBtn.disabled = true; 52 53 // Note: During starting a presentation, the connectionavailable event is fired (step 9) 54 // after the promise P is resolved (step 8). 55 return request.start(); 56 }).then(c => { 57 connection = c; 58 assert_equals(connection.state, 'connecting', 'The initial state of the presentation connection is "connecting".'); 59 assert_true(!!connection.id, 'The connection ID is set.'); 60 assert_equals(typeof connection.id, 'string', 'The connection ID is a string.'); 61 assert_true(connection instanceof PresentationConnection, 'The connection is an instance of PresentationConnection.'); 62 63 const eventWatcher = new EventWatcher(t, request, 'connectionavailable'); 64 const timeout = new Promise((_, reject) => { 65 // This test fails if request.onconnectionavailable is not invoked although the presentation is started successfully 66 // or the presentation fails to be started. 67 t.step_timeout(() => { reject('The connectionavailable event was not fired (timeout).'); }, 5000);} 68 ); 69 return Promise.race([ watchEvent(request, eventWatcher, 'connectionavailable'), timeout ]); 70 }).then(evt => { 71 assert_true(evt instanceof PresentationConnectionAvailableEvent, 'An event using PresentationConnectionAvailableEvent is fired.'); 72 assert_true(evt.isTrusted, 'The event is a trusted event.'); 73 assert_false(evt.bubbles, 'The event does not bubbles.'); 74 assert_false(evt.cancelable, 'The event is not cancelable.'); 75 assert_equals(evt.type, 'connectionavailable', 'The event name is "connectionavailable".'); 76 assert_equals(evt.target, request, 'event.target is the presentation request.'); 77 assert_true(evt.connection instanceof PresentationConnection, 'event.connection is a presentation connection.'); 78 assert_equals(evt.connection, connection, 'event.connection is set to the presentation which the promise is resolved with.'); 79 }); 80 }); 81 // ------------------------------------------------------------------------ 82 // Start New PresentationRequest.onconnectionavailable Test (success) - end 83 // ------------------------------------------------------------------------ 84 </script>