Skip to content

Commit

Permalink
Clipboard API Tests: Automate Web Platform Tests
Browse files Browse the repository at this point in the history
Since #20461 and
#5671 were fixed, it now
becomes possible to automate some Web Platform Tests, and have them run
on all browsers automatically.

Clipboard API tests previously were duplicated in non-WPT in order to automate
them via PermissionsHelper and detect regressions.

This allows us to shed a lot of duplicate tests, as well as a difficult-to-remember
process of remembering to update both test suites when changes were made.

Bug: 1076691
Change-Id: Ibdfdf37e75da68d7a4b1e2ba986e4198b73230ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2065346
Commit-Queue: Victor Costan <pwnall@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#764967}
  • Loading branch information
Darwin Huang authored and Hexcles committed May 14, 2020
1 parent 90015af commit 957c333
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
async function loadBlob(fileName) {
Expand All @@ -14,6 +16,9 @@
}

promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const blobText = new Blob(['test text'], {type: 'text/plain'});
const blobImage = await loadBlob('resources/greenbox.png');

Expand All @@ -37,8 +42,3 @@
assert_equals(blobImageOutput.type, 'image/png');
}, 'Verify write and read clipboard (multiple types)');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
<p>
<p>The bottom image should display the same image as the top image.</p>
<p>Original Image:</p>
Expand Down Expand Up @@ -39,6 +40,9 @@
}

promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const blobInput = await loadBlob('resources/greenbox.png');

assert_equals(blobInput.type, 'image/png');
Expand All @@ -63,15 +67,13 @@
}, 'Verify write and read clipboard [image/png Blob]');

promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const invalidPngBlob = new Blob(['this text is not a valid png image'],
{type: "image/png"});
{type: 'image/png'});
const clipboardItemInput = new ClipboardItem({'image/png' : invalidPngBlob});
await promise_rejects_dom(t, "DataError",
await promise_rejects_dom(t, 'DataError',
navigator.clipboard.write([clipboardItemInput]));
}, 'Verify write error on malformed data [image/png ClipboardItem]');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<link rel='help' href='https://w3c.github.io/clipboard-apis/#async-clipboard-api'>
<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>
<iframe id="iframe"></iframe>
<script>
'use strict';
Expand All @@ -12,9 +14,12 @@
// Note: This tests proper behavior on a detaching iframe. text/plain is
// chosen for simplicity, and the test should fail the same way no matter what
// the input type is.
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const iframe = document.getElementById('iframe');
const iframeClipboard = iframe.contentWindow.navigator.clipboard;
const blobInput = new Blob(["test string"], {type: 'text/plain'});
const blobInput = new Blob(['test string'], {type: 'text/plain'});
const clipboardItemInput = new ClipboardItem({'text/plain': blobInput});
// Clipboard API must only be available in focused documents.
// reference: https://www.w3.org/TR/clipboard-apis/#privacy-async
Expand All @@ -25,10 +30,5 @@
// while the read operation is running.
iframeClipboard.read([clipboardItemInput]);
iframe.parentNode.removeChild(iframe);
}, "Verify read fails on detaching iframe");
}, 'Verify read fails on detaching iframe');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<link rel='help' href='https://w3c.github.io/clipboard-apis/#async-clipboard-api'>
<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>
<iframe id="iframe"></iframe>
<script>
'use strict';
Expand All @@ -12,9 +14,12 @@
// Note: This tests proper behavior on a detaching iframe. text/plain is
// chosen for simplicity, and the test should fail the same way no matter what
// the input type is.
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const iframe = document.getElementById('iframe');
const iframeClipboard = iframe.contentWindow.navigator.clipboard;
const blobInput = new Blob(["test string"], {type: 'text/plain'});
const blobInput = new Blob(['test string'], {type: 'text/plain'});
const clipboardItemInput = new ClipboardItem({'text/plain': blobInput});
// Clipboard API must only be available in focused documents.
// reference: https://www.w3.org/TR/clipboard-apis/#privacy-async
Expand All @@ -25,10 +30,5 @@
// while the write operation is running.
iframeClipboard.write([clipboardItemInput]);
iframe.parentNode.removeChild(iframe);
}, "Verify write fails on detaching iframe");
}, 'Verify write fails on detaching iframe');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<link rel='help' href='https://w3c.github.io/clipboard-apis/#async-clipboard-api'>
<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>
<iframe id="iframe"></iframe>
<script>
'use strict';
Expand All @@ -12,9 +14,12 @@
// Note: This tests proper detached iframe behavior. text/plain is chosen for
// simplicity, and the test should fail the same way no matter what the input
// type is.
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const iframe = document.getElementById('iframe');
const iframeClipboard = iframe.contentWindow.navigator.clipboard;
const blobInput = new Blob(["test string"], {type: 'text/plain'});
const blobInput = new Blob(['test string'], {type: 'text/plain'});
const clipboardItemInput = new ClipboardItem({'text/plain': blobInput});
// Clipboard API must only be available in focused documents.
// reference: https://www.w3.org/TR/clipboard-apis/#privacy-async
Expand All @@ -25,18 +30,13 @@
const readResultAttached = await iframeClipboard.read();
assert_not_equals(readResultAttached, undefined);
assert_equals(readResultAttached.length, 1,
"attached iframes should be able to read and write normally");
'attached iframes should be able to read and write normally');

