Async Clipboard: Automate/duplicate WPT tests

- Having WPT tests run on buildbots automatically should increase velocity
(no longer need to run them manually and reduce chance of regressions
- Note to update corresponding tests so that buildbots and web platform are both
up to date.

Bug: 896479
Change-Id: Ic2dcbe46fac51dcd1ef01af7f5ed0f360a3ee00f
Reviewed-on: https://chromium-review.googlesource.com/c/1404401
Reviewed-by: Gary Kacmarcik <garykac@chromium.org>
Reviewed-by: Victor Costan <pwnall@chromium.org>
Commit-Queue: Darwin Huang <huangdarwin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#622275}
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/OWNERS b/third_party/blink/web_tests/clipboard/async-clipboard/OWNERS
new file mode 100644
index 0000000..bfe6b61
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/OWNERS
@@ -0,0 +1,5 @@
+# TEAM: storage-dev@chromium.org
+# COMPONENT: Blink>Storage>DataTransfer
+
+garykac@chromium.org
+pwnall@chromium.org
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/README.md b/third_party/blink/web_tests/clipboard/async-clipboard/README.md
new file mode 100644
index 0000000..81ddccd
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/README.md
@@ -0,0 +1,9 @@
+spec: https://w3c.github.io/clipboard-apis/
+
+This directory contains async clipboard tests automated through use of
+Chrome-specific test helper `permissions-helper.js`. Related tests not requiring
+`permissions-helper.js` can be found in
+[`web_tests/external/wpt/clipboard-apis/`](https://cs.chromium.org/chromium/src/third_party/blink/web_tests/external/wpt/clipboard-apis/).
+
+Whenever tests here or there are updated, please be sure to update corresponding
+tests, so that the web platform and automated buildbots can both keep updated.
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/async-navigator-clipboard-basics.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/async-navigator-clipboard-basics.https.html
new file mode 100644
index 0000000..e37669d
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/async-navigator-clipboard-basics.https.html
@@ -0,0 +1,80 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Async Clipboard basic tests</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
+<script>
+// Permissions are required in order to invoke navigator.clipboard functions in
+// an automated test.
+async function getPermissions() {
+  await PermissionsHelper.setPermission('clipboard-read', 'granted');
+  await PermissionsHelper.setPermission('clipboard-write', 'granted');
+}
+
+test(() => {
+  assert_not_equals(navigator.clipboard, undefined);
+  assert_true(navigator.clipboard instanceof Clipboard);
+  assert_equals(navigator.clipboard, navigator.clipboard);
+}, "navigator.clipboard exists");
+
+/* clipboard.write() */
+
+promise_test(async () => {
+  await getPermissions();
+  const dt = new DataTransfer();
+  dt.items.add("Howdy", "text/plain");
+  await navigator.clipboard.write(dt);
+}, "navigator.clipboard.write(DataTransfer) succeeds");
+
+promise_test(async t => {
+  await getPermissions();
+  await promise_rejects(t, new TypeError(),
+                         navigator.clipboard.write());
+}, "navigator.clipboard.write() fails (expect DataTransfer)");
+
+promise_test(async t => {
+  await getPermissions();
+  await promise_rejects(t, new TypeError(),
+                         navigator.clipboard.write(null));
+}, "navigator.clipboard.write(null) fails (expect DataTransfer)");
+
+promise_test(async t => {
+  await getPermissions();
+  await promise_rejects(t, new TypeError(),
+                         navigator.clipboard.write("Bad string"));
+}, "navigator.clipboard.write(DOMString) fails (expect DataTransfer)");
+
+
+/* clipboard.writeText() */
+
+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(t, new TypeError(),
+                         navigator.clipboard.writeText());
+}, "navigator.clipboard.writeText() fails (expect DOMString)");
+
+
+/* clipboard.read() */
+
+promise_test(async () => {
+  await getPermissions();
+  const result = await navigator.clipboard.read();
+  assert_true(result instanceof DataTransfer);
+}, "navigator.clipboard.read() succeeds");
+
+
+/* clipboard.readText() */
+
+promise_test(async () => {
+  await getPermissions();
+  const result = await navigator.clipboard.readText();
+  assert_equals(typeof result, "string");
+}, "navigator.clipboard.readText() succeeds");
+
+</script>
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-dttext.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-dttext.https.html
new file mode 100644
index 0000000..22838cd
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-dttext.https.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Async Clipboard write (dt/text) -> read (dt/text) tests</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
+<script>
+promise_test(async t => {
+  await PermissionsHelper.setPermission('clipboard-read', 'granted');
+  await PermissionsHelper.setPermission('clipboard-write', 'granted');
+
+  const input = "Clipboard write (dt/text) -> read (dt/text) test data";
+  const dt = new DataTransfer();
+  dt.items.add(input, "text/plain");
+
+  await navigator.clipboard.write(dt);
+  const output = await navigator.clipboard.read();
+
+  assert_equals(output.items.length, 1);
+  const result_promise = new Promise(resolve => {
+    output.items[0].getAsString(resolve);
+  });
+  const string_output = await result_promise;
+  assert_equals(string_output, input);
+}, "Verify write and read clipboard (DataTransfer/text)");
+</script>
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-text.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-text.https.html
new file mode 100644
index 0000000..22bd804
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-dttext-read-text.https.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Async Clipboard writeText -> read (dt/text) tests</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
+<script>
+promise_test(async t => {
+  await PermissionsHelper.setPermission('clipboard-read', 'granted');
+  await PermissionsHelper.setPermission('clipboard-write', 'granted');
+
+  const input = "Clipboard writeText -> read (dt/text) test data";
+
+  await navigator.clipboard.writeText(input);
+  const output = await navigator.clipboard.read();
+
+  assert_equals(output.items.length, 1);
+  const result_promise = new Promise(resolve => {
+    output.items[0].getAsString(resolve);
+  });
+  const string_output = await result_promise;
+  assert_equals(string_output, input);
+}, "Verify write and read clipboard (DOMString)");
+</script>
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-dttext.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-dttext.https.html
new file mode 100644
index 0000000..22bd804
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-dttext.https.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Async Clipboard writeText -> read (dt/text) tests</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
+<script>
+promise_test(async t => {
+  await PermissionsHelper.setPermission('clipboard-read', 'granted');
+  await PermissionsHelper.setPermission('clipboard-write', 'granted');
+
+  const input = "Clipboard writeText -> read (dt/text) test data";
+
+  await navigator.clipboard.writeText(input);
+  const output = await navigator.clipboard.read();
+
+  assert_equals(output.items.length, 1);
+  const result_promise = new Promise(resolve => {
+    output.items[0].getAsString(resolve);
+  });
+  const string_output = await result_promise;
+  assert_equals(string_output, input);
+}, "Verify write and read clipboard (DOMString)");
+</script>
diff --git a/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-text.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-text.https.html
new file mode 100644
index 0000000..b649316
--- /dev/null
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/async-write-text-read-text.https.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Async Clipboard writeText -> readText tests</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
+<script>
+promise_test(async t => {
+  await PermissionsHelper.setPermission('clipboard-read', 'granted');
+  await PermissionsHelper.setPermission('clipboard-write', 'granted');
+
+  const input = "Clipboard writeText -> readText test data";
+
+  await navigator.clipboard.writeText(input);
+  const output = await navigator.clipboard.readText();
+
+  assert_equals(output, input);
+}, "Verify write and read clipboard (DOMString)");
+</script>
diff --git a/third_party/blink/web_tests/clipboard/readtext-denied.https-expected.txt b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-denied.https-expected.txt
similarity index 100%
rename from third_party/blink/web_tests/clipboard/readtext-denied.https-expected.txt
rename to third_party/blink/web_tests/clipboard/async-clipboard/readtext-denied.https-expected.txt
diff --git a/third_party/blink/web_tests/clipboard/readtext-denied.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-denied.https.html
similarity index 84%
rename from third_party/blink/web_tests/clipboard/readtext-denied.https.html
rename to third_party/blink/web_tests/clipboard/async-clipboard/readtext-denied.https.html
index c089a3a..65a79998 100644
--- a/third_party/blink/web_tests/clipboard/readtext-denied.https.html
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-denied.https.html
@@ -1,8 +1,8 @@
 <!DOCTYPE html>
 <html>
 <head>
