Merge pull request #12837 from Snuffleupagus/unit-test-factories
Remove unncessary `CanvasFactory`/`CMapReaderFactory`/`FileReaderFactory` duplication in unit-tests
This commit is contained in:
commit
5fed65203e
@ -3060,4 +3060,6 @@ export {
|
|||||||
setPDFNetworkStreamFactory,
|
setPDFNetworkStreamFactory,
|
||||||
version,
|
version,
|
||||||
build,
|
build,
|
||||||
|
DefaultCanvasFactory,
|
||||||
|
DefaultCMapReaderFactory,
|
||||||
};
|
};
|
||||||
|
@ -29,21 +29,14 @@ import {
|
|||||||
stringToBytes,
|
stringToBytes,
|
||||||
stringToUTF8String,
|
stringToUTF8String,
|
||||||
} from "../../src/shared/util.js";
|
} from "../../src/shared/util.js";
|
||||||
import { createIdFactory, XRefMock } from "./test_utils.js";
|
import { CMAP_PARAMS, createIdFactory, XRefMock } from "./test_utils.js";
|
||||||
import { Dict, Name, Ref, RefSetCache } from "../../src/core/primitives.js";
|
import { Dict, Name, Ref, RefSetCache } from "../../src/core/primitives.js";
|
||||||
import { Lexer, Parser } from "../../src/core/parser.js";
|
import { Lexer, Parser } from "../../src/core/parser.js";
|
||||||
import { DOMCMapReaderFactory } from "../../src/display/display_utils.js";
|
import { DefaultCMapReaderFactory } from "../../src/display/api.js";
|
||||||
import { isNodeJS } from "../../src/shared/is_node.js";
|
|
||||||
import { NodeCMapReaderFactory } from "../../src/display/node_utils.js";
|
|
||||||
import { PartialEvaluator } from "../../src/core/evaluator.js";
|
import { PartialEvaluator } from "../../src/core/evaluator.js";
|
||||||
import { StringStream } from "../../src/core/stream.js";
|
import { StringStream } from "../../src/core/stream.js";
|
||||||
import { WorkerTask } from "../../src/core/worker.js";
|
import { WorkerTask } from "../../src/core/worker.js";
|
||||||
|
|
||||||
const cMapUrl = {
|
|
||||||
dom: "../../external/bcmaps/",
|
|
||||||
node: "./external/bcmaps/",
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("annotation", function () {
|
describe("annotation", function () {
|
||||||
class PDFManagerMock {
|
class PDFManagerMock {
|
||||||
constructor(params) {
|
constructor(params) {
|
||||||
@ -86,32 +79,24 @@ describe("annotation", function () {
|
|||||||
|
|
||||||
let pdfManagerMock, idFactoryMock, partialEvaluator;
|
let pdfManagerMock, idFactoryMock, partialEvaluator;
|
||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(async function (done) {
|
||||||
pdfManagerMock = new PDFManagerMock({
|
pdfManagerMock = new PDFManagerMock({
|
||||||
docBaseUrl: null,
|
docBaseUrl: null,
|
||||||
});
|
});
|
||||||
|
|
||||||
let CMapReaderFactory;
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
if (isNodeJS) {
|
baseUrl: CMAP_PARAMS.cMapUrl,
|
||||||
CMapReaderFactory = new NodeCMapReaderFactory({
|
isCompressed: CMAP_PARAMS.cMapPacked,
|
||||||
baseUrl: cMapUrl.node,
|
});
|
||||||
isCompressed: true,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
CMapReaderFactory = new DOMCMapReaderFactory({
|
|
||||||
baseUrl: cMapUrl.dom,
|
|
||||||
isCompressed: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const builtInCMapCache = new Map();
|
const builtInCMapCache = new Map();
|
||||||
builtInCMapCache.set(
|
builtInCMapCache.set(
|
||||||
"UniJIS-UTF16-H",
|
"UniJIS-UTF16-H",
|
||||||
CMapReaderFactory.fetch({ name: "UniJIS-UTF16-H" })
|
await CMapReaderFactory.fetch({ name: "UniJIS-UTF16-H" })
|
||||||
);
|
);
|
||||||
builtInCMapCache.set(
|
builtInCMapCache.set(
|
||||||
"Adobe-Japan1-UCS2",
|
"Adobe-Japan1-UCS2",
|
||||||
CMapReaderFactory.fetch({ name: "Adobe-Japan1-UCS2" })
|
await CMapReaderFactory.fetch({ name: "Adobe-Japan1-UCS2" })
|
||||||
);
|
);
|
||||||
|
|
||||||
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
||||||
|
@ -15,8 +15,7 @@
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
buildGetDocumentParams,
|
buildGetDocumentParams,
|
||||||
DOMFileReaderFactory,
|
DefaultFileReaderFactory,
|
||||||
NodeFileReaderFactory,
|
|
||||||
TEST_PDFS_PATH,
|
TEST_PDFS_PATH,
|
||||||
} from "./test_utils.js";
|
} from "./test_utils.js";
|
||||||
import {
|
import {
|
||||||
@ -32,23 +31,22 @@ import {
|
|||||||
StreamType,
|
StreamType,
|
||||||
} from "../../src/shared/util.js";
|
} from "../../src/shared/util.js";
|
||||||
import {
|
import {
|
||||||
DOMCanvasFactory,
|
DefaultCanvasFactory,
|
||||||
RenderingCancelledException,
|
|
||||||
StatTimer,
|
|
||||||
} from "../../src/display/display_utils.js";
|
|
||||||
import {
|
|
||||||
getDocument,
|
getDocument,
|
||||||
PDFDataRangeTransport,
|
PDFDataRangeTransport,
|
||||||
PDFDocumentProxy,
|
PDFDocumentProxy,
|
||||||
PDFPageProxy,
|
PDFPageProxy,
|
||||||
PDFWorker,
|
PDFWorker,
|
||||||
} from "../../src/display/api.js";
|
} from "../../src/display/api.js";
|
||||||
|
import {
|
||||||
|
RenderingCancelledException,
|
||||||
|
StatTimer,
|
||||||
|
} from "../../src/display/display_utils.js";
|
||||||
import { AutoPrintRegExp } from "../../web/ui_utils.js";
|
import { AutoPrintRegExp } from "../../web/ui_utils.js";
|
||||||
import { GlobalImageCache } from "../../src/core/image_utils.js";
|
import { GlobalImageCache } from "../../src/core/image_utils.js";
|
||||||
import { GlobalWorkerOptions } from "../../src/display/worker_options.js";
|
import { GlobalWorkerOptions } from "../../src/display/worker_options.js";
|
||||||
import { isNodeJS } from "../../src/shared/is_node.js";
|
import { isNodeJS } from "../../src/shared/is_node.js";
|
||||||
import { Metadata } from "../../src/display/metadata.js";
|
import { Metadata } from "../../src/display/metadata.js";
|
||||||
import { NodeCanvasFactory } from "../../src/display/node_utils.js";
|
|
||||||
|
|
||||||
describe("api", function () {
|
describe("api", function () {
|
||||||
const basicApiFileName = "basicapi.pdf";
|
const basicApiFileName = "basicapi.pdf";
|
||||||
@ -58,11 +56,7 @@ describe("api", function () {
|
|||||||
let CanvasFactory;
|
let CanvasFactory;
|
||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(function (done) {
|
||||||
if (isNodeJS) {
|
CanvasFactory = new DefaultCanvasFactory();
|
||||||
CanvasFactory = new NodeCanvasFactory();
|
|
||||||
} else {
|
|
||||||
CanvasFactory = new DOMCanvasFactory();
|
|
||||||
}
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -132,16 +126,9 @@ describe("api", function () {
|
|||||||
.catch(done.fail);
|
.catch(done.fail);
|
||||||
});
|
});
|
||||||
it("creates pdf doc from typed array", function (done) {
|
it("creates pdf doc from typed array", function (done) {
|
||||||
let typedArrayPdfPromise;
|
const typedArrayPdfPromise = DefaultFileReaderFactory.fetch({
|
||||||
if (isNodeJS) {
|
path: TEST_PDFS_PATH + basicApiFileName,
|
||||||
typedArrayPdfPromise = NodeFileReaderFactory.fetch({
|
});
|
||||||
path: TEST_PDFS_PATH.node + basicApiFileName,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
typedArrayPdfPromise = DOMFileReaderFactory.fetch({
|
|
||||||
path: TEST_PDFS_PATH.dom + basicApiFileName,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
typedArrayPdfPromise
|
typedArrayPdfPromise
|
||||||
.then(typedArrayPdf => {
|
.then(typedArrayPdf => {
|
||||||
@ -2197,15 +2184,9 @@ describe("api", function () {
|
|||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(function (done) {
|
||||||
const fileName = "tracemonkey.pdf";
|
const fileName = "tracemonkey.pdf";
|
||||||
if (isNodeJS) {
|
dataPromise = DefaultFileReaderFactory.fetch({
|
||||||
dataPromise = NodeFileReaderFactory.fetch({
|
path: TEST_PDFS_PATH + fileName,
|
||||||
path: TEST_PDFS_PATH.node + fileName,
|
});
|
||||||
});
|
|
||||||
} else {
|
|
||||||
dataPromise = DOMFileReaderFactory.fetch({
|
|
||||||
path: TEST_PDFS_PATH.dom + fileName,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -14,35 +14,20 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
|
import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
|
||||||
import { DOMCMapReaderFactory } from "../../src/display/display_utils.js";
|
import { CMAP_PARAMS } from "./test_utils.js";
|
||||||
import { isNodeJS } from "../../src/shared/is_node.js";
|
import { DefaultCMapReaderFactory } from "../../src/display/api.js";
|
||||||
import { Name } from "../../src/core/primitives.js";
|
import { Name } from "../../src/core/primitives.js";
|
||||||
import { NodeCMapReaderFactory } from "../../src/display/node_utils.js";
|
|
||||||
import { StringStream } from "../../src/core/stream.js";
|
import { StringStream } from "../../src/core/stream.js";
|
||||||
|
|
||||||
const cMapUrl = {
|
|
||||||
dom: "../../external/bcmaps/",
|
|
||||||
node: "./external/bcmaps/",
|
|
||||||
};
|
|
||||||
const cMapPacked = true;
|
|
||||||
|
|
||||||
describe("cmap", function () {
|
describe("cmap", function () {
|
||||||
let fetchBuiltInCMap;
|
let fetchBuiltInCMap;
|
||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(function (done) {
|
||||||
// Allow CMap testing in Node.js, e.g. for Travis.
|
// Allow CMap testing in Node.js, e.g. for Travis.
|
||||||
let CMapReaderFactory;
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
if (isNodeJS) {
|
baseUrl: CMAP_PARAMS.cMapUrl,
|
||||||
CMapReaderFactory = new NodeCMapReaderFactory({
|
isCompressed: CMAP_PARAMS.cMapPacked,
|
||||||
baseUrl: cMapUrl.node,
|
});
|
||||||
isCompressed: cMapPacked,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
CMapReaderFactory = new DOMCMapReaderFactory({
|
|
||||||
baseUrl: cMapUrl.dom,
|
|
||||||
isCompressed: cMapPacked,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fetchBuiltInCMap = function (name) {
|
fetchBuiltInCMap = function (name) {
|
||||||
return CMapReaderFactory.fetch({
|
return CMapReaderFactory.fetch({
|
||||||
@ -298,9 +283,8 @@ describe("cmap", function () {
|
|||||||
|
|
||||||
it("attempts to load a built-in CMap without the necessary API parameters", function (done) {
|
it("attempts to load a built-in CMap without the necessary API parameters", function (done) {
|
||||||
function tmpFetchBuiltInCMap(name) {
|
function tmpFetchBuiltInCMap(name) {
|
||||||
const CMapReaderFactory = isNodeJS
|
const CMapReaderFactory = new DefaultCMapReaderFactory({});
|
||||||
? new NodeCMapReaderFactory({})
|
|
||||||
: new DOMCMapReaderFactory({});
|
|
||||||
return CMapReaderFactory.fetch({
|
return CMapReaderFactory.fetch({
|
||||||
name,
|
name,
|
||||||
});
|
});
|
||||||
@ -328,18 +312,11 @@ describe("cmap", function () {
|
|||||||
|
|
||||||
it("attempts to load a built-in CMap with inconsistent API parameters", function (done) {
|
it("attempts to load a built-in CMap with inconsistent API parameters", function (done) {
|
||||||
function tmpFetchBuiltInCMap(name) {
|
function tmpFetchBuiltInCMap(name) {
|
||||||
let CMapReaderFactory;
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
if (isNodeJS) {
|
baseUrl: CMAP_PARAMS.cMapUrl,
|
||||||
CMapReaderFactory = new NodeCMapReaderFactory({
|
isCompressed: false,
|
||||||
baseUrl: cMapUrl.node,
|
});
|
||||||
isCompressed: false,
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
CMapReaderFactory = new DOMCMapReaderFactory({
|
|
||||||
baseUrl: cMapUrl.dom,
|
|
||||||
isCompressed: false,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return CMapReaderFactory.fetch({
|
return CMapReaderFactory.fetch({
|
||||||
name,
|
name,
|
||||||
});
|
});
|
||||||
|
@ -13,11 +13,8 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { DefaultCanvasFactory, getDocument } from "../../src/display/api.js";
|
||||||
import { buildGetDocumentParams } from "./test_utils.js";
|
import { buildGetDocumentParams } from "./test_utils.js";
|
||||||
import { DOMCanvasFactory } from "../../src/display/display_utils.js";
|
|
||||||
import { getDocument } from "../../src/display/api.js";
|
|
||||||
import { isNodeJS } from "../../src/shared/is_node.js";
|
|
||||||
import { NodeCanvasFactory } from "../../src/display/node_utils.js";
|
|
||||||
|
|
||||||
function getTopLeftPixel(canvasContext) {
|
function getTopLeftPixel(canvasContext) {
|
||||||
const imgData = canvasContext.getImageData(0, 0, 1, 1);
|
const imgData = canvasContext.getImageData(0, 0, 1, 1);
|
||||||
@ -39,11 +36,8 @@ describe("custom canvas rendering", function () {
|
|||||||
let page;
|
let page;
|
||||||
|
|
||||||
beforeAll(function (done) {
|
beforeAll(function (done) {
|
||||||
if (isNodeJS) {
|
CanvasFactory = new DefaultCanvasFactory();
|
||||||
CanvasFactory = new NodeCanvasFactory();
|
|
||||||
} else {
|
|
||||||
CanvasFactory = new DOMCanvasFactory();
|
|
||||||
}
|
|
||||||
loadingTask = getDocument(transparentGetDocumentParams);
|
loadingTask = getDocument(transparentGetDocumentParams);
|
||||||
loadingTask.promise
|
loadingTask.promise
|
||||||
.then(function (doc) {
|
.then(function (doc) {
|
||||||
@ -156,10 +150,8 @@ describe("custom ownerDocument", function () {
|
|||||||
getElementsByTagName: () => [{ appendChild: () => {} }],
|
getElementsByTagName: () => [{ appendChild: () => {} }],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const CanvasFactory = new DefaultCanvasFactory({ ownerDocument });
|
||||||
|
|
||||||
const CanvasFactory = isNodeJS
|
|
||||||
? new NodeCanvasFactory()
|
|
||||||
: new DOMCanvasFactory({ ownerDocument });
|
|
||||||
return {
|
return {
|
||||||
elements,
|
elements,
|
||||||
ownerDocument,
|
ownerDocument,
|
||||||
|
@ -19,6 +19,13 @@ import { assert } from "../../src/shared/util.js";
|
|||||||
import { isNodeJS } from "../../src/shared/is_node.js";
|
import { isNodeJS } from "../../src/shared/is_node.js";
|
||||||
import { StringStream } from "../../src/core/stream.js";
|
import { StringStream } from "../../src/core/stream.js";
|
||||||
|
|
||||||
|
const TEST_PDFS_PATH = isNodeJS ? "./test/pdfs/" : "../pdfs/";
|
||||||
|
|
||||||
|
const CMAP_PARAMS = {
|
||||||
|
cMapUrl: isNodeJS ? "./external/bcmaps/" : "../../external/bcmaps/",
|
||||||
|
cMapPacked: true,
|
||||||
|
};
|
||||||
|
|
||||||
class DOMFileReaderFactory {
|
class DOMFileReaderFactory {
|
||||||
static async fetch(params) {
|
static async fetch(params) {
|
||||||
const response = await fetch(params.path);
|
const response = await fetch(params.path);
|
||||||
@ -45,18 +52,16 @@ class NodeFileReaderFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const TEST_PDFS_PATH = {
|
const DefaultFileReaderFactory = isNodeJS
|
||||||
dom: "../pdfs/",
|
? NodeFileReaderFactory
|
||||||
node: "./test/pdfs/",
|
: DOMFileReaderFactory;
|
||||||
};
|
|
||||||
|
|
||||||
function buildGetDocumentParams(filename, options) {
|
function buildGetDocumentParams(filename, options) {
|
||||||
const params = Object.create(null);
|
const params = Object.create(null);
|
||||||
if (isNodeJS) {
|
params.url = isNodeJS
|
||||||
params.url = TEST_PDFS_PATH.node + filename;
|
? TEST_PDFS_PATH + filename
|
||||||
} else {
|
: new URL(TEST_PDFS_PATH + filename, window.location).href;
|
||||||
params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href;
|
|
||||||
}
|
|
||||||
for (const option in options) {
|
for (const option in options) {
|
||||||
params[option] = options[option];
|
params[option] = options[option];
|
||||||
}
|
}
|
||||||
@ -136,11 +141,11 @@ function isEmptyObj(obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
DOMFileReaderFactory,
|
DefaultFileReaderFactory,
|
||||||
NodeFileReaderFactory,
|
|
||||||
XRefMock,
|
XRefMock,
|
||||||
buildGetDocumentParams,
|
buildGetDocumentParams,
|
||||||
TEST_PDFS_PATH,
|
TEST_PDFS_PATH,
|
||||||
|
CMAP_PARAMS,
|
||||||
createIdFactory,
|
createIdFactory,
|
||||||
isEmptyObj,
|
isEmptyObj,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user