Commit d209add6 authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Split PDF annotations test files

The original tests in PDFExtensionJSTest.AnnotationsFeatureEnabled
tested the behaviors of the ink controller, and did not clear the DOM.

The new tests, which only test parts of the UI inside the toolbar, clear
the DOM, exposing the tests to potential flakiness if any calls are
made to the plugin near when the DOM is being cleared. When the DOM is
cleared, references to the plugin's members, including the postMessage()
function, vanish.

Bug: 1136319
Change-Id: I3e6b4dd8c1ef8a23281c100c4a8748d36e642365
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2478104
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#821114}
parent d0ba0483
...@@ -1024,6 +1024,12 @@ IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Printing) { ...@@ -1024,6 +1024,12 @@ IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, Printing) {
IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, MAYBE_AnnotationsFeatureEnabled) { IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, MAYBE_AnnotationsFeatureEnabled) {
RunTestsInJsModule("annotations_feature_enabled_test.js", "test.pdf"); RunTestsInJsModule("annotations_feature_enabled_test.js", "test.pdf");
} }
IN_PROC_BROWSER_TEST_P(PDFExtensionJSTest, AnnotationsToolbar) {
// Although this test file does not require a PDF to be loaded, loading the
// elements without loading a PDF is difficult.
RunTestsInJsModule("annotations_toolbar_test.js", "test.pdf");
}
#endif // defined(OS_CHROMEOS) #endif // defined(OS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P(/* no prefix */, PDFExtensionJSTest, testing::Bool()); INSTANTIATE_TEST_SUITE_P(/* no prefix */, PDFExtensionJSTest, testing::Bool());
......
...@@ -2,9 +2,8 @@ ...@@ -2,9 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import {eventToPromise, waitBeforeNextRender} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/_test_resources/webui/test_util.m.js';
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js'; import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {testAsync} from './test_util.js'; import {testAsync, waitFor} from './test_util.js';
window.onerror = e => chrome.test.fail(e.stack); window.onerror = e => chrome.test.fail(e.stack);
window.onunhandledrejection = e => chrome.test.fail(e.reason); window.onunhandledrejection = e => chrome.test.fail(e.reason);
...@@ -13,16 +12,6 @@ function animationFrame() { ...@@ -13,16 +12,6 @@ function animationFrame() {
return new Promise(resolve => requestAnimationFrame(resolve)); return new Promise(resolve => requestAnimationFrame(resolve));
} }
// Async spin until predicate() returns true.
function waitFor(predicate) {
if (predicate()) {
return;
}
return new Promise(resolve => setTimeout(() => {
resolve(waitFor(predicate));
}, 0));
}
function contentElement() { function contentElement() {
return viewer.shadowRoot.elementFromPoint(innerWidth / 2, innerHeight / 2); return viewer.shadowRoot.elementFromPoint(innerWidth / 2, innerHeight / 2);
} }
...@@ -385,108 +374,4 @@ chrome.test.runTests([ ...@@ -385,108 +374,4 @@ chrome.test.runTests([
chrome.test.assertEq('EMBED', contentElement().tagName); chrome.test.assertEq('EMBED', contentElement().tagName);
}); });
}, },
function testHidingAnnotationsExitsAnnotationsMode() {
testAsync(async () => {
document.body.innerHTML = '';
const toolbar = document.createElement('viewer-pdf-toolbar-new');
document.body.appendChild(toolbar);
toolbar.toggleAnnotation();
// This is normally done by the parent in response to the event fired by
// toggleAnnotation().
toolbar.annotationMode = true;
await toolbar.addEventListener('display-annotations-changed', async e => {
chrome.test.assertFalse(e.detail);
await waitFor(() => toolbar.annotationMode === false);
chrome.test.succeed();
});
toolbar.shadowRoot.querySelector('#show-annotations-button').click();
});
},
function testEnteringAnnotationsModeShowsAnnotations() {
document.body.innerHTML = '';
const toolbar = document.createElement('viewer-pdf-toolbar-new');
document.body.appendChild(toolbar);
chrome.test.assertFalse(toolbar.annotationMode);
// Hide annotations.
toolbar.shadowRoot.querySelector('#show-annotations-button').click();
toolbar.addEventListener('annotation-mode-toggled', e => {
chrome.test.assertTrue(e.detail);
chrome.test.succeed();
});
toolbar.toggleAnnotation();
},
function testEnteringAnnotationsModeDisablesTwoUp() {
document.body.innerHTML = '';
const toolbar = document.createElement('viewer-pdf-toolbar-new');
document.body.appendChild(toolbar);
chrome.test.assertFalse(toolbar.annotationMode);
toolbar.toggleAnnotation();
// This is normally done by the parent in response to the event fired by
// toggleAnnotation().
toolbar.annotationMode = true;
chrome.test.assertTrue(
toolbar.shadowRoot.querySelector('#two-page-view-button').disabled);
chrome.test.succeed();
},
function testRotateOrTwoUpViewTriggersDialog() {
document.body.innerHTML = '';
const toolbar = document.createElement('viewer-pdf-toolbar-new');
document.body.appendChild(toolbar);
toolbar.annotationAvailable = true;
toolbar.pdfAnnotationsEnabled = true;
toolbar.rotated = false;
toolbar.twoUpViewEnabled = false;
testAsync(async () => {
await waitBeforeNextRender(toolbar);
chrome.test.assertFalse(toolbar.annotationMode);
// If rotation is enabled clicking the button shows the dialog.
toolbar.rotated = true;
const annotateButton = toolbar.shadowRoot.querySelector('#annotate');
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
let dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
// Cancel the dialog.
const whenClosed = eventToPromise('close', dialog);
dialog.shadowRoot.querySelector('.cancel-button').click();
chrome.test.assertFalse(dialog.isOpen());
await whenClosed;
// If both two up and rotate are enabled, the dialog opens.
toolbar.twoUpViewEnabled = true;
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
// When "Edit" is clicked, the toolbar should fire
// annotation-mode-dialog-confirmed.
const whenConfirmed =
eventToPromise('annotation-mode-dialog-confirmed', toolbar);
dialog.shadowRoot.querySelector('.action-button').click();
await whenConfirmed;
chrome.test.assertFalse(dialog.isOpen());
await waitBeforeNextRender(toolbar);
// Dialog shows in two up view (un-rotated).
toolbar.rotated = false;
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
});
}
]); ]);
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {eventToPromise, waitBeforeNextRender} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/_test_resources/webui/test_util.m.js';
import {ViewerPdfToolbarNewElement} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/elements/viewer-pdf-toolbar-new.js';
import {testAsync, waitFor} from './test_util.js';
/** @return {!ViewerPdfToolbarNewElement} */
function createToolbar() {
document.body.innerHTML = '';
const toolbar = /** @type {!ViewerPdfToolbarNewElement} */ (
document.createElement('viewer-pdf-toolbar-new'));
document.body.appendChild(toolbar);
return toolbar;
}
const tests = [
function testHidingAnnotationsExitsAnnotationsMode() {
testAsync(async () => {
const toolbar = createToolbar();
toolbar.toggleAnnotation();
// This is normally done by the parent in response to the event fired by
// toggleAnnotation().
toolbar.annotationMode = true;
await toolbar.addEventListener('display-annotations-changed', async e => {
chrome.test.assertFalse(e.detail);
await waitFor(() => toolbar.annotationMode === false);
chrome.test.succeed();
});
toolbar.shadowRoot.querySelector('#show-annotations-button').click();
});
},
function testEnteringAnnotationsModeShowsAnnotations() {
const toolbar = createToolbar();
chrome.test.assertFalse(toolbar.annotationMode);
// Hide annotations.
toolbar.shadowRoot.querySelector('#show-annotations-button').click();
toolbar.addEventListener('annotation-mode-toggled', e => {
chrome.test.assertTrue(e.detail);
chrome.test.succeed();
});
toolbar.toggleAnnotation();
},
function testEnteringAnnotationsModeDisablesTwoUp() {
const toolbar = createToolbar();
chrome.test.assertFalse(toolbar.annotationMode);
toolbar.toggleAnnotation();
// This is normally done by the parent in response to the event fired by
// toggleAnnotation().
toolbar.annotationMode = true;
chrome.test.assertTrue(
toolbar.shadowRoot.querySelector('#two-page-view-button').disabled);
chrome.test.succeed();
},
function testRotateOrTwoUpViewTriggersDialog() {
const toolbar = createToolbar();
toolbar.annotationAvailable = true;
toolbar.pdfAnnotationsEnabled = true;
toolbar.rotated = false;
toolbar.twoUpViewEnabled = false;
testAsync(async () => {
await waitBeforeNextRender(toolbar);
chrome.test.assertFalse(toolbar.annotationMode);
// If rotation is enabled clicking the button shows the dialog.
toolbar.rotated = true;
const annotateButton = toolbar.shadowRoot.querySelector('#annotate');
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
let dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
// Cancel the dialog.
const whenClosed = eventToPromise('close', dialog);
dialog.shadowRoot.querySelector('.cancel-button').click();
chrome.test.assertFalse(dialog.isOpen());
await whenClosed;
// If both two up and rotate are enabled, the dialog opens.
toolbar.twoUpViewEnabled = true;
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
// When "Edit" is clicked, the toolbar should fire
// annotation-mode-dialog-confirmed.
const whenConfirmed =
eventToPromise('annotation-mode-dialog-confirmed', toolbar);
dialog.shadowRoot.querySelector('.action-button').click();
await whenConfirmed;
chrome.test.assertFalse(dialog.isOpen());
await waitBeforeNextRender(toolbar);
// Dialog shows in two up view (un-rotated).
toolbar.rotated = false;
chrome.test.assertFalse(annotateButton.disabled);
annotateButton.click();
await waitBeforeNextRender(toolbar);
dialog =
toolbar.shadowRoot.querySelector('viewer-annotations-mode-dialog');
chrome.test.assertTrue(dialog.isOpen());
});
}
];
chrome.test.runTests(tests);
...@@ -332,3 +332,17 @@ export async function testAsync(f) { ...@@ -332,3 +332,17 @@ export async function testAsync(f) {
chrome.test.fail(e.stack); chrome.test.fail(e.stack);
} }
} }
/**
* Async spin until predicate() returns true.
* @param {function(): boolean} predicate
* @return {!Promise|undefined}
*/
export function waitFor(predicate) {
if (predicate()) {
return;
}
return new Promise(resolve => setTimeout(() => {
resolve(waitFor(predicate));
}, 0));
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment