2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2021-11-23 11:45:18 -05:00
|
|
|
import { assertEquals } from "./test_util.ts";
|
2020-08-11 08:00:53 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function fileReaderConstruct() {
|
2020-08-11 08:00:53 -04:00
|
|
|
const fr = new FileReader();
|
|
|
|
assertEquals(fr.readyState, FileReader.EMPTY);
|
|
|
|
|
|
|
|
assertEquals(FileReader.EMPTY, 0);
|
|
|
|
assertEquals(FileReader.LOADING, 1);
|
|
|
|
assertEquals(FileReader.DONE, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlob() {
|
2020-08-11 08:00:53 -04:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
|
|
|
|
assertEquals(fr.readyState, FileReader.EMPTY);
|
|
|
|
|
|
|
|
const hasOnEvents = {
|
|
|
|
load: false,
|
|
|
|
loadend: false,
|
|
|
|
loadstart: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
const hasDispatchedEvents = {
|
|
|
|
load: false,
|
|
|
|
loadend: false,
|
|
|
|
loadstart: false,
|
|
|
|
progress: 0,
|
|
|
|
};
|
|
|
|
let result: string | null = null;
|
|
|
|
|
|
|
|
fr.addEventListener("load", () => {
|
|
|
|
hasDispatchedEvents.load = true;
|
|
|
|
});
|
|
|
|
fr.addEventListener("loadend", () => {
|
|
|
|
hasDispatchedEvents.loadend = true;
|
|
|
|
});
|
|
|
|
fr.addEventListener("loadstart", () => {
|
|
|
|
hasDispatchedEvents.loadstart = true;
|
|
|
|
});
|
|
|
|
fr.addEventListener("progress", () => {
|
|
|
|
hasDispatchedEvents.progress += 1;
|
|
|
|
});
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadstart = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.loadstart = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onprogress = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
assertEquals(fr.readyState, FileReader.LOADING);
|
|
|
|
|
|
|
|
hasOnEvents.progress += 1;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onload = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.load = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (ev) => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.loadend = true;
|
|
|
|
result = fr.result as string;
|
|
|
|
|
|
|
|
assertEquals(hasOnEvents.loadstart, true);
|
|
|
|
assertEquals(hasDispatchedEvents.loadstart, true);
|
|
|
|
assertEquals(hasOnEvents.load, true);
|
|
|
|
assertEquals(hasDispatchedEvents.load, true);
|
|
|
|
assertEquals(hasOnEvents.loadend, true);
|
|
|
|
assertEquals(hasDispatchedEvents.loadend, true);
|
|
|
|
|
|
|
|
assertEquals(fr.readyState, FileReader.DONE);
|
|
|
|
|
|
|
|
assertEquals(result, "Hello World");
|
|
|
|
assertEquals(ev.lengthComputable, true);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsText(b1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlobDouble() {
|
2020-08-11 08:00:53 -04:00
|
|
|
// impl note from https://w3c.github.io/FileAPI/
|
|
|
|
// Event handler for the load or error events could have started another load,
|
|
|
|
// if that happens the loadend event for the first load is not fired
|
|
|
|
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["First load"]);
|
|
|
|
const b2 = new Blob(["Second load"]);
|
|
|
|
|
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
let result: string | null = null;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onload = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
result = fr.result as string;
|
|
|
|
assertEquals(result === "First load" || result === "Second load", true);
|
|
|
|
|
|
|
|
if (result === "First load") {
|
|
|
|
fr.readAsText(b2);
|
|
|
|
}
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
assertEquals(result, "Second load");
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsText(b1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlobArrayBuffer() {
|
2020-08-11 08:00:53 -04:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
let result: ArrayBuffer | null = null;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (ev) => {
|
2020-08-11 08:00:53 -04:00
|
|
|
assertEquals(fr.result instanceof ArrayBuffer, true);
|
|
|
|
result = fr.result as ArrayBuffer;
|
|
|
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
const text = decoder.decode(result);
|
|
|
|
|
|
|
|
assertEquals(text, "Hello World");
|
|
|
|
assertEquals(ev.lengthComputable, true);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsArrayBuffer(b1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlobDataUrl() {
|
2020-08-11 08:00:53 -04:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
let result: string | null = null;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (ev) => {
|
2020-08-11 08:00:53 -04:00
|
|
|
result = fr.result as string;
|
|
|
|
assertEquals(
|
|
|
|
result,
|
|
|
|
"data:application/octet-stream;base64,SGVsbG8gV29ybGQ=",
|
|
|
|
);
|
|
|
|
assertEquals(ev.lengthComputable, true);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsDataURL(b1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlobAbort() {
|
2020-08-11 08:00:53 -04:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
|
|
|
|
const hasOnEvents = {
|
|
|
|
load: false,
|
|
|
|
loadend: false,
|
|
|
|
abort: false,
|
|
|
|
};
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onload = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.load = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (ev) => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.loadend = true;
|
|
|
|
|
|
|
|
assertEquals(hasOnEvents.load, false);
|
|
|
|
assertEquals(hasOnEvents.loadend, true);
|
|
|
|
assertEquals(hasOnEvents.abort, true);
|
|
|
|
|
|
|
|
assertEquals(fr.readyState, FileReader.DONE);
|
|
|
|
assertEquals(fr.result, null);
|
|
|
|
assertEquals(ev.lengthComputable, false);
|
|
|
|
resolve();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onabort = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.abort = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsDataURL(b1);
|
|
|
|
fr.abort();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fileReaderLoadBlobAbort() {
|
2020-08-11 08:00:53 -04:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
|
|
|
|
const hasOnEvents = {
|
|
|
|
load: false,
|
|
|
|
loadend: false,
|
|
|
|
abort: false,
|
|
|
|
};
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onload = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.load = true;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (ev) => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.loadend = true;
|
|
|
|
|
|
|
|
assertEquals(hasOnEvents.load, false);
|
|
|
|
assertEquals(hasOnEvents.loadend, true);
|
|
|
|
assertEquals(hasOnEvents.abort, true);
|
|
|
|
|
|
|
|
assertEquals(fr.readyState, FileReader.DONE);
|
|
|
|
assertEquals(fr.result, null);
|
|
|
|
assertEquals(ev.lengthComputable, false);
|
|
|
|
resolve();
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onabort = () => {
|
2020-08-11 08:00:53 -04:00
|
|
|
hasOnEvents.abort = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
fr.readAsDataURL(b1);
|
|
|
|
fr.abort();
|
|
|
|
});
|
|
|
|
});
|
2020-11-11 10:59:49 -05:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(
|
2021-08-05 07:08:58 -04:00
|
|
|
async function fileReaderDispatchesEventsInCorrectOrder() {
|
2020-11-11 10:59:49 -05:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
const fr = new FileReader();
|
|
|
|
const b1 = new Blob(["Hello World"]);
|
|
|
|
let out = "";
|
|
|
|
fr.addEventListener("loadend", () => {
|
|
|
|
out += "1";
|
|
|
|
});
|
2021-08-05 07:08:58 -04:00
|
|
|
fr.onloadend = (_ev) => {
|
2020-11-11 10:59:49 -05:00
|
|
|
out += "2";
|
|
|
|
};
|
|
|
|
fr.addEventListener("loadend", () => {
|
|
|
|
assertEquals(out, "12");
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
fr.readAsDataURL(b1);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|