/** * Whichever control holds the cursor shows the form's focus ring: a push button, an image field * and a signature field as a checkbox does, while their button has the focus and no longer. * Against a real kernel and the real engine. */ import { readFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { Component, signal } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { afterEach, describe, expect, it, vi } from 'vitest'; import type { Engine, PageRef } from '@embedpdf/core'; import { pageTransform } from '@embedpdf/core-geometry'; import { createLocalEngine } from '@embedpdf/engine'; import { createPageContext, EPDF_PAGE } from '@embedpdf/angular/runtime'; import { withInteraction } from '@embedpdf/angular/interaction'; import { withAnnotation } from '@embedpdf/angular/annotation'; import { EpdfFormLayer, FormToken, withForm } from '@embedpdf/angular/form'; import { withSignature } from '@embedpdf/angular/signature'; import { kernelOf, mount, viewerHost } from './fixtures'; const here = dirname(fileURLToPath(import.meta.url)); const blank = resolve(here, '../../../engine/main/test/fixtures/hello_world.pdf'); const wasm = resolve(here, '../../../engine/runtime/npm/wasm32/lib/embedpdf.wasm'); /** * The real engine. happy-dom's browser-shaped globals would steer the wasm toward fetch(): hand * it over. */ async function realEngine(): Promise { const wasmBinary = new Uint8Array(await readFile(wasm)); return createLocalEngine({ runtime: { prefer: 'wasm', wasmBinary } }); } const RING = '[data-embedpdf-form-focus-ring]'; const PLACEHOLDER = '[data-epdf-placeholder]'; /** * A press by hand on `element`: the click a mouse or a finger ends with. Only the browser can * make one, so the test marks it; `trusted: false` is a click a script made with a click count. */ const pressed = (element: HTMLElement, trusted = true) => { const event = new MouseEvent('click', { bubbles: true, cancelable: true, detail: 1 }); Object.defineProperty(event, 'isTrusted', { value: trusted }); element.dispatchEvent(event); }; const LETTER = { width: 612, height: 792 }; /** The page, at scale 1 from the top-left corner of the screen, once the test names it. */ const shownPage = signal<{ documentId: string; ref: PageRef } | null>(null); /** A page with the form layer on it, as a Stage's page has it. */ @Component({ selector: 'test-focus-ring-page', imports: [EpdfFormLayer], providers: [ { provide: EPDF_PAGE, useFactory: () => createPageContext({ documentId: () => shownPage()!.documentId, ref: () => shownPage()!.ref, view: () => 'test-view', pageIndex: signal(0), frame: signal({ top: 0, right: 0, bottom: 0, left: 0 }), transform: signal( pageTransform({ pageSize: LETTER, rotation: 0, scale: 1, baseScale: 1, dpr: 1 }), ), getRect: () => new DOMRect(0, 0, LETTER.width, LETTER.height), }), }, ], template: `
`, }) class FocusRingPage {} @Component({ selector: 'test-focus-ring-shell', imports: [FocusRingPage], template: ` @if (page()) { } `, }) class FocusRingShell { protected readonly page = shownPage; } afterEach(() => { shownPage.set(null); TestBed.resetTestingModule(); }); describe('the focus ring of a push button, an image field and a signature field', () => { it( 'shows the ring a checkbox shows while the button has the focus, and never the browser’s outline', { timeout: 60_000 }, async () => { const bytes = new Uint8Array(await readFile(blank)); const fixture = await mount( viewerHost({ template: '', imports: [FocusRingShell], // The viewer's own engine: destroyed after the kernel, when the test's module goes. config: { engine: realEngine, initialDocuments: [{ source: { kind: 'bytes', id: 'blank', bytes } }], }, features: [withInteraction(), withAnnotation(), withForm(), withSignature()], }), ); await vi.waitFor(() => expect(kernelOf(fixture).tryCapability(FormToken)).toBeTruthy(), { timeout: 20_000, }); const form = kernelOf(fixture).capability(FormToken); await form.refresh(); const page = kernelOf(fixture).documents.getPage(0, 'blank')!.ref; const at = (y: number) => ({ x: 72, y, width: 120, height: 30 }); await form.create({ kind: 'checkbox', name: 'agree', widgets: [{ page, rect: at(60) }] }); await form.create({ kind: 'pushbutton', name: 'send', widgets: [{ page, rect: at(100), caption: 'Send' }], }); await form.create({ kind: 'pushbutton', name: 'initials', purpose: 'initials', takesImage: true, widgets: [{ page, rect: at(140), layout: 'image' }], }); const { field: signed } = await form.create({ kind: 'signature', name: 'sign', widgets: [{ page, rect: at(180) }], }); await form.create({ kind: 'checkbox', name: 'fixed', readOnly: true, widgets: [{ page, rect: at(220) }], }); shownPage.set({ documentId: 'blank', ref: page }); await fixture.whenStable(); const root = fixture.nativeElement as HTMLElement; const layer = root.querySelector('[data-layer="form"]')!; /** The form layer's control named `label`, once the layer has drawn it. */ const control = (label: string) => vi.waitFor(async () => { await fixture.whenStable(); const found = layer.querySelector(`[aria-label="${label}"]`); expect(found).not.toBeNull(); return found!; }); /** Once `check` holds of what the page shows. */ const shows = (check: () => void) => vi.waitFor(async () => { await fixture.whenStable(); check(); }); // The ring a checkbox shows: what the three buttons are held to. const checkbox = await control('agree'); checkbox.focus(); await fixture.whenStable(); const checkboxRing = checkbox.querySelector(RING); expect(checkboxRing).not.toBeNull(); const ringStyle = checkboxRing!.getAttribute('style'); checkbox.blur(); await fixture.whenStable(); expect(document.querySelector(RING)).toBeNull(); for (const name of ['send', 'initials', 'sign']) { const button = await control(name); expect(button.tagName).toBe('BUTTON'); const box = button.parentElement as HTMLElement; // Never the browser's own outline: the layer draws the ring. expect(button.style.outlineStyle).toBe('none'); expect(box.querySelector(RING)).toBeNull(); button.focus(); await fixture.whenStable(); const ring = box.querySelector(RING); expect(ring, name).not.toBeNull(); expect(ring!.getAttribute('style'), name).toBe(ringStyle); // It takes no pointer, so a press still reaches the button. expect(ring!.style.pointerEvents).toBe('none'); expect(document.querySelectorAll(RING)).toHaveLength(1); button.blur(); await fixture.whenStable(); expect(box.querySelector(RING), name).toBeNull(); } // A press puts the cursor in the layer's own control where the browser left it out, as // Safari does for a button: the push button takes it and shows the ring. const send = await control('send'); expect(document.activeElement).not.toBe(send); pressed(send); await fixture.whenStable(); expect(document.activeElement).toBe(send); expect((send.parentElement as HTMLElement).querySelector(RING)).not.toBeNull(); send.blur(); await fixture.whenStable(); // A click a script makes moves no cursor, whatever click count it gives itself. pressed(send, false); await fixture.whenStable(); expect(document.activeElement).not.toBe(send); expect(document.querySelector(RING)).toBeNull(); // A box the form's cursor passes over is bound too: a read-only checkbox takes a press. const fixed = await control('fixed'); pressed(fixed); await fixture.whenStable(); expect(document.activeElement).toBe(fixed); fixed.blur(); await fixture.whenStable(); // An empty image field keeps saying what it is for under the ring. const initials = await control('initials'); initials.focus(); await fixture.whenStable(); const initialsBox = initials.parentElement as HTMLElement; expect(initialsBox.querySelector(PLACEHOLDER)!.textContent).toBe('Initials'); expect(initialsBox.querySelector(RING)).not.toBeNull(); initials.blur(); await fixture.whenStable(); // A button that goes away while it holds the cursor takes its ring with it, and the // one that comes back starts without one. const sign = await control('sign'); const signBox = sign.parentElement as HTMLElement; sign.focus(); await fixture.whenStable(); expect(signBox.querySelector(RING)).not.toBeNull(); await form.update(signed.ref, { readOnly: true }); await shows(() => expect(document.querySelector('[aria-label="sign"]')).toBeNull()); expect(document.querySelector(RING)).toBeNull(); await form.update(signed.ref, { readOnly: false }); await shows(() => expect(document.querySelector('[aria-label="sign"]')).not.toBeNull()); expect(document.querySelector(RING)).toBeNull(); }, ); });