-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Expand file tree
/
Copy pathtest_utils.mjs
More file actions
1106 lines (1021 loc) · 29.2 KB
/
test_utils.mjs
File metadata and controls
1106 lines (1021 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2020 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import os from "os";
const isMac = os.platform() === "darwin";
function loadAndWait(filename, selector, zoom, setups, options, viewport) {
return Promise.all(
global.integrationSessions.map(async session => {
const page = await session.browser.newPage();
if (viewport) {
await page.setViewport(viewport);
}
// In order to avoid errors because of checks which depend on
// a locale.
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, "language", {
get() {
return "en-US";
},
});
Object.defineProperty(navigator, "languages", {
get() {
return ["en-US", "en"];
},
});
});
let app_options = "";
if (options) {
const optionsObject =
typeof options === "function"
? await options(page, session.name)
: options;
// Options must be handled in app.js::_parseHashParams.
for (const [key, value] of Object.entries(optionsObject || {})) {
app_options += `&${key}=${encodeURIComponent(value)}`;
}
}
const fileParam = filename.startsWith("http")
? filename
: `/test/pdfs/${filename}`;
const url = `${global.integrationBaseUrl}?file=${fileParam}#zoom=${zoom ?? "page-fit"}${app_options}`;
if (setups) {
// page.evaluateOnNewDocument allows us to run code before the
// first js script is executed.
// The idea here is to set up some setters for PDFViewerApplication
// and EventBus, so we can inject some code to do whatever we want
// soon enough especially before the first event in the eventBus is
// dispatched.
const { prePageSetup, appSetup, earlySetup, eventBusSetup } = setups;
await prePageSetup?.(page);
if (earlySetup || appSetup || eventBusSetup) {
await page.evaluateOnNewDocument(
(eaSetup, aSetup, evSetup) => {
if (eaSetup) {
// eslint-disable-next-line no-eval
eval(`(${eaSetup})`)();
}
let app;
let eventBus;
Object.defineProperty(window, "PDFViewerApplication", {
get() {
return app;
},
set(newValue) {
app = newValue;
if (aSetup) {
// eslint-disable-next-line no-eval
eval(`(${aSetup})`)(app);
}
Object.defineProperty(app, "eventBus", {
get() {
return eventBus;
},
set(newV) {
eventBus = newV;
if (evSetup) {
// eslint-disable-next-line no-eval
eval(`(${evSetup})`)(eventBus);
}
},
});
},
});
},
earlySetup?.toString(),
appSetup?.toString(),
eventBusSetup?.toString()
);
}
}
await page.goto(url);
await setups?.postPageSetup?.(page);
await page.bringToFront();
if (selector) {
await page.waitForSelector(selector, {
timeout: 0,
});
}
return [session.name, page];
})
);
}
function createPromise(page, callback) {
return page.evaluateHandle(
// eslint-disable-next-line no-eval
cb => [new Promise(eval(`(${cb})`))],
callback.toString()
);
}
function createPromiseWithArgs(page, callback, args) {
return page.evaluateHandle(
// eslint-disable-next-line no-eval, no-shadow
(cb, args) => [new Promise(eval(`(${cb})`))],
callback.toString(),
args
);
}
function awaitPromise(promise) {
return promise.evaluate(([p]) => p);
}
function closePages(pages) {
return Promise.all(pages.map(([_, page]) => closeSinglePage(page)));
}
async function closeSinglePage(page) {
// Avoid to keep something from a previous test.
await page.evaluate(async () => {
await window.PDFViewerApplication.testingClose();
window.localStorage.clear();
});
await page.close({ runBeforeUnload: false });
}
async function waitForSandboxTrip(page) {
const handle = await page.evaluateHandle(() => [
new Promise(resolve => {
window.addEventListener("sandboxtripend", resolve, { once: true });
window.PDFViewerApplication.pdfScriptingManager.sandboxTrip();
}),
]);
await awaitPromise(handle);
}
async function waitForDOMMutation(page, callback) {
return page.evaluateHandle(
cb => [
new Promise(resolve => {
const mutationObserver = new MutationObserver(mutationList => {
// eslint-disable-next-line no-eval
if (eval(`(${cb})`)(mutationList)) {
mutationObserver.disconnect();
resolve();
}
});
mutationObserver.observe(document, { childList: true, subtree: true });
}),
],
callback.toString()
);
}
function waitForTimeout(milliseconds) {
/**
* Wait for the given number of milliseconds.
*
* Note that waiting for an arbitrary time in tests is discouraged because it
* can easily cause intermittent failures, which is why this functionality is
* no longer provided by Puppeteer 22+ and we have to implement it ourselves
* for the remaining callers in the integration tests. We should avoid
* creating new usages of this function; instead please refer to the better
* alternatives at https://github.com/puppeteer/puppeteer/pull/11780.
*/
return new Promise(resolve => {
setTimeout(resolve, milliseconds);
});
}
async function clearInput(page, selector, waitForInputEvent = false) {
const action = async () => {
await page.click(selector);
await kbSelectAll(page);
await page.keyboard.press("Backspace");
await page.waitForFunction(
`document.querySelector('${selector}').value === ""`
);
};
return waitForInputEvent
? waitForEvent({
page,
eventName: "input",
action,
selector,
})
: action();
}
async function waitAndClick(page, selector, clickOptions = {}) {
await page.waitForSelector(selector, { visible: true });
await page.click(selector, clickOptions);
}
function waitForPointerUp(page) {
return createPromise(page, resolve => {
window.addEventListener("pointerup", resolve, { once: true });
});
}
function getSelector(id) {
return `[data-element-id="${id}"]`;
}
async function getRect(page, selector) {
// In Chrome something is wrong when serializing a `DomRect`,
// so we extract the values and return them ourselves.
await page.waitForSelector(selector, { visible: true });
return page.$eval(selector, el => {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
});
}
function getQuerySelector(id) {
return `document.querySelector('${getSelector(id)}')`;
}
function getComputedStyleSelector(id) {
return `getComputedStyle(${getQuerySelector(id)})`;
}
function getEditorSelector(n) {
return `#pdfjs_internal_editor_${n}`;
}
function getAnnotationSelector(id) {
return `[data-annotation-id="${id}"]`;
}
function getThumbnailSelector(pageNumber) {
return `.thumbnailImageContainer[data-l10n-args^='{"page":${pageNumber}']`;
}
async function getSpanRectFromText(page, pageNumber, text) {
await page.waitForSelector(
`.page[data-page-number="${pageNumber}"] > .textLayer .endOfContent`
);
return page.evaluate(
(number, content) => {
for (const el of document.querySelectorAll(
`.page[data-page-number="${number}"] > .textLayer span:not(:has(> span))`
)) {
if (el.textContent === content) {
const { x, y, width, height } = el.getBoundingClientRect();
return { x, y, width, height };
}
}
return null;
},
pageNumber,
text
);
}
async function waitForEvent({
page,
eventName,
action,
selector = null,
validator = null,
timeout = 5000,
}) {
const handle = await page.evaluateHandle(
(name, sel, validate, timeOut) => {
let callback = null,
timeoutId = null;
const element = sel ? document.querySelector(sel) : document;
return [
Promise.race([
new Promise(resolve => {
// The promise is resolved if the event fired in the context of the
// selector and, if a validator is defined, the event data satisfies
// the conditions of the validator function.
callback = e => {
if (timeoutId) {
clearTimeout(timeoutId);
}
// eslint-disable-next-line no-eval
resolve(validate ? eval(`(${validate})`)(e) : true);
};
element.addEventListener(name, callback, { once: true });
}),
new Promise(resolve => {
timeoutId = setTimeout(() => {
element.removeEventListener(name, callback);
resolve(null);
}, timeOut);
}),
]),
];
},
eventName,
selector,
validator ? validator.toString() : null,
timeout
);
await action();
const success = await awaitPromise(handle);
if (success === null) {
console.warn(
`waitForEvent: ${eventName} didn't trigger within the timeout`
);
} else if (!success) {
console.warn(`waitForEvent: ${eventName} triggered, but validation failed`);
}
}
async function countStorageEntries(page) {
return page.evaluate(
() => window.PDFViewerApplication.pdfDocument.annotationStorage.size
);
}
async function waitForStorageEntries(page, nEntries) {
return page.waitForFunction(
n => window.PDFViewerApplication.pdfDocument.annotationStorage.size === n,
{},
nEntries
);
}
async function countSerialized(page) {
return page.evaluate(
() =>
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable.map
?.size ?? 0
);
}
async function waitForSerialized(page, nEntries) {
return page.waitForFunction(
n => {
try {
return (
(window.PDFViewerApplication.pdfDocument.annotationStorage
.serializable.map?.size ?? 0) === n
);
} catch {
// When serializing a stamp annotation with a SVG, the transfer
// can fail because of the SVG, so we just retry.
return false;
}
},
{},
nEntries
);
}
async function applyFunctionToEditor(page, editorId, func) {
return page.evaluate(
(id, f) => {
const editor =
window.PDFViewerApplication.pdfDocument.annotationStorage.getRawValue(
id
);
// eslint-disable-next-line no-eval
eval(`(${f})`)(editor);
},
editorId,
func.toString()
);
}
async function selectEditor(page, selector, count = 1) {
const editorRect = await getRect(page, selector);
await page.mouse.click(
editorRect.x + editorRect.width / 2,
editorRect.y + editorRect.height / 2,
{ count }
);
await waitForSelectedEditor(page, selector);
}
async function waitForSelectedEditor(page, selector) {
return page.waitForSelector(`${selector}.selectedEditor`);
}
async function unselectEditor(page, selector) {
await page.keyboard.press("Escape");
await waitForUnselectedEditor(page, selector);
}
async function waitForUnselectedEditor(page, selector) {
return page.waitForSelector(`${selector}:not(.selectedEditor)`);
}
async function mockClipboard(pages) {
return Promise.all(
pages.map(async ([_, page]) => {
await page.evaluate(() => {
let data = null;
const clipboard = {
writeText: async text => (data = text),
readText: async () => data,
};
Object.defineProperty(navigator, "clipboard", { value: clipboard });
});
})
);
}
async function copy(page) {
await waitForEvent({
page,
eventName: "copy",
action: () => kbCopy(page),
});
}
async function copyToClipboard(page, data) {
await page.evaluate(async dat => {
const items = Object.create(null);
for (const [type, value] of Object.entries(dat)) {
if (value.startsWith("data:")) {
const resp = await fetch(value);
items[type] = await resp.blob();
} else {
items[type] = new Blob([value], { type });
}
}
await navigator.clipboard.write([new ClipboardItem(items)]);
}, data);
}
async function paste(page) {
await waitForEvent({
page,
eventName: "paste",
action: () => kbPaste(page),
});
}
async function pasteFromClipboard(page, selector = null) {
const validator = e => e.clipboardData.items.length !== 0;
await waitForEvent({
page,
eventName: "paste",
action: () => kbPaste(page),
selector,
validator,
});
}
async function getSerialized(page, filter = undefined) {
const values = await page.evaluate(() => {
const { map } =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
if (!map) {
return [];
}
const vals = Array.from(map.values());
for (const value of vals) {
for (const [k, v] of Object.entries(value)) {
// Puppeteer don't serialize typed array correctly, so we convert them
// to arrays.
if (ArrayBuffer.isView(v)) {
value[k] = Array.from(v);
}
}
}
return vals;
});
return filter ? values.map(filter) : values;
}
async function getFirstSerialized(page, filter = undefined) {
return (await getSerialized(page, filter))[0];
}
function getAnnotationStorage(page) {
return page.evaluate(() =>
Object.fromEntries(
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
.map || []
)
);
}
function waitForEntryInStorage(page, key, value, checker = (x, y) => x === y) {
return page.waitForFunction(
(k, v, c) => {
const { map } =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
// eslint-disable-next-line no-eval
return map && eval(`(${c})`)(JSON.stringify(map.get(k)), v);
},
{},
key,
JSON.stringify(value),
checker.toString()
);
}
function getEditors(page, kind) {
return page.evaluate(aKind => {
const elements = document.querySelectorAll(`.${aKind}Editor`);
const results = [];
for (const { id } of elements) {
results.push(parseInt(id.split("_").at(-1)));
}
results.sort();
return results;
}, kind);
}
function getEditorDimensions(page, selector) {
return page.evaluate(sel => {
const { style } = document.querySelector(sel);
return {
left: style.left,
top: style.top,
width: style.width,
height: style.height,
};
}, selector);
}
async function serializeBitmapDimensions(page) {
await page.waitForFunction(() => {
try {
const map =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable
.map;
return !!map;
} catch {
return false;
}
});
return page.evaluate(() => {
const { map } =
window.PDFViewerApplication.pdfDocument.annotationStorage.serializable;
return map
? Array.from(map.values(), x => ({
width: x.bitmap.width,
height: x.bitmap.height,
}))
: [];
});
}
async function dragAndDrop(page, selector, translations, steps = 1) {
const rect = await getRect(page, selector);
const startX = rect.x + rect.width / 2;
const startY = rect.y + rect.height / 2;
await page.mouse.move(startX, startY);
await page.mouse.down();
for (const [tX, tY] of translations) {
await page.mouse.move(startX + tX, startY + tY, { steps });
}
await page.mouse.up();
await page.waitForSelector("#viewer:not(.noUserSelect)");
}
function waitForPageChanging(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on("pagechanging", resolve, {
once: true,
});
});
}
function waitForAnnotationEditorLayer(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on(
"annotationeditorlayerrendered",
resolve,
{ once: true }
);
});
}
function waitForAnnotationModeChanged(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on(
"annotationeditormodechanged",
resolve,
{ once: true }
);
});
}
function waitForPageRendered(page, pageNumber) {
return page.evaluateHandle(
number => [
new Promise(resolve => {
const { eventBus } = window.PDFViewerApplication;
eventBus.on("pagerendered", function handler(e) {
if (
!e.isDetailView &&
(number === undefined || e.pageNumber === number)
) {
resolve();
eventBus.off("pagerendered", handler);
}
});
}),
],
pageNumber
);
}
function waitForEditorMovedInDOM(page) {
return createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on("editormovedindom", resolve, {
once: true,
});
});
}
async function scrollIntoView(page, selector) {
await page.waitForSelector(selector, { visible: true });
const handle = await page.evaluateHandle(
sel => [
new Promise(resolve => {
const container = document.getElementById("viewerContainer");
if (container.scrollHeight <= container.clientHeight) {
resolve();
return;
}
container.addEventListener("scrollend", resolve, { once: true });
const element = document.querySelector(sel);
element.scrollIntoView({ behavior: "instant", block: "start" });
}),
],
selector
);
return awaitPromise(handle);
}
async function firstPageOnTop(page) {
const handle = await page.evaluateHandle(() => [
new Promise(resolve => {
const container = document.getElementById("viewerContainer");
if (container.scrollTop === 0 && container.scrollLeft === 0) {
resolve();
return;
}
container.addEventListener("scrollend", resolve, { once: true });
container.scrollTo(0, 0);
}),
]);
return awaitPromise(handle);
}
async function setCaretAt(page, pageNumber, text, position) {
await page.evaluate(
(pageN, string, pos) => {
for (const el of document.querySelectorAll(
`.page[data-page-number="${pageN}"] > .textLayer > span`
)) {
if (el.textContent === string) {
window.getSelection().setPosition(el.firstChild, pos);
break;
}
}
},
pageNumber,
text,
position
);
}
const modifier = isMac ? "Meta" : "Control";
async function kbCopy(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("c", { commands: ["Copy"] });
await page.keyboard.up(modifier);
}
async function kbCut(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("x", { commands: ["Cut"] });
await page.keyboard.up(modifier);
}
async function kbDelete(page) {
await page.keyboard.press("Delete");
}
async function kbPaste(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("v", { commands: ["Paste"] });
await page.keyboard.up(modifier);
}
async function kbUndo(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("z");
await page.keyboard.up(modifier);
}
async function kbRedo(page) {
if (isMac) {
await page.keyboard.down("Meta");
await page.keyboard.down("Shift");
await page.keyboard.press("z");
await page.keyboard.up("Shift");
await page.keyboard.up("Meta");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("y");
await page.keyboard.up("Control");
}
}
async function kbSelectAll(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("a", { commands: ["SelectAll"] });
await page.keyboard.up(modifier);
}
async function kbModifierDown(page) {
await page.keyboard.down(modifier);
}
async function kbModifierUp(page) {
await page.keyboard.up(modifier);
}
async function kbGoToEnd(page) {
if (isMac) {
await page.keyboard.down("Meta");
await page.keyboard.press("ArrowDown", {
commands: ["MoveToEndOfDocument"],
});
await page.keyboard.up("Meta");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("End");
await page.keyboard.up("Control");
}
}
async function kbGoToBegin(page) {
if (isMac) {
await page.keyboard.down("Meta");
await page.keyboard.press("ArrowUp", {
commands: ["MoveToBeginningOfDocument"],
});
await page.keyboard.up("Meta");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("Home");
await page.keyboard.up("Control");
}
}
async function kbBigMoveLeft(page) {
if (isMac) {
await page.keyboard.down("Shift");
await page.keyboard.press("ArrowLeft");
await page.keyboard.up("Shift");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("ArrowLeft");
await page.keyboard.up("Control");
}
}
async function kbBigMoveRight(page) {
if (isMac) {
await page.keyboard.down("Shift");
await page.keyboard.press("ArrowRight");
await page.keyboard.up("Shift");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("ArrowRight");
await page.keyboard.up("Control");
}
}
async function kbBigMoveUp(page) {
if (isMac) {
await page.keyboard.down("Shift");
await page.keyboard.press("ArrowUp");
await page.keyboard.up("Shift");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("ArrowUp");
await page.keyboard.up("Control");
}
}
async function kbBigMoveDown(page) {
if (isMac) {
await page.keyboard.down("Shift");
await page.keyboard.press("ArrowDown");
await page.keyboard.up("Shift");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("ArrowDown");
await page.keyboard.up("Control");
}
}
async function kbDeleteLastWord(page) {
if (isMac) {
await page.keyboard.down("Alt");
await page.keyboard.press("Backspace");
await page.keyboard.up("Alt");
} else {
await page.keyboard.down("Control");
await page.keyboard.press("Backspace");
await page.keyboard.up("Control");
}
}
async function kbFocusNext(page) {
const handle = await createPromise(page, resolve => {
window.addEventListener("focusin", resolve, { once: true });
});
await page.keyboard.press("Tab");
await awaitPromise(handle);
}
async function kbFocusPrevious(page) {
const handle = await createPromise(page, resolve => {
window.addEventListener("focusin", resolve, { once: true });
});
await page.keyboard.down("Shift");
await page.keyboard.press("Tab");
await page.keyboard.up("Shift");
await awaitPromise(handle);
}
async function kbSave(page) {
await page.keyboard.down(modifier);
await page.keyboard.press("s");
await page.keyboard.up(modifier);
}
async function switchToEditor(name, page, disable = false) {
const modeChangedHandle = await createPromise(page, resolve => {
window.PDFViewerApplication.eventBus.on(
"annotationeditormodechanged",
resolve,
{ once: true }
);
});
await page.click(`#editor${name}Button`);
name = name.toLowerCase();
await page.waitForSelector(
".annotationEditorLayer" +
(disable ? `:not(.${name}Editing)` : `.${name}Editing`)
);
await awaitPromise(modeChangedHandle);
}
async function selectEditors(name, page) {
await kbSelectAll(page);
await page.waitForFunction(
() => !document.querySelector(`.${name}Editor:not(.selectedEditor)`)
);
}
async function clearEditors(name, page) {
await selectEditors(name, page);
await page.keyboard.press("Backspace");
await waitForStorageEntries(page, 0);
}
function waitForNoElement(page, selector) {
return page.waitForFunction(
sel => !document.querySelector(sel),
{},
selector
);
}
function waitForTextToBe(page, selector, text) {
return page.waitForFunction(
(sel, str) => document.querySelector(sel)?.textContent.trim() === str,
{},
selector,
text
);
}
function waitForTooltipToBe(page, selector, text) {
return page.waitForFunction(
(sel, str) => document.querySelector(sel)?.title === str,
{},
selector,
text
);
}
function isCanvasMonochrome(page, pageNumber, rectangle, color) {
return page.evaluate(
(rect, pageN, col) => {
const canvas = document.querySelector(
`.page[data-page-number = "${pageN}"] .canvasWrapper canvas`
);
const canvasRect = canvas.getBoundingClientRect();
const ctx = canvas.getContext("2d");
rect ||= canvasRect;
const { data } = ctx.getImageData(
rect.x - canvasRect.x,
rect.y - canvasRect.y,
rect.width,
rect.height
);
return new Uint32Array(data.buffer).every(x => x === col);
},
rectangle,
pageNumber,
color
);
}
async function getXY(page, selector) {
const rect = await getRect(page, selector);
return `${rect.x}::${rect.y}`;
}
function waitForPositionChange(page, selector, xy) {
return page.waitForFunction(
(sel, currentXY) => {
const bbox = document.querySelector(sel).getBoundingClientRect();
return `${bbox.x}::${bbox.y}` !== currentXY;
},
{},
selector,
xy
);
}
async function moveEditor(page, selector, n, pressKey) {
let xy = await getXY(page, selector);
for (let i = 0; i < n; i++) {
const handle = await waitForEditorMovedInDOM(page);
await pressKey();
await awaitPromise(handle);
await waitForPositionChange(page, selector, xy);
xy = await getXY(page, selector);
}
}
async function getNextEditorId(page) {
return page.evaluate(() =>
window.PDFViewerApplication.pdfViewer._layerProperties.annotationEditorUIManager.getNextEditorId()
);
}
async function highlightSpan(
page,
pageIndex,
text,
xRatio = 0.5,
yRatio = 0.5
) {
const nextId = await getNextEditorId(page);
const rect = await getSpanRectFromText(page, pageIndex, text);
const x = rect.x + rect.width * xRatio;
const y = rect.y + rect.height * yRatio;
// We add a small delay between press and release to make sure that a
// pointerup event is triggered after selectionchange.
// It works with a value of 1ms, but we use 100ms to be sure.
await page.mouse.click(x, y, { count: 2, delay: 100 });
await page.waitForSelector(getEditorSelector(nextId));
}
async function showViewsManager(page) {
const hasAnimations = await page.evaluate(
() => !window.matchMedia("(prefers-reduced-motion: reduce)").matches
);
const movingPromise = hasAnimations
? page.waitForSelector("#outerContainer.viewsManagerMoving", {
visible: true,
})
: Promise.resolve();
await page.click("#viewsManagerToggleButton");
if (hasAnimations) {
await movingPromise;
}
await page.waitForSelector("#viewsManager", { visible: true });
await page.waitForSelector(