Merge pull request #13222 from timvandermeij/unit-test-async

Convert done callbacks to async/await in the smaller unit test files
This commit is contained in:
Tim van der Meij 2021-04-14 20:37:17 +02:00 committed by GitHub
commit cd2c4e277c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 132 additions and 203 deletions

View File

@ -35,28 +35,22 @@ describe("custom canvas rendering", function () {
let loadingTask; let loadingTask;
let page; let page;
beforeAll(function (done) { beforeAll(async function () {
CanvasFactory = new DefaultCanvasFactory(); CanvasFactory = new DefaultCanvasFactory();
loadingTask = getDocument(transparentGetDocumentParams); loadingTask = getDocument(transparentGetDocumentParams);
loadingTask.promise const doc = await loadingTask.promise;
.then(function (doc) { const data = await doc.getPage(1);
return doc.getPage(1); page = data;
})
.then(function (data) {
page = data;
done();
})
.catch(done.fail);
}); });
afterAll(function (done) { afterAll(async function () {
CanvasFactory = null; CanvasFactory = null;
page = null; page = null;
loadingTask.destroy().then(done); await loadingTask.destroy();
}); });
it("renders to canvas with a default white background", function (done) { it("renders to canvas with a default white background", async function () {
const viewport = page.getViewport({ scale: 1 }); const viewport = page.getViewport({ scale: 1 });
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height); const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
@ -64,21 +58,18 @@ describe("custom canvas rendering", function () {
canvasContext: canvasAndCtx.context, canvasContext: canvasAndCtx.context,
viewport, viewport,
}); });
renderTask.promise await renderTask.promise;
.then(function () {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({ expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255, r: 255,
g: 255, g: 255,
b: 255, b: 255,
a: 255, a: 255,
}); });
CanvasFactory.destroy(canvasAndCtx); CanvasFactory.destroy(canvasAndCtx);
done();
})
.catch(done.fail);
}); });
it("renders to canvas with a custom background", function (done) { it("renders to canvas with a custom background", async function () {
const viewport = page.getViewport({ scale: 1 }); const viewport = page.getViewport({ scale: 1 });
const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height); const canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
@ -87,18 +78,15 @@ describe("custom canvas rendering", function () {
viewport, viewport,
background: "rgba(255,0,0,1.0)", background: "rgba(255,0,0,1.0)",
}); });
renderTask.promise await renderTask.promise;
.then(function () {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({ expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255, r: 255,
g: 0, g: 0,
b: 0, b: 0,
a: 255, a: 255,
}); });
CanvasFactory.destroy(canvasAndCtx); CanvasFactory.destroy(canvasAndCtx);
done();
})
.catch(done.fail);
}); });
}); });

View File

@ -20,7 +20,7 @@ describe("fetch_stream", function () {
const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href; const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href;
const pdfLength = 1016315; const pdfLength = 1016315;
it("read with streaming", function (done) { it("read with streaming", async function () {
const stream = new PDFFetchStream({ const stream = new PDFFetchStream({
url: pdfUrl, url: pdfUrl,
disableStream: false, disableStream: false,
@ -47,18 +47,14 @@ describe("fetch_stream", function () {
}); });
}; };
const readPromise = Promise.all([read(), promise]); await Promise.all([read(), promise]);
readPromise
.then(function () { expect(len).toEqual(pdfLength);
expect(len).toEqual(pdfLength); expect(isStreamingSupported).toEqual(true);
expect(isStreamingSupported).toEqual(true); expect(isRangeSupported).toEqual(false);
expect(isRangeSupported).toEqual(false);
done();
})
.catch(done.fail);
}); });
it("read ranges with streaming", function (done) { it("read ranges with streaming", async function () {
const rangeSize = 32768; const rangeSize = 32768;
const stream = new PDFFetchStream({ const stream = new PDFFetchStream({
url: pdfUrl, url: pdfUrl,
@ -98,20 +94,16 @@ describe("fetch_stream", function () {
}); });
}; };
const readPromise = Promise.all([ await Promise.all([
read(rangeReader1, result1), read(rangeReader1, result1),
read(rangeReader2, result2), read(rangeReader2, result2),
promise, promise,
]); ]);
readPromise
.then(function () { expect(isStreamingSupported).toEqual(true);
expect(isStreamingSupported).toEqual(true); expect(isRangeSupported).toEqual(true);
expect(isRangeSupported).toEqual(true); expect(fullReaderCancelled).toEqual(true);
expect(fullReaderCancelled).toEqual(true); expect(result1.value).toEqual(rangeSize);
expect(result1.value).toEqual(rangeSize); expect(result2.value).toEqual(tailSize);
expect(result2.value).toEqual(tailSize);
done();
})
.catch(done.fail);
}); });
}); });

View File

