Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit-tests for the onPassword/onProgress callbacks of the API #6540

Merged
merged 1 commit into from
Oct 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 57 additions & 23 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/* globals PDFJS, expect, it, describe, Promise, combineUrl, waitsFor,
InvalidPDFException, MissingPDFException, StreamType, FontType,
PDFDocumentProxy, PasswordException, PasswordResponses,
PDFPageProxy */
PDFPageProxy, createPromiseCapability */

'use strict';

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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',
Expand Down