1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | 'use strict';
|
23 |
|
24 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
|
25 |
|
26 | var _test_utils = require('./test_utils');
|
27 |
|
28 | var _util = require('../../shared/util');
|
29 |
|
30 | var _dom_utils = require('../../display/dom_utils');
|
31 |
|
32 | var _api = require('../../display/api');
|
33 |
|
34 | var _worker_options = require('../../display/worker_options');
|
35 |
|
36 | var _is_node = require('../../shared/is_node');
|
37 |
|
38 | var _is_node2 = _interopRequireDefault(_is_node);
|
39 |
|
40 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41 |
|
42 | describe('api', function () {
|
43 | var basicApiFileName = 'basicapi.pdf';
|
44 | var basicApiFileLength = 105779;
|
45 | var basicApiGetDocumentParams = (0, _test_utils.buildGetDocumentParams)(basicApiFileName);
|
46 | var CanvasFactory = void 0;
|
47 | beforeAll(function (done) {
|
48 | if ((0, _is_node2.default)()) {} else {
|
49 | CanvasFactory = new _dom_utils.DOMCanvasFactory();
|
50 | }
|
51 | done();
|
52 | });
|
53 | afterAll(function (done) {
|
54 | CanvasFactory = null;
|
55 | done();
|
56 | });
|
57 | function waitSome(callback) {
|
58 | var WAIT_TIMEOUT = 10;
|
59 | setTimeout(function () {
|
60 | callback();
|
61 | }, WAIT_TIMEOUT);
|
62 | }
|
63 | describe('getDocument', function () {
|
64 | it('creates pdf doc from URL', function (done) {
|
65 | var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
66 | var isProgressReportedResolved = false;
|
67 | var progressReportedCapability = (0, _util.createPromiseCapability)();
|
68 | loadingTask.onProgress = function (progressData) {
|
69 | if (!isProgressReportedResolved) {
|
70 | isProgressReportedResolved = true;
|
71 | progressReportedCapability.resolve(progressData);
|
72 | }
|
73 | };
|
74 | var promises = [progressReportedCapability.promise, loadingTask.promise];
|
75 | Promise.all(promises).then(function (data) {
|
76 | expect(data[0].loaded / data[0].total > 0).toEqual(true);
|
77 | expect(data[1] instanceof _api.PDFDocumentProxy).toEqual(true);
|
78 | expect(loadingTask).toEqual(data[1].loadingTask);
|
79 | loadingTask.destroy().then(done);
|
80 | }).catch(function (reason) {
|
81 | done.fail(reason);
|
82 | });
|
83 | });
|
84 | it('creates pdf doc from URL and aborts before worker initialized', function (done) {
|
85 | var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
86 | var destroyed = loadingTask.destroy();
|
87 | loadingTask.promise.then(function (reason) {
|
88 | done.fail('shall fail loading');
|
89 | }).catch(function (reason) {
|
90 | expect(true).toEqual(true);
|
91 | destroyed.then(done);
|
92 | });
|
93 | });
|
94 | it('creates pdf doc from URL and aborts loading after worker initialized', function (done) {
|
95 | var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
96 | var destroyed = loadingTask._worker.promise.then(function () {
|
97 | return loadingTask.destroy();
|
98 | });
|
99 | destroyed.then(function (data) {
|
100 | expect(true).toEqual(true);
|
101 | done();
|
102 | }).catch(function (reason) {
|
103 | done.fail(reason);
|
104 | });
|
105 | });
|
106 | it('creates pdf doc from typed array', function (done) {
|
107 | var typedArrayPdf;
|
108 | if ((0, _is_node2.default)()) {
|
109 | typedArrayPdf = _test_utils.NodeFileReaderFactory.fetch({ path: _test_utils.TEST_PDFS_PATH.node + basicApiFileName });
|
110 | } else {
|
111 | var nonBinaryRequest = false;
|
112 | var request = new XMLHttpRequest();
|
113 | request.open('GET', _test_utils.TEST_PDFS_PATH.dom + basicApiFileName, false);
|
114 | try {
|
115 | request.responseType = 'arraybuffer';
|
116 | nonBinaryRequest = request.responseType !== 'arraybuffer';
|
117 | } catch (e) {
|
118 | nonBinaryRequest = true;
|
119 | }
|
120 | if (nonBinaryRequest && request.overrideMimeType) {
|
121 | request.overrideMimeType('text/plain; charset=x-user-defined');
|
122 | }
|
123 | request.send(null);
|
124 | if (nonBinaryRequest) {
|
125 | typedArrayPdf = (0, _util.stringToBytes)(request.responseText);
|
126 | } else {
|
127 | typedArrayPdf = new Uint8Array(request.response);
|
128 | }
|
129 | }
|
130 | expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
131 | var loadingTask = (0, _api.getDocument)(typedArrayPdf);
|
132 | loadingTask.promise.then(function (data) {
|
133 | expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
|
134 | loadingTask.destroy().then(done);
|
135 | }).catch(function (reason) {
|
136 | done.fail(reason);
|
137 | });
|
138 | });
|
139 | it('creates pdf doc from invalid PDF file', function (done) {
|
140 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1020226.pdf'));
|
141 | loadingTask.promise.then(function () {
|
142 | done.fail('shall fail loading');
|
143 | }).catch(function (error) {
|
144 | expect(error instanceof _util.InvalidPDFException).toEqual(true);
|
145 | loadingTask.destroy().then(done);
|
146 | });
|
147 | });
|
148 | it('creates pdf doc from non-existent URL', function (done) {
|
149 | if ((0, _is_node2.default)()) {
|
150 | pending('XMLHttpRequest is not supported in Node.js.');
|
151 | }
|
152 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('non-existent.pdf'));
|
153 | loadingTask.promise.then(function (error) {
|
154 | done.fail('shall fail loading');
|
155 | }).catch(function (error) {
|
156 | expect(error instanceof _util.MissingPDFException).toEqual(true);
|
157 | loadingTask.destroy().then(done);
|
158 | });
|
159 | });
|
160 | it('creates pdf doc from PDF file protected with user and owner password', function (done) {
|
161 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('pr6531_1.pdf'));
|
162 | var isPasswordNeededResolved = false;
|
163 | var passwordNeededCapability = (0, _util.createPromiseCapability)();
|
164 | var isPasswordIncorrectResolved = false;
|
165 | var passwordIncorrectCapability = (0, _util.createPromiseCapability)();
|
166 | loadingTask.onPassword = function (updatePassword, reason) {
|
167 | if (reason === _util.PasswordResponses.NEED_PASSWORD && !isPasswordNeededResolved) {
|
168 | isPasswordNeededResolved = true;
|
169 | passwordNeededCapability.resolve();
|
170 | updatePassword('qwerty');
|
171 | return;
|
172 | }
|
173 | if (reason === _util.PasswordResponses.INCORRECT_PASSWORD && !isPasswordIncorrectResolved) {
|
174 | isPasswordIncorrectResolved = true;
|
175 | passwordIncorrectCapability.resolve();
|
176 | updatePassword('asdfasdf');
|
177 | return;
|
178 | }
|
179 | expect(false).toEqual(true);
|
180 | };
|
181 | var promises = [passwordNeededCapability.promise, passwordIncorrectCapability.promise, loadingTask.promise];
|
182 | Promise.all(promises).then(function (data) {
|
183 | expect(data[2] instanceof _api.PDFDocumentProxy).toEqual(true);
|
184 | loadingTask.destroy().then(done);
|
185 | }).catch(function (reason) {
|
186 | done.fail(reason);
|
187 | });
|
188 | });
|
189 | it('creates pdf doc from PDF file protected with only a user password', function (done) {
|
190 | var filename = 'pr6531_2.pdf';
|
191 | var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: '' }));
|
192 | var result1 = passwordNeededLoadingTask.promise.then(function () {
|
193 | done.fail('shall fail with no password');
|
194 | return Promise.reject(new Error('loadingTask should be rejected'));
|
195 | }, function (data) {
|
196 | expect(data instanceof _util.PasswordException).toEqual(true);
|
197 | expect(data.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
|
198 | return passwordNeededLoadingTask.destroy();
|
199 | });
|
200 | var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'qwerty' }));
|
201 | var result2 = passwordIncorrectLoadingTask.promise.then(function () {
|
202 | done.fail('shall fail with wrong password');
|
203 | return Promise.reject(new Error('loadingTask should be rejected'));
|
204 | }, function (data) {
|
205 | expect(data instanceof _util.PasswordException).toEqual(true);
|
206 | expect(data.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
|
207 | return passwordIncorrectLoadingTask.destroy();
|
208 | });
|
209 | var passwordAcceptedLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'asdfasdf' }));
|
210 | var result3 = passwordAcceptedLoadingTask.promise.then(function (data) {
|
211 | expect(data instanceof _api.PDFDocumentProxy).toEqual(true);
|
212 | return passwordAcceptedLoadingTask.destroy();
|
213 | });
|
214 | Promise.all([result1, result2, result3]).then(function () {
|
215 | done();
|
216 | }).catch(function (reason) {
|
217 | done.fail(reason);
|
218 | });
|
219 | });
|
220 | it('creates pdf doc from password protected PDF file and aborts/throws ' + 'in the onPassword callback (issue 7806)', function (done) {
|
221 | var filename = 'issue3371.pdf';
|
222 | var passwordNeededLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
|
223 | var passwordIncorrectLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { password: 'qwerty' }));
|
224 | var passwordNeededDestroyed = void 0;
|
225 | passwordNeededLoadingTask.onPassword = function (callback, reason) {
|
226 | if (reason === _util.PasswordResponses.NEED_PASSWORD) {
|
227 | passwordNeededDestroyed = passwordNeededLoadingTask.destroy();
|
228 | return;
|
229 | }
|
230 | expect(false).toEqual(true);
|
231 | };
|
232 | var result1 = passwordNeededLoadingTask.promise.then(function () {
|
233 | done.fail('shall fail since the loadingTask should be destroyed');
|
234 | return Promise.reject(new Error('loadingTask should be rejected'));
|
235 | }, function (reason) {
|
236 | expect(reason instanceof _util.PasswordException).toEqual(true);
|
237 | expect(reason.code).toEqual(_util.PasswordResponses.NEED_PASSWORD);
|
238 | return passwordNeededDestroyed;
|
239 | });
|
240 | passwordIncorrectLoadingTask.onPassword = function (callback, reason) {
|
241 | if (reason === _util.PasswordResponses.INCORRECT_PASSWORD) {
|
242 | throw new Error('Incorrect password');
|
243 | }
|
244 | expect(false).toEqual(true);
|
245 | };
|
246 | var result2 = passwordIncorrectLoadingTask.promise.then(function () {
|
247 | done.fail('shall fail since the onPassword callback should throw');
|
248 | return Promise.reject(new Error('loadingTask should be rejected'));
|
249 | }, function (reason) {
|
250 | expect(reason instanceof _util.PasswordException).toEqual(true);
|
251 | expect(reason.code).toEqual(_util.PasswordResponses.INCORRECT_PASSWORD);
|
252 | return passwordIncorrectLoadingTask.destroy();
|
253 | });
|
254 | Promise.all([result1, result2]).then(function () {
|
255 | done();
|
256 | }).catch(function (reason) {
|
257 | done.fail(reason);
|
258 | });
|
259 | });
|
260 | });
|
261 | describe('PDFWorker', function () {
|
262 | it('worker created or destroyed', function (done) {
|
263 | if ((0, _is_node2.default)()) {
|
264 | pending('Worker is not supported in Node.js.');
|
265 | }
|
266 | var worker = new _api.PDFWorker({ name: 'test1' });
|
267 | worker.promise.then(function () {
|
268 | expect(worker.name).toEqual('test1');
|
269 | expect(!!worker.port).toEqual(true);
|
270 | expect(worker.destroyed).toEqual(false);
|
271 | expect(!!worker._webWorker).toEqual(true);
|
272 | expect(worker.port === worker._webWorker).toEqual(true);
|
273 | worker.destroy();
|
274 | expect(!!worker.port).toEqual(false);
|
275 | expect(worker.destroyed).toEqual(true);
|
276 | done();
|
277 | }).catch(function (reason) {
|
278 | done.fail(reason);
|
279 | });
|
280 | });
|
281 | it('worker created or destroyed by getDocument', function (done) {
|
282 | if ((0, _is_node2.default)()) {
|
283 | pending('Worker is not supported in Node.js.');
|
284 | }
|
285 | var loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
286 | var worker;
|
287 | loadingTask.promise.then(function () {
|
288 | worker = loadingTask._worker;
|
289 | expect(!!worker).toEqual(true);
|
290 | });
|
291 | var destroyPromise = loadingTask.promise.then(function () {
|
292 | return loadingTask.destroy();
|
293 | });
|
294 | destroyPromise.then(function () {
|
295 | var destroyedWorker = loadingTask._worker;
|
296 | expect(!!destroyedWorker).toEqual(false);
|
297 | expect(worker.destroyed).toEqual(true);
|
298 | done();
|
299 | }).catch(function (reason) {
|
300 | done.fail(reason);
|
301 | });
|
302 | });
|
303 | it('worker created and can be used in getDocument', function (done) {
|
304 | if ((0, _is_node2.default)()) {
|
305 | pending('Worker is not supported in Node.js.');
|
306 | }
|
307 | var worker = new _api.PDFWorker({ name: 'test1' });
|
308 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(basicApiFileName, { worker: worker }));
|
309 | loadingTask.promise.then(function () {
|
310 | var docWorker = loadingTask._worker;
|
311 | expect(!!docWorker).toEqual(false);
|
312 | var messageHandlerPort = loadingTask._transport.messageHandler.comObj;
|
313 | expect(messageHandlerPort === worker.port).toEqual(true);
|
314 | });
|
315 | var destroyPromise = loadingTask.promise.then(function () {
|
316 | return loadingTask.destroy();
|
317 | });
|
318 | destroyPromise.then(function () {
|
319 | expect(worker.destroyed).toEqual(false);
|
320 | worker.destroy();
|
321 | done();
|
322 | }).catch(function (reason) {
|
323 | done.fail(reason);
|
324 | });
|
325 | });
|
326 | it('creates more than one worker', function (done) {
|
327 | if ((0, _is_node2.default)()) {
|
328 | pending('Worker is not supported in Node.js.');
|
329 | }
|
330 | var worker1 = new _api.PDFWorker({ name: 'test1' });
|
331 | var worker2 = new _api.PDFWorker({ name: 'test2' });
|
332 | var worker3 = new _api.PDFWorker({ name: 'test3' });
|
333 | var ready = Promise.all([worker1.promise, worker2.promise, worker3.promise]);
|
334 | ready.then(function () {
|
335 | expect(worker1.port !== worker2.port && worker1.port !== worker3.port && worker2.port !== worker3.port).toEqual(true);
|
336 | worker1.destroy();
|
337 | worker2.destroy();
|
338 | worker3.destroy();
|
339 | done();
|
340 | }).catch(function (reason) {
|
341 | done.fail(reason);
|
342 | });
|
343 | });
|
344 | it('gets current workerSrc', function () {
|
345 | if ((0, _is_node2.default)()) {
|
346 | pending('Worker is not supported in Node.js.');
|
347 | }
|
348 | var workerSrc = _api.PDFWorker.getWorkerSrc();
|
349 | expect(typeof workerSrc === 'undefined' ? 'undefined' : _typeof(workerSrc)).toEqual('string');
|
350 | expect(workerSrc).toEqual(_worker_options.GlobalWorkerOptions.workerSrc);
|
351 | });
|
352 | });
|
353 | describe('PDFDocument', function () {
|
354 | var loadingTask;
|
355 | var doc;
|
356 | beforeAll(function (done) {
|
357 | loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
358 | loadingTask.promise.then(function (data) {
|
359 | doc = data;
|
360 | done();
|
361 | });
|
362 | });
|
363 | afterAll(function (done) {
|
364 | loadingTask.destroy().then(done);
|
365 | });
|
366 | it('gets number of pages', function () {
|
367 | expect(doc.numPages).toEqual(3);
|
368 | });
|
369 | it('gets fingerprint', function () {
|
370 | var fingerprint = doc.fingerprint;
|
371 | expect(typeof fingerprint === 'undefined' ? 'undefined' : _typeof(fingerprint)).toEqual('string');
|
372 | expect(fingerprint.length > 0).toEqual(true);
|
373 | });
|
374 | it('gets page', function (done) {
|
375 | var promise = doc.getPage(1);
|
376 | promise.then(function (data) {
|
377 | expect(data instanceof _api.PDFPageProxy).toEqual(true);
|
378 | expect(data.pageIndex).toEqual(0);
|
379 | done();
|
380 | }).catch(function (reason) {
|
381 | done.fail(reason);
|
382 | });
|
383 | });
|
384 | it('gets non-existent page', function (done) {
|
385 | var outOfRangePromise = doc.getPage(100);
|
386 | var nonIntegerPromise = doc.getPage(2.5);
|
387 | var nonNumberPromise = doc.getPage('1');
|
388 | outOfRangePromise = outOfRangePromise.then(function () {
|
389 | throw new Error('shall fail for out-of-range pageNumber parameter');
|
390 | }, function (reason) {
|
391 | expect(reason instanceof Error).toEqual(true);
|
392 | });
|
393 | nonIntegerPromise = nonIntegerPromise.then(function () {
|
394 | throw new Error('shall fail for non-integer pageNumber parameter');
|
395 | }, function (reason) {
|
396 | expect(reason instanceof Error).toEqual(true);
|
397 | });
|
398 | nonNumberPromise = nonNumberPromise.then(function () {
|
399 | throw new Error('shall fail for non-number pageNumber parameter');
|
400 | }, function (reason) {
|
401 | expect(reason instanceof Error).toEqual(true);
|
402 | });
|
403 | Promise.all([outOfRangePromise, nonIntegerPromise, nonNumberPromise]).then(function () {
|
404 | done();
|
405 | }).catch(function (reason) {
|
406 | done.fail(reason);
|
407 | });
|
408 | });
|
409 | it('gets page index', function (done) {
|
410 | var ref = {
|
411 | num: 17,
|
412 | gen: 0
|
413 | };
|
414 | var promise = doc.getPageIndex(ref);
|
415 | promise.then(function (pageIndex) {
|
416 | expect(pageIndex).toEqual(1);
|
417 | done();
|
418 | }).catch(function (reason) {
|
419 | done.fail(reason);
|
420 | });
|
421 | });
|
422 | it('gets invalid page index', function (done) {
|
423 | var ref = {
|
424 | num: 3,
|
425 | gen: 0
|
426 | };
|
427 | var promise = doc.getPageIndex(ref);
|
428 | promise.then(function () {
|
429 | done.fail('shall fail for invalid page reference.');
|
430 | }).catch(function (reason) {
|
431 | expect(reason instanceof Error).toEqual(true);
|
432 | done();
|
433 | });
|
434 | });
|
435 | it('gets destinations, from /Dests dictionary', function (done) {
|
436 | var promise = doc.getDestinations();
|
437 | promise.then(function (data) {
|
438 | expect(data).toEqual({
|
439 | chapter1: [{
|
440 | gen: 0,
|
441 | num: 17
|
442 | }, { name: 'XYZ' }, 0, 841.89, null]
|
443 | });
|
444 | done();
|
445 | }).catch(function (reason) {
|
446 | done.fail(reason);
|
447 | });
|
448 | });
|
449 | it('gets a destination, from /Dests dictionary', function (done) {
|
450 | var promise = doc.getDestination('chapter1');
|
451 | promise.then(function (data) {
|
452 | expect(data).toEqual([{
|
453 | gen: 0,
|
454 | num: 17
|
455 | }, { name: 'XYZ' }, 0, 841.89, null]);
|
456 | done();
|
457 | }).catch(function (reason) {
|
458 | done.fail(reason);
|
459 | });
|
460 | });
|
461 | it('gets a non-existent destination, from /Dests dictionary', function (done) {
|
462 | var promise = doc.getDestination('non-existent-named-destination');
|
463 | promise.then(function (data) {
|
464 | expect(data).toEqual(null);
|
465 | done();
|
466 | }).catch(function (reason) {
|
467 | done.fail(reason);
|
468 | });
|
469 | });
|
470 | it('gets destinations, from /Names (NameTree) dictionary', function (done) {
|
471 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
|
472 | var promise = loadingTask.promise.then(function (pdfDocument) {
|
473 | return pdfDocument.getDestinations();
|
474 | });
|
475 | promise.then(function (destinations) {
|
476 | expect(destinations).toEqual({
|
477 | 'Page.1': [{
|
478 | num: 1,
|
479 | gen: 0
|
480 | }, { name: 'XYZ' }, 0, 375, null],
|
481 | 'Page.2': [{
|
482 | num: 6,
|
483 | gen: 0
|
484 | }, { name: 'XYZ' }, 0, 375, null]
|
485 | });
|
486 | loadingTask.destroy().then(done);
|
487 | }).catch(function (reason) {
|
488 | done.fail(reason);
|
489 | });
|
490 | });
|
491 | it('gets a destination, from /Names (NameTree) dictionary', function (done) {
|
492 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
|
493 | var promise = loadingTask.promise.then(function (pdfDocument) {
|
494 | return pdfDocument.getDestination('Page.1');
|
495 | });
|
496 | promise.then(function (destination) {
|
497 | expect(destination).toEqual([{
|
498 | num: 1,
|
499 | gen: 0
|
500 | }, { name: 'XYZ' }, 0, 375, null]);
|
501 | loadingTask.destroy().then(done);
|
502 | }).catch(function (reason) {
|
503 | done.fail(reason);
|
504 | });
|
505 | });
|
506 | it('gets a non-existent destination, from /Names (NameTree) dictionary', function (done) {
|
507 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6204.pdf'));
|
508 | var promise = loadingTask.promise.then(function (pdfDocument) {
|
509 | return pdfDocument.getDestination('non-existent-named-destination');
|
510 | });
|
511 | promise.then(function (destination) {
|
512 | expect(destination).toEqual(null);
|
513 | loadingTask.destroy().then(done);
|
514 | }).catch(function (reason) {
|
515 | done.fail(reason);
|
516 | });
|
517 | });
|
518 | it('gets non-existent page labels', function (done) {
|
519 | var promise = doc.getPageLabels();
|
520 | promise.then(function (data) {
|
521 | expect(data).toEqual(null);
|
522 | done();
|
523 | }).catch(function (reason) {
|
524 | done.fail(reason);
|
525 | });
|
526 | });
|
527 | it('gets page labels', function (done) {
|
528 | var loadingTask0 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug793632.pdf'));
|
529 | var promise0 = loadingTask0.promise.then(function (pdfDoc) {
|
530 | return pdfDoc.getPageLabels();
|
531 | });
|
532 | var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue1453.pdf'));
|
533 | var promise1 = loadingTask1.promise.then(function (pdfDoc) {
|
534 | return pdfDoc.getPageLabels();
|
535 | });
|
536 | var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('rotation.pdf'));
|
537 | var promise2 = loadingTask2.promise.then(function (pdfDoc) {
|
538 | return pdfDoc.getPageLabels();
|
539 | });
|
540 | var loadingTask3 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bad-PageLabels.pdf'));
|
541 | var promise3 = loadingTask3.promise.then(function (pdfDoc) {
|
542 | return pdfDoc.getPageLabels();
|
543 | });
|
544 | Promise.all([promise0, promise1, promise2, promise3]).then(function (pageLabels) {
|
545 | expect(pageLabels[0]).toEqual(['i', 'ii', 'iii', '1']);
|
546 | expect(pageLabels[1]).toEqual(['Front Page1']);
|
547 | expect(pageLabels[2]).toEqual(['1', '2']);
|
548 | expect(pageLabels[3]).toEqual(['X3']);
|
549 | Promise.all([loadingTask0.destroy(), loadingTask1.destroy(), loadingTask2.destroy(), loadingTask3.destroy()]).then(done);
|
550 | }).catch(function (reason) {
|
551 | done.fail(reason);
|
552 | });
|
553 | });
|
554 | it('gets default page mode', function (done) {
|
555 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
|
556 | loadingTask.promise.then(function (pdfDocument) {
|
557 | return pdfDocument.getPageMode();
|
558 | }).then(function (mode) {
|
559 | expect(mode).toEqual('UseNone');
|
560 | loadingTask.destroy().then(done);
|
561 | }).catch(function (reason) {
|
562 | done.fail(reason);
|
563 | });
|
564 | });
|
565 | it('gets non-default page mode', function (done) {
|
566 | doc.getPageMode().then(function (mode) {
|
567 | expect(mode).toEqual('UseOutlines');
|
568 | done();
|
569 | }).catch(function (reason) {
|
570 | done.fail(reason);
|
571 | });
|
572 | });
|
573 | it('gets non-existent attachments', function (done) {
|
574 | var promise = doc.getAttachments();
|
575 | promise.then(function (data) {
|
576 | expect(data).toEqual(null);
|
577 | done();
|
578 | }).catch(function (reason) {
|
579 | done.fail(reason);
|
580 | });
|
581 | });
|
582 | it('gets attachments', function (done) {
|
583 | if ((0, _is_node2.default)()) {
|
584 | pending('TODO: Use a non-linked test-case.');
|
585 | }
|
586 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug766138.pdf'));
|
587 | var promise = loadingTask.promise.then(function (pdfDoc) {
|
588 | return pdfDoc.getAttachments();
|
589 | });
|
590 | promise.then(function (data) {
|
591 | var attachment = data['Press Quality.joboptions'];
|
592 | expect(attachment.filename).toEqual('Press Quality.joboptions');
|
593 | expect(attachment.content instanceof Uint8Array).toBeTruthy();
|
594 | expect(attachment.content.length).toEqual(30098);
|
595 | loadingTask.destroy().then(done);
|
596 | }).catch(function (reason) {
|
597 | done.fail(reason);
|
598 | });
|
599 | });
|
600 | it('gets javascript', function (done) {
|
601 | var promise = doc.getJavaScript();
|
602 | promise.then(function (data) {
|
603 | expect(data).toEqual(null);
|
604 | done();
|
605 | }).catch(function (reason) {
|
606 | done.fail(reason);
|
607 | });
|
608 | });
|
609 | var viewerPrintRegExp = /\bprint\s*\(/;
|
610 | it('gets javascript with printing instructions (Print action)', function (done) {
|
611 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('bug1001080.pdf'));
|
612 | var promise = loadingTask.promise.then(function (doc) {
|
613 | return doc.getJavaScript();
|
614 | });
|
615 | promise.then(function (data) {
|
616 | expect(data).toEqual(['print({});']);
|
617 | expect(data[0]).toMatch(viewerPrintRegExp);
|
618 | loadingTask.destroy().then(done);
|
619 | }).catch(function (reason) {
|
620 | done.fail(reason);
|
621 | });
|
622 | });
|
623 | it('gets javascript with printing instructions (JS action)', function (done) {
|
624 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue6106.pdf'));
|
625 | var promise = loadingTask.promise.then(function (doc) {
|
626 | return doc.getJavaScript();
|
627 | });
|
628 | promise.then(function (data) {
|
629 | expect(data).toEqual(['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
|
630 | expect(data[0]).toMatch(viewerPrintRegExp);
|
631 | loadingTask.destroy().then(done);
|
632 | }).catch(function (reason) {
|
633 | done.fail(reason);
|
634 | });
|
635 | });
|
636 | it('gets non-existent outline', function (done) {
|
637 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf'));
|
638 | var promise = loadingTask.promise.then(function (pdfDocument) {
|
639 | return pdfDocument.getOutline();
|
640 | });
|
641 | promise.then(function (outline) {
|
642 | expect(outline).toEqual(null);
|
643 | loadingTask.destroy().then(done);
|
644 | }).catch(function (reason) {
|
645 | done.fail(reason);
|
646 | });
|
647 | });
|
648 | it('gets outline', function (done) {
|
649 | var promise = doc.getOutline();
|
650 | promise.then(function (outline) {
|
651 | expect(outline instanceof Array).toEqual(true);
|
652 | expect(outline.length).toEqual(2);
|
653 | var outlineItem = outline[1];
|
654 | expect(outlineItem.title).toEqual('Chapter 1');
|
655 | expect(outlineItem.dest instanceof Array).toEqual(true);
|
656 | expect(outlineItem.url).toEqual(null);
|
657 | expect(outlineItem.unsafeUrl).toBeUndefined();
|
658 | expect(outlineItem.newWindow).toBeUndefined();
|
659 | expect(outlineItem.bold).toEqual(true);
|
660 | expect(outlineItem.italic).toEqual(false);
|
661 | expect(outlineItem.color).toEqual(new Uint8Array([0, 64, 128]));
|
662 | expect(outlineItem.items.length).toEqual(1);
|
663 | expect(outlineItem.items[0].title).toEqual('Paragraph 1.1');
|
664 | done();
|
665 | }).catch(function (reason) {
|
666 | done.fail(reason);
|
667 | });
|
668 | });
|
669 | it('gets outline containing a url', function (done) {
|
670 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue3214.pdf'));
|
671 | loadingTask.promise.then(function (pdfDocument) {
|
672 | pdfDocument.getOutline().then(function (outline) {
|
673 | expect(outline instanceof Array).toEqual(true);
|
674 | expect(outline.length).toEqual(5);
|
675 | var outlineItemTwo = outline[2];
|
676 | expect(_typeof(outlineItemTwo.title)).toEqual('string');
|
677 | expect(outlineItemTwo.dest).toEqual(null);
|
678 | expect(outlineItemTwo.url).toEqual('http://google.com/');
|
679 | expect(outlineItemTwo.unsafeUrl).toEqual('http://google.com');
|
680 | expect(outlineItemTwo.newWindow).toBeUndefined();
|
681 | var outlineItemOne = outline[1];
|
682 | expect(outlineItemOne.bold).toEqual(false);
|
683 | expect(outlineItemOne.italic).toEqual(true);
|
684 | expect(outlineItemOne.color).toEqual(new Uint8Array([0, 0, 0]));
|
685 | loadingTask.destroy().then(done);
|
686 | });
|
687 | }).catch(function (reason) {
|
688 | done.fail(reason);
|
689 | });
|
690 | });
|
691 | it('gets metadata', function (done) {
|
692 | var promise = doc.getMetadata();
|
693 | promise.then(function (metadata) {
|
694 | expect(metadata.info['Title']).toEqual('Basic API Test');
|
695 | expect(metadata.info['PDFFormatVersion']).toEqual('1.7');
|
696 | expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test');
|
697 | expect(metadata.contentDispositionFilename).toEqual(null);
|
698 | done();
|
699 | }).catch(function (reason) {
|
700 | done.fail(reason);
|
701 | });
|
702 | });
|
703 | it('gets data', function (done) {
|
704 | var promise = doc.getData();
|
705 | promise.then(function (data) {
|
706 | expect(data instanceof Uint8Array).toEqual(true);
|
707 | expect(data.length).toEqual(basicApiFileLength);
|
708 | done();
|
709 | }).catch(function (reason) {
|
710 | done.fail(reason);
|
711 | });
|
712 | });
|
713 | it('gets download info', function (done) {
|
714 | var promise = doc.getDownloadInfo();
|
715 | promise.then(function (data) {
|
716 | expect(data).toEqual({ length: basicApiFileLength });
|
717 | done();
|
718 | }).catch(function (reason) {
|
719 | done.fail(reason);
|
720 | });
|
721 | });
|
722 | it('gets stats', function (done) {
|
723 | var promise = doc.getStats();
|
724 | promise.then(function (stats) {
|
725 | expect(stats).toEqual({
|
726 | streamTypes: [],
|
727 | fontTypes: []
|
728 | });
|
729 | done();
|
730 | }).catch(function (reason) {
|
731 | done.fail(reason);
|
732 | });
|
733 | });
|
734 | it('checks that fingerprints are unique', function (done) {
|
735 | var loadingTask1 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4436r.pdf'));
|
736 | var loadingTask2 = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('issue4575.pdf'));
|
737 | var promises = [loadingTask1.promise, loadingTask2.promise];
|
738 | Promise.all(promises).then(function (data) {
|
739 | var fingerprint1 = data[0].fingerprint;
|
740 | expect(typeof fingerprint1 === 'undefined' ? 'undefined' : _typeof(fingerprint1)).toEqual('string');
|
741 | expect(fingerprint1.length > 0).toEqual(true);
|
742 | var fingerprint2 = data[1].fingerprint;
|
743 | expect(typeof fingerprint2 === 'undefined' ? 'undefined' : _typeof(fingerprint2)).toEqual('string');
|
744 | expect(fingerprint2.length > 0).toEqual(true);
|
745 | expect(fingerprint1).not.toEqual(fingerprint2);
|
746 | Promise.all([loadingTask1.destroy(), loadingTask2.destroy()]).then(done);
|
747 | }).catch(function (reason) {
|
748 | done.fail(reason);
|
749 | });
|
750 | });
|
751 | describe('Cross-origin', function () {
|
752 | var loadingTask;
|
753 | function _checkCanLoad(expectSuccess, filename, options) {
|
754 | if ((0, _is_node2.default)()) {
|
755 | pending('Cannot simulate cross-origin requests in Node.js');
|
756 | }
|
757 | var params = (0, _test_utils.buildGetDocumentParams)(filename, options);
|
758 | var url = new URL(params.url);
|
759 | if (url.hostname === 'localhost') {
|
760 | url.hostname = '127.0.0.1';
|
761 | } else if (params.url.hostname === '127.0.0.1') {
|
762 | url.hostname = 'localhost';
|
763 | } else {
|
764 | pending('Can only run cross-origin test on localhost!');
|
765 | }
|
766 | params.url = url.href;
|
767 | loadingTask = (0, _api.getDocument)(params);
|
768 | return loadingTask.promise.then(function (pdf) {
|
769 | return pdf.destroy();
|
770 | }).then(function () {
|
771 | expect(expectSuccess).toEqual(true);
|
772 | }, function (error) {
|
773 | if (expectSuccess) {
|
774 | expect(error).toEqual('There should not be any error');
|
775 | }
|
776 | expect(expectSuccess).toEqual(false);
|
777 | });
|
778 | }
|
779 | function testCanLoad(filename, options) {
|
780 | return _checkCanLoad(true, filename, options);
|
781 | }
|
782 | function testCannotLoad(filename, options) {
|
783 | return _checkCanLoad(false, filename, options);
|
784 | }
|
785 | afterEach(function (done) {
|
786 | if (loadingTask) {
|
787 | loadingTask.destroy().then(done);
|
788 | } else {
|
789 | done();
|
790 | }
|
791 | });
|
792 | it('server disallows cors', function (done) {
|
793 | testCannotLoad('basicapi.pdf').then(done);
|
794 | });
|
795 | it('server allows cors without credentials, default withCredentials', function (done) {
|
796 | testCanLoad('basicapi.pdf?cors=withoutCredentials').then(done);
|
797 | });
|
798 | it('server allows cors without credentials, and withCredentials=false', function (done) {
|
799 | testCanLoad('basicapi.pdf?cors=withoutCredentials', { withCredentials: false }).then(done);
|
800 | });
|
801 | it('server allows cors without credentials, but withCredentials=true', function (done) {
|
802 | testCannotLoad('basicapi.pdf?cors=withoutCredentials', { withCredentials: true }).then(done);
|
803 | });
|
804 | it('server allows cors with credentials, and withCredentials=true', function (done) {
|
805 | testCanLoad('basicapi.pdf?cors=withCredentials', { withCredentials: true }).then(done);
|
806 | });
|
807 | it('server allows cors with credentials, and withCredentials=false', function (done) {
|
808 | testCanLoad('basicapi.pdf?cors=withCredentials', { withCredentials: false }).then(done);
|
809 | });
|
810 | });
|
811 | });
|
812 | describe('Page', function () {
|
813 | var loadingTask;
|
814 | var pdfDocument, page;
|
815 | beforeAll(function (done) {
|
816 | loadingTask = (0, _api.getDocument)(basicApiGetDocumentParams);
|
817 | loadingTask.promise.then(function (doc) {
|
818 | pdfDocument = doc;
|
819 | pdfDocument.getPage(1).then(function (data) {
|
820 | page = data;
|
821 | done();
|
822 | });
|
823 | }).catch(function (reason) {
|
824 | done.fail(reason);
|
825 | });
|
826 | });
|
827 | afterAll(function (done) {
|
828 | loadingTask.destroy().then(done);
|
829 | });
|
830 | it('gets page number', function () {
|
831 | expect(page.pageNumber).toEqual(1);
|
832 | });
|
833 | it('gets rotate', function () {
|
834 | expect(page.rotate).toEqual(0);
|
835 | });
|
836 | it('gets ref', function () {
|
837 | expect(page.ref).toEqual({
|
838 | num: 15,
|
839 | gen: 0
|
840 | });
|
841 | });
|
842 | it('gets userUnit', function () {
|
843 | expect(page.userUnit).toEqual(1.0);
|
844 | });
|
845 | it('gets view', function () {
|
846 | expect(page.view).toEqual([0, 0, 595.28, 841.89]);
|
847 | });
|
848 | it('gets viewport', function () {
|
849 | var viewport = page.getViewport(1.5, 90);
|
850 | expect(viewport.viewBox).toEqual(page.view);
|
851 | expect(viewport.scale).toEqual(1.5);
|
852 | expect(viewport.rotation).toEqual(90);
|
853 | expect(viewport.transform).toEqual([0, 1.5, 1.5, 0, 0, 0]);
|
854 | expect(viewport.width).toEqual(1262.835);
|
855 | expect(viewport.height).toEqual(892.92);
|
856 | });
|
857 | it('gets viewport respecting "dontFlip" argument', function () {
|
858 | var scale = 1;
|
859 | var rotation = 135;
|
860 | var viewport = page.getViewport(scale, rotation);
|
861 | var dontFlipViewport = page.getViewport(scale, rotation, true);
|
862 | expect(dontFlipViewport).not.toEqual(viewport);
|
863 | expect(dontFlipViewport).toEqual(viewport.clone({ dontFlip: true }));
|
864 | expect(viewport.transform).toEqual([1, 0, 0, -1, 0, 841.89]);
|
865 | expect(dontFlipViewport.transform).toEqual([1, 0, -0, 1, 0, 0]);
|
866 | });
|
867 | it('gets annotations', function (done) {
|
868 | var defaultPromise = page.getAnnotations().then(function (data) {
|
869 | expect(data.length).toEqual(4);
|
870 | });
|
871 | var displayPromise = page.getAnnotations({ intent: 'display' }).then(function (data) {
|
872 | expect(data.length).toEqual(4);
|
873 | });
|
874 | var printPromise = page.getAnnotations({ intent: 'print' }).then(function (data) {
|
875 | expect(data.length).toEqual(4);
|
876 | });
|
877 | Promise.all([defaultPromise, displayPromise, printPromise]).then(function () {
|
878 | done();
|
879 | }).catch(function (reason) {
|
880 | done.fail(reason);
|
881 | });
|
882 | });
|
883 | it('gets annotations containing relative URLs (bug 766086)', function (done) {
|
884 | var filename = 'bug766086.pdf';
|
885 | var defaultLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename));
|
886 | var defaultPromise = defaultLoadingTask.promise.then(function (pdfDoc) {
|
887 | return pdfDoc.getPage(1).then(function (pdfPage) {
|
888 | return pdfPage.getAnnotations();
|
889 | });
|
890 | });
|
891 | var docBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { docBaseUrl: 'http://www.example.com/test/pdfs/qwerty.pdf' }));
|
892 | var docBaseUrlPromise = docBaseUrlLoadingTask.promise.then(function (pdfDoc) {
|
893 | return pdfDoc.getPage(1).then(function (pdfPage) {
|
894 | return pdfPage.getAnnotations();
|
895 | });
|
896 | });
|
897 | var invalidDocBaseUrlLoadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)(filename, { docBaseUrl: 'qwerty.pdf' }));
|
898 | var invalidDocBaseUrlPromise = invalidDocBaseUrlLoadingTask.promise.then(function (pdfDoc) {
|
899 | return pdfDoc.getPage(1).then(function (pdfPage) {
|
900 | return pdfPage.getAnnotations();
|
901 | });
|
902 | });
|
903 | Promise.all([defaultPromise, docBaseUrlPromise, invalidDocBaseUrlPromise]).then(function (data) {
|
904 | var defaultAnnotations = data[0];
|
905 | var docBaseUrlAnnotations = data[1];
|
906 | var invalidDocBaseUrlAnnotations = data[2];
|
907 | expect(defaultAnnotations[0].url).toBeUndefined();
|
908 | expect(defaultAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
|
909 | expect(docBaseUrlAnnotations[0].url).toEqual('http://www.example.com/0021/002156/215675E.pdf#15');
|
910 | expect(docBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
|
911 | expect(invalidDocBaseUrlAnnotations[0].url).toBeUndefined();
|
912 | expect(invalidDocBaseUrlAnnotations[0].unsafeUrl).toEqual('../../0021/002156/215675E.pdf#15');
|
913 | Promise.all([defaultLoadingTask.destroy(), docBaseUrlLoadingTask.destroy(), invalidDocBaseUrlLoadingTask.destroy()]).then(done);
|
914 | }).catch(function (reason) {
|
915 | done.fail(reason);
|
916 | });
|
917 | });
|
918 | it('gets text content', function (done) {
|
919 | var defaultPromise = page.getTextContent();
|
920 | var parametersPromise = page.getTextContent({
|
921 | normalizeWhitespace: true,
|
922 | disableCombineTextItems: true
|
923 | });
|
924 | var promises = [defaultPromise, parametersPromise];
|
925 | Promise.all(promises).then(function (data) {
|
926 | expect(!!data[0].items).toEqual(true);
|
927 | expect(data[0].items.length).toEqual(7);
|
928 | expect(!!data[0].styles).toEqual(true);
|
929 | expect(JSON.stringify(data[0])).toEqual(JSON.stringify(data[1]));
|
930 | done();
|
931 | }).catch(function (reason) {
|
932 | done.fail(reason);
|
933 | });
|
934 | });
|
935 | it('gets operator list', function (done) {
|
936 | var promise = page.getOperatorList();
|
937 | promise.then(function (oplist) {
|
938 | expect(!!oplist.fnArray).toEqual(true);
|
939 | expect(!!oplist.argsArray).toEqual(true);
|
940 | expect(oplist.lastChunk).toEqual(true);
|
941 | done();
|
942 | }).catch(function (reason) {
|
943 | done.fail(reason);
|
944 | });
|
945 | });
|
946 | it('gets operatorList with JPEG image (issue 4888)', function (done) {
|
947 | var loadingTask = (0, _api.getDocument)((0, _test_utils.buildGetDocumentParams)('cmykjpeg.pdf'));
|
948 | loadingTask.promise.then(function (pdfDoc) {
|
949 | pdfDoc.getPage(1).then(function (pdfPage) {
|
950 | pdfPage.getOperatorList().then(function (opList) {
|
951 | var imgIndex = opList.fnArray.indexOf(_util.OPS.paintImageXObject);
|
952 | var imgArgs = opList.argsArray[imgIndex];
|
953 |
|
954 | var _pdfPage$objs$get = pdfPage.objs.get(imgArgs[0]),
|
955 | imgData = _pdfPage$objs$get.data;
|
956 |
|
957 | expect(imgData instanceof Uint8ClampedArray).toEqual(true);
|
958 | expect(imgData.length).toEqual(90000);
|
959 | done();
|
960 | });
|
961 | });
|
962 | }).catch(function (reason) {
|
963 | done.fail(reason);
|
964 | });
|
965 | });
|
966 | it('gets stats after parsing page', function (done) {
|
967 | var promise = page.getOperatorList().then(function () {
|
968 | return pdfDocument.getStats();
|
969 | });
|
970 | var expectedStreamTypes = [];
|
971 | expectedStreamTypes[_util.StreamType.FLATE] = true;
|
972 | var expectedFontTypes = [];
|
973 | expectedFontTypes[_util.FontType.TYPE1] = true;
|
974 | expectedFontTypes[_util.FontType.CIDFONTTYPE2] = true;
|
975 | promise.then(function (stats) {
|
976 | expect(stats).toEqual({
|
977 | streamTypes: expectedStreamTypes,
|
978 | fontTypes: expectedFontTypes
|
979 | });
|
980 | done();
|
981 | }).catch(function (reason) {
|
982 | done.fail(reason);
|
983 | });
|
984 | });
|
985 | it('cancels rendering of page', function (done) {
|
986 | if ((0, _is_node2.default)()) {
|
987 | pending('TODO: Support Canvas testing in Node.js.');
|
988 | }
|
989 | var viewport = page.getViewport(1);
|
990 | var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
991 | var renderTask = page.render({
|
992 | canvasContext: canvasAndCtx.context,
|
993 | viewport: viewport
|
994 | });
|
995 | renderTask.cancel();
|
996 | renderTask.promise.then(function () {
|
997 | done.fail('shall cancel rendering');
|
998 | }).catch(function (error) {
|
999 | expect(error instanceof _dom_utils.RenderingCancelledException).toEqual(true);
|
1000 | expect(error.type).toEqual('canvas');
|
1001 | CanvasFactory.destroy(canvasAndCtx);
|
1002 | done();
|
1003 | });
|
1004 | });
|
1005 | it('multiple render() on the same canvas', function (done) {
|
1006 | if ((0, _is_node2.default)()) {
|
1007 | pending('TODO: Support Canvas testing in Node.js.');
|
1008 | }
|
1009 | var viewport = page.getViewport(1);
|
1010 | var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
1011 | var renderTask1 = page.render({
|
1012 | canvasContext: canvasAndCtx.context,
|
1013 | viewport: viewport
|
1014 | });
|
1015 | var renderTask2 = page.render({
|
1016 | canvasContext: canvasAndCtx.context,
|
1017 | viewport: viewport
|
1018 | });
|
1019 | Promise.all([renderTask1.promise, renderTask2.promise.then(function () {
|
1020 | done.fail('shall fail rendering');
|
1021 | }, function (reason) {
|
1022 | expect(/multiple render\(\)/.test(reason.message)).toEqual(true);
|
1023 | })]).then(done);
|
1024 | });
|
1025 | });
|
1026 | describe('Multiple `getDocument` instances', function () {
|
1027 | var pdf1 = (0, _test_utils.buildGetDocumentParams)('tracemonkey.pdf');
|
1028 | var pdf2 = (0, _test_utils.buildGetDocumentParams)('TAMReview.pdf');
|
1029 | var pdf3 = (0, _test_utils.buildGetDocumentParams)('issue6068.pdf');
|
1030 | var loadingTasks = [];
|
1031 | var pdfDocuments = [];
|
1032 | function renderPDF(filename) {
|
1033 | var loadingTask = (0, _api.getDocument)(filename);
|
1034 | loadingTasks.push(loadingTask);
|
1035 | return loadingTask.promise.then(function (pdf) {
|
1036 | pdfDocuments.push(pdf);
|
1037 | return pdf.getPage(1);
|
1038 | }).then(function (page) {
|
1039 | var viewport = page.getViewport(1.2);
|
1040 | var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
1041 | return page.render({
|
1042 | canvasContext: canvasAndCtx.context,
|
1043 | viewport: viewport
|
1044 | }).then(function () {
|
1045 | var data = canvasAndCtx.canvas.toDataURL();
|
1046 | CanvasFactory.destroy(canvasAndCtx);
|
1047 | return data;
|
1048 | });
|
1049 | });
|
1050 | }
|
1051 | afterEach(function (done) {
|
1052 | var destroyPromises = pdfDocuments.map(function (pdfDocument) {
|
1053 | return pdfDocument.destroy();
|
1054 | });
|
1055 | var destroyPromises2 = loadingTasks.map(function (loadingTask) {
|
1056 | return loadingTask.destroy();
|
1057 | });
|
1058 | Promise.all(destroyPromises.concat(destroyPromises2)).then(function () {
|
1059 | done();
|
1060 | });
|
1061 | });
|
1062 | it('should correctly render PDFs in parallel', function (done) {
|
1063 | if ((0, _is_node2.default)()) {
|
1064 | pending('TODO: Support Canvas testing in Node.js.');
|
1065 | }
|
1066 | var baseline1, baseline2, baseline3;
|
1067 | var promiseDone = renderPDF(pdf1).then(function (data1) {
|
1068 | baseline1 = data1;
|
1069 | return renderPDF(pdf2);
|
1070 | }).then(function (data2) {
|
1071 | baseline2 = data2;
|
1072 | return renderPDF(pdf3);
|
1073 | }).then(function (data3) {
|
1074 | baseline3 = data3;
|
1075 | return Promise.all([renderPDF(pdf1), renderPDF(pdf2), renderPDF(pdf3)]);
|
1076 | }).then(function (dataUrls) {
|
1077 | expect(dataUrls[0]).toEqual(baseline1);
|
1078 | expect(dataUrls[1]).toEqual(baseline2);
|
1079 | expect(dataUrls[2]).toEqual(baseline3);
|
1080 | return true;
|
1081 | });
|
1082 | promiseDone.then(function () {
|
1083 | done();
|
1084 | }).catch(function (reason) {
|
1085 | done.fail(reason);
|
1086 | });
|
1087 | });
|
1088 | });
|
1089 | describe('PDFDataRangeTransport', function () {
|
1090 | var loadPromise;
|
1091 | function getDocumentData() {
|
1092 | var pdfPath = new URL('../pdfs/tracemonkey.pdf', window.location).href;
|
1093 | if (loadPromise) {
|
1094 | return loadPromise;
|
1095 | }
|
1096 | loadPromise = new Promise(function (resolve, reject) {
|
1097 | var xhr = new XMLHttpRequest(pdfPath);
|
1098 | xhr.open('GET', pdfPath);
|
1099 | xhr.responseType = 'arraybuffer';
|
1100 | xhr.onload = function () {
|
1101 | resolve(new Uint8Array(xhr.response));
|
1102 | };
|
1103 | xhr.onerror = function () {
|
1104 | reject(new Error('PDF is not loaded'));
|
1105 | };
|
1106 | xhr.send();
|
1107 | });
|
1108 | return loadPromise;
|
1109 | }
|
1110 | it('should fetch document info and page using ranges', function (done) {
|
1111 | if ((0, _is_node2.default)()) {
|
1112 | pending('XMLHttpRequest is not supported in Node.js.');
|
1113 | }
|
1114 | var transport;
|
1115 | var initialDataLength = 4000;
|
1116 | var fetches = 0;
|
1117 | var getDocumentPromise = getDocumentData().then(function (data) {
|
1118 | var initialData = data.subarray(0, initialDataLength);
|
1119 | transport = new _api.PDFDataRangeTransport(data.length, initialData);
|
1120 | transport.requestDataRange = function (begin, end) {
|
1121 | fetches++;
|
1122 | waitSome(function () {
|
1123 | transport.onDataProgress(4000);
|
1124 | transport.onDataRange(begin, data.subarray(begin, end));
|
1125 | });
|
1126 | };
|
1127 | var loadingTask = (0, _api.getDocument)(transport);
|
1128 | return loadingTask.promise;
|
1129 | });
|
1130 | var pdfDocument;
|
1131 | var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
|
1132 | pdfDocument = pdfDocument_;
|
1133 | var pagePromise = pdfDocument.getPage(10);
|
1134 | return pagePromise;
|
1135 | });
|
1136 | getPagePromise.then(function (page) {
|
1137 | expect(pdfDocument.numPages).toEqual(14);
|
1138 | expect(page.rotate).toEqual(0);
|
1139 | expect(fetches).toBeGreaterThan(2);
|
1140 | done();
|
1141 | }).catch(function (reason) {
|
1142 | done.fail(reason);
|
1143 | });
|
1144 | });
|
1145 | it('should fetch document info and page using range and streaming', function (done) {
|
1146 | if ((0, _is_node2.default)()) {
|
1147 | pending('XMLHttpRequest is not supported in Node.js.');
|
1148 | }
|
1149 | var transport;
|
1150 | var initialDataLength = 4000;
|
1151 | var fetches = 0;
|
1152 | var getDocumentPromise = getDocumentData().then(function (data) {
|
1153 | var initialData = data.subarray(0, initialDataLength);
|
1154 | transport = new _api.PDFDataRangeTransport(data.length, initialData);
|
1155 | transport.requestDataRange = function (begin, end) {
|
1156 | fetches++;
|
1157 | if (fetches === 1) {
|
1158 | transport.onDataProgressiveRead(data.subarray(initialDataLength));
|
1159 | }
|
1160 | waitSome(function () {
|
1161 | transport.onDataRange(begin, data.subarray(begin, end));
|
1162 | });
|
1163 | };
|
1164 | var loadingTask = (0, _api.getDocument)(transport);
|
1165 | return loadingTask.promise;
|
1166 | });
|
1167 | var pdfDocument;
|
1168 | var getPagePromise = getDocumentPromise.then(function (pdfDocument_) {
|
1169 | pdfDocument = pdfDocument_;
|
1170 | var pagePromise = pdfDocument.getPage(10);
|
1171 | return pagePromise;
|
1172 | });
|
1173 | getPagePromise.then(function (page) {
|
1174 | expect(pdfDocument.numPages).toEqual(14);
|
1175 | expect(page.rotate).toEqual(0);
|
1176 | expect(fetches).toEqual(1);
|
1177 | done();
|
1178 | }).catch(function (reason) {
|
1179 | done.fail(reason);
|
1180 | });
|
1181 | });
|
1182 | });
|
1183 | }); |
\ | No newline at end of file |