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 PDFPageProxy.stats (PR 9245 follow-up) #9839

Merged
merged 1 commit into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
77 changes: 74 additions & 3 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: [], });
Expand Down Expand Up @@ -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();
});
Expand All @@ -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.');
Expand Down