FileAPI: Add web platform tests for new Blob APIs.

Change-Id: Ie12aa7cd3d79fa579cb68345b8d201342ed99ae5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1526796
Commit-Queue: Jarryd Goodman <jarrydg@chromium.org>
Reviewed-by: Marijn Kruisselbrink <mek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#650972}
diff --git a/FileAPI/blob/Blob-array-buffer.any.js b/FileAPI/blob/Blob-array-buffer.any.js
new file mode 100644
index 0000000..b72427f
--- /dev/null
+++ b/FileAPI/blob/Blob-array-buffer.any.js
@@ -0,0 +1,34 @@
+// META: title=Blob Array Buffer
+// META: script=../support/Blob.js
+'use strict';
+
+promise_test(async () => {
+  const input_arr = new TextEncoder().encode("PASS");
+  const blob = new Blob([input_arr]);
+  const array_buffer = await blob.arrayBuffer();
+  assert_true(array_buffer instanceof ArrayBuffer);
+  assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
+}, "Blob.arrayBuffer()")
+
+promise_test(async () => {
+  const input_arr = new TextEncoder().encode("");
+  const blob = new Blob([input_arr]);
+  const array_buffer = await blob.arrayBuffer();
+  assert_true(array_buffer instanceof ArrayBuffer);
+  assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
+}, "Blob.arrayBuffer() empty Blob data")
+
+promise_test(async () => {
+  const input_arr = new TextEncoder().encode("\u08B8\u000a");
+  const blob = new Blob([input_arr]);
+  const array_buffer = await blob.arrayBuffer();
+  assert_equals_typed_array(new Uint8Array(array_buffer), input_arr);
+}, "Blob.arrayBuffer() non-ascii input")
+
+promise_test(async () => {
+  const input_arr = [8, 241, 48, 123, 151];
+  const typed_arr = new Uint8Array(input_arr);
+  const blob = new Blob([typed_arr]);
+  const array_buffer = await blob.arrayBuffer();
+  assert_equals_typed_array(new Uint8Array(array_buffer), typed_arr);
+}, "Blob.arrayBuffer() non-unicode input")
diff --git a/FileAPI/blob/Blob-stream.any.js b/FileAPI/blob/Blob-stream.any.js
new file mode 100644
index 0000000..894f09f
--- /dev/null
+++ b/FileAPI/blob/Blob-stream.any.js
@@ -0,0 +1,50 @@
+// META: title=Blob Stream
+// META: script=../support/Blob.js
+'use strict';
+
+// Takes in a ReadableStream and reads from it until it is done, returning
+// an array that contains the results of each read operation
+async function read_all_chunks(stream) {
+  assert_true(stream instanceof ReadableStream);
+  assert_true('getReader' in stream);
+  const reader = stream.getReader();
+
+  assert_true('read' in reader);
+  let read_value = await reader.read();
+
+  let out = [];
+  let i = 0;
+  while (!read_value.done) {
+    for (let val of read_value.value) {
+      out[i++] = val;
+    }
+    read_value = await reader.read();
+  }
+  return out;
+}
+
+promise_test(async () => {
+  const blob = new Blob(["PASS"]);
+  const stream = await blob.stream()
+  const chunks = await read_all_chunks(stream);
+  for (let [index, value] of chunks.entries()) {
+    assert_equals(value, "PASS".charCodeAt(index));
+  }
+}, "Blob.stream()")
+
+promise_test(async () => {
+  const blob = new Blob();
+  const stream = await blob.stream()
+  const chunks = await read_all_chunks(stream);
+
+  assert_array_equals(chunks, []);
+}, "Blob.stream() empty Blob")
+
+promise_test(async () => {
+  const input_arr = [8, 241, 48, 123, 151];
+  const typed_arr = new Uint8Array(input_arr);
+  const blob = new Blob([typed_arr]);
+  const stream = await blob.stream()
+  const chunks = await read_all_chunks(stream);
+  assert_array_equals(chunks, input_arr)
+}, "Blob.stream() non-unicode input")
diff --git a/FileAPI/blob/Blob-text.any.js b/FileAPI/blob/Blob-text.any.js
new file mode 100644
index 0000000..960c960
--- /dev/null
+++ b/FileAPI/blob/Blob-text.any.js
@@ -0,0 +1,52 @@
+// META: title=Blob Text
+// META: script=../support/Blob.js
+'use strict';
+
+promise_test(async () => {
+  const blob = new Blob(["PASS"]);
+  const text = await blob.text();
+  assert_equals(text, "PASS");
+}, "Blob.text()")
+
+promise_test(async () => {
+  const blob = new Blob();
+  const text = await blob.text();
+  assert_equals(text, "");
+}, "Blob.text() empty blob data")
+
+promise_test(async () => {
+  const blob = new Blob(["P", "A", "SS"]);
+  const text = await blob.text();
+  assert_equals(text, "PASS");
+}, "Blob.text() multi-element array in constructor")
+
+promise_test(async () => {
+  const non_unicode = "\u0061\u030A";
+  const input_arr = new TextEncoder().encode(non_unicode);
+  const blob = new Blob([input_arr]);
+  const text = await blob.text();
+  assert_equals(text, non_unicode);
+}, "Blob.text() non-unicode")
+
+promise_test(async () => {
+  const blob = new Blob(["PASS"], { type: "text/plain;charset=utf-16le" });
+  const text = await blob.text();
+  assert_equals(text, "PASS");
+}, "Blob.text() different charset param in type option")
+
+promise_test(async () => {
+  const non_unicode = "\u0061\u030A";
+  const input_arr = new TextEncoder().encode(non_unicode);
+  const blob = new Blob([input_arr], { type: "text/plain;charset=utf-16le" });
+  const text = await blob.text();
+  assert_equals(text, non_unicode);
+}, "Blob.text() different charset param with non-ascii input")
+
+promise_test(async () => {
+  const input_arr = new Uint8Array([192, 193, 245, 246, 247, 248, 249, 250, 251,
+      252, 253, 254, 255]);
+  const blob = new Blob([input_arr]);
+  const text = await blob.text();
+  assert_equals(text, "\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd" +
+      "\ufffd\ufffd\ufffd\ufffd");
+}, "Blob.text() invalid utf-8 input")
diff --git a/FileAPI/support/Blob.js b/FileAPI/support/Blob.js
index 1d66f23..04069ac 100644
--- a/FileAPI/support/Blob.js
+++ b/FileAPI/support/Blob.js
@@ -1,3 +1,5 @@
+'use strict'
+
 function test_blob(fn, expectations) {
   var expected = expectations.expected,
       type = expectations.type,
@@ -47,3 +49,22 @@
     fr.readAsArrayBuffer(blob);
   });
 }
+
+// Assert that two TypedArray objects have the same byte values
+self.assert_equals_typed_array = (array1, array2) => {
+  const [view1, view2] = [array1, array2].map((array) => {
+    assert_true(array.buffer instanceof ArrayBuffer,
+      'Expect input ArrayBuffers to contain field `buffer`');
+    return new DataView(array.buffer, array.byteOffset, array.byteLength);
+  });
+
+  assert_equals(view1.byteLength, view2.byteLength,
+    'Expect both arrays to be of the same byte length');
+
+  const byteLength = view1.byteLength;
+
+  for (let i = 0; i < byteLength; ++i) {
+    assert_equals(view1.getUint8(i), view2.getUint8(i),
+      `Expect byte at buffer position ${i} to be equal`);
+  }
+}