diff options
Diffstat (limited to 'testing/web-platform/tests/presentation-api/receiving-ua')
23 files changed, 1845 insertions, 0 deletions
diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnectionList_onconnectionavailable-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnectionList_onconnectionavailable-manual.https.html new file mode 100644 index 0000000000..9b75a85e9f --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnectionList_onconnectionavailable-manual.https.html @@ -0,0 +1,77 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Monitoring incoming presentation connections</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#monitoring-incoming-presentation-connections"> +<script src="common.js"></script> +<script src="support/stash.js"></script> +<style> +iframe { display: none; } +</style> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn" disabled>Start Presentation Test</button> +<iframe id="childFrame" sandbox="allow-scripts allow-presentation" src="support/iframe.html"></iframe> + +<script> + let connection; + const presentBtn = document.getElementById('presentBtn'); + const child = document.getElementById('childFrame'); + + child.onload = () => { presentBtn.disabled = false; }; + + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationConnectionList_onconnectionavailable_receiving-ua.html'); + + return request.start().then(c => { + connection = c; + return stash.init(); + }).then(() => { + return stash.receive(); + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + connection.close(); + return stash.receive(); + } + else + return data; + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + request.reconnect(connection.id); + return stash.receive(); + } + else + return data; + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + child.contentWindow.postMessage({ type: 'connect', id: connection.id, url: connection.url }, '*'); + return stash.receive(); + } + else + return data; + }).then(result => { + const json = JSON.parse(result); + + // notify receiver's results of a parent window (e.g. test runner) + if (window.opener && 'completion_callback' in window.opener) { + window.opener.completion_callback(json.tests, json.status); + } + // display receiver's results as HTML + const log = document.createElement('div'); + log.id = 'log'; + log.innerHTML = json.log; + document.body.appendChild(log); + + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onclose-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onclose-manual.https.html new file mode 100644 index 0000000000..50585f7813 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onclose-manual.https.html @@ -0,0 +1,84 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Closing a PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#closing-a-presentationconnection"> +<script src="common.js"></script> +<script src="support/stash.js"></script> +<style> +iframe { display: none; } +</style> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn" disabled>Start Presentation Test</button> +<iframe id="childFrame" sandbox="allow-scripts allow-presentation" src="support/iframe.html"></iframe> + +<script> + let connection; + const presentBtn = document.getElementById('presentBtn'); + const child = document.getElementById('childFrame'); + + child.onload = () => { presentBtn.disabled = false; }; + + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationConnection_onclose_receiving-ua.html'); + + return request.start().then(c => { + connection = c; + return stash.init(); + }).then(() => { + return stash.receive(); + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + return request.reconnect(connection.id).then(() => data); + } + else + return data; + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + connection.onclose = null; + connection.close(); + return stash.receive(); + } + else + return data; + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + child.contentWindow.postMessage({ type: 'connect', id: connection.id, url: connection.url }, '*'); + return stash.receive(); + } + else + return data; + }).then(data => { + const result = JSON.parse(data); + if (result.type === 'ok') { + child.parentNode.removeChild(child); + return stash.receive(); + } + else + return data; + }).then(result => { + const json = JSON.parse(result); + // notify receiver's results of a parent window (e.g. test runner) + if (window.opener && 'completion_callback' in window.opener) { + window.opener.completion_callback(json.tests, json.status); + } + // display receiver's results as HTML + const log = document.createElement('div'); + log.id = 'log'; + log.innerHTML = json.log; + document.body.appendChild(log); + + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onmessage-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onmessage-manual.https.html new file mode 100644 index 0000000000..53451a6cef --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_onmessage-manual.https.html @@ -0,0 +1,64 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Receiving a message through PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="http://w3c.github.io/presentation-api/#receiving-a-message-through-presentationconnection"> +<script src="common.js"></script> +<script src="support/stash.js"></script> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn">Start Presentation Test</button> + +<script> + const message1 = '1st'; + const message2 = '2nd'; + const message3 = new Uint8Array([51, 114, 100]); // "3rd" + const message4 = new Uint8Array([52, 116, 104]); // "4th" + const message5 = new Uint8Array([108, 97, 115, 116]); // "last" + + let connection; + const presentBtn = document.getElementById('presentBtn'); + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationConnection_onmessage_receiving-ua.html'); + + request.start().then(c => { + c.onconnect = () => { + connection = c; + connection.send(message1); // string + connection.send(message2); // string + connection.send(new Blob([message3])); // Blob + connection.send(message4.buffer); // ArrayBuffer + connection.send(message5); // ArrayBufferView + return stash.receive().then(data => { + const result = JSON.parse(data); + if (result.type === 'blob') { + connection.send(message5); + return stash.receive(); + } + else + return data; + }).then(result => { + const json = JSON.parse(result); + + // notify receiver's results of a parent window (e.g. test runner) + if (window.opener && 'completion_callback' in window.opener) { + window.opener.completion_callback(json.tests, json.status); + } + // display receiver's results as HTML + const log = document.createElement('div'); + log.id = 'log'; + log.innerHTML = json.log; + document.body.appendChild(log); + + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + }); + }; + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_send-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_send-manual.https.html new file mode 100644 index 0000000000..188f7e3fc1 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_send-manual.https.html @@ -0,0 +1,97 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Sending a message through PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="http://w3c.github.io/presentation-api/#sending-a-message-through-presentationconnection"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="common.js"></script> +<script src="support/stash.js"></script> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn">Start Presentation Test</button> + +<script> + setup({explicit_timeout: true}); + + const message1 = '1st'; + const message2 = '2nd'; + const message3 = new Uint8Array([51, 114, 100]); // "3rd" + const message4 = new Uint8Array([52, 116, 104]); // "4th" + const message5 = new Uint8Array([108, 97, 115, 116]); // "last" + + const toUint8Array = buf => { + return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; + } + + // compare two ArrayBuffer or Uint8Array + const compare = (a, b) => { + const p = toUint8Array(a); + const q = toUint8Array(b); + return !!p && !!q && p.every((item, index) => { return item === q[index]; }); + }; + + let connection; + + const presentBtn = document.getElementById('presentBtn'); + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationConnection_send_receiving-ua.html'); + + let eventWatcher; + + promise_test(t => { + t.add_cleanup(() => { + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + }); + + return request.start().then(c => { + // enable timeout again, cause no user action is needed from here. + t.step_timeout(() => { + t.force_timeout(); + t.done(); + }, 3000); + + connection = c; + eventWatcher = new EventWatcher(t, connection, 'connect'); + return eventWatcher.wait_for('connect'); + }).then(() => { + return stash.init(); + }).then(() => { + stash.send({ type: 'ok' }); + eventWatcher = new EventWatcher(t, connection, 'message'); + return eventWatcher.wait_for('message'); + }).then(evt => { + assert_true(typeof evt.data === 'string' && evt.data === message1, 'send a string correctly'); + return eventWatcher.wait_for('message'); + }).then(evt => { + assert_true(typeof evt.data === 'string' && evt.data === message2, 'send a string correctly'); + return eventWatcher.wait_for('message'); + }).then(evt => { + assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message3), 'send a Blob correctly'); + return eventWatcher.wait_for('message'); + }).then(evt => { + assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message4), 'send an ArrayBuffer correctly'); + return eventWatcher.wait_for('message'); + }).then(evt => { + assert_true(evt.data instanceof ArrayBuffer && compare(evt.data, message5), 'send an ArrayBufferView correctly'); + return stash.send({ type: 'ok' }); + }).then(() => { + return stash.receive(); + }).then(data => { + const result = JSON.parse(data); + if (!result.type || result.type !== 'error') + assert_unreached('an InvalidStateError is thrown if the state is "closed"'); + else + assert_throws_dom('InvalidStateError', () => { + throw new DOMException(result.message, result.name); + }, 'an InvalidStateError is thrown if the state is "closed"'); + }); + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_terminate-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_terminate-manual.https.html new file mode 100644 index 0000000000..6484e97c5a --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationConnection_terminate-manual.https.html @@ -0,0 +1,71 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Terminating a presentation in a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="https://w3c.github.io/presentation-api/#terminating-a-presentation-in-a-receiving-browsing-context"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="common.js"></script> +<script src="support/stash.js"></script> +<style> +iframe { display: none; } +</style> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn" disabled>Start Presentation Test</button> +<iframe id="childFrame" sandbox="allow-scripts allow-presentation" src="support/iframe.html"></iframe> + +<script> + setup({explicit_timeout: true}); + + let connection; + + const presentBtn = document.getElementById('presentBtn'); + const child = document.getElementById('childFrame'); + + child.onload = () => { presentBtn.disabled = false; }; + + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationConnection_terminate_receiving-ua.html'); + + promise_test(t => { + t.add_cleanup(() => { + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + stash.stop(); + }); + + return request.start().then(c => { + // enable timeout again, cause no user action is needed from here. + t.step_timeout(() => { + t.force_timeout(); + t.done(); + }, 5000); + + connection = c; + const eventWatcher = new EventWatcher(t, connection, 'connect'); + return eventWatcher.wait_for('connect'); + }).then(() => { + return stash.init(); + }).then(() => { + child.contentWindow.postMessage({ type: 'connect', id: connection.id, url: connection.url }, '*'); + const eventWatcher1 = new EventWatcher(t, connection, 'terminate'); + const eventWatcher2 = new EventWatcher(t, window, 'message'); + return Promise.all([ eventWatcher1.wait_for('terminate'), eventWatcher2.wait_for('message') ]); + }).then(() => { + return Promise.race([ + stash.receive().then(data => { + if (data.type === 'error') + assert_unreached('The presentation is not terminated successfully.'); + }), + new Promise(resolve => { t.step_timeout(resolve, 2000); }) + ]); + }); + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/PresentationReceiver_create-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationReceiver_create-manual.https.html new file mode 100644 index 0000000000..96fc6f345d --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/PresentationReceiver_create-manual.https.html @@ -0,0 +1,272 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Creating a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#creating-a-receiving-browsing-context"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="common.js"></script> +<script src="support/stash.js"></script> + +<p id="notice"> + Click the button below and select the available presentation display, to start the manual test. The test passes if a "PASS" result appears.<br> + This test asks you to click the button twice, unless the test fails.<br> + <button id="presentBtn">Start Presentation Test</button> +</p> + +<script> + setup({explicit_timeout: true}); + + let receiverStack; + add_completion_callback(() => { + // overwrite a stack written in the test result + if (receiverStack) { + document.querySelector('#log pre').textContent = receiverStack; + } + }); + + let connection; + const presentBtn = document.getElementById('presentBtn'); + + const dbName = { + controller: 'db-presentation-api-controlling-ua', + receiver: 'db-presentation-api-receiving-ua' + }; + + const main = () => { + promise_test(t => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/PresentationReceiver_create_receiving-ua.html'); + + t.add_cleanup(() => { + const notice = document.getElementById('notice'); + notice.parentNode.removeChild(notice); + stash.stop(); + + // history.back(); + document.cookie = 'PresentationApiTest=true; Expires=' + new Date().toUTCString(); + sessionStorage.removeItem('presentation_api_test'); + localStorage.removeItem('presentation_api_test'); + + Object.values(dbName).forEach(name => { + indexedDB.deleteDatabase(name); + }); + + if (connection) { + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + } + + if ('serviceWorker' in navigator) { + navigator.serviceWorker.getRegistrations().then(registrations => { + return Promise.all(registrations.map(reg => reg.unregister())); + }); + } + if ('caches' in window) { + caches.keys().then(keys => { + return Promise.all(keys.map(key => caches.delete(key))); + }); + } + }); + + history.pushState(null, 'test', 'PresentationReceiver_create-manual.https.html'); + document.cookie = 'PresentationApiTest=Controlling-UA'; + + const storageName = 'presentation_api_test'; + const storageValue = 'receiving-ua'; + sessionStorage.setItem(storageName, storageValue); + localStorage.setItem(storageName, storageValue); + + const openIndexedDB = () => { + if ('indexedDB' in window) { + const req = indexedDB.open(dbName.controller, 1); + const eventWatcher = new EventWatcher(t, req, 'upgradeneeded'); + return eventWatcher.wait_for('upgradeneeded').then(evt => { + evt.target.result.close(); + }); + } + else + return Promise.resolve(); + }; + + const cacheName = 'controlling-ua'; + let clientUrls; + const getClientUrls = () => { + return new Promise(resolve => { + navigator.serviceWorker.getRegistration().then(reg => { + if (reg) { + const channel = new MessageChannel(); + channel.port1.onmessage = event => { + resolve(event.data); + }; + reg.active.postMessage('', [channel.port2]); + } + else + resolve([]); + }); + }); + }; + const registerServiceWorker = () => { + return ('serviceWorker' in navigator ? + navigator.serviceWorker.register('serviceworker.js').then(registration => { + return new Promise((resolve, reject) => { + if (registration.installing) { + registration.installing.addEventListener('statechange', event => { + if(event.target.state === 'installed') + resolve(); + }); + } + else + resolve(); + }); + }) : Promise.resolve()).then(getClientUrls).then(urls => { + clientUrls = urls; + }); + }; + const openCaches = () => { + return 'caches' in window ? caches.open(cacheName).then(cache => cache.add('cache.txt')) : Promise.resolve(); + }; + + const checkUpdates = () => { + // Cookie + assert_equals(document.cookie, 'PresentationApiTest=Controlling-UA', 'A cookie store is not shared with a receiving user agent.'); + + // Web Storage + assert_equals(sessionStorage.length, 1, 'Session storage is not shared with a receiving user agent.'); + assert_equals(sessionStorage.getItem(storageName), storageValue, 'Session storage is not shared with a receiving user agent.'); + assert_equals(localStorage.length, 1, 'Local storage is not shared with a receiving user agent.'); + assert_equals(localStorage.getItem(storageName), storageValue, 'Local storage is not shared with a receiving user agent.'); + }; + + // Indexed Database + const checkIndexedDB = t => { + if ('indexedDB' in window) { + const req = indexedDB.open(dbName.receiver); + const upgradeneededWatcher = new EventWatcher(t, req, 'upgradeneeded'); + const successWatcher = new EventWatcher(t, req, 'success'); + return Promise.race([ + upgradeneededWatcher.wait_for('upgradeneeded').then(evt => { + evt.target.result.close(); + }), + successWatcher.wait_for('success').then(evt => { + evt.target.result.close(); + // This would fail if the database created by the receiving UA is visible to the controlling UA + assert_unreached('Indexed Database is not shared with a receiving user agent.'); + }) + ]); + } + else + return Promise.resolve(); + }; + + // Service Workers + const checkServiceWorkers = () => { + return 'serviceWorker' in navigator ? navigator.serviceWorker.getRegistrations().then(registrations => { + const message = 'List of registered service worker registrations is not shared with a receiving user agent.'; + assert_equals(registrations.length, 1, message); + assert_equals(registrations[0].active.scriptURL, new Request('serviceworker.js').url, message); + }) : Promise.resolve(); + }; + const checkCaches = () => { + const message = 'Cache storage is not shared with a receiving user agent.'; + return 'caches' in window ? caches.keys().then(keys => { + assert_equals(keys.length, 1, message); + assert_equals(keys[0], cacheName, message); + return caches.open(keys[0]); + }).then(cache => cache.matchAll()) + .then(responses => { + assert_equals(responses.length, 1, message); + assert_equals(responses[0].url, new Request('cache.txt').url, message); + }) : Promise.resolve(); + }; + + let timeout; + const enableTimeout = () => { + timeout = t.step_timeout(() => { + t.force_timeout(); + t.done(); + }, 5000); + }; + const disableTimeout = () => { + clearTimeout(timeout); + }; + const cancelWait = () => { + connection.removeEventListener('close', onTerminate); + connection.removeEventListener('terminate', onTerminate); + }; + const onTerminate = (reject, event) => { + cancelWait(); + reject('A receiving user agent unexpectedly ' + event.type + 'd a presentation. '); + }; + const waitForTerminate = () => { + return new Promise((resolve, reject) => { + connection.addEventListener('close', onTerminate.bind(this, reject)); + connection.addEventListener('terminate', onTerminate.bind(this, reject)); + }); + }; + + // Start a presentation for receiving user agent tests + return request.start().then(c => { + connection = c; + enableTimeout(); + + // This Promise.race will be rejected if a receiving side terminates/closes the connection when window.close() is invoked + return Promise.race([ + openIndexedDB() + .then(registerServiceWorker) + .then(openCaches) + .then(() => { return stash.init(); }) + .then(() => { return stash.receive(); }), + waitForTerminate()]); + }).then(result => { + // terminate and connect again if the result is PASS + cancelWait(); + const json = JSON.parse(result); + if (json.test.status !== 0) + return json; + + // Check accessibility to window clients before terminating a presentation + return getClientUrls().then(urls => { + assert_true(urls.length === clientUrls.length && urls.every((value, index) => { return clientUrls[index] === value}), + 'A window client in a receiving user agent is not accessible to a service worker on a controlling user agent.'); + const eventWatcher = new EventWatcher(t, connection, 'terminate'); + connection.terminate(); + return eventWatcher.wait_for('terminate'); + }).then(() => { + connection = null; + disableTimeout(); + presentBtn.removeEventListener('click', main); + presentBtn.textContent = 'Continue Presentation Test'; + presentBtn.disabled = false; + const eventWatcher = new EventWatcher(t, presentBtn, 'click'); + return eventWatcher.wait_for('click'); + }).then(() => { + presentBtn.disabled = true; + return request.start(); + }).then(c => { + connection = c; + enableTimeout(); + return stash.receive(); + }).then(result => { + return JSON.parse(result); + }); + }).then(json => { + if (json.test.status === 0) { + checkUpdates(); + return checkIndexedDB(t) + .then(checkServiceWorkers) + .then(checkCaches); + } + else { + receiverStack = json.test.stack; + parseResult(json.test.message); + } + }); + }); + }; + presentBtn.addEventListener('click', main); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/cache.txt b/testing/web-platform/tests/presentation-api/receiving-ua/cache.txt new file mode 100644 index 0000000000..551e8ff210 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/cache.txt @@ -0,0 +1 @@ +Hello, Presentation API!
\ No newline at end of file diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/common.js b/testing/web-platform/tests/presentation-api/receiving-ua/common.js new file mode 100644 index 0000000000..cbee5b4702 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/common.js @@ -0,0 +1,63 @@ +(window => { + // Both a controlling side and a receiving one must share the same Stash ID to + // transmit data from one to the other. On the other hand, due to polling mechanism + // which cleans up a stash, stashes in both controller-to-receiver direction + // and one for receiver-to-controller are necessary. + window.stashIds = { + toController: '0c382524-5738-4df0-837d-4f53ea8addc2', + toReceiver: 'a9618cd1-ca2b-4155-b7f6-630dce953c44' + } + + // handle a test result received from a receiving page + const parseValue = value => { + let r; + + // String + if (r = value.match(/^(\(string\)\s+)?"(.*)"$/)) + return r[2]; + // Object + else if (r = value.match(/^(\(object\)\s+)?object\s+"\[object\s+(.*)\]"$/)) + return window[r[2]].prototype; + // Number, boolean, null, undefined + else { + if (r = value.match(/^(\(\S+\)\s+)?(\S+)$/)) { + try { + return JSON.parse(r[2]); + } catch(e) { + return value; + } + } + else + return value; + } + }; + + window.parseResult = message => { + let r = message.match(/^(assert_.*):\s+(.*)$/); + if (r) { + const assertion = r[1]; + const body = r[2]; + let args; + switch (assertion) { + case 'assert_equals': + if (r = body.match(/^((.*)\s+)?expected\s+((\(\S*\)\s+)?(\S+|(\S+\s+)?\".*\"))\s+but\s+got\s+((\(\S*\)\s+)?(\S+|(\S+\s+)?\".*\"))$/)) + args = [parseValue(r[7]), parseValue(r[3]), r[2]]; + break; + case 'assert_true': + if (r = body.match(/^((.*)\s+)?expected\s+(true|false)\s+got\s+(\S+|(\S+\s+)?\".*\")$/)) + args = [parseValue(r[4]), r[2]]; + break; + case 'assert_unreached': + if (r = body.match(/^((.*)\s+)?Reached\s+unreachable\s+code$/)) + args = [r[2]]; + break; + } + if (args) { + window[assertion](args[0], args[1], args[2]); + return; + } + } + // default + assert_unreached('Test result received from a receiving user agent: ' + message + ': '); + }; +})(window);
\ No newline at end of file diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/idlharness-manual.https.html b/testing/web-platform/tests/presentation-api/receiving-ua/idlharness-manual.https.html new file mode 100644 index 0000000000..c7bd7344d5 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/idlharness-manual.https.html @@ -0,0 +1,44 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Presentation API IDL tests for Receiving User Agent</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="http://w3c.github.io/presentation-api/#dfn-receiving-user-agent"> +<script src="common.js"></script> +<script src="support/stash.js"></script> + +<p>Click the button below and select the available presentation display, to start the manual test.</p> +<button id="presentBtn">Start Presentation Test</button> + +<script> + let connection; + const presentBtn = document.getElementById('presentBtn'); + presentBtn.onclick = () => { + presentBtn.disabled = true; + const stash = new Stash(stashIds.toController, stashIds.toReceiver); + const request = new PresentationRequest('support/idlharness_receiving-ua.html'); + + return request.start().then(c => { + connection = c; + return stash.init(); + }).then(() => { + return stash.receive(); + }).then(result => { + const json = JSON.parse(result); + // notify receiver's results of a parent window (e.g. test runner) + if (window.opener && 'completion_callback' in window.opener) { + window.opener.completion_callback(json.tests, json.status); + } + // display receiver's results as HTML + const log = document.createElement('div'); + log.id = 'log'; + log.innerHTML = json.log; + document.body.appendChild(log); + + connection.onconnect = () => { connection.terminate(); }; + if (connection.state === 'closed') + request.reconnect(connection.id); + else + connection.terminate(); + }); + }; +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/serviceworker.js b/testing/web-platform/tests/presentation-api/receiving-ua/serviceworker.js new file mode 100644 index 0000000000..ae50ad6498 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/serviceworker.js @@ -0,0 +1,15 @@ +self.addEventListener('install', () => { + // activate this service worker immediately + self.skipWaiting(); +}); + +self.addEventListener('activate', event => { + // let this service worker control window clients immediately + event.waitUntil(self.clients.claim()); +}); + +self.addEventListener('message', event => { + event.waitUntil(clients.matchAll().then(windows => { + event.ports[0].postMessage(windows.map(w => { return w.url; }).sort()); + })); +}); diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnectionList_onconnectionavailable_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnectionList_onconnectionavailable_receiving-ua.html new file mode 100644 index 0000000000..74d8dfa0ed --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnectionList_onconnectionavailable_receiving-ua.html @@ -0,0 +1,92 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Monitoring incoming presentation connections</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#monitoring-incoming-presentation-connections"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + + add_completion_callback((tests, status) => { + const log = document.getElementById('log'); + stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML })); + }); + + promise_test(t => { + const receiver = navigator.presentation.receiver; + return receiver.connectionList.then(list => { + let connections = list.connections; + let number = connections.length; + + const checkConnectionList = (connection, action) => { + assert_equals(connections.length, number, 'PresentationConnectionList.connections is a frozen array.'); + + // Note: When a presentation is terminated, a receiving user agent unloads a document + // without firing a "terminate" event. + return receiver.connectionList.then(list => { + connections = list.connections; + if (action === 'close') { + assert_true(connections.length === number - 1 && !connections.includes(connection), + 'A closed presentation connection is removed from the set of presentation controllers.'); + } else if (action === 'connect') { + assert_true(connections.length === number + 1 && connections.includes(connection), + 'A new presentation connection is added to the set of presentation controllers.'); + } + number = connections.length; + }); + }; + + const checkEvent = evt => { + assert_true(evt instanceof PresentationConnectionAvailableEvent, 'An event using PresentationConnectionAvailableEvent is fired.'); + assert_true(evt.isTrusted, 'The event is a trusted event.'); + assert_false(evt.bubbles, 'The event does not bubbles.'); + assert_false(evt.cancelable, 'The event is not cancelable.'); + assert_equals(evt.type, 'connectionavailable', 'The event name is "connectionavailable".'); + assert_equals(evt.target, list, 'event.target is the presentation connection list.'); + assert_true(evt.connection instanceof PresentationConnection, 'event.connection is a presentation connection.'); + + return checkConnectionList(evt.connection, 'connect'); + }; + + const watchEvent = (obj, watcher, type) => { + const watchHandler = new Promise(resolve => { + obj['on' + type] = evt => { resolve(evt); }; + }); + return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { + assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); + return results[0]; + }); + }; + + // Step 1: check the first connection in "connected" state + let connection = list.connections[0]; + assert_equals(number, 1, 'A presentation connection list is populated with a first presentation connection.'); + assert_true(list instanceof PresentationConnectionList, 'navigator.presentation.receiver.connectionList is resolved with a presentation connection list.'); + assert_true(list.connections instanceof Array, 'A presentation connection list is an array.'); + assert_true(connection instanceof PresentationConnection, 'A presentation connection is added to a presentation connection list.'); + + // Step 2: check the connection in "closed" state + stash.send(JSON.stringify({ type: 'ok' })); + let eventWatcher = new EventWatcher(t, connection, 'close'); + return eventWatcher.wait_for('close').then(() => { + return checkConnectionList(connection, 'close'); + }) + // Step 3: check the first connection when reconnected + .then(() => { + stash.send(JSON.stringify({ type: 'ok' })); + eventWatcher = new EventWatcher(t, list, 'connectionavailable'); + return watchEvent(list, eventWatcher, 'connectionavailable') + }).then(checkEvent) + // Step 4: check the second connection (with <iframe>) in "connected" state + .then(() => { + stash.send(JSON.stringify({ type: 'ok' })); + return watchEvent(list, eventWatcher, 'connectionavailable') + }).then(checkEvent); + }); + }); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onclose_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onclose_receiving-ua.html new file mode 100644 index 0000000000..7194c6f4d9 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onclose_receiving-ua.html @@ -0,0 +1,89 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Closing a PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#closing-a-presentationconnection"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + + add_completion_callback((tests, status) => { + const log = document.getElementById('log'); + stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML })); + }); + + const checkCloseEvent = (evt, connection, reason) => { + assert_true(evt instanceof PresentationConnectionCloseEvent, 'An event using PresentationConnectionCloseEvent is fired.'); + assert_true(evt.isTrusted, 'The event is a trusted event.'); + assert_false(evt.bubbles, 'The event does not bubbles.'); + assert_false(evt.cancelable, 'The event is not cancelable.'); + assert_equals(evt.type, 'close', 'The event name is "close".'); + assert_equals(evt.target, connection, 'event.target is the presentation connection.'); + assert_equals(connection.state, 'closed', 'State of the presentation connection is "closed".'); + assert_equals(evt.reason, reason, 'The reason for closing the presentation connection is "' + reason + '".'); + }; + + const watchEvent = (obj, watcher, type) => { + const watchHandler = new Promise(resolve => { + obj['on' + type] = evt => { resolve(evt); }; + }); + return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { + assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); + return results[0]; + }); + }; + + promise_test(t => { + return navigator.presentation.receiver.connectionList.then(list => { + let connection = list.connections[0]; + assert_equals(list.connections.length, 1, 'A presentation connection list is populated with a first presentation connection.'); + + // Step 1: close the presentation connection in "connected" state + connection.close(); + let watchClose = new EventWatcher(t, connection, 'close'); + const watchNewConnection = new EventWatcher(t, list, 'connectionavailable'); + return watchEvent(connection, watchClose, 'close').then(evt => { + checkCloseEvent(evt, connection, 'closed'); + + // Step 2: check a connection closed by the controlling user agent + stash.send(JSON.stringify({ type: 'ok' })); + return watchNewConnection.wait_for('connectionavailable'); + }).then(evt => { + connection = evt.connection; + watchClose = new EventWatcher(t, connection, 'close'); + return watchEvent(connection, watchClose, 'close'); + }).then(evt => { + checkCloseEvent(evt, connection, 'closed'); + + // Step 3: try to close the presentation connection in "closed" state (nothing should happen) + connection.close(); + return Promise.race([ + new Promise(resolve => { t.step_timeout(resolve, 1000); }), + watchEvent(connection, watchClose, 'close').then(() => { + assert_unreached('Invoking PresentationConnection.close() in the "closed" state causes nothing.'); }) + ]); + }).then(() => { + + // Step 4: check a connection closed due to aborting the nested browsing context + stash.send(JSON.stringify({ type: 'ok' })); + return watchNewConnection.wait_for('connectionavailable'); + }).then(evt => { + connection = evt.connection; + stash.send(JSON.stringify({ type: 'ok' })); + watchClose = new EventWatcher(t, connection, 'close'); + return Promise.race([ + watchEvent(connection, watchClose, 'close'), + new Promise(resolve => { t.step_timeout(() => { resolve(null); }, 3000); }) + ]); + }).then(evt => { + assert_true(!!evt, 'A presentation connection is closed when its controller\'s browsing context is discarded.'); + checkCloseEvent(evt, connection, 'wentaway'); + }); + }); + }); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onmessage_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onmessage_receiving-ua.html new file mode 100644 index 0000000000..0734a5570c --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_onmessage_receiving-ua.html @@ -0,0 +1,108 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Receiving a message through PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="http://w3c.github.io/presentation-api/#receiving-a-message-through-presentationconnection"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> +setup({explicit_timeout: true}); + +const message1 = '1st'; +const message2 = '2nd'; +const message3 = new Uint8Array([51, 114, 100]); // "3rd" +const message4 = new Uint8Array([52, 116, 104]); // "4th" +const message5 = new Uint8Array([108, 97, 115, 116]); // "last" + +const toUint8Array = buf => { + return buf instanceof ArrayBuffer ? new Uint8Array(buf) : buf; +} + +// compare two ArrayBuffer or Uint8Array +const compare = (a, b) => { + const p = toUint8Array(a); + const q = toUint8Array(b); + return !!p && !!q && p.every((item, index) => { return item === q[index]; }); +}; + +const stash = new Stash(stashIds.toReceiver, stashIds.toController); + +add_completion_callback((tests, status) => { + const log = document.getElementById('log'); + stash.send(JSON.stringify({ type: 'result', tests: tests, status: status, log: log.innerHTML })); +}); + +navigator.presentation.receiver.connectionList.then(list => { + const checkEvent = event => { + assert_true(event.isTrusted, 'a trusted event is fired'); + assert_true(event instanceof MessageEvent, 'The event uses the MessageEvent interface'); + assert_false(event.bubbles, 'the event does not bubble'); + assert_false(event.cancelable, 'the event is not cancelable'); + }; + + const watchEvent = (obj, watcher, type) => { + const watchHandler = new Promise(resolve => { + obj['on' + type] = evt => { resolve(evt); }; + }); + return Promise.all([ watchHandler, watcher.wait_for(type) ]).then(results => { + assert_equals(results[0], results[1], 'Both on' + type + ' and addEventListener pass the same event object.'); + return results[0]; + }); + }; + + const connection = list.connections[0]; + + promise_test(t => { + t.step_timeout(() => { + t.force_timeout(); + t.done(); + }, 3000); + + assert_equals(connection.binaryType, 'arraybuffer', 'the default value of binaryType is "arraybuffer"'); + const eventWatcher = new EventWatcher(t, connection, 'message'); + return eventWatcher.wait_for('message').then(event => { + checkEvent(event); + assert_equals(event.data, message1, 'receive a string correctly'); + return watchEvent(connection, eventWatcher, 'message'); + }).then(event => { + checkEvent(event); + assert_equals(event.data, message2, 'receive a string correctly'); + return watchEvent(connection, eventWatcher, 'message'); + }).then(event => { + checkEvent(event); + assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); + assert_true(compare(event.data, message3), 'receive an ArrayBuffer correctly (originally a Blob at a controlling user agent)'); + return watchEvent(connection, eventWatcher, 'message'); + }).then(event => { + checkEvent(event); + assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); + assert_true(compare(event.data, message4), 'receive an ArrayBuffer correctly (originally an ArrayBuffer at a controlling user agent)'); + return watchEvent(connection, eventWatcher, 'message'); + }).then(event => { + checkEvent(event); + assert_true(event.data instanceof ArrayBuffer, 'receive binary data as ArrayBuffer'); + assert_true(compare(event.data, message5), 'receive an ArrayBuffer correctly (originally an ArrayBufferView at a controlling user agent)'); + + connection.binaryType = 'blob'; + return Promise.all([ + stash.send(JSON.stringify({ type: 'blob' })), + watchEvent(connection, eventWatcher, 'message') + ]).then(results => results[1]); + }).then(event => { + assert_true(event.data instanceof Blob, 'receive binary data as Blob'); + return new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.onload = resolve; + reader.onerror = reject; + reader.readAsArrayBuffer(event.data); + }); + }).then(event => { + assert_true(compare(event.target.result, message5), 'receive a Blob correctly'); + connection.terminate(); + }); + }); +}); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_send_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_send_receiving-ua.html new file mode 100644 index 0000000000..bc482e9dfd --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_send_receiving-ua.html @@ -0,0 +1,45 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Sending a message through PresentationConnection</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="http://w3c.github.io/presentation-api/#sending-a-message-through-presentationconnection"> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> +const stash = new Stash(stashIds.toReceiver, stashIds.toController); + +navigator.presentation.receiver.connectionList.then(list => { + const connection = list.connections[0]; + + const message1 = '1st'; + const message2 = '2nd'; + const message3 = new Uint8Array([51, 114, 100]); // "3rd" + const message4 = new Uint8Array([52, 116, 104]); // "4th" + const message5 = new Uint8Array([108, 97, 115, 116]); // "last" + + // send messages + return stash.receive().then(() => { + connection.send(message1); // string + connection.send(message2); // string + connection.send(new Blob([message3])); // Blob + connection.send(message4.buffer); // ArrayBuffer + connection.send(message5); // ArrayBufferView + + return stash.receive(); + }).then(() => { + // try to send a message in the "closed" state + // Note: a receiving user agent does not fire a "terminate" event + connection.close(); + connection.onclose = () => { + try { + connection.send(message1); + stash.send(JSON.stringify({ type: 'ok' })); + } catch (e) { + stash.send(JSON.stringify({ type: 'error', name: e.name, message: e.message })); + } + }; + }); +}); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_terminate_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_terminate_receiving-ua.html new file mode 100644 index 0000000000..3a234aaa83 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationConnection_terminate_receiving-ua.html @@ -0,0 +1,23 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Terminating a presentation in a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="https://w3c.github.io/presentation-api/#terminating-a-presentation-in-a-receiving-browsing-context"> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + + navigator.presentation.receiver.connectionList.then(list => { + const connection = list.connections[0]; + + // terminate a presentation when two presentation connections become "connected" + list.onconnectionavailable = evt => { + connection.terminate(); + + stash.sendBeacon({ type: (window.closed ? 'ok' : 'error') }); + }; + }); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua.html new file mode 100644 index 0000000000..53f7601442 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua.html @@ -0,0 +1,328 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Creating a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="https://w3c.github.io/presentation-api/#creating-a-receiving-browsing-context"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> +<style> +a { visibility: hidden; } +iframe { width: 100%; } +</style> + +<iframe id="child" src="PresentationReceiver_create_receiving-ua_child.html"></iframe> +<p id="notice">Checking <code id="modal"></code>: if you see this message, please wait for test to time out.</p> +<a href="PresentationReceiver_unreached_receiving-ua.html">do not navigate</a> +<script> +let finished = false; + +const sendResult = (test, status) => { + if(!finished) { + finished = true; + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + stash.send(JSON.stringify({ test: test, status: status })); + } +}; + +add_completion_callback((tests, status) => { + // note: a single test result is supposed to appear here. + sendResult(tests[0], status); +}); + +const child = document.getElementById('child'); +child.addEventListener('load', () => { + const notice = document.getElementById('notice'); + const modal = document.getElementById('modal'); + + const dbName = { + controller: 'db-presentation-api-controlling-ua', + receiver: 'db-presentation-api-receiving-ua' + }; + + promise_test(t => { + t.add_cleanup(() => { + document.cookie = cookieName + '=False;Expires=' + new Date().toUTCString(); + document.cookie = cookieNameChild + '=False;Expires=' + new Date().toUTCString(); + sessionStorage.removeItem(storageName); + localStorage.removeItem(storageName); + sessionStorage.removeItem(storageNameChild); + localStorage.removeItem(storageNameChild); + + Object.values(dbName).forEach(name => { + indexedDB.deleteDatabase(name); + }); + + if ('serviceWorker' in navigator) { + navigator.serviceWorker.getRegistrations().then(registrations => { + return Promise.all(registrations.map(reg => reg.unregister())); + }); + } + if ('caches' in window) { + caches.keys().then(keys => { + return Promise.all(keys.map(key => caches.delete(key))); + }); + } + }); + + // Session history + assert_equals(window.history.length, 1, 'Session history consists of the current page only.'); + + // The Sandboxed auxiliary navigation browsing context flag + assert_equals(window.open('PresentationReceiver_unreached_receiving-ua.html'), null, 'Sandboxed auxiliary navigation browsing context flag is set.'); + + // The sandboxed top-level navigation browsing context flag (codes below are expected to be ignored) + window.close(); + document.querySelector('a').click(); + location.href = 'PresentationReceiver_unreached_receiving-ua.html'; + + // The sandboxed modals flag (codes below are expected to be ignored): + // If user agent prompts user, a timeout will occur and the test will eventually fail + let message = 'If you see this prompt, do not dismiss it and wait for test to time out.'; + notice.style.display = 'block'; + modal.textContent = 'alert()'; + alert(message); + modal.textContent = 'confirm()'; + confirm(message); + modal.textContent = 'print()'; + print(); + modal.textContent = 'prompt()'; + prompt(message); + notice.style.display = 'none'; + + // Permissions + const checkPermission = query => { + return navigator.permissions ? navigator.permissions.query(query).then(status => { + assert_equals(status.state, 'denied', 'The state of the "' + query.name + '" permission is set to "denied".'); + }, () => { /* skip this assertion if the specified permission is not implemented */ }) : Promise.resolve(); + } + + // Cookie + assert_equals(document.cookie, '', 'A cookie store is set to an empty store.') + + // Indexed Database + const checkIndexedDB = () => { + if ('indexedDB' in window) { + // The test would fail when the database is already created by the controlling UA + const req = indexedDB.open(dbName.controller); + const upgradeneededWatcher = new EventWatcher(t, req, 'upgradeneeded'); + const successWatcher = new EventWatcher(t, req, 'success'); + return Promise.race([ + upgradeneededWatcher.wait_for('upgradeneeded').then(evt => { + evt.target.result.close(); + const req = indexedDB.open(dbName.receiver, 2); + const eventWatcher = new EventWatcher(t, req, 'upgradeneeded'); + return eventWatcher.wait_for('upgradeneeded'); + }).then(evt => { + evt.target.result.close(); + }), + successWatcher.wait_for('success').then(evt => { + evt.target.result.close(); + // This would fail if the database created by the controlling UA is visible to the receiving UA + assert_unreached('Indexed Database is set to an empty storage.'); + }) + ]); + } + else + return Promise.resolve(); + }; + + // Web Storage + assert_equals(sessionStorage.length, 0, 'Session storage is set to an empty storage.'); + assert_equals(localStorage.length, 0, 'Local storage is set to an empty storage.'); + + // Service Workers + const checkServiceWorkers = () => { + return 'serviceWorker' in navigator ? navigator.serviceWorker.getRegistrations().then(registrations => { + assert_equals(registrations.length, 0, 'List of registered service worker registrations is empty.'); + }) : Promise.resolve(); + }; + const checkCaches = () => { + return 'caches' in window ? caches.keys().then(keys => { + assert_equals(keys.length, 0, 'Cache storage is empty.') + }) : Promise.resolve(); + }; + + // Navigation + const checkNavigation = () => { + return navigator.presentation.receiver.connectionList.then(connectionList => { + assert_equals(connectionList.connections.length, 1, 'The initial number of presentation connections is one'); + assert_equals(location.href, connectionList.connections[0].url, 'A receiving browsing context is navigated to the presentation URL.'); + }); + }; + + // Update storages and service workers shared with the top-level brosing context + const cookieName = 'PresentationApiTest'; + const cookieValue = 'Receiving-UA'; + const storageName = 'presentation_api_test'; + const storageValue = 'receiving-ua'; + document.cookie = cookieName + '=' + cookieValue; + sessionStorage.setItem(storageName, storageValue); + localStorage.setItem(storageName, storageValue); + + // Service Workers and Caches + const cacheName = 'receiving-ua'; + const getClientUrls = () => { + return new Promise(resolve => { + navigator.serviceWorker.getRegistration().then(reg => { + const channel = new MessageChannel(); + channel.port1.onmessage = event => { + resolve(event.data); + }; + reg.active.postMessage('test', [channel.port2]); + }); + }); + }; + + const registerServiceWorker = () => { + return ('serviceWorker' in navigator ? + navigator.serviceWorker.register('../serviceworker.js').then(registration => { + return new Promise((resolve, reject) => { + if (registration.installing) { + registration.installing.addEventListener('statechange', event => { + if(event.target.state === 'installed') + resolve(); + }); + } + else + resolve(); + }); + }) : Promise.resolve()).then(getClientUrls).then(urls => { + assert_true(urls.every(url => { return url !== new Request('../PresentationReceiver_create-manual.https.html').url }), + 'A window client in a controlling user agent is not accessible to a service worker on a receiving user agent.'); + }); + }; + + const openCaches = () => { + return 'caches' in window ? caches.open(cacheName).then(cache => cache.add('../cache.txt')) : Promise.resolve(); + }; + + const getChildFrameResult = () => { + return new Promise(resolve => { + window.addEventListener('message', t.step_func(event => { + const result = event.data; + if (result.type === 'presentation-api') { + // if the test in iframe failed, report the result and abort the test + if (result.test.status === 0) + resolve(); + else { + sendResult(result.test, result.status); + assert_unreached('A test for a nested browsing context failed.'); + } + } + })); + child.contentWindow.postMessage('start', location.origin); + }); + }; + + // check the results from updates by iframe + const cookieNameChild = 'NestedBrowsingContext'; + const cookieValueChild = 'True'; + const storageNameChild = 'nested_browsing_context'; + const storageValueChild = 'yes'; + + const checkUpdatedResult = () => { + // cookie + const cookies = document.cookie.split(/;\s*/).reduce((result, item) => { + const t = item.split('='); + result[t[0]] = t[1]; + return result; + }, {}); + message = 'A cookie store is shared by top-level and nested browsing contexts.' + assert_equals(Object.keys(cookies).length, 2, message); + assert_equals(cookies[cookieName], cookieValue, message); + assert_equals(cookies[cookieNameChild], cookieValueChild, message); + + // Web Storage + message = 'Session storage is shared by top-level and nested browsing contexts.'; + assert_equals(sessionStorage.length, 2, message); + assert_equals(sessionStorage.getItem(storageName), storageValue, message); + assert_equals(sessionStorage.getItem(storageNameChild), storageValueChild, message); + message = 'Local storage is shared by top-level and nested browsing contexts.'; + assert_equals(localStorage.length, 2, message); + assert_equals(localStorage.getItem(storageName), storageValue, message); + assert_equals(localStorage.getItem(storageNameChild), storageValueChild, message); + }; + + // Indexed Database + const checkUpdatedIndexedDB = () => { + if ('indexedDB' in window) { + message = 'Indexed Database is shared by top-level and nested browsing contexts.'; + const req = indexedDB.open(dbName.receiver); + const upgradeneededWatcher = new EventWatcher(t, req, 'upgradeneeded'); + const successWatcher = new EventWatcher(t, req, 'success'); + return Promise.race([ + upgradeneededWatcher.wait_for('upgradeneeded').then(evt => { + evt.target.result.close(); + // Check if the version of the database is upgraded to 3 by the nested browsing context + assert_unreached(message); + }), + successWatcher.wait_for('success').then(evt => { + const db = evt.target.result; + const version = db.version; + db.close(); + // Check if the version of the database is upgraded to 3 by the nested browsing context + assert_equals(version, 3, message); + }) + ]); + } + else + return Promise.resolve(); + }; + + // Service Workers + const checkUpdatedServiceWorkers = () => { + return 'serviceWorker' in window ? navigator.serviceWorker.getRegistrations().then(registrations => { + message = 'List of registered service worker registrations is shared by top-level and nested browsing contexts.'; + assert_equals(registrations.length, 2, message); + const scriptURLs = registrations.map(reg => { return reg.active.scriptURL; }).sort(); + assert_equals(scriptURLs[0], new Request('../serviceworker.js').url, message); + assert_equals(scriptURLs[1], new Request('serviceworker.js').url, message); + }) : Promise.resolve(); + }; + const cacheNameChild = 'nested-browsing-context'; + const checkUpdatedCaches = () => { + message = 'Cache storage is shared by top-level and nested browsing contexts.'; + return 'caches' in window ? caches.keys().then(keys => { + assert_equals(keys.length, 2, message); + const cacheKeys = keys.sort(); + assert_equals(cacheKeys[0], cacheNameChild, message); + assert_equals(cacheKeys[1], cacheName, message); + return Promise.all(keys.map( + key => caches.open(key) + .then(cache => cache.matchAll()) + .then(responses => { + assert_equals(responses.length, 1, message); + assert_equals(responses[0].url, new Request('../cache.txt').url, message); + }))); + }) : Promise.resolve(); + }; + + // Asynchronous tests + const permissionList = [ + { name: 'geolocation' }, + { name: 'notifications' }, + { name: 'push', userVisibleOnly: true }, + { name: 'midi' }, + { name: 'camera' }, + { name: 'microphone' }, + { name: 'speaker' }, + { name: 'background-sync' } + ]; + return Promise.all(permissionList.map(perm => { return checkPermission(perm); })) + .then(checkIndexedDB) + .then(checkServiceWorkers) + .then(checkCaches) + .then(checkNavigation) + .then(registerServiceWorker) + .then(openCaches) + .then(getChildFrameResult) + .then(checkUpdatedResult) + .then(checkUpdatedIndexedDB) + .then(checkUpdatedServiceWorkers) + .then(checkUpdatedCaches); + }); +}); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua_child.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua_child.html new file mode 100644 index 0000000000..2f67f7fac0 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_create_receiving-ua_child.html @@ -0,0 +1,171 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Creating a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="https://w3c.github.io/presentation-api/#creating-a-receiving-browsing-context"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<p id="notice">Checking <code id="modal"></code>: if you see this message, please wait for test to time out.</p> + +<script> +add_completion_callback((tests, status) => { + // remove unserializable attributes, then send the result to the parent window + // note: a single test result is supposed to appear here. + window.parent.postMessage(JSON.parse(JSON.stringify({ + type: 'presentation-api', test: tests[0], status: status + })), location.origin); +}); + +window.addEventListener('message', event => { + if(event.data !== 'start') + return; + + promise_test(t => { + const notice = document.getElementById('notice'); + const modal = document.getElementById('modal'); + + // Presentation and PresentationReceiver + assert_true(navigator.presentation instanceof Presentation, 'navigator.presentation in a nested browsing context is an instanceof Presentation.'); + assert_equals(navigator.presentation.receiver, null, 'navigator.presentation.receiver in a nested browsing context is set to null.'); + + // Session history + assert_equals(window.history.length, 1, 'Session history consists of the current page only.'); + + // The Sandboxed auxiliary navigation browsing context flag (codes below are expected to be ignored) + assert_equals(window.open('./idlharness_receiving-ua.html'), null, 'Sandboxed auxiliary navigation browsing context flag is set.'); + + // The sandboxed modals flag (codes below are expected to be ignored): + // If user agent prompts user, a timeout will occur and the test will eventually fail + let message = 'If you see this prompt, do not dismiss it and wait for test to time out.'; + notice.style.display = 'block'; + modal.textContent = 'alert()'; + alert(message); + modal.textContent = 'confirm()'; + confirm(message); + modal.textContent = 'print()'; + print(); + modal.textContent = 'prompt()'; + prompt(message); + notice.style.display = 'none'; + + // Permissions + const checkPermission = query => { + return navigator.permissions ? navigator.permissions.query(query).then(status => { + assert_equals(status.state, 'denied', 'The state of the "' + query.name + '" permission in a nested browsing context is set to "denied".'); + }, () => { /* skip this assertion if the specified permission is not implemented */ }) : Promise.resolve(); + }; + + // Cookie + assert_equals(document.cookie, 'PresentationApiTest=Receiving-UA', 'A cookie store is shared by top-level and nested browsing contexts.'); + + // Indexed Database + const dbName = 'db-presentation-api-receiving-ua'; + const checkIndexedDB = () => { + if ('indexedDB' in window) { + message = 'Indexed Database is shared by top-level and nested browsing contexts.'; + + const req = indexedDB.open(dbName); + const upgradeneededWatcher = new EventWatcher(t, req, 'upgradeneeded'); + const successWatcher = new EventWatcher(t, req, 'success'); + return Promise.race([ + upgradeneededWatcher.wait_for('upgradeneeded').then(evt => { + evt.target.result.close(); + // This would fail if the database is not created by the top-level browsing context + assert_unreached(message); + }), + successWatcher.wait_for('success').then(evt => { + evt.target.result.close(); + const db = evt.target.result; + const version = db.version; + db.close(); + // Check if the version of the database is 2 as specified by the top-level browsing context + assert_equals(version, 2, message); + + // Upgrade the version + const req = indexedDB.open(dbName, 3); + const eventWatcher = new EventWatcher(t, req, 'upgradeneeded'); + return eventWatcher.wait_for('upgradeneeded'); + }).then(evt => { + evt.target.result.close(); + }) + ]); + } + else + return Promise.resolve(); + }; + + // Web Storage + assert_equals(sessionStorage.length, 1, 'Session storage is shared by top-level and nested browsing contexts.'); + assert_equals(sessionStorage.getItem('presentation_api_test'), 'receiving-ua', 'Session storage is shared by top-level and nested browsing contexts.'); + assert_equals(localStorage.length, 1, 'Local storage is shared by top-level and nested browsing contexts.'); + assert_equals(localStorage.getItem('presentation_api_test'), 'receiving-ua', 'Local storage is shared by top-level and nested browsing contexts.'); + + // Service Workers + const checkServiceWorkers = () => { + return 'serviceWorker' in navigator ? navigator.serviceWorker.getRegistrations().then(registrations => { + message = 'List of registered service worker registrations is shared by top-level and nested browsing contexts.'; + assert_equals(registrations.length, 1, message); + assert_equals(registrations[0].active.scriptURL, new Request('../serviceworker.js').url, message); + }) : Promise.resolve(); + }; + const checkCaches = () => { + message = 'Cache storage is shared by top-level and nested browsing contexts.'; + return 'caches' in window ? caches.keys().then(keys => { + assert_equals(keys.length, 1, message); + assert_equals(keys[0], 'receiving-ua', message); + return caches.open(keys[0]); + }).then(cache => cache.matchAll()) + .then(responses => { + assert_equals(responses.length, 1, message); + assert_equals(responses[0].url, new Request('../cache.txt').url, message); + }) : Promise.resolve(); + }; + + // Update storages and service workers shared with the top-level brosing context + document.cookie = 'NestedBrowsingContext=True'; + sessionStorage.setItem('nested_browsing_context', 'yes'); + localStorage.setItem('nested_browsing_context', 'yes'); + + // register a service worker with different scope from that of the top-level browsing context + const registerServiceWorker = () => { + return 'serviceWorker' in navigator ? + navigator.serviceWorker.register('serviceworker.js').then(registration => { + return new Promise((resolve, reject) => { + if (registration.installing) { + registration.installing.addEventListener('statechange', event => { + if(event.target.state === 'installed') + resolve(); + }); + } + else + resolve(); + }); + }) : Promise.resolve(); + }; + + const openCaches = () => { + return 'caches' in window + ? caches.open('nested-browsing-context').then(cache => cache.add('../cache.txt')) : Promise.resolve(); + }; + + // Asynchronous tests + const permissionList = [ + { name: 'geolocation' }, + { name: 'notifications' }, + { name: 'push', userVisibleOnly: true }, + { name: 'midi' }, + { name: 'camera' }, + { name: 'microphone' }, + { name: 'speaker' }, + { name: 'background-sync' } + ]; + return Promise.all(permissionList.map(perm => { return checkPermission(perm); })) + .then(checkIndexedDB) + .then(checkServiceWorkers) + .then(checkCaches) + .then(registerServiceWorker) + .then(openCaches); + }); +}); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_unreached_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_unreached_receiving-ua.html new file mode 100644 index 0000000000..2b2a8b315e --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/PresentationReceiver_unreached_receiving-ua.html @@ -0,0 +1,21 @@ +<!DOCTYPE html> + +<meta charset="utf-8"> +<title>Creating a receiving browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs/"> +<link rel="help" href="https://w3c.github.io/presentation-api/#creating-a-receiving-browsing-context"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> +<script> +add_completion_callback((tests, status) => { + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + const log = document.getElementById('log'); + stash.send(JSON.stringify({ test: tests[0], status: status, log: log.innerHTML })); +}); + +test(() => { + assert_unreached('Top-level navigation is disabled.'); +}); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/idlharness_receiving-ua.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/idlharness_receiving-ua.html new file mode 100644 index 0000000000..2d5a5b17f1 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/idlharness_receiving-ua.html @@ -0,0 +1,41 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Presentation API IDL tests for Receiving User Agent</title> +<link rel="author" title="Louay Bassbouss" href="http://www.fokus.fraunhofer.de"> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="http://w3c.github.io/presentation-api/#dfn-receiving-user-agent"> +<script src="/resources/testharness.js"></script> +<script src="/resources/testharnessreport.js"></script> +<script src="/resources/WebIDLParser.js"></script> +<script src="/resources/idlharness.js"></script> +<script src="../common.js"></script> +<script src="stash.js"></script> + +<script> + 'use strict'; + (async () => { + const srcs = ['presentation-api', 'dom', 'html']; + const [idl, dom, html] = await Promise.all( + srcs.map(i => fetch(`/interfaces/${i}.idl`).then(r => r.text()))); + + const idl_array = new IdlArray(); + idl_array.add_idls(idl, { + except: [ + 'PresentationRequest', + 'PresentationAvailability', + ] + }); + idl_array.add_dependency_idls(dom); + idl_array.add_dependency_idls(html); + idl_array.add_objects({ + Presentation: ['navigator.presentation'], + PresentationReceiver: ['navigator.presentation.receiver'] + }); + add_completion_callback((tests, status) => { + const stash = new Stash(stashIds.toReceiver, stashIds.toController); + const log = document.getElementById('log'); + stash.send(JSON.stringify({ tests: tests, status: status, log: log.innerHTML })); + }); + idl_array.test(); + })(); +</script> diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/iframe.html b/testing/web-platform/tests/presentation-api/receiving-ua/support/iframe.html new file mode 100644 index 0000000000..61f18b8912 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/iframe.html @@ -0,0 +1,22 @@ +<!DOCTYPE html> +<meta charset="utf-8"> +<title>Helper functions invoked by a nested browsing context</title> +<link rel="author" title="Tomoyuki Shimizu" href="https://github.com/tomoyukilabs"> +<link rel="help" href="https://w3c.github.io/presentation-api/#monitoring-incoming-presentation-connections"> + +<script> + window.addEventListener('message', event => { + const data = event.data; + + if (!data.type) + return; + else if (data.type === 'connect') { + const request = new PresentationRequest(data.url); + request.reconnect(data.id).then(c => { + c.onterminate = () => { + window.parent.postMessage({ type: 'terminated' }, '*'); + }; + }); + } + }); +</script>
\ No newline at end of file diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/serviceworker.js b/testing/web-platform/tests/presentation-api/receiving-ua/support/serviceworker.js new file mode 100644 index 0000000000..d2d4449ded --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/serviceworker.js @@ -0,0 +1,9 @@ +self.addEventListener('install', () => { + // activate this service worker immediately + self.skipWaiting(); +}); + +self.addEventListener('activate', event => { + // let this service worker control window clients immediately + event.waitUntil(self.clients.claim()); +}); diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.js b/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.js new file mode 100644 index 0000000000..eb63bb8623 --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.js @@ -0,0 +1,98 @@ +var Stash = function(inbound, outbound) { + this.stashPath = '/presentation-api/receiving-ua/support/stash.py?id='; + this.inbound = inbound; + this.outbound = outbound; +} + +// initialize a stash on wptserve +Stash.prototype.init = function() { + return Promise.all([ + fetch(this.stashPath + this.inbound).then(response => { + return response.text(); + }), + fetch(this.stashPath + this.outbound).then(response => { + return response.text(); + }) + ]); +} + +// upload a test result to a stash on wptserve +Stash.prototype.send = function(result) { + return fetch(this.stashPath + this.outbound, { + method: 'POST', + body: JSON.stringify({ type: 'data', data: result }) + }).then(response => { + return response.text(); + }).then(text => { + return text === 'ok' ? null : Promise.reject(); + }) +}; + +// upload a test result to a stash on wptserve via navigator.sendBeacon +Stash.prototype.sendBeacon = function(result) { + if ('sendBeacon' in navigator) { + navigator.sendBeacon(this.stashPath + this.outbound, JSON.stringify({ type: 'data', data: result })); + } + // Note: The following could be discarded, since XHR in synchronous mode is now being deprecated. + else { + return new Promise(resolve, reject => { + const xhr = new XMLHttpRequest(); + xhr.open('POST', this.stashPath + this.outbound, false); + xhr.send(JSON.stringify({ type: 'data', data: result })); + }); + } +}; + +// wait until a test result is uploaded to a stash on wptserve +Stash.prototype.receive = function() { + return new Promise((resolve, reject) => { + let intervalId; + const interval = 500; // msec + const polling = () => { + return fetch(this.stashPath + this.inbound).then(response => { + return response.text(); + }).then(text => { + if (text) { + try { + const json = JSON.parse(text); + if (json.type === 'data') + resolve(json.data); + else + reject(); + } catch(e) { + resolve(text); + } + clearInterval(intervalId); + } + }); + }; + intervalId = setInterval(polling, interval); + }); +}; + +// reset a stash on wptserve +Stash.prototype.stop = function() { + return Promise.all([ + fetch(this.stashPath + this.inbound).then(response => { + return response.text(); + }), + fetch(this.stashPath + this.outbound).then(response => { + return response.text(); + }) + ]).then(() => { + return Promise.all([ + fetch(this.stashPath + this.inbound, { + method: 'POST', + body: JSON.stringify({ type: 'stop' }) + }).then(response => { + return response.text(); + }), + fetch(this.stashPath + this.outbound, { + method: 'POST', + body: JSON.stringify({ type: 'stop' }) + }).then(response => { + return response.text(); + }) + ]); + }); +} diff --git a/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.py b/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.py new file mode 100644 index 0000000000..83653d365a --- /dev/null +++ b/testing/web-platform/tests/presentation-api/receiving-ua/support/stash.py @@ -0,0 +1,10 @@ +def main(request, response): + key = request.GET.first(b"id") + + if request.method == u"POST": + request.server.stash.put(key, request.body) + return b"ok" + else: + value = request.server.stash.take(key) + assert request.server.stash.take(key) is None + return value
\ No newline at end of file |