Merge pull request #6540 from Snuffleupagus/getDocument-onPassword-onProgress-unittests

Add unit-tests for the `onPassword/onProgress` callbacks of the API
This commit is contained in:
Yury Delendik 2015-10-21 09:05:55 -04:00
commit f12d692c78

View File

@ -3,7 +3,7 @@
/* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor, /* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor,
InvalidPDFException, MissingPDFException, StreamType, FontType, InvalidPDFException, MissingPDFException, StreamType, FontType,
PDFDocumentProxy, PasswordException, PasswordResponses, PDFDocumentProxy, PasswordException, PasswordResponses,
PDFPageProxy */ PDFPageProxy, createPromiseCapability */
'use strict'; 'use strict';
@ -38,12 +38,31 @@ describe('api', function() {
return data !== undefined; return data !== undefined;
}, 20000); }, 20000);
} }
describe('PDFJS', function() { describe('PDFJS', function() {
describe('getDocument', function() { describe('getDocument', function() {
it('creates pdf doc from URL', function() { it('creates pdf doc from URL', function() {
var promise = PDFJS.getDocument(basicApiUrl); var loadingTask = PDFJS.getDocument(basicApiUrl);
waitsForPromiseResolved(promise, function(data) {
expect(data instanceof PDFDocumentProxy).toEqual(true); 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() { 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', it('creates pdf doc from PDF file protected with user and owner password',
function () { function () {
var url = combineUrl(window.location.href, '../pdfs/pr6531_1.pdf'); var url = combineUrl(window.location.href, '../pdfs/pr6531_1.pdf');
var loadingTask = PDFJS.getDocument(url);
var passwordNeededPromise = PDFJS.getDocument({ var isPasswordNeededResolved = false;
url: url, password: '', var passwordNeededCapability = createPromiseCapability();
}); var isPasswordIncorrectResolved = false;
waitsForPromiseRejected(passwordNeededPromise, function (data) { var passwordIncorrectCapability = createPromiseCapability();
expect(data instanceof PasswordException).toEqual(true);
expect(data.code).toEqual(PasswordResponses.NEED_PASSWORD);
});
var passwordIncorrectPromise = PDFJS.getDocument({ // Attach the callback that is used to request a password;
url: url, password: 'qwerty', // similarly to how viewer.js handles passwords.
}); loadingTask.onPassword = function (updatePassword, reason) {
waitsForPromiseRejected(passwordIncorrectPromise, function (data) { if (reason === PasswordResponses.NEED_PASSWORD &&
expect(data instanceof PasswordException).toEqual(true); !isPasswordNeededResolved) {
expect(data.code).toEqual(PasswordResponses.INCORRECT_PASSWORD); isPasswordNeededResolved = true;
}); passwordNeededCapability.resolve();
var passwordAcceptedPromise = PDFJS.getDocument({ updatePassword('qwerty'); // Provide an incorrect password.
url: url, password: 'asdfasdf', return;
}); }
waitsForPromiseResolved(passwordAcceptedPromise, function (data) { if (reason === PasswordResponses.INCORRECT_PASSWORD &&
expect(data instanceof PDFDocumentProxy).toEqual(true); !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', it('creates pdf doc from PDF file protected with only a user password',