Commit 25ca0549 authored by dpapad's avatar dpapad Committed by Commit Bot

PDF Viewer: Remove unnecessary test helper testAsync().

Instead, the "async" keyword is directly used on the functions that are
passed to the testing framework.

Bug: None
Change-Id: Ife6c66a85cf60cf2bc3d9ec47a33d088c44b38ee
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2531861
Commit-Queue: dpapad <dpapad@chromium.org>
Reviewed-by: default avatarRebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#826708}
parent 2271ebc3
......@@ -117,7 +117,6 @@ js_library("material_elements_test") {
js_library("download_controls_test") {
deps = [
":test_util",
"//chrome/browser/resources/pdf:constants",
"//chrome/browser/resources/pdf/elements:viewer-download-controls",
"//third_party/polymer/v3_0/components-chromium/polymer:polymer_bundled",
......@@ -202,7 +201,6 @@ js_library("viewer_pdf_toolbar_new_test") {
js_library("viewer_thumbnail_bar_test") {
deps = [
":test_util",
"../webui:test_util.m",
"//chrome/browser/resources/pdf:controller",
"//chrome/browser/resources/pdf/elements:viewer-thumbnail",
......
......@@ -3,7 +3,7 @@
// found in the LICENSE file.
import {loadTimeData} from 'chrome://resources/js/load_time_data.m.js';
import {testAsync, waitFor} from './test_util.js';
import {waitFor} from './test_util.js';
window.onerror = e => chrome.test.fail(e.stack);
window.onunhandledrejection = e => chrome.test.fail(e.reason);
......@@ -28,350 +28,341 @@ chrome.test.runTests([
toolbar.shadowRoot.querySelector('#annotate') != null);
chrome.test.succeed();
},
function testEnterAnnotationMode() {
testAsync(async () => {
chrome.test.assertEq('EMBED', contentElement().tagName);
// Enter annotation mode.
viewer.shadowRoot.querySelector('#toolbar').toggleAnnotation();
await viewer.loaded;
chrome.test.assertEq('VIEWER-INK-HOST', contentElement().tagName);
});
async function testEnterAnnotationMode() {
chrome.test.assertEq('EMBED', contentElement().tagName);
// Enter annotation mode.
viewer.shadowRoot.querySelector('#toolbar').toggleAnnotation();
await viewer.loaded;
chrome.test.assertEq('VIEWER-INK-HOST', contentElement().tagName);
chrome.test.succeed();
},
function testViewportToCameraConversion() {
testAsync(async () => {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
const cameras = [];
inkHost.ink_.setCamera = camera => cameras.push(camera);
viewer.viewport.setZoom(1);
viewer.viewport.setZoom(2);
chrome.test.assertEq(2, cameras.length);
const updateEnabled =
document.documentElement.hasAttribute('pdf-viewer-update-enabled');
const scrollingContainer =
updateEnabled ? viewer.shadowRoot.querySelector('#scroller') : window;
scrollingContainer.scrollTo(100, 100);
await animationFrame();
chrome.test.assertEq(3, cameras.length);
const expectations = [
{top: 44.25, left: -106.5, right: 718.5, bottom: -448.5},
{top: 23.25, left: -3.75, right: 408.75, bottom: -223.125},
{top: -14.25, left: 33.75, right: 446.25, bottom: -260.625},
];
for (const expectation of expectations) {
const actual = cameras.shift();
const expectationTop = updateEnabled ?
Math.min(2.25, expectation.top - 21) :
expectation.top;
chrome.test.assertEq(expectationTop, actual.top);
chrome.test.assertEq(expectation.left, actual.left);
chrome.test.assertEq(expectation.bottom, actual.bottom);
chrome.test.assertEq(expectation.right, actual.right);
}
});
async function testViewportToCameraConversion() {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
const cameras = [];
inkHost.ink_.setCamera = camera => cameras.push(camera);
viewer.viewport.setZoom(1);
viewer.viewport.setZoom(2);
chrome.test.assertEq(2, cameras.length);
const updateEnabled =
document.documentElement.hasAttribute('pdf-viewer-update-enabled');
const scrollingContainer =
updateEnabled ? viewer.shadowRoot.querySelector('#scroller') : window;
scrollingContainer.scrollTo(100, 100);
await animationFrame();
chrome.test.assertEq(3, cameras.length);
const expectations = [
{top: 44.25, left: -106.5, right: 718.5, bottom: -448.5},
{top: 23.25, left: -3.75, right: 408.75, bottom: -223.125},
{top: -14.25, left: 33.75, right: 446.25, bottom: -260.625},
];
for (const expectation of expectations) {
const actual = cameras.shift();
const expectationTop = updateEnabled ?
Math.min(2.25, expectation.top - 21) :
expectation.top;
chrome.test.assertEq(expectationTop, actual.top);
chrome.test.assertEq(expectation.left, actual.left);
chrome.test.assertEq(expectation.bottom, actual.bottom);
chrome.test.assertEq(expectation.right, actual.right);
}
chrome.test.succeed();
},
function testPenOptions() {
testAsync(async () => {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
let tool = null;
inkHost.ink_.setAnnotationTool = value => tool = value;
// Pen defaults.
const viewerPdfToolbar = viewer.shadowRoot.querySelector('#toolbar');
const viewerAnnotationsBar =
viewerPdfToolbar.shadowRoot.querySelector('viewer-annotations-bar');
const pen = viewerAnnotationsBar.shadowRoot.querySelector('#pen');
pen.click();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(0.1429, tool.size);
chrome.test.assertEq('#000000', tool.color);
// Selected size and color.
const penOptions = viewerAnnotationsBar.shadowRoot.querySelector(
'#pen viewer-pen-options');
penOptions.$$('#sizes [value="1"]').click();
penOptions.$$('#colors [value="#00b0ff"]').click();
await animationFrame();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#00b0ff', tool.color);
// Eraser defaults.
viewerAnnotationsBar.shadowRoot.querySelector('#eraser').click();
chrome.test.assertEq('eraser', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq(null, tool.color);
// Pen keeps previous settings.
pen.click();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#00b0ff', tool.color);
// Highlighter defaults.
viewerAnnotationsBar.shadowRoot.querySelector('#highlighter').click();
chrome.test.assertEq('highlighter', tool.tool);
chrome.test.assertEq(0.7143, tool.size);
chrome.test.assertEq('#ffbc00', tool.color);
// Need to expand to use this color.
const highlighterOptions = viewerAnnotationsBar.shadowRoot.querySelector(
'#highlighter viewer-pen-options');
highlighterOptions.$$('#colors [value="#d1c4e9"]').click();
chrome.test.assertEq('#ffbc00', tool.color);
// Selected size and expanded color.
highlighterOptions.$$('#sizes [value="1"]').click();
highlighterOptions.$$('#colors #expand').click();
highlighterOptions.$$('#colors [value="#d1c4e9"]').click();
chrome.test.assertEq('highlighter', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#d1c4e9', tool.color);
});
async function testPenOptions() {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
let tool = null;
inkHost.ink_.setAnnotationTool = value => tool = value;
// Pen defaults.
const viewerPdfToolbar = viewer.shadowRoot.querySelector('#toolbar');
const viewerAnnotationsBar =
viewerPdfToolbar.shadowRoot.querySelector('viewer-annotations-bar');
const pen = viewerAnnotationsBar.shadowRoot.querySelector('#pen');
pen.click();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(0.1429, tool.size);
chrome.test.assertEq('#000000', tool.color);
// Selected size and color.
const penOptions = viewerAnnotationsBar.shadowRoot.querySelector(
'#pen viewer-pen-options');
penOptions.$$('#sizes [value="1"]').click();
penOptions.$$('#colors [value="#00b0ff"]').click();
await animationFrame();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#00b0ff', tool.color);
// Eraser defaults.
viewerAnnotationsBar.shadowRoot.querySelector('#eraser').click();
chrome.test.assertEq('eraser', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq(null, tool.color);
// Pen keeps previous settings.
pen.click();
chrome.test.assertEq('pen', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#00b0ff', tool.color);
// Highlighter defaults.
viewerAnnotationsBar.shadowRoot.querySelector('#highlighter').click();
chrome.test.assertEq('highlighter', tool.tool);
chrome.test.assertEq(0.7143, tool.size);
chrome.test.assertEq('#ffbc00', tool.color);
// Need to expand to use this color.
const highlighterOptions = viewerAnnotationsBar.shadowRoot.querySelector(
'#highlighter viewer-pen-options');
highlighterOptions.$$('#colors [value="#d1c4e9"]').click();
chrome.test.assertEq('#ffbc00', tool.color);
// Selected size and expanded color.
highlighterOptions.$$('#sizes [value="1"]').click();
highlighterOptions.$$('#colors #expand').click();
highlighterOptions.$$('#colors [value="#d1c4e9"]').click();
chrome.test.assertEq('highlighter', tool.tool);
chrome.test.assertEq(1, tool.size);
chrome.test.assertEq('#d1c4e9', tool.color);
chrome.test.succeed();
},
function testStrokeUndoRedo() {
testAsync(async () => {
const inkHost = contentElement();
const viewerPdfToolbar = viewer.shadowRoot.querySelector('#toolbar');
const viewerAnnotationsBar =
viewerPdfToolbar.shadowRoot.querySelector('viewer-annotations-bar');
const undo = viewerAnnotationsBar.shadowRoot.querySelector('#undo');
const redo = viewerAnnotationsBar.shadowRoot.querySelector('#redo');
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: inkHost.offsetWidth / 2,
clientY: inkHost.offsetHeight / 2,
buttons: 0,
};
// Initial state.
chrome.test.assertEq(undo.disabled, true);
chrome.test.assertEq(redo.disabled, true);
// Draw a stroke.
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
await waitFor(() => undo.disabled === false);
chrome.test.assertEq(redo.disabled, true);
undo.click();
await waitFor(() => undo.disabled === true);
chrome.test.assertEq(redo.disabled, false);
redo.click();
await waitFor(() => undo.disabled === false);
chrome.test.assertEq(redo.disabled, true);
});
async function testStrokeUndoRedo() {
const inkHost = contentElement();
const viewerPdfToolbar = viewer.shadowRoot.querySelector('#toolbar');
const viewerAnnotationsBar =
viewerPdfToolbar.shadowRoot.querySelector('viewer-annotations-bar');
const undo = viewerAnnotationsBar.shadowRoot.querySelector('#undo');
const redo = viewerAnnotationsBar.shadowRoot.querySelector('#redo');
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: inkHost.offsetWidth / 2,
clientY: inkHost.offsetHeight / 2,
buttons: 0,
};
// Initial state.
chrome.test.assertEq(undo.disabled, true);
chrome.test.assertEq(redo.disabled, true);
// Draw a stroke.
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
await waitFor(() => undo.disabled === false);
chrome.test.assertEq(redo.disabled, true);
undo.click();
await waitFor(() => undo.disabled === true);
chrome.test.assertEq(redo.disabled, false);
redo.click();
await waitFor(() => undo.disabled === false);
chrome.test.assertEq(redo.disabled, true);
chrome.test.succeed();
},
function testPointerEvents() {
testAsync(async () => {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
inkHost.resetPenMode();
const events = [];
inkHost.ink_.dispatchPointerEvent = (ev) => void events.push(ev);
const mouse = {pointerId: 1, pointerType: 'mouse', buttons: 1};
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: 3,
clientY: 4,
buttons: 0,
};
const touch1 = {pointerId: 11, pointerType: 'touch'};
const touch2 = {pointerId: 22, pointerType: 'touch'};
function checkExpectations(expectations) {
chrome.test.assertEq(expectations.length, events.length);
while (expectations.length) {
const event = events.shift();
const expectation = expectations.shift();
chrome.test.assertEq(expectation.type, event.type);
for (const key of Object.keys(expectation.init)) {
chrome.test.assertEq(expectation.init[key], event[key]);
}
async function testPointerEvents() {
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
inkHost.resetPenMode();
const events = [];
inkHost.ink_.dispatchPointerEvent = (ev) => void events.push(ev);
const mouse = {pointerId: 1, pointerType: 'mouse', buttons: 1};
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: 3,
clientY: 4,
buttons: 0,
};
const touch1 = {pointerId: 11, pointerType: 'touch'};
const touch2 = {pointerId: 22, pointerType: 'touch'};
function checkExpectations(expectations) {
chrome.test.assertEq(expectations.length, events.length);
while (expectations.length) {
const event = events.shift();
const expectation = expectations.shift();
chrome.test.assertEq(expectation.type, event.type);
for (const key of Object.keys(expectation.init)) {
chrome.test.assertEq(expectation.init[key], event[key]);
}
}
// Normal sequence.
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: pen},
{type: 'pointermove', init: pen},
{type: 'pointerup', init: pen},
]);
// Multi-touch gesture should cancel and suppress first pointer.
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch2));
inkHost.dispatchEvent(new PointerEvent('pointermove', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerup', touch1));
checkExpectations([
{type: 'pointerdown', init: touch1},
{type: 'pointercancel', init: touch1},
]);
// Pointers which are not active should be suppressed.
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', mouse));
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointermove', mouse));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerup', mouse));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
checkExpectations([
{type: 'pointerdown', init: mouse},
{type: 'pointermove', init: mouse},
{type: 'pointerup', init: mouse},
]);
// pointerleave should cause mouseup
inkHost.dispatchEvent(new PointerEvent('pointerdown', mouse));
inkHost.dispatchEvent(new PointerEvent('pointerleave', mouse));
checkExpectations([
{type: 'pointerdown', init: mouse},
{type: 'pointerup', init: mouse},
]);
// pointerleave does not apply to non-mouse pointers
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerleave', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: pen},
{type: 'pointerup', init: pen},
]);
// Browser will cancel touch on pen input
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointercancel', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: touch1},
{type: 'pointercancel', init: touch1},
{type: 'pointerdown', init: pen},
{type: 'pointerup', init: pen},
]);
});
}
// Normal sequence.
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: pen},
{type: 'pointermove', init: pen},
{type: 'pointerup', init: pen},
]);
// Multi-touch gesture should cancel and suppress first pointer.
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch2));
inkHost.dispatchEvent(new PointerEvent('pointermove', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerup', touch1));
checkExpectations([
{type: 'pointerdown', init: touch1},
{type: 'pointercancel', init: touch1},
]);
// Pointers which are not active should be suppressed.
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', mouse));
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointermove', mouse));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
inkHost.dispatchEvent(new PointerEvent('pointermove', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerup', mouse));
inkHost.dispatchEvent(new PointerEvent('pointermove', pen));
checkExpectations([
{type: 'pointerdown', init: mouse},
{type: 'pointermove', init: mouse},
{type: 'pointerup', init: mouse},
]);
// pointerleave should cause mouseup
inkHost.dispatchEvent(new PointerEvent('pointerdown', mouse));
inkHost.dispatchEvent(new PointerEvent('pointerleave', mouse));
checkExpectations([
{type: 'pointerdown', init: mouse},
{type: 'pointerup', init: mouse},
]);
// pointerleave does not apply to non-mouse pointers
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerleave', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: pen},
{type: 'pointerup', init: pen},
]);
// Browser will cancel touch on pen input
inkHost.resetPenMode();
inkHost.dispatchEvent(new PointerEvent('pointerdown', touch1));
inkHost.dispatchEvent(new PointerEvent('pointercancel', touch1));
inkHost.dispatchEvent(new PointerEvent('pointerdown', pen));
inkHost.dispatchEvent(new PointerEvent('pointerup', pen));
checkExpectations([
{type: 'pointerdown', init: touch1},
{type: 'pointercancel', init: touch1},
{type: 'pointerdown', init: pen},
{type: 'pointerup', init: pen},
]);
chrome.test.succeed();
},
function testTouchPanGestures() {
testAsync(async () => {
// Ensure that we have an out-of-bounds area.
viewer.viewport_.setZoom(0.5);
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
function dispatchPointerEvent(type, init) {
const pointerEvent = new PointerEvent(type, init);
inkHost.dispatchEvent(pointerEvent);
return pointerEvent;
}
function dispatchPointerAndTouchEvents(type, init) {
const pointerEvent = dispatchPointerEvent(type, init);
let touchPrevented = false;
inkHost.onTouchStart_({
timeStamp: pointerEvent.timeStamp,
preventDefault() {
touchPrevented = true;
}
});
return touchPrevented;
}
async function testTouchPanGestures() {
// Ensure that we have an out-of-bounds area.
viewer.viewport_.setZoom(0.5);
chrome.test.assertTrue(isAnnotationMode());
const inkHost = contentElement();
function dispatchPointerEvent(type, init) {
const pointerEvent = new PointerEvent(type, init);
inkHost.dispatchEvent(pointerEvent);
return pointerEvent;
}
function dispatchPointerAndTouchEvents(type, init) {
const pointerEvent = dispatchPointerEvent(type, init);
let touchPrevented = false;
inkHost.onTouchStart_({
timeStamp: pointerEvent.timeStamp,
preventDefault() {
touchPrevented = true;
}
});
return touchPrevented;
}
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: innerWidth / 2,
clientY: innerHeight / 2,
};
const outOfBoundsPen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: 2,
clientY: 3,
};
const touch = {
pointerId: 3,
pointerType: 'touch',
pressure: 0.5,
clientX: innerWidth / 2,
clientY: innerHeight / 2,
};
const outOfBoundsTouch = {
pointerId: 4,
pointerType: 'touch',
pressure: 0.5,
clientX: 4,
clientY: 5,
};
inkHost.resetPenMode();
let prevented = dispatchPointerAndTouchEvents('pointerdown', touch);
dispatchPointerEvent('pointerup', touch);
chrome.test.assertTrue(
prevented, 'in document touch should prevent default');
prevented = dispatchPointerAndTouchEvents('pointerdown', outOfBoundsTouch);
dispatchPointerEvent('pointerup', outOfBoundsTouch);
chrome.test.assertFalse(
prevented, 'out of bounds touch should start gesture');
prevented = dispatchPointerAndTouchEvents('pointerdown', pen);
dispatchPointerEvent('pointerup', pen);
chrome.test.assertTrue(prevented, 'in document pen should prevent default');
const pen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: innerWidth / 2,
clientY: innerHeight / 2,
};
const outOfBoundsPen = {
pointerId: 2,
pointerType: 'pen',
pressure: 0.5,
clientX: 2,
clientY: 3,
};
const touch = {
pointerId: 3,
pointerType: 'touch',
pressure: 0.5,
clientX: innerWidth / 2,
clientY: innerHeight / 2,
};
const outOfBoundsTouch = {
pointerId: 4,
pointerType: 'touch',
pressure: 0.5,
clientX: 4,
clientY: 5,
};
inkHost.resetPenMode();
let prevented = dispatchPointerAndTouchEvents('pointerdown', touch);
dispatchPointerEvent('pointerup', touch);
chrome.test.assertTrue(
prevented, 'in document touch should prevent default');
prevented =
dispatchPointerAndTouchEvents('pointerdown', outOfBoundsTouch);
dispatchPointerEvent('pointerup', outOfBoundsTouch);
chrome.test.assertFalse(
prevented, 'out of bounds touch should start gesture');
prevented = dispatchPointerAndTouchEvents('pointerdown', pen);
dispatchPointerEvent('pointerup', pen);
chrome.test.assertTrue(
prevented, 'in document pen should prevent default');
prevented = dispatchPointerAndTouchEvents('pointerdown', outOfBoundsPen);
dispatchPointerEvent('pointerup', outOfBoundsPen);
chrome.test.assertFalse(
prevented, 'out of bounds pen should start gesture');
chrome.test.assertTrue(
inkHost.penMode_, 'pen input should switch to pen mode');
prevented = dispatchPointerAndTouchEvents('pointerdown', touch);
dispatchPointerEvent('pointerup', touch);
chrome.test.assertFalse(
prevented, 'in document touch in pen mode should start gesture');
});
prevented = dispatchPointerAndTouchEvents('pointerdown', outOfBoundsPen);
dispatchPointerEvent('pointerup', outOfBoundsPen);
chrome.test.assertFalse(
prevented, 'out of bounds pen should start gesture');
chrome.test.assertTrue(
inkHost.penMode_, 'pen input should switch to pen mode');
prevented = dispatchPointerAndTouchEvents('pointerdown', touch);
dispatchPointerEvent('pointerup', touch);
chrome.test.assertFalse(
prevented, 'in document touch in pen mode should start gesture');
chrome.test.succeed();
},
function testExitAnnotationMode() {
testAsync(async () => {
chrome.test.assertTrue(isAnnotationMode());
// Exit annotation mode.
viewer.shadowRoot.querySelector('#toolbar').toggleAnnotation();
await viewer.loaded;
chrome.test.assertEq('EMBED', contentElement().tagName);
});
async function testExitAnnotationMode() {
chrome.test.assertTrue(isAnnotationMode());
// Exit annotation mode.
viewer.shadowRoot.querySelector('#toolbar').toggleAnnotation();
await viewer.loaded;
chrome.test.assertEq('EMBED', contentElement().tagName);
chrome.test.succeed();
},
]);
......@@ -5,8 +5,6 @@
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 = '';
......@@ -18,20 +16,17 @@ function createToolbar() {
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;
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();
toolbar.addEventListener('display-annotations-changed', async e => {
chrome.test.assertFalse(e.detail);
chrome.test.succeed();
});
toolbar.shadowRoot.querySelector('#show-annotations-button').click();
},
function testEnteringAnnotationsModeShowsAnnotations() {
const toolbar = createToolbar();
......@@ -58,60 +53,57 @@ const tests = [
toolbar.shadowRoot.querySelector('#two-page-view-button').disabled);
chrome.test.succeed();
},
function testRotateOrTwoUpViewTriggersDialog() {
async 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);
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());
// 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;
// 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());
// 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);
// 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());
});
// 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.succeed();
}
];
......
......@@ -7,14 +7,12 @@ import {ViewerDownloadControlsElement} from 'chrome-extension://mhjfbmdgcfjbbpae
import {listenOnce} from 'chrome://resources/js/util.m.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {testAsync} from './test_util.js';
const tests = [
/**
* Test that the toolbar shows an option to download the edited PDF if
* available.
*/
function testEditedPdfOption() {
async function testEditedPdfOption() {
document.body.innerHTML = '';
/** @type {!ViewerDownloadControlsElement} */
const downloadsElement = /** @type {!ViewerDownloadControlsElement} */ (
......@@ -50,80 +48,79 @@ const tests = [
downloadsElement, 'download-menu-shown-for-testing', resolve));
};
testAsync(async () => {
// No edits, and feature is off.
let onSave = whenSave();
downloadButton.click();
let requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(1, numRequests);
// Still does not show the menu if there are no edits.
downloadsElement.pdfFormSaveEnabled = true;
onSave = whenSave();
downloadButton.click();
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(2, numRequests);
// Set form field focused.
downloadsElement.isFormFieldFocused = true;
onSave = whenSave();
downloadButton.click();
// Unfocus, without making any edits. Saves the original document.
downloadsElement.isFormFieldFocused = false;
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(3, numRequests);
// Focus again.
downloadsElement.isFormFieldFocused = true;
downloadButton.click();
// Set editing mode and change the form focus. Now, the menu should
// open.
downloadsElement.hasEdits = true;
downloadsElement.isFormFieldFocused = false;
await whenDownloadMenuShown();
chrome.test.assertTrue(actionMenu.open);
chrome.test.assertEq(3, numRequests);
// Click on "Edited".
const buttons = downloadsElement.shadowRoot.querySelectorAll('button');
onSave = whenSave();
buttons[0].click();
requestType = await onSave;
chrome.test.assertEq(SaveRequestType.EDITED, requestType);
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(4, numRequests);
// Click again to re-open menu.
downloadButton.click();
await whenDownloadMenuShown();
chrome.test.assertTrue(actionMenu.open);
// Click on "Original".
onSave = whenSave();
buttons[1].click();
requestType = await onSave;
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(5, numRequests);
// Even if the document has been edited, always download the original
// if the feature flag is off.
downloadsElement.pdfFormSaveEnabled = false;
onSave = whenSave();
downloadButton.click();
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(6, numRequests);
});
// No edits, and feature is off.
let onSave = whenSave();
downloadButton.click();
let requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(1, numRequests);
// Still does not show the menu if there are no edits.
downloadsElement.pdfFormSaveEnabled = true;
onSave = whenSave();
downloadButton.click();
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(2, numRequests);
// Set form field focused.
downloadsElement.isFormFieldFocused = true;
onSave = whenSave();
downloadButton.click();
// Unfocus, without making any edits. Saves the original document.
downloadsElement.isFormFieldFocused = false;
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(3, numRequests);
// Focus again.
downloadsElement.isFormFieldFocused = true;
downloadButton.click();
// Set editing mode and change the form focus. Now, the menu should
// open.
downloadsElement.hasEdits = true;
downloadsElement.isFormFieldFocused = false;
await whenDownloadMenuShown();
chrome.test.assertTrue(actionMenu.open);
chrome.test.assertEq(3, numRequests);
// Click on "Edited".
const buttons = downloadsElement.shadowRoot.querySelectorAll('button');
onSave = whenSave();
buttons[0].click();
requestType = await onSave;
chrome.test.assertEq(SaveRequestType.EDITED, requestType);
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(4, numRequests);
// Click again to re-open menu.
downloadButton.click();
await whenDownloadMenuShown();
chrome.test.assertTrue(actionMenu.open);
// Click on "Original".
onSave = whenSave();
buttons[1].click();
requestType = await onSave;
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(5, numRequests);
// Even if the document has been edited, always download the original
// if the feature flag is off.
downloadsElement.pdfFormSaveEnabled = false;
onSave = whenSave();
downloadButton.click();
requestType = await onSave;
chrome.test.assertFalse(actionMenu.open);
chrome.test.assertEq(SaveRequestType.ORIGINAL, requestType);
chrome.test.assertEq(6, numRequests);
chrome.test.succeed();
},
];
......
......@@ -7,7 +7,7 @@ import {NavigatorDelegate, PdfNavigator, WindowOpenDisposition} from 'chrome-ext
import {OpenPdfParamsParser} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/open_pdf_params_parser.js';
import {PDFScriptingAPI} from 'chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/pdf_scripting_api.js';
import {getZoomableViewport, MockDocumentDimensions, MockElement, MockSizer, MockViewportChangedCallback, testAsync} from './test_util.js';
import {getZoomableViewport, MockDocumentDimensions, MockElement, MockSizer, MockViewportChangedCallback} from './test_util.js';
/** @implements {NavigatorDelegate} */
class MockNavigatorDelegate {
......@@ -121,7 +121,7 @@ const tests = [
* Test navigation within the page, opening a url in the same tab and
* opening a url in a new tab.
*/
function testNavigate() {
async function testNavigate() {
const mockWindow = new MockElement(100, 100, null);
const mockSizer = new MockSizer();
const mockCallback = new MockViewportChangedCallback();
......@@ -153,54 +153,53 @@ const tests = [
viewport.setDocumentDimensions(documentDimensions);
viewport.setZoom(1);
testAsync(async () => {
mockCallback.reset();
let navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should move viewport to page 0.
navigator.navigate(url + '#US', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
navigatorDelegate.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should open "http://xyz.pdf#US" in a new tab. So current tab
// viewport should not update and viewport position should remain same.
navigator.navigate(url + '#US', WindowOpenDisposition.NEW_BACKGROUND_TAB);
await navigatingDone;
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(navigatorDelegate.navigateInNewTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should move viewport to page 2.
navigator.navigate(url + '#UY', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
mockCallback.reset();
navigatorDelegate.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// #ABC is not a named destination in the page so viewport should not
// update, and the viewport position should remain same as testNavigate3's
// navigating results, as this link will open in the same tab.
navigator.navigate(url + '#ABC', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(navigatorDelegate.navigateInCurrentTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
});
mockCallback.reset();
let navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should move viewport to page 0.
navigator.navigate(url + '#US', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
navigatorDelegate.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should open "http://xyz.pdf#US" in a new tab. So current tab
// viewport should not update and viewport position should remain same.
navigator.navigate(url + '#US', WindowOpenDisposition.NEW_BACKGROUND_TAB);
await navigatingDone;
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(navigatorDelegate.navigateInNewTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(0, viewport.position.y);
mockCallback.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// This should move viewport to page 2.
navigator.navigate(url + '#UY', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertTrue(mockCallback.wasCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
mockCallback.reset();
navigatorDelegate.reset();
navigatingDone =
eventToPromise('navigate-for-testing', navigator.getEventTarget());
// #ABC is not a named destination in the page so viewport should not
// update, and the viewport position should remain same as testNavigate3's
// navigating results, as this link will open in the same tab.
navigator.navigate(url + '#ABC', WindowOpenDisposition.CURRENT_TAB);
await navigatingDone;
chrome.test.assertFalse(mockCallback.wasCalled);
chrome.test.assertTrue(navigatorDelegate.navigateInCurrentTabCalled);
chrome.test.assertEq(0, viewport.position.x);
chrome.test.assertEq(300, viewport.position.y);
chrome.test.succeed();
},
/**
* Test opening a url in the same tab, in a new tab, and in a new window for
......
......@@ -323,16 +323,6 @@ export function getZoomableViewport(
return viewport;
}
/** @param {Function} f */
export async function testAsync(f) {
try {
await f();
chrome.test.succeed();
} catch (e) {
chrome.test.fail(e.stack);
}
}
/**
* Async spin until predicate() returns true.
* @param {function(): boolean} predicate
......
......@@ -10,8 +10,6 @@ import {FocusOutlineManager} from 'chrome://resources/js/cr/ui/focus_outline_man
import {keyDownOn} from 'chrome://resources/polymer/v3_0/iron-test-helpers/mock-interactions.js';
import {flush} from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
import {testAsync} from './test_util.js';
/** @return {!ViewerThumbnailBarElement} */
function createThumbnailBar() {
document.body.innerHTML = '';
......@@ -62,7 +60,7 @@ function whenThumbnailCleared(thumbnail) {
const tests = [
// Test that the thumbnail bar has the correct number of thumbnails and
// correspond to the right pages.
function testThumbnails() {
async function testThumbnails() {
const testDocLength = 10;
const thumbnailBar = createThumbnailBar();
thumbnailBar.docLength = testDocLength;
......@@ -75,31 +73,30 @@ const tests = [
thumbnailBar.shadowRoot.querySelectorAll('viewer-thumbnail'));
chrome.test.assertEq(testDocLength, thumbnails.length);
testAsync(async () => {
/**
* @param {!ViewerThumbnailElement} thumbnail
* @param {number} expectedPageIndex
* @return {!Promise}
*/
function testNavigateThumbnail(thumbnail, expectedPageIndex) {
const whenChanged = eventToPromise('change-page', thumbnailBar);
thumbnail.getClickTarget().click();
return whenChanged.then(e => {
chrome.test.assertEq(expectedPageIndex, e.detail.page);
chrome.test.assertEq('thumbnail', e.detail.origin);
});
}
// Test that each thumbnail has the correct page number and navigates to
// the corresponding page.
for (let i = 0; i < thumbnails.length; i++) {
const thumbnail = thumbnails[i];
chrome.test.assertEq(i + 1, thumbnail.pageNumber);
await testNavigateThumbnail(thumbnail, i);
}
});
/**
* @param {!ViewerThumbnailElement} thumbnail
* @param {number} expectedPageIndex
* @return {!Promise}
*/
function testNavigateThumbnail(thumbnail, expectedPageIndex) {
const whenChanged = eventToPromise('change-page', thumbnailBar);
thumbnail.getClickTarget().click();
return whenChanged.then(e => {
chrome.test.assertEq(expectedPageIndex, e.detail.page);
chrome.test.assertEq('thumbnail', e.detail.origin);
});
}
// Test that each thumbnail has the correct page number and navigates to
// the corresponding page.
for (let i = 0; i < thumbnails.length; i++) {
const thumbnail = thumbnails[i];
chrome.test.assertEq(i + 1, thumbnail.pageNumber);
await testNavigateThumbnail(thumbnail, i);
}
chrome.test.succeed();
},
function testTriggerPaint() {
async function testTriggerPaint() {
const thumbnailBarHeight = getTestThumbnailBarHeight();
// Clear HTML for just the thumbnail bar.
......@@ -122,147 +119,144 @@ const tests = [
/** @type {!NodeList<!ViewerThumbnailElement>} */ (
thumbnailBar.shadowRoot.querySelectorAll('viewer-thumbnail'));
testAsync(async () => {
// Only two thumbnails should be "painted" upon load.
const whenRequestedPaintingFirst = [
whenThumbnailPainted(thumbnails[0]),
whenThumbnailPainted(thumbnails[1]),
];
await Promise.all(whenRequestedPaintingFirst);
chrome.test.assertEq(testDocLength, thumbnails.length);
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i < 2, thumbnails[i].isPainted());
}
// Test that scrolling to the sixth thumbnail triggers 'paint-thumbnail'
// for thumbnails 3 through 7. When on the sixth thumbnail, five
// thumbnails above and one thumbnail below should also be painted because
// of the 500% top and 100% bottom root margins.
const whenRequestedPaintingNext = [];
for (let i = 2; i < 7; i++) {
whenRequestedPaintingNext.push(whenThumbnailPainted(thumbnails[i]));
}
scroller.scrollTop = 5 * thumbnailBarHeight;
await Promise.all(whenRequestedPaintingNext);
// First seven thumbnails should be painted.
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i < 7, thumbnails[i].isPainted());
}
// Only two thumbnails should be "painted" upon load.
const whenRequestedPaintingFirst = [
whenThumbnailPainted(thumbnails[0]),
whenThumbnailPainted(thumbnails[1]),
];
await Promise.all(whenRequestedPaintingFirst);
// Test that scrolling down to the eighth thumbnail will clear the
// thumbnails outside the root margin, namely the first two. A paint
// should also be triggered for the eighth thumbnail.
const whenRequestedPaintingLast = [
whenThumbnailPainted(thumbnails[7]),
whenThumbnailCleared(thumbnails[0]),
whenThumbnailCleared(thumbnails[1]),
];
scroller.scrollTop = 7 * thumbnailBarHeight;
await Promise.all(whenRequestedPaintingLast);
// Only first two thumbnails should not be painted.
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i > 1, thumbnails[i].isPainted());
}
});
chrome.test.assertEq(testDocLength, thumbnails.length);
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i < 2, thumbnails[i].isPainted());
}
// Test that scrolling to the sixth thumbnail triggers 'paint-thumbnail'
// for thumbnails 3 through 7. When on the sixth thumbnail, five
// thumbnails above and one thumbnail below should also be painted because
// of the 500% top and 100% bottom root margins.
const whenRequestedPaintingNext = [];
for (let i = 2; i < 7; i++) {
whenRequestedPaintingNext.push(whenThumbnailPainted(thumbnails[i]));
}
scroller.scrollTop = 5 * thumbnailBarHeight;
await Promise.all(whenRequestedPaintingNext);
// First seven thumbnails should be painted.
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i < 7, thumbnails[i].isPainted());
}
// Test that scrolling down to the eighth thumbnail will clear the
// thumbnails outside the root margin, namely the first two. A paint
// should also be triggered for the eighth thumbnail.
const whenRequestedPaintingLast = [
whenThumbnailPainted(thumbnails[7]),
whenThumbnailCleared(thumbnails[0]),
whenThumbnailCleared(thumbnails[1]),
];
scroller.scrollTop = 7 * thumbnailBarHeight;
await Promise.all(whenRequestedPaintingLast);
// Only first two thumbnails should not be painted.
for (let i = 0; i < thumbnails.length; i++) {
chrome.test.assertEq(i > 1, thumbnails[i].isPainted());
}
chrome.test.succeed();
},
function testThumbnailForwardFocus() {
async function testThumbnailForwardFocus() {
const testDocLength = 10;
const thumbnailBar = createThumbnailBar();
thumbnailBar.docLength = testDocLength;
flush();
testAsync(async () => {
/**
* @param {number} pageNumber
* @return {!Promise}
*/
function waitForwardFocus(pageNumber) {
// Reset focus.
thumbnailBar.blur();
const toThumbnail = /** @type {!ViewerThumbnailElement} */ (
thumbnailBar.getThumbnailForPage(pageNumber));
const whenActiveThumbnailFocused = eventToPromise('focus', toThumbnail);
// Calling focus() on `thumbnailBar` in this test doesn't trigger the
// event listener, but manually dispatching an event does.
thumbnailBar.dispatchEvent(new FocusEvent('focus'));
return whenActiveThumbnailFocused;
}
// When there's no active page, focus should forward to the first
// thumbnail.
await waitForwardFocus(1);
// When there's an active page, focus should forward to the thumbnail of
// the active page.
let activePage = 3;
thumbnailBar.activePage = activePage;
await waitForwardFocus(activePage);
activePage = 10;
thumbnailBar.activePage = activePage;
await waitForwardFocus(activePage);
});
/**
* @param {number} pageNumber
* @return {!Promise}
*/
function waitForwardFocus(pageNumber) {
// Reset focus.
thumbnailBar.blur();
const toThumbnail = /** @type {!ViewerThumbnailElement} */ (
thumbnailBar.getThumbnailForPage(pageNumber));
const whenActiveThumbnailFocused = eventToPromise('focus', toThumbnail);
// Calling focus() on `thumbnailBar` in this test doesn't trigger the
// event listener, but manually dispatching an event does.
thumbnailBar.dispatchEvent(new FocusEvent('focus'));
return whenActiveThumbnailFocused;
}
// When there's no active page, focus should forward to the first
// thumbnail.
await waitForwardFocus(1);
// When there's an active page, focus should forward to the thumbnail of
// the active page.
let activePage = 3;
thumbnailBar.activePage = activePage;
await waitForwardFocus(activePage);
activePage = 10;
thumbnailBar.activePage = activePage;
await waitForwardFocus(activePage);
chrome.test.succeed();
},
function testThumbnailUpDownFocus() {
async function testThumbnailUpDownFocus() {
const testDocLength = 3;
const thumbnailBar = createThumbnailBar();
thumbnailBar.docLength = testDocLength;
flush();
testAsync(async () => {
/**
* @param {!ViewerThumbnailElement} fromThumbnail
* @param {boolean} up
*/
async function pressUpDownArrow(fromThumbnail, up) {
const fromPageNumber = fromThumbnail.pageNumber;
const toPageNumber = up ? fromPageNumber - 1 : fromPageNumber + 1;
const toThumbnail = /** @type {!ViewerThumbnailElement} */ (
thumbnailBar.getThumbnailForPage(toPageNumber));
const whenToThumbnailFocused = eventToPromise('focus', toThumbnail);
keydown(fromThumbnail, up ? 'ArrowUp' : 'ArrowDown');
await whenToThumbnailFocused;
}
/**
* @param {!ViewerThumbnailElement} fromThumbnail
* @param {boolean} up
*/
async function pressUpDownArrow(fromThumbnail, up) {
const fromPageNumber = fromThumbnail.pageNumber;
const toPageNumber = up ? fromPageNumber - 1 : fromPageNumber + 1;
const toThumbnail = /** @type {!ViewerThumbnailElement} */ (
thumbnailBar.getThumbnailForPage(toPageNumber));
const whenToThumbnailFocused = eventToPromise('focus', toThumbnail);
keydown(fromThumbnail, up ? 'ArrowUp' : 'ArrowDown');
await whenToThumbnailFocused;
}
const thumbnails =
/** @type {!NodeList<!ViewerThumbnailElement>} */ (
thumbnailBar.shadowRoot.querySelectorAll('viewer-thumbnail'));
// Start focus on the second of three thumbnails.
// Simulate this focus by mouse so `ViewerThumbnailElement.onFocus_()`
// doesn't forward the focus.
FocusOutlineManager.forDocument(document).visible = false;
let focusedIndex = 1;
thumbnails[focusedIndex].focus();
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ true);
focusedIndex -= 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ false);
focusedIndex += 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ false);
focusedIndex += 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ true);
focusedIndex -= 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
});
const thumbnails =
/** @type {!NodeList<!ViewerThumbnailElement>} */ (
thumbnailBar.shadowRoot.querySelectorAll('viewer-thumbnail'));
// Start focus on the second of three thumbnails.
// Simulate this focus by mouse so `ViewerThumbnailElement.onFocus_()`
// doesn't forward the focus.
FocusOutlineManager.forDocument(document).visible = false;
let focusedIndex = 1;
thumbnails[focusedIndex].focus();
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ true);
focusedIndex -= 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ false);
focusedIndex += 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ false);
focusedIndex += 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
await pressUpDownArrow(thumbnails[focusedIndex], /*up=*/ true);
focusedIndex -= 1;
chrome.test.assertEq(
thumbnails[focusedIndex], thumbnailBar.shadowRoot.activeElement);
chrome.test.succeed();
},
function testThumbnailLeftRightSelect() {
const testDocLength = 2;
......@@ -291,7 +285,7 @@ const tests = [
chrome.test.succeed();
});
},
function testReactToNoPlugin() {
async function testReactToNoPlugin() {
const thumbnailBar = createThumbnailBar();
thumbnailBar.docLength = 1;
......@@ -308,27 +302,26 @@ const tests = [
/** @type {!ViewerThumbnailElement} */ (
thumbnailBar.shadowRoot.querySelector('viewer-thumbnail'));
testAsync(async () => {
const whenPaintTriggered = whenThumbnailPainted(thumbnail).then(() => {
// The thumbnail shouldn't paint when the controller is inactive.
if (!pluginController.isActive) {
chrome.test.fail();
}
});
const whenPaintTriggered = whenThumbnailPainted(thumbnail).then(() => {
// The thumbnail shouldn't paint when the controller is inactive.
if (!pluginController.isActive) {
chrome.test.fail();
}
});
// Give the test a chance to fail.
await waitAfterNextRender(thumbnailBar);
// Give the test a chance to fail.
await waitAfterNextRender(thumbnailBar);
// The thumbnail should paint when reactivating the plugin.
pluginController.isActive = true;
chrome.test.assertFalse(scroller.hidden);
await whenPaintTriggered;
// The thumbnail should paint when reactivating the plugin.
pluginController.isActive = true;
chrome.test.assertFalse(scroller.hidden);
await whenPaintTriggered;
// The thumbnail should clear when deactivating the plugin.
pluginController.isActive = false;
chrome.test.assertTrue(scroller.hidden);
await whenThumbnailCleared(thumbnail);
});
// The thumbnail should clear when deactivating the plugin.
pluginController.isActive = false;
chrome.test.assertTrue(scroller.hidden);
await whenThumbnailCleared(thumbnail);
chrome.test.succeed();
},
];
......
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