From 42c71cd99f8de4b81313652b0ef19b3fdc902914 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 28 Jan 2018 14:34:45 +0100 Subject: [PATCH] Utilize `PDFNodeStream` to run more API unit-tests on Node.js/Travis --- test/unit/api_spec.js | 9 --------- test/unit/clitests.json | 5 +++++ test/unit/clitests_helper.js | 29 +++++++++++++++++++++++++++++ test/unit/jasmine-boot.js | 8 +++++++- test/unit/test_utils.js | 4 +--- 5 files changed, 42 insertions(+), 13 deletions(-) create mode 100644 test/unit/clitests_helper.js diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 48dd07c91..04e543bdf 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -61,9 +61,6 @@ describe('api', function() { describe('PDFJS', function() { describe('getDocument', function() { it('creates pdf doc from URL', function(done) { - if (isNodeJS()) { - pending('XMLHttpRequest is not supported in Node.js.'); - } var loadingTask = getDocument(basicApiGetDocumentParams); var isProgressReportedResolved = false; @@ -93,9 +90,6 @@ describe('api', function() { }); it('creates pdf doc from URL and aborts before worker initialized', function(done) { - if (isNodeJS()) { - pending('XMLHttpRequest is not supported in Node.js.'); - } var loadingTask = getDocument(basicApiGetDocumentParams); let destroyed = loadingTask.destroy(); @@ -108,9 +102,6 @@ describe('api', function() { }); it('creates pdf doc from URL and aborts loading after worker initialized', function(done) { - if (isNodeJS()) { - pending('XMLHttpRequest is not supported in Node.js.'); - } var loadingTask = getDocument(basicApiGetDocumentParams); // This can be somewhat random -- we cannot guarantee perfect // 'Terminate' message to the worker before/after setting up pdfManager. diff --git a/test/unit/clitests.json b/test/unit/clitests.json index 49ba72a94..01510af67 100644 --- a/test/unit/clitests.json +++ b/test/unit/clitests.json @@ -1,5 +1,10 @@ { "spec_dir": "build/lib/test/unit", + + "helpers": [ + "clitests_helper.js" + ], + "spec_files": [ "annotation_spec.js", "api_spec.js", diff --git a/test/unit/clitests_helper.js b/test/unit/clitests_helper.js new file mode 100644 index 000000000..15f6ffa2d --- /dev/null +++ b/test/unit/clitests_helper.js @@ -0,0 +1,29 @@ +/* Copyright 2018 Mozilla Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import isNodeJS from '../../src/shared/is_node'; +import { PDFNodeStream } from '../../src/display/node_stream'; +import { setPDFNetworkStreamFactory } from '../../src/display/api'; + +// Ensure that this script only runs in Node.js environments. +if (!isNodeJS()) { + throw new Error('The `gulp unittestcli` command can only be used in ' + + 'Node.js environments.'); +} + +// Set the network stream factory for the unit-tests. +setPDFNetworkStreamFactory(function(params) { + return new PDFNodeStream(params); +}); diff --git a/test/unit/jasmine-boot.js b/test/unit/jasmine-boot.js index fbf85b1cd..4f2f5169a 100644 --- a/test/unit/jasmine-boot.js +++ b/test/unit/jasmine-boot.js @@ -46,6 +46,7 @@ function initializePDFJS(callback) { 'pdfjs/display/api', 'pdfjs/display/network', 'pdfjs/display/fetch_stream', + 'pdfjs/shared/is_node', 'pdfjs-test/unit/annotation_spec', 'pdfjs-test/unit/api_spec', 'pdfjs-test/unit/bidi_spec', @@ -81,8 +82,13 @@ function initializePDFJS(callback) { var displayApi = modules[1]; var PDFNetworkStream = modules[2].PDFNetworkStream; var PDFFetchStream = modules[3].PDFFetchStream; + const isNodeJS = modules[4]; - // Set network stream class for unit tests. + if (isNodeJS()) { + throw new Error('The `gulp unittest` command cannot be used in ' + + 'Node.js environments.'); + } + // Set the network stream factory for unit-tests. if (typeof Response !== 'undefined' && 'body' in Response.prototype && typeof ReadableStream !== 'undefined') { displayApi.setPDFNetworkStreamFactory(function(params) { diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index db6253e02..e57247c3c 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -33,9 +33,7 @@ const TEST_PDFS_PATH = { function buildGetDocumentParams(filename, options) { let params = Object.create(null); if (isNodeJS()) { - params.data = NodeFileReaderFactory.fetch({ - path: TEST_PDFS_PATH.node + filename, - }); + params.url = TEST_PDFS_PATH.node + filename; } else { params.url = new URL(TEST_PDFS_PATH.dom + filename, window.location).href; }