-<script src="../resources/js-test.js"></script>
-<script src="../http/tests/resources/permissions-helper.js"></script>
+<script src="../../resources/js-test.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
 </head>
 <body>
 <script>
diff --git a/third_party/blink/web_tests/clipboard/readtext-granted.https-expected.txt b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-granted.https-expected.txt
similarity index 100%
rename from third_party/blink/web_tests/clipboard/readtext-granted.https-expected.txt
rename to third_party/blink/web_tests/clipboard/async-clipboard/readtext-granted.https-expected.txt
diff --git a/third_party/blink/web_tests/clipboard/readtext-granted.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-granted.https.html
similarity index 84%
rename from third_party/blink/web_tests/clipboard/readtext-granted.https.html
rename to third_party/blink/web_tests/clipboard/async-clipboard/readtext-granted.https.html
index 6f2ada9..8fa93da4 100644
--- a/third_party/blink/web_tests/clipboard/readtext-granted.https.html
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/readtext-granted.https.html
@@ -1,8 +1,8 @@
 <!DOCTYPE html>
 <html>
 <head>
-<script src="../resources/js-test.js"></script>
-<script src="../http/tests/resources/permissions-helper.js"></script>
+<script src="../../resources/js-test.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
 </head>
 <body>
 <script>
diff --git a/third_party/blink/web_tests/clipboard/writetext-denied.https-expected.txt b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-denied.https-expected.txt
similarity index 100%
rename from third_party/blink/web_tests/clipboard/writetext-denied.https-expected.txt
rename to third_party/blink/web_tests/clipboard/async-clipboard/writetext-denied.https-expected.txt
diff --git a/third_party/blink/web_tests/clipboard/writetext-denied.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-denied.https.html
similarity index 84%
rename from third_party/blink/web_tests/clipboard/writetext-denied.https.html
rename to third_party/blink/web_tests/clipboard/async-clipboard/writetext-denied.https.html
index 3abd9b74..e664acb 100644
--- a/third_party/blink/web_tests/clipboard/writetext-denied.https.html
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-denied.https.html
@@ -1,8 +1,8 @@
 <!DOCTYPE html>
 <html>
 <head>