@ -20,7 +20,7 @@ describe("network", function () {
const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href; const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
const pdf1Length = 1016315; const pdf1Length = 1016315;
it("read without stream and range", function (done) { it("read without stream and range", async function () {
const stream = new PDFNetworkStream({ const stream = new PDFNetworkStream({
url: pdf1, url: pdf1,
rangeChunkSize: 65536, rangeChunkSize: 65536,
@ -49,22 +49,15 @@ describe("network", function () {
}); });
}; };
const readPromise = Promise.all([read(), promise]); await Promise.all([read(), promise]);
readPromise expect(len).toEqual(pdf1Length);
.then(function (page) { expect(count).toEqual(1);
expect(len).toEqual(pdf1Length); expect(isStreamingSupported).toEqual(false);
expect(count).toEqual(1); expect(isRangeSupported).toEqual(false);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(false);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("read custom ranges", function (done) { it("read custom ranges", async function () {
// We don't test on browsers that don't support range request, so // We don't test on browsers that don't support range request, so
// requiring this test to pass. // requiring this test to pass.
const rangeSize = 32768; const rangeSize = 32768;
@ -111,23 +104,16 @@ describe("network", function () {
}); });
}; };
const readPromises = Promise.all([ await Promise.all([
read(range1Reader, result1), read(range1Reader, result1),
read(range2Reader, result2), read(range2Reader, result2),
promise, promise,
]); ]);
readPromises expect(result1.value).toEqual(rangeSize);
.then(function () { expect(result2.value).toEqual(tailSize);
expect(result1.value).toEqual(rangeSize); expect(isStreamingSupported).toEqual(false);
expect(result2.value).toEqual(tailSize); expect(isRangeSupported).toEqual(true);
expect(isStreamingSupported).toEqual(false); expect(fullReaderCancelled).toEqual(true);
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
}); });

View File

@ -18,7 +18,7 @@ import { AbortException } from "../../src/shared/util.js";
import { isNodeJS } from "../../src/shared/is_node.js"; import { isNodeJS } from "../../src/shared/is_node.js";
import { PDFNodeStream } from "../../src/display/node_stream.js"; import { PDFNodeStream } from "../../src/display/node_stream.js";
// Ensure that these test only runs in Node.js environments. // Ensure that these tests only run in Node.js environments.
if (!isNodeJS) { if (!isNodeJS) {
throw new Error( throw new Error(
'The "node_stream" unit-tests can only be run in Node.js environments.' 'The "node_stream" unit-tests can only be run in Node.js environments.'
@ -84,7 +84,7 @@ describe("node_stream", function () {
server.close(); server.close();
}); });
it("read both http(s) and filesystem pdf files", function (done) { it("read both http(s) and filesystem pdf files", async function () {
const stream1 = new PDFNodeStream({ const stream1 = new PDFNodeStream({
url: `http://127.0.0.1:${port}/tracemonkey.pdf`, url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
rangeChunkSize: 65536, rangeChunkSize: 65536,
@ -135,23 +135,17 @@ describe("node_stream", function () {
}); });
}; };
const readPromise = Promise.all([read1(), read2(), promise1, promise2]); await Promise.all([read1(), read2(), promise1, promise2]);
readPromise
.then(result => { expect(isStreamingSupported1).toEqual(false);
expect(isStreamingSupported1).toEqual(false); expect(isRangeSupported1).toEqual(false);
expect(isRangeSupported1).toEqual(false); expect(isStreamingSupported2).toEqual(false);
expect(isStreamingSupported2).toEqual(false); expect(isRangeSupported2).toEqual(false);
expect(isRangeSupported2).toEqual(false); expect(len1).toEqual(pdfLength);
expect(len1).toEqual(pdfLength); expect(len1).toEqual(len2);
expect(len1).toEqual(len2);
done();
})
.catch(reason => {
done.fail(reason);
});
}); });
it("read custom ranges for both http(s) and filesystem urls", function (done) { it("read custom ranges for both http(s) and filesystem urls", async function () {
const rangeSize = 32768; const rangeSize = 32768;
const stream1 = new PDFNodeStream({ const stream1 = new PDFNodeStream({
url: `http://127.0.0.1:${port}/tracemonkey.pdf`, url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
@ -225,7 +219,7 @@ describe("node_stream", function () {
}); });
}; };
const readPromises = Promise.all([ await Promise.all([
read(range11Reader, result11), read(range11Reader, result11),
read(range12Reader, result12), read(range12Reader, result12),
read(range21Reader, result21), read(range21Reader, result21),
@ -234,22 +228,15 @@ describe("node_stream", function () {
promise2, promise2,
]); ]);
readPromises expect(result11.value).toEqual(rangeSize);
.then(function () { expect(result12.value).toEqual(tailSize);
expect(result11.value).toEqual(rangeSize); expect(result21.value).toEqual(rangeSize);
expect(result12.value).toEqual(tailSize); expect(result22.value).toEqual(tailSize);
expect(result21.value).toEqual(rangeSize); expect(isStreamingSupported1).toEqual(false);
expect(result22.value).toEqual(tailSize); expect(isRangeSupported1).toEqual(true);
expect(isStreamingSupported1).toEqual(false); expect(fullReaderCancelled1).toEqual(true);
expect(isRangeSupported1).toEqual(true); expect(isStreamingSupported2).toEqual(false);
expect(fullReaderCancelled1).toEqual(true); expect(isRangeSupported2).toEqual(true);
expect(isStreamingSupported2).toEqual(false); expect(fullReaderCancelled2).toEqual(true);
expect(isRangeSupported2).toEqual(true);
expect(fullReaderCancelled2).toEqual(true);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
}); });

View File

@ -169,40 +169,28 @@ describe("primitives", function () {
).toEqual(testFontFile); ).toEqual(testFontFile);
}); });
it("should asynchronously fetch unknown keys", function (done) { it("should asynchronously fetch unknown keys", async function () {
const keyPromises = [ const keyPromises = [
dictWithManyKeys.getAsync("Size"), dictWithManyKeys.getAsync("Size"),
dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3"), dictWithSizeKey.getAsync("FontFile", "FontFile2", "FontFile3"),
]; ];
Promise.all(keyPromises) const values = await Promise.all(keyPromises);
.then(function (values) { expect(values[0]).toBeUndefined();
expect(values[0]).toBeUndefined(); expect(values[1]).toBeUndefined();
expect(values[1]).toBeUndefined();
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("should asynchronously fetch correct values for multiple stored keys", function (done) { it("should asynchronously fetch correct values for multiple stored keys", async function () {
const keyPromises = [ const keyPromises = [
dictWithManyKeys.getAsync("FontFile3"), dictWithManyKeys.getAsync("FontFile3"),
dictWithManyKeys.getAsync("FontFile2", "FontFile3"), dictWithManyKeys.getAsync("FontFile2", "FontFile3"),
dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3"), dictWithManyKeys.getAsync("FontFile", "FontFile2", "FontFile3"),
]; ];
Promise.all(keyPromises) const values = await Promise.all(keyPromises);
.then(function (values) { expect(values[0]).toEqual(testFontFile3);
expect(values[0]).toEqual(testFontFile3); expect(values[1]).toEqual(testFontFile2);
expect(values[1]).toEqual(testFontFile2); expect(values[2]).toEqual(testFontFile);
expect(values[2]).toEqual(testFontFile);
done();
})
.catch(function (reason) {
done.fail(reason);
});
}); });
it("should callback for each stored key", function () { it("should callback for each stored key", function () {
@ -218,7 +206,7 @@ describe("primitives", function () {
expect(callbackSpyCalls.count()).toEqual(3); expect(callbackSpyCalls.count()).toEqual(3);
}); });
it("should handle keys pointing to indirect objects, both sync and async", function (done) { it("should handle keys pointing to indirect objects, both sync and async", async function () {
const fontRef = Ref.get(1, 0); const fontRef = Ref.get(1, 0);
const xref = new XRefMock([{ ref: fontRef, data: testFontFile }]); const xref = new XRefMock([{ ref: fontRef, data: testFontFile }]);
const fontDict = new Dict(xref); const fontDict = new Dict(xref);
@ -229,15 +217,12 @@ describe("primitives", function () {
testFontFile testFontFile
); );
fontDict const value = await fontDict.getAsync(
.getAsync("FontFile", "FontFile2", "FontFile3") "FontFile",
.then(function (value) { "FontFile2",
expect(value).toEqual(testFontFile); "FontFile3"
done(); );
}) expect(value).toEqual(testFontFile);
.catch(function (reason) {
done.fail(reason);
});
}); });
it("should handle arrays containing indirect objects", function () { it("should handle arrays containing indirect objects", function () {

View File

@ -178,9 +178,9 @@ describe("ui_utils", function () {
expect(onceCount).toEqual(1); expect(onceCount).toEqual(1);
}); });
it("should not re-dispatch to DOM", function (done) { it("should not re-dispatch to DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const eventBus = new EventBus(); const eventBus = new EventBus();
let count = 0; let count = 0;
@ -189,18 +189,17 @@ describe("ui_utils", function () {
count++; count++;
}); });
function domEventListener() { function domEventListener() {
done.fail("shall not dispatch DOM event."); // Shouldn't get here.
expect(false).toEqual(true);
} }
document.addEventListener("test", domEventListener); document.addEventListener("test", domEventListener);
eventBus.dispatch("test"); eventBus.dispatch("test");
Promise.resolve().then(() => { await Promise.resolve();
expect(count).toEqual(1); expect(count).toEqual(1);
document.removeEventListener("test", domEventListener); document.removeEventListener("test", domEventListener);
done();
});
}); });
}); });
@ -265,13 +264,14 @@ describe("ui_utils", function () {
eventBus = null; eventBus = null;
}); });
it("should reject invalid parameters", function (done) { it("should reject invalid parameters", async function () {
const invalidTarget = waitOnEventOrTimeout({ const invalidTarget = waitOnEventOrTimeout({
target: "window", target: "window",
name: "DOMContentLoaded", name: "DOMContentLoaded",
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
@ -283,7 +283,8 @@ describe("ui_utils", function () {
name: "", name: "",
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
@ -296,22 +297,20 @@ describe("ui_utils", function () {
delay: -1000, delay: -1000,
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
} }
); );
Promise.all([invalidTarget, invalidName, invalidDelay]).then( await Promise.all([invalidTarget, invalidName, invalidDelay]);
done,
done.fail
);
}); });
it("should resolve on event, using the DOM", function (done) { it("should resolve on event, using the DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const button = document.createElement("button"); const button = document.createElement("button");
@ -323,15 +322,13 @@ describe("ui_utils", function () {
// Immediately dispatch the expected event. // Immediately dispatch the expected event.
button.click(); button.click();
buttonClicked.then(function (type) { const type = await buttonClicked;
expect(type).toEqual(WaitOnType.EVENT); expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
}); });
it("should resolve on timeout, using the DOM", function (done) { it("should resolve on timeout, using the DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const button = document.createElement("button"); const button = document.createElement("button");
@ -342,13 +339,11 @@ describe("ui_utils", function () {
}); });
// Do *not* dispatch the event, and wait for the timeout. // Do *not* dispatch the event, and wait for the timeout.
buttonClicked.then(function (type) { const type = await buttonClicked;
expect(type).toEqual(WaitOnType.TIMEOUT); expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
}); });
it("should resolve on event, using the EventBus", function (done) { it("should resolve on event, using the EventBus", async function () {
const pageRendered = waitOnEventOrTimeout({ const pageRendered = waitOnEventOrTimeout({
target: eventBus, target: eventBus,
name: "pagerendered", name: "pagerendered",
@ -357,13 +352,11 @@ describe("ui_utils", function () {
// Immediately dispatch the expected event. // Immediately dispatch the expected event.
eventBus.dispatch("pagerendered"); eventBus.dispatch("pagerendered");
pageRendered.then(function (type) { const type = await pageRendered;
expect(type).toEqual(WaitOnType.EVENT); expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
}); });
it("should resolve on timeout, using the EventBus", function (done) { it("should resolve on timeout, using the EventBus", async function () {
const pageRendered = waitOnEventOrTimeout({ const pageRendered = waitOnEventOrTimeout({
target: eventBus, target: eventBus,
name: "pagerendered", name: "pagerendered",
@ -371,10 +364,8 @@ describe("ui_utils", function () {
}); });
// Do *not* dispatch the event, and wait for the timeout. // Do *not* dispatch the event, and wait for the timeout.
pageRendered.then(function (type) { const type = await pageRendered;
expect(type).toEqual(WaitOnType.TIMEOUT); expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
}); });
}); });

View File

@ -289,33 +289,33 @@ describe("util", function () {
}); });
describe("createPromiseCapability", function () { describe("createPromiseCapability", function () {
it("should resolve with correct data", function (done) { it("should resolve with correct data", async function () {
const promiseCapability = createPromiseCapability(); const promiseCapability = createPromiseCapability();
expect(promiseCapability.settled).toEqual(false); expect(promiseCapability.settled).toEqual(false);
promiseCapability.resolve({ test: "abc" }); promiseCapability.resolve({ test: "abc" });
promiseCapability.promise.then(function (data) { const data = await promiseCapability.promise;
expect(promiseCapability.settled).toEqual(true); expect(promiseCapability.settled).toEqual(true);
expect(data).toEqual({ test: "abc" });
expect(data).toEqual({ test: "abc" });
done();
}, done.fail);
}); });
it("should reject with correct reason", function (done) { it("should reject with correct reason", async function () {
const promiseCapability = createPromiseCapability(); const promiseCapability = createPromiseCapability();
expect(promiseCapability.settled).toEqual(false); expect(promiseCapability.settled).toEqual(false);
promiseCapability.reject(new Error("reason")); promiseCapability.reject(new Error("reason"));
promiseCapability.promise.then(done.fail, function (reason) { try {
expect(promiseCapability.settled).toEqual(true); await promiseCapability.promise;
// Shouldn't get here.
expect(false).toEqual(true);
} catch (reason) {
expect(promiseCapability.settled).toEqual(true);
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
expect(reason.message).toEqual("reason"); expect(reason.message).toEqual("reason");
done(); }
});
}); });
}); });