From 4cbe5a3baaa32c2a3a18e39e90949f2881b68657 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 25 Jun 2018 13:19:29 +0200 Subject: [PATCH] Add unit-tests for `PDFPageProxy.stats` (PR 9245 follow-up) This wasn't included in PR 9245, since all the API options were still global at that time. Writing the unit-tests also uncovered an issue with `getOperatorList` not starting the "Page Request" timer. --- src/display/api.js | 1 + test/unit/api_spec.js | 77 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index c2db27f09af85..ede847f9e18da 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1037,6 +1037,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() { lastChunk: false, }; + this._stats.time('Page Request'); this.transport.messageHandler.send('RenderPageRequest', { pageIndex: this.pageIndex, intent: renderingIntent, diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index d8532f02bb04f..fcb1558be4eaf 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -21,7 +21,7 @@ import { OPS, PasswordException, PasswordResponses, StreamType, stringToBytes } from '../../src/shared/util'; import { - DOMCanvasFactory, RenderingCancelledException + DOMCanvasFactory, RenderingCancelledException, StatTimer } from '../../src/display/dom_utils'; import { getDocument, PDFDataRangeTransport, PDFDocumentProxy, PDFPageProxy, PDFWorker @@ -831,7 +831,7 @@ describe('api', function() { done.fail(reason); }); }); - it('gets stats', function(done) { + it('gets document stats', function(done) { var promise = doc.getStats(); promise.then(function (stats) { expect(stats).toEqual({ streamTypes: [], fontTypes: [], }); @@ -1141,7 +1141,7 @@ describe('api', function() { done.fail(reason); }); }); - it('gets stats after parsing page', function (done) { + it('gets document stats after parsing page', function(done) { var promise = page.getOperatorList().then(function () { return pdfDocument.getStats(); }); @@ -1160,6 +1160,77 @@ describe('api', function() { }); }); + it('gets page stats after parsing page, without `pdfBug` set', + function(done) { + page.getOperatorList().then((opList) => { + return page.stats; + }).then((stats) => { + expect(stats).toEqual(null); + done(); + }, done.fail); + }); + it('gets page stats after parsing page, with `pdfBug` set', function(done) { + let loadingTask = getDocument( + buildGetDocumentParams(basicApiFileName, { pdfBug: true, })); + + loadingTask.promise.then((pdfDoc) => { + return pdfDoc.getPage(1).then((pdfPage) => { + return pdfPage.getOperatorList().then((opList) => { + return pdfPage.stats; + }); + }); + }).then((stats) => { + expect(stats instanceof StatTimer).toEqual(true); + expect(stats.times.length).toEqual(1); + + let [statEntry] = stats.times; + expect(statEntry.name).toEqual('Page Request'); + expect(statEntry.end - statEntry.start).toBeGreaterThan(0); + + loadingTask.destroy().then(done); + }, done.fail); + }); + it('gets page stats after rendering page, with `pdfBug` set', + function(done) { + if (isNodeJS()) { + pending('TODO: Support Canvas testing in Node.js.'); + } + let loadingTask = getDocument( + buildGetDocumentParams(basicApiFileName, { pdfBug: true, })); + let canvasAndCtx; + + loadingTask.promise.then((pdfDoc) => { + return pdfDoc.getPage(1).then((pdfPage) => { + let viewport = pdfPage.getViewport(1); + canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height); + + let renderTask = pdfPage.render({ + canvasContext: canvasAndCtx.context, + viewport, + }); + return renderTask.promise.then(() => { + return pdfPage.stats; + }); + }); + }).then((stats) => { + expect(stats instanceof StatTimer).toEqual(true); + expect(stats.times.length).toEqual(3); + + let [statEntryOne, statEntryTwo, statEntryThree] = stats.times; + expect(statEntryOne.name).toEqual('Page Request'); + expect(statEntryOne.end - statEntryOne.start).toBeGreaterThan(0); + + expect(statEntryTwo.name).toEqual('Rendering'); + expect(statEntryTwo.end - statEntryTwo.start).toBeGreaterThan(0); + + expect(statEntryThree.name).toEqual('Overall'); + expect(statEntryThree.end - statEntryThree.start).toBeGreaterThan(0); + + CanvasFactory.destroy(canvasAndCtx); + loadingTask.destroy().then(done); + }, done.fail); + }); + it('cancels rendering of page', function(done) { if (isNodeJS()) { pending('TODO: Support Canvas testing in Node.js.');