@ -3,7 +3,7 @@
@@ -3,7 +3,7 @@
/ * g l o b a l s P D F J S , e x p e c t , i t , d e s c r i b e , P r o m i s e , c o m b i n e U r l , w a i t s F o r ,
InvalidPDFException , MissingPDFException , StreamType , FontType ,
PDFDocumentProxy , PasswordException , PasswordResponses ,
PDFPageProxy * /
PDFPageProxy , createPromiseCapability * /
'use strict' ;
@ -38,12 +38,31 @@ describe('api', function() {
@@ -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() {
@@ -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' ,