Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert done callbacks to async/await in the smaller unit test files #13222

Merged
merged 7 commits into from
Apr 14, 2021
64 changes: 26 additions & 38 deletions test/unit/custom_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,41 @@ describe("custom canvas rendering", function () {
let loadingTask;
let page;

beforeAll(function (done) {
beforeAll(async function () {
CanvasFactory = new DefaultCanvasFactory();

loadingTask = getDocument(transparentGetDocumentParams);
loadingTask.promise
.then(function (doc) {
return doc.getPage(1);
})
.then(function (data) {
page = data;
done();
})
.catch(done.fail);
const doc = await loadingTask.promise;
const data = await doc.getPage(1);
page = data;
});

afterAll(function (done) {
afterAll(async function () {
CanvasFactory = 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 canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);

const renderTask = page.render({
canvasContext: canvasAndCtx.context,
viewport,
});
renderTask.promise
.then(function () {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255,
g: 255,
b: 255,
a: 255,
});
CanvasFactory.destroy(canvasAndCtx);
done();
})
.catch(done.fail);
await renderTask.promise;

expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255,
g: 255,
b: 255,
a: 255,
});
CanvasFactory.destroy(canvasAndCtx);
});

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 canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);

Expand All @@ -87,18 +78,15 @@ describe("custom canvas rendering", function () {
viewport,
background: "rgba(255,0,0,1.0)",
});
renderTask.promise
.then(function () {
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255,
g: 0,
b: 0,
a: 255,
});
CanvasFactory.destroy(canvasAndCtx);
done();
})
.catch(done.fail);
await renderTask.promise;

expect(getTopLeftPixel(canvasAndCtx.context)).toEqual({
r: 255,
g: 0,
b: 0,
a: 255,
});
CanvasFactory.destroy(canvasAndCtx);
});
});

