iframe.html (8528B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>Presentation API - controlling ua - sandboxing</title> 4 <link rel="author" title="Francois Daoust" href="https://www.w3.org/People/#fd"> 5 <link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> 6 <link rel="help" href="http://w3c.github.io/presentation-api/#dom-presentationrequest-start"> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 <script src="../common.js"></script> 10 <script> 11 add_completion_callback((tests, status) => { 12 // remove unserializable attributes, then send the result to the parent window 13 // note: a single test result is supposed to appear here. 14 parent.window.postMessage(JSON.parse(JSON.stringify({ 15 type: 'presentation-api', test: tests[0], status: status 16 })), '*'); 17 }); 18 19 // disable timeout for manual tests 20 setup({explicit_timeout: true}); 21 22 window.onmessage = function (ev) { 23 try { 24 // Presentation URLs are relative to the "controlling-ua" folder, 25 // update relative URLs for this folder 26 var urls = presentationUrls.map(function (url) { 27 if (/:\/\//.test(url)) { 28 return url; 29 } 30 else { 31 return '../' + url; 32 } 33 }); 34 var request = null; 35 if (ev.data === 'create') { 36 try { 37 request = new PresentationRequest(urls); 38 parent.window.postMessage('success', '*'); 39 } 40 catch (err) { 41 parent.window.postMessage(err.name, '*'); 42 } 43 } 44 else if (ev.data === 'start') { 45 request = new PresentationRequest(urls); 46 request.start() 47 .then(function () { 48 parent.window.postMessage('success', '*'); 49 }) 50 .catch(function (err) { 51 if ((err.name === 'NotFoundError') || 52 (err.name === 'NotAllowedError')) { 53 // These errors either mean that the user dismissed the dialog 54 // box or that the UA could not find any available or suitable 55 // screen. This is equivalent of succeeding for the purpose of 56 // iframe tests. 57 parent.window.postMessage('success', '*'); 58 } 59 else { 60 parent.window.postMessage(err.name, '*'); 61 } 62 }); 63 } 64 else if (ev.data === 'reconnect') { 65 request = new PresentationRequest(urls); 66 request.reconnect('someid') 67 .then(function () { 68 parent.window.postMessage('success', '*'); 69 }) 70 .catch(function (err) { 71 parent.window.postMessage(err.name, '*'); 72 }); 73 } 74 else if (ev.data.match(/^reconnect\?id=(.*)$/)) { 75 promise_test(function (t) { 76 var presentationId = RegExp.$1; 77 var phase = -1, actual = -1, connection, waitConnection; 78 var description = [ 79 "Phase #1: Promise is resolved", 80 "Phase #2: 'connectionavailable' event fired", 81 "Phase #3: 'connect' event fired" 82 ].map(d => { return '(Reconnecting in a nested browsing context) ' + d; }); 83 84 var count = function(evt) { actual++; return evt; }; 85 var checkPhase = function(evt) { 86 phase++; 87 assert_equals(description[actual], description[phase], 'Event order is incorrect.'); 88 return evt; 89 }; 90 91 request = new PresentationRequest(urls); 92 93 var eventWatcher = new EventWatcher(t, request, 'connectionavailable'); 94 var waitConnectionavailable = eventWatcher.wait_for('connectionavailable').then(count).then(function (evt) { 95 connection = connection || evt.connection; return evt; 96 }); 97 98 return request.reconnect(presentationId).then(count).then(checkPhase).then(function (c) { 99 // Reconnecting Phase #1: Promise is resolved 100 connection = c; 101 assert_equals(connection.state, 'connecting', 'Check the initial state of the presentation connection.'); 102 assert_equals(connection.id, presentationId, "The same presentation ID is set to the newly created presentation connection."); 103 assert_true(connection instanceof PresentationConnection, 'The connection is an instance of PresentationConnection.'); 104 105 var eventWatcher = new EventWatcher(t, connection, 'connect'); 106 waitConnect = eventWatcher.wait_for('connect').then(count); 107 108 // Reconnecting Phase #2: "connectionavailable" event is fired 109 return waitConnectionavailable; 110 }).then(checkPhase).then(function (evt) { 111 assert_true(evt instanceof PresentationConnectionAvailableEvent, 'An event using PresentationConnectionAvailableEvent is fired.'); 112 assert_true(evt.isTrusted, 'The event is a trusted event.'); 113 assert_false(evt.bubbles, 'The event does not bubbles.'); 114 assert_false(evt.cancelable, 'The event is not cancelable.'); 115 assert_equals(evt.type, 'connectionavailable', 'The event name is "connectionavailable".'); 116 assert_equals(evt.target, request, 'event.target is the presentation request.'); 117 assert_true(evt.connection instanceof PresentationConnection, 'event.connection is a presentation connection.'); 118 assert_equals(evt.connection, connection, 'event.connection is set to the presentation which the promise is resolved with.'); 119 120 // Reconnecting Phase #3: "connect" event is fired 121 return waitConnect; 122 }).then(checkPhase).then(function (evt) { 123 assert_true(evt.isTrusted && !evt.bubbles && !evt.cancelable && evt instanceof Event, 'A simple event is fired.'); 124 assert_equals(evt.type, 'connect', 'The event name is "connect".'); 125 assert_equals(evt.target, connection, 'event.target is the presentation connection.'); 126 assert_equals(connection.state, 'connected', 'The presentation connection state is set to "connected".'); 127 parent.window.postMessage({ type: 'presentation-api', test: { status: 0 } }, '*'); 128 var terminateWatcher = new EventWatcher(t, connection, 'terminate'); 129 130 // "terminate" event is fired 131 return terminateWatcher.wait_for('terminate'); 132 }).then(function (evt) { 133 assert_true(evt.isTrusted && !evt.bubbles && !evt.cancelable && evt instanceof Event, 'A simple event is fired.'); 134 assert_equals(evt.type, 'terminate', 'The event name is "terminate".'); 135 assert_equals(evt.target, connection, 'event.target is the presentation connection.'); 136 assert_equals(connection.state, 'terminated', 'The presentation connection state is set to "terminated".'); 137 }); 138 }); 139 } 140 else if (ev.data.match(/^terminate\?id=(.*)$/)) { 141 var presentationId = RegExp.$1; 142 request = new PresentationRequest(urls); 143 request.reconnect(presentationId) 144 .then(function (c) { 145 parent.window.postMessage('reconnected', '*'); 146 c.onterminate = function(evt) { 147 parent.window.postMessage({ 148 isSimpleEvent: evt.isTrusted && !evt.bubbles && !evt.cancelable && evt instanceof Event, 149 type: evt.type, 150 checkConnection: evt.target === c, 151 state: c.state 152 }, '*'); 153 }; 154 }) 155 .catch(function (err) { 156 parent.window.postMessage(err.name, '*'); 157 }); 158 } 159 else if (ev.data === 'getAvailability') { 160 request = new PresentationRequest(urls); 161 request.getAvailability() 162 .then(function () { 163 parent.window.postMessage('success', '*'); 164 }) 165 .catch(function (err) { 166 if (err.name === 'NotSupportedError') { 167 parent.window.postMessage('success', '*'); 168 } 169 else { 170 parent.window.postMessage(err.name, '*'); 171 } 172 }); 173 } 174 } 175 catch (err) { 176 parent.window.postMessage('Could not create PresentationRequest', '*'); 177 } 178 } 179 parent.window.postMessage('ready', '*'); 180 </script>