-<script src="../resources/js-test.js"></script>
-<script src="../http/tests/resources/permissions-helper.js"></script>
+<script src="../../resources/js-test.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
 </head>
 <body>
 <script>
diff --git a/third_party/blink/web_tests/clipboard/writetext-granted.https-expected.txt b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-granted.https-expected.txt
similarity index 100%
rename from third_party/blink/web_tests/clipboard/writetext-granted.https-expected.txt
rename to third_party/blink/web_tests/clipboard/async-clipboard/writetext-granted.https-expected.txt
diff --git a/third_party/blink/web_tests/clipboard/writetext-granted.https.html b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-granted.https.html
similarity index 84%
rename from third_party/blink/web_tests/clipboard/writetext-granted.https.html
rename to third_party/blink/web_tests/clipboard/async-clipboard/writetext-granted.https.html
index b0716bc..1b31de6 100644
--- a/third_party/blink/web_tests/clipboard/writetext-granted.https.html
+++ b/third_party/blink/web_tests/clipboard/async-clipboard/writetext-granted.https.html
@@ -1,8 +1,8 @@
 <!DOCTYPE html>
 <html>
 <head>
-<script src="../resources/js-test.js"></script>
-<script src="../http/tests/resources/permissions-helper.js"></script>
+<script src="../../resources/js-test.js"></script>
+<script src="../../http/tests/resources/permissions-helper.js"></script>
 </head>
 <body>
 <script>