diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index 0c9c56918..b183b9793 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -3,7 +3,7 @@ /* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor, InvalidPDFException, MissingPDFException, StreamType, FontType, PDFDocumentProxy, PasswordException, PasswordResponses, - PDFPageProxy */ + PDFPageProxy, createPromiseCapability */ 'use strict'; @@ -38,12 +38,31 @@ describe('api', function() { return data !== undefined; }, 20000); } + describe('PDFJS', function() { describe('getDocument', function() { it('creates pdf doc from URL', function() { - var promise = PDFJS.getDocument(basicApiUrl); - waitsForPromiseResolved(promise, function(data) { - expect(data instanceof PDFDocumentProxy).toEqual(true); + var loadingTask = PDFJS.getDocument(basicApiUrl); + + var isProgressReportedResolved = false; + var progressReportedCapability = createPromiseCapability(); + + // Attach the callback that is used to report loading progress; + // similarly to how viewer.js works. + loadingTask.onProgress = function (progressData) { + if (!isProgressReportedResolved) { + isProgressReportedResolved = true; + progressReportedCapability.resolve(progressData); + } + }; + + var promises = [ + progressReportedCapability.promise, + loadingTask.promise + ]; + waitsForPromiseResolved(Promise.all(promises), function (data) { + expect((data[0].loaded / data[0].total) > 0).toEqual(true); + expect(data[1] instanceof PDFDocumentProxy).toEqual(true); }); }); it('creates pdf doc from typed array', function() { @@ -101,28 +120,43 @@ describe('api', function() { 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 loadingTask = PDFJS.getDocument(url); - 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 isPasswordNeededResolved = false; + var passwordNeededCapability = createPromiseCapability(); + var isPasswordIncorrectResolved = false; + var passwordIncorrectCapability = createPromiseCapability(); - 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); - }); + // Attach the callback that is used to request a password; + // similarly to how viewer.js handles passwords. + loadingTask.onPassword = function (updatePassword, reason) { + if (reason === PasswordResponses.NEED_PASSWORD && + !isPasswordNeededResolved) { + isPasswordNeededResolved = true; + passwordNeededCapability.resolve(); - var passwordAcceptedPromise = PDFJS.getDocument({ - url: url, password: 'asdfasdf', - }); - waitsForPromiseResolved(passwordAcceptedPromise, function (data) { - expect(data instanceof PDFDocumentProxy).toEqual(true); + updatePassword('qwerty'); // Provide an incorrect password. + return; + } + if (reason === PasswordResponses.INCORRECT_PASSWORD && + !isPasswordIncorrectResolved) { + isPasswordIncorrectResolved = true; + passwordIncorrectCapability.resolve(); + + updatePassword('asdfasdf'); // Provide the correct password. + return; + } + // Shouldn't get here. + expect(false).toEqual(true); + }; + + var promises = [ + passwordNeededCapability.promise, + passwordIncorrectCapability.promise, + loadingTask.promise + ]; + waitsForPromiseResolved(Promise.all(promises), function (data) { + expect(data[2] instanceof PDFDocumentProxy).toEqual(true); }); }); it('creates pdf doc from PDF file protected with only a user password',