/** * The render service and layer: the settings with no document (and their changes), redraws as * `invalidated$`, and one `` picture per page on a Stage, never handed a * revoked URL, leaving out what a layer on the page paints. */ import { Component } from '@angular/core'; import { TestBed } from '@angular/core/testing'; import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from 'vitest'; import { EpdfRender, EpdfRenderLayer, RENDER_DEFAULTS, RenderToken, withRender, type PicturePage, type PicturePainter, } from '@embedpdf/angular/render'; import { injectPage, paintsPagePart } from '@embedpdf/angular/runtime'; import { EpdfPageTemplate, EpdfStage, StageToken, withStage } from '@embedpdf/angular/stage'; import { definePlugin, type Engine } from '@embedpdf/core'; import { bytesInput, fakeEngine, handleFor, kernelOf, mount, viewerHost } from './fixtures'; const measured = ['clientWidth', 'clientHeight'] as const; const originals = measured.map((name) => Object.getOwnPropertyDescriptor(HTMLElement.prototype, name), ); beforeAll(() => { Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, get: () => 800, }); Object.defineProperty(HTMLElement.prototype, 'clientHeight', { configurable: true, get: () => 600, }); }); afterAll(() => { measured.forEach((name, index) => { const original = originals[index]; if (original) Object.defineProperty(HTMLElement.prototype, name, original); }); }); afterEach(() => TestBed.resetTestingModule()); describe('EpdfRender', () => { it('reads and changes the settings with no document', async () => { const fixture = await mount( viewerHost({ template: '', features: [withRender({ fullPage: { maxWidth: 320 } })] }), ); const render = fixture.debugElement.injector.get(EpdfRender); expect(render.settings().fullPage.maxWidth).toBe(320); expect(render.settings().tiles).toEqual(RENDER_DEFAULTS.tiles); render.updateSettings({ tiles: false }); expect(render.settings().tiles).toBe(false); }); it('streams redraws as invalidated$', async () => { const fixture = await mount( viewerHost({ template: '', config: { engine: fakeEngine(2).engine }, features: [withRender()], }), ); const render = fixture.debugElement.injector.get(EpdfRender); const redraws: number[] = []; render.invalidated$.subscribe(({ pages }) => redraws.push(pages.length)); await kernelOf(fixture).documents.open(bytesInput('a')); render.invalidate({ pages: [0] }); render.invalidate(); expect(redraws).toEqual([1, 2]); }); }); /** A layer that paints the page's annotations itself, as `` does. */ @Component({ selector: 'test-annotation-painter', template: '' }) class AnnotationPainter { constructor() { const page = injectPage(''); paintsPagePart(() => page.ref, 'annotations'); } } describe('', () => { it('leaves out what a layer on the page paints, from the first request', async () => { const asked: Record[] = []; const fixture = await mount( viewerHost({ imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer, AnnotationPainter], config: { engine: recordingEngine(asked) }, features: [withStage({ zoom: { pageWidth: 100 } }), withRender()], template: ` `, }), ); await kernelOf(fixture).documents.open(bytesInput('a')); await vi.waitFor(async () => { await fixture.whenStable(); expect(asked.length).toBeGreaterThan(0); }); const parts = asked.map(({ includeAnnotations, includeFormFields }) => [ includeAnnotations, includeFormFields, ]); expect(parts).toEqual(asked.map(() => [false, true])); }); it('draws one picture on every page of a Stage', async () => { const fixture = await mount( viewerHost({ imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer], config: { engine: fakeEngine(2).engine }, features: [withStage({ zoom: { pageWidth: 100 } }), withRender()], template: ` `, }), ); await kernelOf(fixture).documents.open(bytesInput('a')); await vi.waitFor(async () => { await fixture.whenStable(); expect(fixture.nativeElement.querySelectorAll('epdf-render-layer img')).toHaveLength(2); }); }); // A binding applies the URL at the next change detection, and the page's picture can move on // before that, revoking the URL: the image would then be handed a revoked URL and lose its // picture. Here the page is redrawn while its first picture's URL is being handed out. it('never hands an image a revoked URL when the picture moves on before change detection', async () => { const revoked = new Set(); const handedRevoked: string[] = []; let render: EpdfRender | undefined; const setAttribute = Element.prototype.setAttribute; const spy = vi.spyOn(Element.prototype, 'setAttribute').mockImplementation(function ( this: Element, name: string, value: string, ) { if (name === 'src' && revoked.has(value)) handedRevoked.push(value); setAttribute.call(this, name, value); }); try { const fixture = await mount( viewerHost({ imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer], config: { engine: renderingEngine( (url) => { if (url === 'blob:picture-1') render?.invalidate(); }, (url) => revoked.add(url), ), }, features: [withStage({ zoom: { pageWidth: 100 } }), withRender({ tiles: false })], template: ` `, }), ); render = fixture.debugElement.injector.get(EpdfRender); await kernelOf(fixture).documents.open(bytesInput('a')); await vi.waitFor(async () => { await fixture.whenStable(); const image = fixture.nativeElement.querySelector('epdf-render-layer img'); expect(image?.src).toBe('blob:picture-2'); }); expect(revoked.has('blob:picture-1')).toBe(true); expect(handedRevoked).toEqual([]); } finally { spy.mockRestore(); } }); }); /** A promise with the `abort` / `abortWith` the engine's tasks have. */ function task(value: T) { const promise = Promise.resolve(value); return Object.assign(promise, { abort: () => {}, abortWith: () => promise }); } /** * An engine whose one page renders at once. `onUrl` hears each picture's URL as it's handed out, * `onRevoke` each one let go of. */ /** An engine whose pictures record the options they were asked with. */ function recordingEngine(asked: Record[]) { const handle = { ...handleFor('a', 2), render: { getPolicy: () => Promise.resolve({ kind: 'continuous' }) }, page: () => ({ render: { image: (options: Record) => { asked.push(options); const url = `blob:picture-${asked.length}`; return task({ objectUrl: () => task({ url, revoke: () => {} }) }); }, }, }), }; return { open: () => Promise.resolve(handle), destroy: () => Promise.resolve(), } as unknown as Engine; } function renderingEngine(onUrl: (url: string) => void, onRevoke: (url: string) => void) { let calls = 0; const handle = { ...handleFor('a'), render: { getPolicy: () => Promise.resolve({ kind: 'continuous' }) }, page: () => ({ render: { image: () => { const url = `blob:picture-${++calls}`; return task({ objectUrl: () => { onUrl(url); return task({ url, revoke: () => onRevoke(url) }); }, }); }, }, }), }; return { open: () => Promise.resolve(handle), destroy: () => Promise.resolve(), } as unknown as Engine; } /** * A plugin that takes the pictures over as it connects, as a plugin that paints them does, and * records what it's lent: the box and page it paints, its updates, its end. */ function paintingFeature(paints: (view: string) => boolean = () => true) { const painted: { host: HTMLElement; page: number; scale: number }[] = []; const updates: PicturePage[] = []; const destroyed = vi.fn(); const painter: PicturePainter = { paints, paint: (host, page) => { painted.push({ host, page: page.ref.objectNumber, scale: page.transform.viewScale }); return { update: (next) => updates.push(next), destroy: destroyed }; }, }; let giveBack = () => {}; const plugin = definePlugin({ id: 'painting', scope: 'document', requires: [RenderToken], create: (ctx) => ({ api: {}, connect() { giveBack = ctx.get(RenderToken).handOverPictures(painter); }, }), }); return { feature: { plugins: [plugin], services: [] }, painted, updates, destroyed, giveBack: () => giveBack(), }; } async function paintedStage(painting: ReturnType) { const asked: Record[] = []; const fixture = await mount( viewerHost({ imports: [EpdfStage, EpdfPageTemplate, EpdfRenderLayer], config: { engine: recordingEngine(asked) }, features: [withStage({ zoom: { pageWidth: 100 } }), withRender(), painting.feature], template: ` `, }), ); await kernelOf(fixture).documents.open(bytesInput('a')); return { fixture, asked }; } describe('a plugin that paints the pictures', () => { it('is lent the page box, follows the zoom, and never lets the raster be fetched', async () => { const painting = paintingFeature(); const { fixture, asked } = await paintedStage(painting); await vi.waitFor(async () => { await fixture.whenStable(); expect(painting.painted.length).toBeGreaterThan(0); }); expect(painting.painted[0]!.page).toBe(1); expect(painting.painted[0]!.host.isConnected).toBe(true); expect(fixture.nativeElement.querySelectorAll('epdf-render-layer img')).toHaveLength(0); expect(asked).toEqual([]); const painted = painting.painted.length; kernelOf(fixture).capability(StageToken).zoomTo(2); await fixture.whenStable(); expect(painting.updates.at(-1)!.transform.viewScale).toBeGreaterThan( painting.painted[0]!.scale, ); expect(painting.painted).toHaveLength(painted); // the same pictures, updated // Given back, the render plugin draws the pages again. painting.giveBack(); await vi.waitFor(async () => { await fixture.whenStable(); expect(asked.length).toBeGreaterThan(0); }); expect(painting.destroyed).toHaveBeenCalledTimes(painted); }); it('leaves the views it doesn’t paint to the render plugin', async () => { const painting = paintingFeature(() => false); const { fixture, asked } = await paintedStage(painting); await vi.waitFor(async () => { await fixture.whenStable(); expect(asked.length).toBeGreaterThan(0); }); expect(painting.painted).toEqual([]); }); });