iframe.parentNode.removeChild(iframe);
// Writing onto a detached iframe's clipboard should fail, but not crash.
await iframeClipboard.write([clipboardItemInput]);
const readResultDetached = await iframeClipboard.read();
assert_equals(readResultDetached, undefined,
"reading from detached iframes should output undefined");
}, "Verify read and write fail on detached iframe");
'reading from detached iframes should output undefined');
}, 'Verify read and write fail on detached iframe');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,35 @@
<link rel='help' href='https://w3c.github.io/clipboard-apis/#async-clipboard-api'>
<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>
<iframe id="iframe"></iframe>
<script>
'use strict';

promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const iframe = document.getElementById('iframe');
// Clipboard API must only be available in focused documents.
// reference: https://www.w3.org/TR/clipboard-apis/#privacy-async
iframe.focus();
const iframeClipboard = iframe.contentWindow.navigator.clipboard;

// Writing and reading should succeed on same-origin iframes.
const attachedWriteText = "attached write text"
const attachedWriteText = 'attached write text'
await iframeClipboard.writeText(attachedWriteText);
const attachedWriteResult = await iframeClipboard.readText();
assert_equals(attachedWriteResult, attachedWriteText,
"attached iframes should be able to readText and writeText normally");
'attached iframes should be able to readText and writeText normally');

iframe.parentNode.removeChild(iframe);
// Writing onto a detached iframe's clipboard should fail, but not crash.
const detachedWriteText = "detached write text";
const detachedWriteText = 'detached write text';
await iframeClipboard.writeText(detachedWriteText);
const readResultDetached = await iframeClipboard.readText();
assert_equals(readResultDetached, undefined,
"reading from detached iframes should output undefined");
}, "Verify readText and writeText fails on detached iframe");
'reading from detached iframes should output undefined');
}, 'Verify readText and writeText fails on detached iframe');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
async function readWriteTest(textInput) {
promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const blobInput = new Blob([textInput], {type: 'text/plain'});
const clipboardItemInput = new ClipboardItem({'text/plain': blobInput});

Expand All @@ -30,8 +35,3 @@
readWriteTest('Clipboard write ([text/plain ClipboardItem]) -> read ([text/plain ClipboardItem]) test');
readWriteTest('non-Latin1 text encoding test データ');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
async function readWriteTest(textInput) {
promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

const blobInput = new Blob([textInput], {type: 'text/plain'});
const clipboardItem = new ClipboardItem({'text/plain': blobInput});

Expand All @@ -22,8 +27,3 @@
readWriteTest('Clipboard write ([text/plain ClipboardItem) -> read text test');
readWriteTest('non-Latin1 text encoding test データ');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
async function readWriteTest(textInput) {
promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

await navigator.clipboard.writeText(textInput);
const clipboardItems = await navigator.clipboard.read();
assert_equals(clipboardItems.length, 1);
Expand All @@ -26,8 +31,3 @@
readWriteTest('Clipboard write text -> read ([text/plain ClipboardItem]) test');
readWriteTest('non-Latin1 text encoding test データ');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
<link rel="help" href="https://w3c.github.io/clipboard-apis/#async-clipboard-api">
<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>
async function readWriteTest(textInput) {
promise_test(async t => {
test_driver.set_permission({name: 'clipboard-read'}, 'granted');
test_driver.set_permission({name: 'clipboard-write'}, 'granted');

await navigator.clipboard.writeText(textInput);
const textOutput = await navigator.clipboard.readText();

Expand All @@ -17,8 +22,3 @@
readWriteTest('Clipboard write text -> read text test');
readWriteTest('non-Latin1 text encoding test データ');
</script>
<p>
Note: This is a manual test because it writes/reads to the shared system
clipboard and thus cannot be run async with other tests that might interact
with the clipboard.
</p>

0 comments on commit 957c333

Please sign in to comment.