2012-04-13 09:59:30 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2014-08-31 05:12:34 +09:00
|
|
|
/* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor,
|
2015-10-03 01:04:08 +09:00
|
|
|
InvalidPDFException, MissingPDFException, StreamType, FontType,
|
2015-10-17 01:48:26 +09:00
|
|
|
PDFDocumentProxy, PasswordException, PasswordResponses,
|
2015-10-03 01:04:08 +09:00
|
|
|
PDFPageProxy */
|
2012-04-13 09:59:30 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('api', function() {
|
2012-06-24 05:35:59 +09:00
|
|
|
var basicApiUrl = combineUrl(window.location.href, '../pdfs/basicapi.pdf');
|
2014-09-23 20:16:58 +09:00
|
|
|
var basicApiFileLength = 105779; // bytes
|
2014-08-24 04:08:27 +09:00
|
|
|
function waitsForPromiseResolved(promise, successCallback) {
|
2013-06-06 04:28:31 +09:00
|
|
|
var data;
|
|
|
|
promise.then(function(val) {
|
|
|
|
data = val;
|
|
|
|
successCallback(data);
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
// Shouldn't get here.
|
|
|
|
expect(false).toEqual(true);
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
waitsFor(function() {
|
2013-06-06 04:28:31 +09:00
|
|
|
return data !== undefined;
|
2013-09-25 01:30:54 +09:00
|
|
|
}, 20000);
|
2012-04-13 09:59:30 +09:00
|
|
|
}
|
2014-08-24 04:08:27 +09:00
|
|
|
function waitsForPromiseRejected(promise, failureCallback) {
|
|
|
|
var data;
|
|
|
|
promise.then(function(val) {
|
|
|
|
// Shouldn't get here.
|
|
|
|
expect(false).toEqual(true);
|
|
|
|
},
|
|
|
|
function(error) {
|
|
|
|
data = error;
|
|
|
|
failureCallback(data);
|
|
|
|
});
|
|
|
|
waitsFor(function() {
|
|
|
|
return data !== undefined;
|
|
|
|
}, 20000);
|
|
|
|
}
|
2012-04-13 09:59:30 +09:00
|
|
|
describe('PDFJS', function() {
|
|
|
|
describe('getDocument', function() {
|
|
|
|
it('creates pdf doc from URL', function() {
|
|
|
|
var promise = PDFJS.getDocument(basicApiUrl);
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2012-04-13 09:59:30 +09:00
|
|
|
expect(true).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('creates pdf doc from typed array', function() {
|
2014-08-15 23:04:39 +09:00
|
|
|
var nonBinaryRequest = PDFJS.disableWorker;
|
|
|
|
var request = new XMLHttpRequest();
|
|
|
|
request.open('GET', basicApiUrl, false);
|
|
|
|
if (!nonBinaryRequest) {
|
|
|
|
try {
|
|
|
|
request.responseType = 'arraybuffer';
|
|
|
|
nonBinaryRequest = request.responseType !== 'arraybuffer';
|
|
|
|
} catch (e) {
|
|
|
|
nonBinaryRequest = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nonBinaryRequest && request.overrideMimeType) {
|
|
|
|
request.overrideMimeType('text/plain; charset=x-user-defined');
|
|
|
|
}
|
|
|
|
request.send(null);
|
|
|
|
|
|
|
|
var typedArrayPdf;
|
|
|
|
if (nonBinaryRequest) {
|
|
|
|
var data = Array.prototype.map.call(request.responseText,
|
|
|
|
function (ch) {
|
|
|
|
return ch.charCodeAt(0) & 0xFF;
|
|
|
|
});
|
|
|
|
typedArrayPdf = new Uint8Array(data);
|
|
|
|
} else {
|
|
|
|
typedArrayPdf = new Uint8Array(request.response);
|
|
|
|
}
|
|
|
|
// Sanity check to make sure that we fetched the entire PDF file.
|
2014-09-23 20:16:58 +09:00
|
|
|
expect(typedArrayPdf.length).toEqual(basicApiFileLength);
|
2014-08-15 23:04:39 +09:00
|
|
|
|
|
|
|
var promise = PDFJS.getDocument(typedArrayPdf);
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2014-08-15 23:04:39 +09:00
|
|
|
expect(true).toEqual(true);
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
2015-10-03 01:04:08 +09:00
|
|
|
it('creates pdf doc from invalid PDF file', function() {
|
|
|
|
// A severely corrupt PDF file (even Adobe Reader fails to open it).
|
|
|
|
var url = combineUrl(window.location.href, '../pdfs/bug1020226.pdf');
|
|
|
|
|
|
|
|
var promise = PDFJS.getDocument(url);
|
|
|
|
waitsForPromiseRejected(promise, function (error) {
|
|
|
|
expect(error instanceof InvalidPDFException).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2014-09-08 00:39:49 +09:00
|
|
|
it('creates pdf doc from non-existent URL', function() {
|
|
|
|
var nonExistentUrl = combineUrl(window.location.href,
|
|
|
|
'../pdfs/non-existent.pdf');
|
|
|
|
var promise = PDFJS.getDocument(nonExistentUrl);
|
|
|
|
waitsForPromiseRejected(promise, function(error) {
|
|
|
|
expect(error instanceof MissingPDFException).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2015-10-17 01:48:26 +09:00
|
|
|
it('creates pdf doc from PDF file protected with user and owner password',
|
|
|
|
function () {
|
|
|
|
var url = combineUrl(window.location.href, '../pdfs/pr6531_1.pdf');
|
|
|
|
|
|
|
|
var passwordNeededPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: '',
|
|
|
|
});
|
|
|
|
waitsForPromiseRejected(passwordNeededPromise, function (data) {
|
|
|
|
expect(data instanceof PasswordException).toEqual(true);
|
|
|
|
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
|
|
|
|
});
|
|
|
|
|
|
|
|
var passwordIncorrectPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: 'qwerty',
|
|
|
|
});
|
|
|
|
waitsForPromiseRejected(passwordIncorrectPromise, function (data) {
|
|
|
|
expect(data instanceof PasswordException).toEqual(true);
|
|
|
|
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
|
|
|
|
});
|
|
|
|
|
|
|
|
var passwordAcceptedPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: 'asdfasdf',
|
|
|
|
});
|
|
|
|
waitsForPromiseResolved(passwordAcceptedPromise, function (data) {
|
|
|
|
expect(data instanceof PDFDocumentProxy).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('creates pdf doc from PDF file protected with only a user password',
|
|
|
|
function () {
|
|
|
|
var url = combineUrl(window.location.href, '../pdfs/pr6531_2.pdf');
|
|
|
|
|
|
|
|
var passwordNeededPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: '',
|
|
|
|
});
|
|
|
|
waitsForPromiseRejected(passwordNeededPromise, function (data) {
|
|
|
|
expect(data instanceof PasswordException).toEqual(true);
|
|
|
|
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
|
|
|
|
});
|
|
|
|
|
|
|
|
var passwordIncorrectPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: 'qwerty',
|
|
|
|
});
|
|
|
|
waitsForPromiseRejected(passwordIncorrectPromise, function (data) {
|
|
|
|
expect(data instanceof PasswordException).toEqual(true);
|
|
|
|
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD);
|
|
|
|
});
|
|
|
|
|
|
|
|
var passwordAcceptedPromise = PDFJS.getDocument({
|
|
|
|
url: url, password: 'asdfasdf',
|
|
|
|
});
|
|
|
|
waitsForPromiseResolved(passwordAcceptedPromise, function (data) {
|
|
|
|
expect(data instanceof PDFDocumentProxy).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('PDFDocument', function() {
|
|
|
|
var promise = PDFJS.getDocument(basicApiUrl);
|
|
|
|
var doc;
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2013-06-06 04:28:31 +09:00
|
|
|
doc = data;
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
it('gets number of pages', function() {
|
|
|
|
expect(doc.numPages).toEqual(3);
|
|
|
|
});
|
|
|
|
it('gets fingerprint', function() {
|
|
|
|
expect(typeof doc.fingerprint).toEqual('string');
|
|
|
|
});
|
|
|
|
it('gets page', function() {
|
|
|
|
var promise = doc.getPage(1);
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2015-10-04 21:28:24 +09:00
|
|
|
expect(data instanceof PDFPageProxy).toEqual(true);
|
|
|
|
expect(data.pageIndex).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('gets non-existent page', function() {
|
|
|
|
var promise = doc.getPage(100);
|
|
|
|
waitsForPromiseRejected(promise, function(data) {
|
|
|
|
expect(data instanceof Error).toEqual(true);
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
});
|
2014-03-19 18:17:58 +09:00
|
|
|
it('gets page index', function() {
|
|
|
|
// reference to second page
|
|
|
|
var ref = {num: 17, gen: 0};
|
|
|
|
var promise = doc.getPageIndex(ref);
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(pageIndex) {
|
2014-03-19 18:17:58 +09:00
|
|
|
expect(pageIndex).toEqual(1);
|
|
|
|
});
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
it('gets destinations', function() {
|
|
|
|
var promise = doc.getDestinations();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2014-05-14 19:54:18 +09:00
|
|
|
expect(data).toEqual({ chapter1: [{ gen: 0, num: 17 }, { name: 'XYZ' },
|
|
|
|
0, 841.89, null] });
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
});
|
2014-10-05 22:56:40 +09:00
|
|
|
it('gets a destination', function() {
|
|
|
|
var promise = doc.getDestination('chapter1');
|
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
|
|
|
expect(data).toEqual([{ gen: 0, num: 17 }, { name: 'XYZ' },
|
|
|
|
0, 841.89, null]);
|
|
|
|
});
|
|
|
|
});
|
2015-07-08 04:48:57 +09:00
|
|
|
it('gets a non-existent destination', function() {
|
|
|
|
var promise = doc.getDestination('non-existent-named-destination');
|
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
|
|
|
expect(data).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
2014-05-19 06:35:29 +09:00
|
|
|
it('gets attachments', function() {
|
|
|
|
var promise = doc.getAttachments();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2014-05-19 06:35:29 +09:00
|
|
|
expect(data).toEqual(null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('gets javascript', function() {
|
|
|
|
var promise = doc.getJavaScript();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2014-05-19 06:35:29 +09:00
|
|
|
expect(data).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2015-07-21 01:25:02 +09:00
|
|
|
// Keep this in sync with the pattern in viewer.js. The pattern is used to
|
|
|
|
// detect whether or not to automatically start printing.
|
|
|
|
var viewerPrintRegExp = /\bprint\s*\(/;
|
|
|
|
it('gets javascript with printing instructions (Print action)', function() {
|
|
|
|
// PDF document with "Print" Named action in OpenAction
|
|
|
|
var pdfUrl = combineUrl(window.location.href, '../pdfs/bug1001080.pdf');
|
|
|
|
var promise = PDFJS.getDocument(pdfUrl).then(function(doc) {
|
|
|
|
return doc.getJavaScript();
|
|
|
|
});
|
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
|
|
|
expect(data).toEqual(['print({});']);
|
|
|
|
expect(data[0]).toMatch(viewerPrintRegExp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('gets javascript with printing instructions (JS action)', function() {
|
|
|
|
// PDF document with "JavaScript" action in OpenAction
|
|
|
|
var pdfUrl = combineUrl(window.location.href, '../pdfs/issue6106.pdf');
|
|
|
|
var promise = PDFJS.getDocument(pdfUrl).then(function(doc) {
|
|
|
|
return doc.getJavaScript();
|
|
|
|
});
|
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
|
|
|
expect(data).toEqual(
|
|
|
|
['this.print({bUI:true,bSilent:false,bShrinkToFit:true});']);
|
|
|
|
expect(data[0]).toMatch(viewerPrintRegExp);
|
|
|
|
});
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
it('gets outline', function() {
|
|
|
|
var promise = doc.getOutline();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(outline) {
|
2012-04-13 09:59:30 +09:00
|
|
|
// Two top level entries.
|
|
|
|
expect(outline.length).toEqual(2);
|
|
|
|
// Make sure some basic attributes are set.
|
|
|
|
expect(outline[1].title).toEqual('Chapter 1');
|
|
|
|
expect(outline[1].items.length).toEqual(1);
|
|
|
|
expect(outline[1].items[0].title).toEqual('Paragraph 1.1');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('gets metadata', function() {
|
|
|
|
var promise = doc.getMetadata();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(metadata) {
|
2012-04-13 09:59:30 +09:00
|
|
|
expect(metadata.info['Title']).toEqual('Basic API Test');
|
2015-05-25 11:45:20 +09:00
|
|
|
expect(metadata.info['PDFFormatVersion']).toEqual('1.7');
|
2012-04-13 09:59:30 +09:00
|
|
|
expect(metadata.metadata.get('dc:title')).toEqual('Basic API Test');
|
|
|
|
});
|
|
|
|
});
|
2014-05-14 18:57:48 +09:00
|
|
|
it('gets data', function() {
|
|
|
|
var promise = doc.getData();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2014-09-23 20:16:58 +09:00
|
|
|
expect(data instanceof Uint8Array).toEqual(true);
|
|
|
|
expect(data.length).toEqual(basicApiFileLength);
|
2014-05-14 18:57:48 +09:00
|
|
|
});
|
|
|
|
});
|
2015-06-26 05:51:17 +09:00
|
|
|
it('gets download info', function() {
|
2014-05-19 06:35:29 +09:00
|
|
|
var promise = doc.getDownloadInfo();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2015-06-26 05:51:17 +09:00
|
|
|
expect(data).toEqual({ length: basicApiFileLength });
|
2014-05-19 06:35:29 +09:00
|
|
|
});
|
|
|
|
});
|
2014-08-31 05:12:34 +09:00
|
|
|
it('gets stats', function() {
|
|
|
|
var promise = doc.getStats();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (stats) {
|
2015-06-26 05:51:17 +09:00
|
|
|
expect(stats).toEqual({ streamTypes: [], fontTypes: [] });
|
2014-08-31 05:12:34 +09:00
|
|
|
});
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
describe('Page', function() {
|
2014-01-04 09:17:05 +09:00
|
|
|
var resolvePromise;
|
|
|
|
var promise = new Promise(function (resolve) {
|
|
|
|
resolvePromise = resolve;
|
|
|
|
});
|
2015-06-26 05:51:17 +09:00
|
|
|
var pdfDocument;
|
2012-04-13 09:59:30 +09:00
|
|
|
PDFJS.getDocument(basicApiUrl).then(function(doc) {
|
|
|
|
doc.getPage(1).then(function(data) {
|
2014-01-04 09:17:05 +09:00
|
|
|
resolvePromise(data);
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
2015-06-26 05:51:17 +09:00
|
|
|
pdfDocument = doc;
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
var page;
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function(data) {
|
2013-06-06 04:28:31 +09:00
|
|
|
page = data;
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
2014-08-12 19:04:00 +09:00
|
|
|
it('gets page number', function () {
|
|
|
|
expect(page.pageNumber).toEqual(1);
|
|
|
|
});
|
|
|
|
it('gets rotate', function () {
|
|
|
|
expect(page.rotate).toEqual(0);
|
|
|
|
});
|
|
|
|
it('gets ref', function () {
|
|
|
|
expect(page.ref).toEqual({ num: 15, gen: 0 });
|
|
|
|
});
|
|
|
|
it('gets view', function () {
|
|
|
|
expect(page.view).toEqual([0, 0, 595.28, 841.89]);
|
|
|
|
});
|
|
|
|
it('gets viewport', function () {
|
|
|
|
var viewport = page.getViewport(1.5, 90);
|
|
|
|
expect(viewport.viewBox).toEqual(page.view);
|
|
|
|
expect(viewport.scale).toEqual(1.5);
|
|
|
|
expect(viewport.rotation).toEqual(90);
|
|
|
|
expect(viewport.transform).toEqual([0, 1.5, 1.5, 0, 0, 0]);
|
|
|
|
expect(viewport.width).toEqual(1262.835);
|
|
|
|
expect(viewport.height).toEqual(892.92);
|
|
|
|
});
|
|
|
|
it('gets annotations', function () {
|
|
|
|
var promise = page.getAnnotations();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2014-08-12 19:04:00 +09:00
|
|
|
expect(data.length).toEqual(4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('gets text content', function () {
|
|
|
|
var promise = page.getTextContent();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (data) {
|
2014-08-12 19:04:00 +09:00
|
|
|
expect(!!data.items).toEqual(true);
|
|
|
|
expect(data.items.length).toEqual(7);
|
|
|
|
expect(!!data.styles).toEqual(true);
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
2014-06-17 03:35:38 +09:00
|
|
|
it('gets operator list', function() {
|
|
|
|
var promise = page.getOperatorList();
|
2014-08-24 04:08:27 +09:00
|
|
|
waitsForPromiseResolved(promise, function (oplist) {
|
2014-06-17 03:35:38 +09:00
|
|
|
expect(!!oplist.fnArray).toEqual(true);
|
|
|
|
expect(!!oplist.argsArray).toEqual(true);
|
|
|
|
expect(oplist.lastChunk).toEqual(true);
|
|
|
|
});
|
|
|
|
});
|
2015-06-26 05:51:17 +09:00
|
|
|
it('gets stats after parsing page', function () {
|
|
|
|
var promise = page.getOperatorList().then(function () {
|
|
|
|
return pdfDocument.getStats();
|
|
|
|
});
|
|
|
|
var expectedStreamTypes = [];
|
|
|
|
expectedStreamTypes[StreamType.FLATE] = true;
|
|
|
|
var expectedFontTypes = [];
|
|
|
|
expectedFontTypes[FontType.TYPE1] = true;
|
|
|
|
expectedFontTypes[FontType.CIDFONTTYPE2] = true;
|
|
|
|
|
|
|
|
waitsForPromiseResolved(promise, function (stats) {
|
|
|
|
expect(stats).toEqual({ streamTypes: expectedStreamTypes,
|
|
|
|
fontTypes: expectedFontTypes });
|
|
|
|
});
|
|
|
|
});
|
2012-04-13 09:59:30 +09:00
|
|
|
});
|
|
|
|
});
|