Source code

Revision control

Copy as Markdown

Other Tools

Test Info: Warnings

<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard input type validation tests</title>
<body>Body needed for test_driver.click()</body>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="resources/user-activation.js"></script>
<script>
// Permissions are required in order to invoke navigator.clipboard functions in
// an automated test.
async function getPermissions() {
await tryGrantReadPermission();
await tryGrantWritePermission();
await waitForUserActivation();
}
test(() => {
assert_not_equals(navigator.clipboard, undefined);
assert_true(navigator.clipboard instanceof Clipboard);
assert_equals(navigator.clipboard, navigator.clipboard);
}, 'navigator.clipboard exists');
promise_test(async t => {
await getPermissions();
const text_plain = "This text was copied using `Clipboard.prototype.write`.";
const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
await navigator.clipboard.write([
new ClipboardItem({
"text/plain": text_plain,
"text/html" : html_text
}),
]);
}, 'navigator.clipboard.write(DOMString) succeeds');
promise_test(async () => {
await getPermissions();
const promise_text_string = Promise.resolve('hello');
const promise_html_string = Promise.resolve("<p style='color: red; font-style: oblique;'>hello</p>");
const item = new ClipboardItem({
'text/plain': promise_text_string,
'text/html': promise_html_string
});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<DOMString>) succeeds');
promise_test(async t => {
await getPermissions();
const text_plain = 'hello';
const html_text = "<p style='color: red; font-style: oblique;'>hello</p>";
const image = await fetch("/clipboard-apis/resources/greenbox.png");
const item = new ClipboardItem({
'text/plain': text_plain,
'text/html': new Blob([html_text], {type: 'text/html'}),
'image/png': image.blob(), // Promise<Blob>
'web text/csv': 'hello,world'
});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(web_custom_format) succeeds');
promise_test(async () => {
await getPermissions();
const html_text = "<p style='color: red; font-style: oblique;'>Test</p>";
const item = new ClipboardItem({
'text/plain': 'hello',
'text/html': new Blob([html_text], {type: 'text/html'})
});
const text = await item.getType('text/plain');
const blob = await item.getType('text/html');
assert_true(text instanceof Blob, "item.getType('text/plain') didn't return a Blob");
assert_true(blob instanceof Blob, "item.getType('text/html') didn't return a Blob");
}, 'validate GetType(type) on a contructed ClipboardItem returns Blob');
promise_test(async () => {
await getPermissions();
const blob = new Blob(['hello'], {type: 'text/plain'});
const item = new ClipboardItem({'text/plain': blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text/plain ClipboardItem]) succeeds');
promise_test(async t => {
await getPermissions();
const blob1 = new Blob(['hello'], {type: 'text/plain'});
const blob2 = new Blob(['world'], {type: 'text/plain'});
const item1 = new ClipboardItem({'text/plain': blob1});
const item2 = new ClipboardItem({'text/plain': blob2});
await promise_rejects_dom(t, "NotAllowedError",
navigator.clipboard.write([item1, item2]));
}, 'navigator.clipboard.write([>1 ClipboardItems]) fails (not implemented)');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError, navigator.clipboard.write());
}, 'navigator.clipboard.write() fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError, navigator.clipboard.write(null));
}, 'navigator.clipboard.write(null) fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError,
navigator.clipboard.write('Bad string'));
}, 'navigator.clipboard.write(DOMString) fails (expect [ClipboardItem])');
promise_test(async t => {
await getPermissions();
const blob = new Blob(['hello'], {type: 'text/plain'});
await promise_rejects_js(t, TypeError, navigator.clipboard.write(blob));
}, 'navigator.clipboard.write(Blob) fails (expect [ClipboardItem])');
promise_test(async () => {
await getPermissions();
await navigator.clipboard.writeText('New clipboard text');
}, 'navigator.clipboard.writeText(DOMString) succeeds');
promise_test(async t => {
await getPermissions();
await promise_rejects_js(t, TypeError,
navigator.clipboard.writeText());
}, 'navigator.clipboard.writeText() fails (expect DOMString)');
promise_test(async () => {
await getPermissions();
const item = new ClipboardItem({'text/plain': 'test'});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : DOMString}) succeeds');
promise_test(async () => {
await getPermissions();
const fetched = await fetch('/clipboard-apis/resources/greenbox.png');
const image = await fetched.blob();
const item = new ClipboardItem({'image/png': image});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write({string : image/png Blob}) succeeds');
promise_test(async() => {
await getPermissions();
const fetched = await fetch('/clipboard-apis/resources/greenbox.png');
const image = await fetched.blob();
const item = new ClipboardItem({
'text/plain': new Blob(['first'], {type: 'text/plain'}),
'image/png': image});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write([text + png] succeeds');
promise_test(async t => {
await getPermissions();
const item = new ClipboardItem({'image/png': 'not an image'});
await promise_rejects_js(t, TypeError, navigator.clipboard.write([item]));
}, 'navigator.clipboard.write(image/png DOMString) fails');
promise_test(async () => {
await getPermissions();
const result = await navigator.clipboard.read();
assert_true(result instanceof Object);
assert_true(result[0] instanceof ClipboardItem);
}, 'navigator.clipboard.read() succeeds');
promise_test(async () => {
await getPermissions();
const result = await navigator.clipboard.readText();
assert_equals(typeof result, 'string');
}, 'navigator.clipboard.readText() succeeds');
promise_test(async () => {
await getPermissions();
const promise_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
const item = new ClipboardItem({'text/plain': promise_blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>) succeeds');
promise_test(async () => {
await getPermissions();
const promise_text_blob = Promise.resolve(new Blob(['hello'], {type: 'text/plain'}));
const promise_html_blob = Promise.resolve(new Blob(["<p style='color: red; font-style: oblique;'>Test</p>"], {type: 'text/html'}));
const item = new ClipboardItem({'text/plain': promise_text_blob, 'text/html': promise_html_blob});
await navigator.clipboard.write([item]);
}, 'navigator.clipboard.write(Promise<Blob>s) succeeds');
</script>