* @fileoverview Tests for text_click.ts.
*/
import type {AnnotationsTapConsumer} from '//ios/web/annotations/resources/text_click.js';
import {TextClick} from '//ios/web/annotations/resources/text_click.js';
import {expectEq, FakeTaskTimer, load, TestSuite} from '//ios/web/annotations/resources/text_test_utils.js';
export class TestTextClick extends TestSuite {
tappedAnnotation?: HTMLElement;
tappedCancel?: boolean;
tapConsumer: AnnotationsTapConsumer =
(annotation: HTMLElement, cancel: boolean): void => {
this.tappedAnnotation = annotation;
this.tappedCancel = cancel;
};
override setUp() {
this.tappedAnnotation = undefined;
this.tappedCancel = undefined;
}
testTextClickNoMutations() {
const decoratedHTML = '<div id="outer">' +
'<chrome_annotation>Hello</chrome_annotation>' +
'</div>';
load(decoratedHTML);
const annotation =
document.querySelector<HTMLElement>('chrome_annotation')!;
const timer = new FakeTaskTimer();
const clicker = new TextClick(
document.documentElement, this.tapConsumer, () => undefined, timer,
50, annotation);
clicker.start();
annotation.click();
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after click:');
timer.moveAhead( 10, 4);
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after 40ms:');
timer.moveAhead( 10, 2);
expectEq(annotation, this.tappedAnnotation, 'tappedAnnotation after 60ms:');
expectEq(false, this.tappedCancel, 'tappedCancel after 60ms:');
clicker.stop();
}
testTextClickStopped() {
const decoratedHTML = '<div id="outer">' +
'<chrome_annotation>Hello</chrome_annotation>' +
'</div>';
load(decoratedHTML);
const outer = document.querySelector('#outer')!;
outer.addEventListener('click', (event: Event) => {
event.stopImmediatePropagation();
});
const annotation =
document.querySelector<HTMLElement>('chrome_annotation')!;
const timer = new FakeTaskTimer();
const clicker = new TextClick(
document.documentElement, this.tapConsumer, () => undefined, timer,
50, annotation);
clicker.start();
annotation.click();
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after click:');
timer.moveAhead( 10, 10);
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after 100ms:');
expectEq(undefined, this.tappedCancel, 'tappedCancel after 100ms:');
clicker.stop();
}
testTextClickPrevented() {
const decoratedHTML = '<div id="outer">' +
'<chrome_annotation>Hello</chrome_annotation>' +
'</div>';
load(decoratedHTML);
const outer = document.querySelector('#outer')!;
outer.addEventListener('click', (event: Event) => {
event.preventDefault();
});
const annotation =
document.querySelector<HTMLElement>('chrome_annotation')!;
const timer = new FakeTaskTimer();
const clicker = new TextClick(
document.documentElement, this.tapConsumer, () => undefined, timer,
50, annotation);
clicker.start();
annotation.click();
expectEq(
annotation, this.tappedAnnotation, 'tappedAnnotation after 100ms:');
expectEq(true, this.tappedCancel, 'tappedCancel after 100ms:');
clicker.stop();
}
testTextClickWithMutationInsideTree() {
const decoratedHTML = '<div id="outer">' +
'<div id="mutate">I will mutate!' +
'<chrome_annotation>Hello</chrome_annotation></div>' +
'</div>';
load(decoratedHTML);
const annotation =
document.querySelector<HTMLElement>('chrome_annotation')!;
const timer = new FakeTaskTimer();
const clicker = new TextClick(
document.documentElement, this.tapConsumer, () => undefined, timer,
50, annotation);
clicker.start();
annotation.click();
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after click:');
timer.moveAhead( 10, 2);
document.querySelector('#mutate')!.appendChild(
document.createTextNode('Mutated!'));
clicker.updateForTesting();
timer.moveAhead( 10, 4);
expectEq(annotation, this.tappedAnnotation, 'tappedAnnotation after 60ms:');
expectEq(true, this.tappedCancel, 'tappedCancel after 60ms:');
clicker.stop();
}
testTextClickWithMutationOutsideTree() {
const decoratedHTML = '<div id="outer">' +
'<div id="mutate">I will mutate!</div>' +
'<chrome_annotation>Hello</chrome_annotation>' +
'</div>';
load(decoratedHTML);
const annotation =
document.querySelector<HTMLElement>('chrome_annotation')!;
const timer = new FakeTaskTimer();
const clicker = new TextClick(
document.documentElement, this.tapConsumer, () => undefined, timer,
50, annotation);
clicker.start();
annotation.click();
expectEq(undefined, this.tappedAnnotation, 'tappedAnnotation after click:');
timer.moveAhead( 10, 2);
document.querySelector('#mutate')!.appendChild(
document.createTextNode('Mutated!'));
clicker.updateForTesting();
timer.moveAhead( 10, 4);
expectEq(annotation, this.tappedAnnotation, 'tappedAnnotation after 60ms:');
expectEq(false, this.tappedCancel, 'tappedCancel after 60ms:');
clicker.stop();
}
}