<!DOCTYPE html>
<title>Clipboard: getAsFile() returns File for image</title>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<style>
.hidden { display: none; }
</style>
<div id=instructions class=hidden>
This file tests the image pasting functionality of
DataTransferItems. To try the test manually, right-click on the
image > Copy Image, click anywhere in the background of the main
page and paste.
</div>
<iframe id="src" src="resources/mozilla.gif"></iframe><br>
<script>
async_test(t => {
window.addEventListener('load', () => {
document.body.addEventListener('paste', t.step_func(e => {
const items = e.clipboardData.items;
assert_true(Array.from(items).some(i => i.kind === 'file'),
'A file should be present in the data');
for (let i = 0; i < items.length; ++i) {
if (items[i].kind == 'file') {
const file = items[i].getAsFile();
assert_class_string(file, 'File', 'IDL type should be File');
assert_equals(file.type, 'image/png', 'MIME type should be PNG');
assert_equals(file.name, 'image.png', 'File name should be generic');
// Allow for clock skew between script engine and DOM, NTP
// updates, slow test bots, etc. See https://crbug.com/324110
const slop_ms = 60 * 60 * 1000;
assert_approx_equals(file.lastModified, Date.now(), slop_ms,
'Should be newish');
t.done();
return;
}
}
}));
try {
const srcElement = document.getElementById('src');
srcElement.contentWindow.document.execCommand('copy');
document.execCommand('paste');
} catch (ex) {
// Fails in browser due to cross-origin restriction when run
// locally via file: so show manual instructions.
document.body.querySelector('#instructions').classList.remove('hidden');
}
});
}, 'DataTransferObject.getAsFile() returns File for image pastes');
</script>