gecko-dev/testing/web-platform/tests/storage/opaque-origin.https.window.js
Joshua Bell 2d0b83d996 Bug 1486832 [wpt PR 12723] - Storage API: Use .any.js (etc) in Web Platform Tests, a=testonly
Automatic update from web-platform-testsStorage API: Use .any.js (etc) in Web Platform Tests

Use ".any.js" tests [1] rather than redundant files for window and
worker variations, and increase coverage where window-only tests
existed. Also use ".worker.js" and ".window.js" to eliminate some
boilerplate. The one manual test is left untouched for ease of
running... manually.

[1] https://web-platform-tests.org/writing-tests/testharness.html

Change-Id: I7be790e0134854c804dbf82072589f07fa6e0bfb
Reviewed-on: https://chromium-review.googlesource.com/1194324
Reviewed-by: Chase Phillips <cmp@chromium.org>
Commit-Queue: Joshua Bell <jsbell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586773}

--

wpt-commits: ba92a402d8f994a92c73b22a153fd7aea89247c3
wpt-pr: 12723
2018-09-05 13:08:33 +00:00

73 lines
2.3 KiB
JavaScript

// META: title=StorageManager API and opaque origins
function load_iframe(src, sandbox) {
return new Promise(resolve => {
const iframe = document.createElement('iframe');
iframe.onload = () => { resolve(iframe); };
if (sandbox)
iframe.sandbox = sandbox;
iframe.srcdoc = src;
iframe.style.display = 'none';
document.documentElement.appendChild(iframe);
});
}
function wait_for_message(iframe) {
return new Promise(resolve => {
self.addEventListener('message', function listener(e) {
if (e.source === iframe.contentWindow) {
resolve(e.data);
self.removeEventListener('message', listener);
}
});
});
}
function make_script(snippet) {
return '<script>' +
' window.onmessage = () => {' +
' try {' +
' (' + snippet + ')' +
' .then(' +
' result => {' +
' window.parent.postMessage({result: "no rejection"}, "*");' +
' }, ' +
' error => {' +
' window.parent.postMessage({result: error.name}, "*");' +
' });' +
' } catch (ex) {' +
// Report if not implemented/exposed, rather than time out.
' window.parent.postMessage({result: ex.message}, "*");' +
' }' +
' };' +
'<\/script>';
}
['navigator.storage.persist()',
'navigator.storage.persisted()',
'navigator.storage.estimate()'
].forEach(snippet => {
promise_test(t => {
return load_iframe(make_script(snippet))
.then(iframe => {
iframe.contentWindow.postMessage({}, '*');
return wait_for_message(iframe);
})
.then(message => {
assert_equals(message.result, 'no rejection',
`${snippet} should not reject`);
});
}, `${snippet} in non-sandboxed iframe should not reject`);
promise_test(t => {
return load_iframe(make_script(snippet), 'allow-scripts')
.then(iframe => {
iframe.contentWindow.postMessage({}, '*');
return wait_for_message(iframe);
})
.then(message => {
assert_equals(message.result, 'TypeError',
`${snippet} should reject with TypeError`);
});
}, `${snippet} in sandboxed iframe should reject with TypeError`);
});