[Pkg-javascript-commits] [pdf.js] 71/174: Add unit-tests for the `onPassword/onProgress` callbacks of the API

David Prévot taffit at moszumanska.debian.org
Thu Nov 19 18:45:15 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository pdf.js.

commit e04113a35b6e7ae6aea97826d3e43eb0596f26fb
Author: Jonas Jenwald <jonas.jenwald at gmail.com>
Date:   Sat Oct 17 16:08:10 2015 +0200

    Add unit-tests for the `onPassword/onProgress` callbacks of the API
    
    The standard viewer implicitly depends on the `onPassword` and `onProgress` callbacks, in order to open password protected PDF files, respectively to report file loading progress. We currently have no unit-tests for this functionality, which seems unfortunate; hence this patch.
    
    *Please note:* Rather than adding more unit-tests to `api_spec.js`, I slightly extended/reworked two existing tests. Specifically for the password test, this *does not* really change what we actually test, just how the test is done.
---
 test/unit/api_spec.js | 80 ++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 57 insertions(+), 23 deletions(-)

diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js
index 0c9c569..b183b97 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',

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/pdf.js.git



More information about the Pkg-javascript-commits mailing list