Expand Down
36 changes: 14 additions & 22 deletions test/unit/fetch_stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("fetch_stream", function () {
const pdfUrl = new URL("../pdfs/tracemonkey.pdf", window.location).href;
const pdfLength = 1016315;

it("read with streaming", function (done) {
it("read with streaming", async function () {
const stream = new PDFFetchStream({
url: pdfUrl,
disableStream: false,
Expand All @@ -47,18 +47,14 @@ describe("fetch_stream", function () {
});
};

const readPromise = Promise.all([read(), promise]);
readPromise
.then(function () {
expect(len).toEqual(pdfLength);
expect(isStreamingSupported).toEqual(true);
expect(isRangeSupported).toEqual(false);
done();
})
.catch(done.fail);
await Promise.all([read(), promise]);

expect(len).toEqual(pdfLength);
expect(isStreamingSupported).toEqual(true);
expect(isRangeSupported).toEqual(false);
});

it("read ranges with streaming", function (done) {
it("read ranges with streaming", async function () {
const rangeSize = 32768;
const stream = new PDFFetchStream({
url: pdfUrl,
Expand Down Expand Up @@ -98,20 +94,16 @@ describe("fetch_stream", function () {
});
};

const readPromise = Promise.all([
await Promise.all([
read(rangeReader1, result1),
read(rangeReader2, result2),
promise,
]);
readPromise
.then(function () {
expect(isStreamingSupported).toEqual(true);
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
expect(result1.value).toEqual(rangeSize);
expect(result2.value).toEqual(tailSize);
done();
})
.catch(done.fail);

expect(isStreamingSupported).toEqual(true);
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
expect(result1.value).toEqual(rangeSize);
expect(result2.value).toEqual(tailSize);
});
});
42 changes: 14 additions & 28 deletions test/unit/network_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("network", function () {
const pdf1 = new URL("../pdfs/tracemonkey.pdf", window.location).href;
const pdf1Length = 1016315;

it("read without stream and range", function (done) {
it("read without stream and range", async function () {
const stream = new PDFNetworkStream({
url: pdf1,
rangeChunkSize: 65536,
Expand Down Expand Up @@ -49,22 +49,15 @@ describe("network", function () {
});
};

const readPromise = Promise.all([read(), promise]);

readPromise
.then(function (page) {
expect(len).toEqual(pdf1Length);
expect(count).toEqual(1);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(false);
done();
})
.catch(function (reason) {
done.fail(reason);
});
await Promise.all([read(), promise]);

expect(len).toEqual(pdf1Length);
expect(count).toEqual(1);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(false);
});

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
// requiring this test to pass.
const rangeSize = 32768;
Expand Down Expand Up @@ -111,23 +104,16 @@ describe("network", function () {
});
};

const readPromises = Promise.all([
await Promise.all([
read(range1Reader, result1),
read(range2Reader, result2),
promise,
]);

readPromises
.then(function () {
expect(result1.value).toEqual(rangeSize);
expect(result2.value).toEqual(tailSize);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
done();
})
.catch(function (reason) {
done.fail(reason);
});
expect(result1.value).toEqual(rangeSize);
expect(result2.value).toEqual(tailSize);
expect(isStreamingSupported).toEqual(false);
expect(isRangeSupported).toEqual(true);
expect(fullReaderCancelled).toEqual(true);
});
});
57 changes: 22 additions & 35 deletions test/unit/node_stream_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AbortException } from "../../src/shared/util.js";
import { isNodeJS } from "../../src/shared/is_node.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) {
throw new Error(
'The "node_stream" unit-tests can only be run in Node.js environments.'
Expand Down Expand Up @@ -84,7 +84,7 @@ describe("node_stream", function () {
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({
url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
rangeChunkSize: 65536,
Expand Down Expand Up @@ -135,23 +135,17 @@ describe("node_stream", function () {
});
};

const readPromise = Promise.all([read1(), read2(), promise1, promise2]);
readPromise
.then(result => {
expect(isStreamingSupported1).toEqual(false);
expect(isRangeSupported1).toEqual(false);
expect(isStreamingSupported2).toEqual(false);
expect(isRangeSupported2).toEqual(false);
expect(len1).toEqual(pdfLength);
expect(len1).toEqual(len2);
done();
})
.catch(reason => {
done.fail(reason);
});
await Promise.all([read1(), read2(), promise1, promise2]);

expect(isStreamingSupported1).toEqual(false);
expect(isRangeSupported1).toEqual(false);
expect(isStreamingSupported2).toEqual(false);
expect(isRangeSupported2).toEqual(false);
expect(len1).toEqual(pdfLength);
expect(len1).toEqual(len2);
});

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 stream1 = new PDFNodeStream({
url: `http://127.0.0.1:${port}/tracemonkey.pdf`,
Expand Down Expand Up @@ -225,7 +219,7 @@ describe("node_stream", function () {
});
};

const readPromises = Promise.all([
await Promise.all([
read(range11Reader, result11),
read(range12Reader, result12),
read(range21Reader, result21),
Expand All @@ -234,22 +228,15 @@ describe("node_stream", function () {
promise2,
]);

readPromises
.then(function () {
expect(result11.value).toEqual(rangeSize);
expect(result12.value).toEqual(tailSize);
expect(result21.value).toEqual(rangeSize);
expect(result22.value).toEqual(tailSize);
expect(isStreamingSupported1).toEqual(false);
expect(isRangeSupported1).toEqual(true);
expect(fullReaderCancelled1).toEqual(true);
expect(isStreamingSupported2).toEqual(false);
expect(isRangeSupported2).toEqual(true);
expect(fullReaderCancelled2).toEqual(true);
done();
})
.catch(function (reason) {
done.fail(reason);
});
expect(result11.value).toEqual(rangeSize);
expect(result12.value).toEqual(tailSize);
expect(result21.value).toEqual(rangeSize);
expect(result22.value).toEqual(tailSize);
expect(isStreamingSupported1).toEqual(false);
expect(isRangeSupported1).toEqual(true);
expect(fullReaderCancelled1).toEqual(true);
expect(isStreamingSupported2).toEqual(false);
expect(isRangeSupported2).toEqual(true);
expect(fullReaderCancelled2).toEqual(true);
});
});