File size: 6,225 Bytes
8d52e72 c7eced2 8d52e72 8f9b02f 8d52e72 8f9b02f 8d52e72 822b94b 8d52e72 | 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 | const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const sandbox = { self: {}, setTimeout, clearTimeout, requestAnimationFrame: (callback) => setTimeout(callback, 0), cancelAnimationFrame: clearTimeout };
sandbox.self = sandbox;
for (const file of ["reader/core/reader-contract.js", "reader/core/reader-runtime.js", "reader/formats/reader-format-adapters.js"]) vm.runInNewContext(fs.readFileSync(path.join(__dirname, "../../../static", file), "utf8"), sandbox);
function testSnapshotOnlyWhenObserved() {
let snapshots = 0;
const runtime = sandbox.VoiceOfMLReaderRuntime.createReaderRuntime({initialState: {formats: {
get pdf() { snapshots++; return {}; }
}}});
runtime.events.on('state:panel', () => assert.fail('unrelated event'));
for (let page = 1; page <= 100; page++) runtime.update('document', {page});
assert.strictEqual(runtime.state.document.page, 100);
assert.strictEqual(snapshots, 0, 'no document observer');
const received = [];
const off = runtime.events.on('state:document', state => {
assert.ok(Object.isFrozen(state) && Object.isFrozen(state.document));
received.push(['specific', state.document.page]);
runtime.events.on('state', next => received.push(['late', next.document.page]));
});
const wildcard = runtime.events.on('*', (state, kind) => received.push([kind, state.document.page]));
runtime.update('document', {page: 101});
assert.strictEqual(snapshots, 1);
assert.deepStrictEqual(received, [['specific',101], ['state:document',101], ['late',101], ['state',101]]);
off(); wildcard();
runtime.events.dispose();
runtime.update('document', {page: 102});
assert.strictEqual(snapshots, 1);
const emitted = [];
const injected = sandbox.VoiceOfMLReaderRuntime.createReaderRuntime({events: {
emit(type, state) { emitted.push([type, state.document.page]); }
}});
injected.update('document', {page: 9});
assert.deepStrictEqual(emitted, [['state:document',9], ['state',9]]);
}
async function main() {
testSnapshotOnlyWhenObserved();
const events = [];
const runtime = sandbox.VoiceOfMLReaderRuntime.createReaderRuntime();
runtime.events.on("*", (_detail, type) => events.push(type));
assert.strictEqual(runtime.update("source", { url: "book" }), true);
assert.strictEqual(runtime.snapshot().source.url, "book");
let documentEvent;
runtime.events.on("state:document", (state) => { documentEvent = state; });
assert.strictEqual(runtime.update("document", { page: 7, restorationReady: true }), true);
assert.strictEqual(documentEvent.document.page, 7);
assert.strictEqual(documentEvent.document.restorationReady, true);
const delayed = runtime.schedule(() => { throw new Error("disposed timer fired"); }, 10000); runtime.cancel(delayed);
assert.strictEqual(runtime.updateFormat("pdf", { activeRenders: 1 }), true);
assert.strictEqual(runtime.snapshot().formats.pdf.activeRenders, 1);
const original = sandbox.VoiceOfMLReader.capability("pdf");
const negotiated = runtime.negotiate(original);
assert.strictEqual(negotiated.extension, original.extension);
assert.strictEqual(negotiated.mode, original.mode);
assert.strictEqual(negotiated.readerMode, original.readerMode);
assert.strictEqual(negotiated.article, original.article);
assert.notStrictEqual(negotiated.features, original.features);
assert.ok(Object.isFrozen(negotiated) && Object.isFrozen(negotiated.features));
const capability = runtime.negotiate({ mode: "pdf", extension: "pdf" });
assert.deepStrictEqual(JSON.parse(JSON.stringify(capability.features)), { toc: true, search: true, zoom: true, bookmarks: true, pagination: true, media: false });
const first = runtime.nextGeneration("navigation");
assert.strictEqual(runtime.currentGeneration("navigation"), first);
assert.strictEqual(runtime.isCurrent("navigation", first), true);
runtime.nextGeneration("navigation");
assert.strictEqual(runtime.isCurrent("navigation", first), false);
assert.strictEqual(runtime.currentGeneration("search"), 0);
const pdfGeneration = runtime.nextGeneration("pdf");
assert.strictEqual(runtime.currentGeneration("pdf"), pdfGeneration);
assert.strictEqual(runtime.currentGeneration("navigation"), first + 1);
assert.strictEqual(runtime.setStage("prepare"), true);
runtime.fail("READER_PARSE");
assert.strictEqual(runtime.state.lifecycle.phase, "failed");
assert.ok(events.includes("state:source") && events.includes("capability") && events.includes("generation") && events.includes("error"));
const calls = [];
runtime.track(() => calls.push("first")); runtime.track({ dispose: () => calls.push("second") }); runtime.dispose();
assert.deepStrictEqual(calls, ["second", "first"]); assert.strictEqual(runtime.state.lifecycle.phase, "disposed");
assert.strictEqual(runtime.currentGeneration("navigation"), first + 2);
assert.strictEqual(runtime.currentGeneration("pdf"), pdfGeneration + 1);
assert.strictEqual(runtime.update("source", { url: "late" }), false); assert.strictEqual(runtime.setPhase("ready"), false);
const registry = sandbox.VoiceOfMLReaderAdapters.createAdapterRegistry();
assert.throws(() => registry.register("bad", {}), /READER_ADAPTER_METHOD:bad:open/);
let finishOpen; const trace = [];
assert.deepStrictEqual(Array.from(sandbox.VoiceOfMLReaderAdapters.METHODS), ["open", "render", "restore", "dispose"]);
const adapter = registry.register("pdf", {
open: () => new Promise((resolve) => { finishOpen = () => { trace.push("open"); resolve("prepared"); }; }),
render: (value) => trace.push(`render:${value}`),
restore: (value) => trace.push(`restore:${value}`),
dispose: () => trace.push("dispose"),
});
assert.strictEqual(registry.activate("pdf"), adapter);
const opening = adapter.open();
adapter.restore("position");
finishOpen();
await adapter.render(await opening);
registry.dispose();
assert.deepStrictEqual(trace, ["restore:position", "open", "render:prepared", "dispose"]);
assert.throws(() => registry.register("pdf", adapter), /READER_ADAPTER_DUPLICATE/);
console.log("reader runtime contracts passed");
}
main().catch((error) => { console.error(error); process.exitCode = 1; });
|