Skip to content

Commit

Permalink
[Bugfix] viewer freezes when switching document (#8820)
Browse files Browse the repository at this point in the history
  • Loading branch information
webviewer-ui committed Apr 30, 2024
1 parent bee7dd6 commit c5e10aa
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/helpers/TabManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default class TabManager {
return console.error(`Tab id not found: ${id}`);
}
if (currentTab) {
await core.getDocumentViewer().getAnnotationManager().exportAnnotations();
await core.getDocumentViewer().getAnnotationsLoadedPromise();
}
fireEvent(Events['BEFORE_TAB_CHANGED'], {
Expand Down
127 changes: 127 additions & 0 deletions tests/ui/tab-manager.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { expect } from 'chai';
import { setupWebViewerInstance, delay } from '../../utils/TestingUtils';

describe('Tab Manager UI Tests', function() {
this.timeout(10000);
let viewerDiv;

beforeEach(async () => {
// Create a new div with an ID and add it to the body before each test
viewerDiv = document.createElement('div');
viewerDiv.id = 'viewerDiv';
document.body.appendChild(viewerDiv);
});

afterEach(() => {
// Clean up the div after each test
document.body.removeChild(viewerDiv);
});

it('Test closing tab with enabled warning modal', async () => {
const options = {
initialDoc: ['https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf', '/base/test/fixtures/pdfs/large-number-annotations.pdf'],
fullAPI: true,
};
const instance = await setupWebViewerInstance(options);
const { annotationManager } = instance.Core;

const documentLoadedPromise = new Promise((resolve) => {
instance.UI.addEventListener('documentLoaded', resolve);
});
await documentLoadedPromise;

const annot = new instance.Core.Annotations.RectangleAnnotation({
PageNumber: 1,
X: 50,
Y: 100,
Width: 150,
Height: 100,
});

annotationManager.addAnnotation(annot);
annotationManager.redrawAnnotation(annot);

instance.UI.TabManager.deleteTab(0);

await delay(1000);
const iframeWindow = instance.UI.iframeWindow;

const warningModal = iframeWindow.document.querySelector('[aria-label="Close without downloading?"]');
expect(warningModal, 'Expecting a Warning Modal').to.not.be.null;
});

it('Test closing tab with disabled warning modal', async () => {
const options = {
initialDoc: ['https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf', '/base/test/fixtures/pdfs/large-number-annotations.pdf'],
fullAPI: true,
};
const instance = await setupWebViewerInstance(options);
const { annotationManager } = instance.Core;
instance.UI.disableFeatures([instance.UI.Feature.Download]);
const documentLoadedPromise = new Promise((resolve) => {
instance.UI.addEventListener('documentLoaded', resolve);
});
await documentLoadedPromise;

const annot = new instance.Core.Annotations.RectangleAnnotation({
PageNumber: 1,
X: 50,
Y: 100,
Width: 150,
Height: 100,
});

annotationManager.addAnnotation(annot);
annotationManager.redrawAnnotation(annot);

instance.UI.TabManager.deleteTab(0);

await delay(1000);
const iframeWindow = instance.UI.iframeWindow;

const warningModal = iframeWindow.document.querySelector('[aria-label="Close without downloading?"]');
expect(warningModal, 'Expecting no Warning Modal should appear').to.be.null;
});

it('Export XFDF on beforeTabChanged event', async () => {
const options = {
initialDoc: ['/base/test/fixtures/pdfs/VirtualizedAnnotTest.pdf', 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf'],
fullAPI: true,
};
const instance = await setupWebViewerInstance(options);
const { UI } = instance;
let resultXFDF;
const documentLoadedPromise = new Promise((resolve) => {
UI.addEventListener('documentLoaded', resolve);
});
await documentLoadedPromise;

UI.addEventListener('beforeTabChanged', async () => {
resultXFDF = await instance.Core.documentViewer.getAnnotationManager().exportAnnotations();
});

await UI.TabManager.setActiveTab(1);
await delay(1000);
expect(resultXFDF).to.not.be.undefined;
});

it('Switching between tabs with a big file won\'t cause lag', async () => {
const options = {
initialDoc: ['/base/test/fixtures/pdfs/semi-big-file.pdf', 'https://pdftron.s3.amazonaws.com/downloads/pl/demo-annotated.pdf'],
fullAPI: true,
};
const instance = await setupWebViewerInstance(options);
const { UI } = instance;

const documentLoadedPromise = new Promise((resolve) => {
UI.addEventListener('documentLoaded', resolve);
});
await documentLoadedPromise;

await UI.TabManager.setActiveTab(1);

await delay(2000);
const activeTab = UI.TabManager.getActiveTab();
expect(activeTab.options.filename).to.equal('demo-annotated.pdf');
});
})

0 comments on commit c5e10aa

Please sign in to comment.