Files
gods-eye-view/src/testSupport/kartaViewFakes.mjs
T
Sameh KhamisandManjunath Paparaju 2a0e21774b Street Level photo viewer: fresh elements per mount, and one viewer drawn
Fixes from review of the move to StreetLevelPhotoViewer:

- Each mount and each panorama canvas now takes a key no viewer reuses,
  and the view keys its root by the mount. Before, switching KartaView and
  Panoramax in one tick let Vue patch the old viewer's canvas, whose
  destroyed renderer had lost its context, into the new one, so 360°
  photos fell back to their front view.
- Of the viewers mounting in the host, the view draws the one mounted
  last, by mount key (photoViewers.js, which subscribePhotoViewer() now
  uses), whatever order they present in. A viewer still mounting when a
  later one is drawn is superseded: it unmounts and its mount fails as
  closed, rather than staying pending and being drawn once the later one
  goes.
- A panorama's first image is uploaded once the view has drawn its canvas
  shown (the view hands the canvas back on every draw), so it is never
  sized into a hidden canvas.
- The tests' fake view now draws as Vue does: once per tick with the final
  value, keeping elements while their keys are unchanged and handing every
  element back on each draw. StreetLevelPhotoViewer.test.mjs covers the
  switch in one tick and the overtaken mount with the real view.
- The guard test's check that the photo viewer writes no DOM covers
  inserting, moving, removing and replacing elements, attributes, classes,
  styles and the hidden, disabled and text properties.

Co-authored-by: Manjunath Paparaju <[email protected]>
2026-11-19 13:52:36 -08:00

191 lines
6.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// DOM stand-ins for the KartaView viewer's tests (no browser in Node).
/** A DOM element with what the viewer touches: children, events, attributes. */
function fakeElement(tag) {
const listeners = new Map();
const element = {
tagName: tag.toUpperCase(),
children: [],
parentNode: null,
style: {},
dataset: {},
attributes: {},
hidden: false,
disabled: false,
clientWidth: 400,
clientHeight: 250,
classList: {
values: new Set(),
add: (name) => element.classList.values.add(name),
remove: (name) => element.classList.values.delete(name),
},
append(...nodes) {
for (const node of nodes) {
node.parentNode = element;
element.children.push(node);
}
},
remove() {
const list = element.parentNode?.children;
if (list) list.splice(list.indexOf(element), 1);
element.parentNode = null;
},
replaceWith(next) {
const parent = element.parentNode;
if (!parent) return;
parent.children[parent.children.indexOf(element)] = next;
next.parentNode = parent;
next.ownerDocument = element.ownerDocument;
element.parentNode = null;
},
setAttribute: (name, value) => (element.attributes[name] = value),
removeAttribute: (name) => {
delete element.attributes[name];
if (name === 'src') element.src = '';
},
addEventListener: (type, handler) => listeners.set(type, handler),
removeEventListener: (type) => listeners.delete(type),
setPointerCapture() {},
/** Fire an event at the element's listener. */
fire(type, event = {}) {
return listeners.get(type)?.({
preventDefault() {
this.defaultPrevented = true;
},
stopPropagation() {},
...event,
});
},
};
return element;
}
/**
* A host element; given a photo viewer, its view is drawn as
* StreetLevelPhotoViewer draws it (see `drawPhotoViewer`).
*/
export function fakeHost(viewer = null) {
const host = fakeElement('div');
if (viewer) drawPhotoViewer(viewer);
return host;
}
/**
* Draw a photo viewer's presentations with fake elements, as
* StreetLevelPhotoViewer does through Vue: changes made in one tick are
* drawn once, on a microtask, with their final value; the root (keyed by
* the mount) holds the picture, the panorama canvas (keyed by its canvas
* key) and the ‹ › buttons, kept while their keys are, and every element
* is handed back on each draw. A value is written only when the drawn
* presentation changes it, and the elements' events reach the viewer's
* actions.
* @returns {() => void} Stop drawing.
*/
export function drawPhotoViewer(viewer) {
let parts = null;
let last = null;
let pending = null;
let queued = false;
const on = (element, type, name) =>
element.addEventListener(type, (event) => last?.actions[name](event));
const canvasFor = () => {
const canvas = fakeElement('canvas');
canvas.classList.add('sl-photo-pano');
for (const type of ['pointerdown', 'pointermove', 'pointerup', 'wheel'])
on(canvas, type, type);
on(canvas, 'pointercancel', 'pointerup');
on(canvas, 'webglcontextlost', 'contextLost');
return canvas;
};
const build = (view) => {
const root = fakeElement('div');
const picture = fakeElement('img');
const canvas = canvasFor();
const button = (delta) => {
const element = fakeElement('button');
element.addEventListener('click', (event) =>
last?.actions.step(delta, event),
);
return element;
};
const prev = button(-1);
const next = button(1);
on(root, 'keydown', 'keydown');
root.append(picture, canvas, prev, next);
view.host.append(root);
return { root, picture, canvas, prev, next };
};
const draw = (view) => {
if (!view) {
parts?.root.remove();
parts = last = null;
return;
}
if (!parts || last.mountKey !== view.mountKey) {
parts?.root.remove();
parts = build(view);
last = null;
} else if (last.host !== view.host) {
parts.root.remove();
view.host.append(parts.root);
}
const changed = (key) => !last || last[key] !== view[key];
let canvasFresh = !last;
if (last && changed('canvasKey')) {
const canvas = canvasFor();
parts.canvas.replaceWith(canvas);
parts.canvas = canvas;
canvasFresh = true;
}
const { root, picture, canvas, prev, next } = parts;
if (changed('providerId')) root.dataset.provider = view.providerId;
if (changed('label')) root.attributes['aria-label'] = view.label;
if (changed('kind') && view.kind !== undefined)
root.dataset.kind = view.kind;
if (changed('alt')) picture.alt = view.alt;
if (changed('src')) {
if (view.src) picture.src = view.src;
else picture.removeAttribute('src');
}
if (changed('fit')) picture.style.objectFit = view.fit;
if (changed('kind')) picture.hidden = view.kind !== 'flat';
if (changed('kind') || canvasFresh) canvas.hidden = view.kind !== 'pano';
if (changed('dragging') || canvasFresh) {
if (view.dragging) canvas.classList.add('sl-photo-dragging');
else canvas.classList.remove('sl-photo-dragging');
}
if (changed('prevLabel')) prev.attributes['aria-label'] = view.prevLabel;
if (changed('nextLabel')) next.attributes['aria-label'] = view.nextLabel;
if (changed('prevDisabled')) prev.disabled = view.prevDisabled;
if (changed('nextDisabled')) next.disabled = view.nextDisabled;
last = view;
for (const name of ['root', 'picture', 'canvas', 'prev', 'next'])
view.actions.attach(name, parts[name]);
};
return viewer.subscribeView((view) => {
pending = view;
if (queued) return;
queued = true;
queueMicrotask(() => {
queued = false;
draw(pending);
});
});
}
/**
* Wait, a macrotask at a time, until `condition` holds; false if it never
* does within `ticks`. A test asserting that something did not happen waits
* on a sign that the work is done instead of a fixed count of ticks.
*/
export async function until(condition, { ticks = 200 } = {}) {
for (let i = 0; i < ticks; i++) {
if (await condition()) return true;
await new Promise((resolve) => setTimeout(resolve, 0));
}
return Boolean(await condition());
}
/** Let a photo viewer's view draw what was presented in this tick. */
export const drawn = () => new Promise((resolve) => queueMicrotask(resolve));