fune/testing/web-platform/tests/clipboard-apis/async-navigator-clipboard-basics.https.html
Kagami Sascha Rosylight 52f9697770 Bug 1875837 - Make clipboard-read/write permission optional in clipboard WPT r=webdriver-reviewers,dom-core,edgar,whimboo
Blink is the only engine supporting it, and thus it should not be the WPT requirement.

Some tests still need the testing pref, and for now this patch gives the pref for such failing tests without modifying them, for simplicity sake. Modification can happen separately.

Differential Revision: https://phabricator.services.mozilla.com/D201999
2024-03-27 17:38:18 +00:00

154 lines
5.7 KiB
HTML

<!doctype html>
<meta charset="utf-8">
<title>Async Clipboard input type validation tests</title>
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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 promise_rejects_dom(t, "NotAllowedError", navigator.clipboard.write([
new ClipboardItem({
"text/plain": text_plain,
"text/html" : html_text
}),
]));
}, 'navigator.clipboard.write(Promise<DOMString>) fails');
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>