Files
gods-eye-view/src/input/testDoubles.mjs
T
Sameh KhamisandManjunath Paparaju 0e5c5e624c Merge origin/main into space-missions-fixes
Main's single click rule (the card or label on top takes the click, read
once per click through clickCard and isUnderOtherCard in
src/data/pickRegistry.js) covers a click on a Space Missions label: a
mission marker label is an interactive world-overlay entry, so the
host's hit test names it as the card on top for every other layer. The
per-layer label guards in CCTV, Drone Airspace, FIRMS, Ghost Maps, News,
Perimeters, Shipwrecks, Vessels, Wildlife Sightings, ALPR, Local ADS-B
and the cockpit orbital focus are dropped for main's versions.

The mission-label tests are kept on main's API: fakeInputUnderLabel now
also names the label as the card on top, and each test shows a click on
the label never acts on the card, camera, vessel, fire, reader, aircraft
or report under it, and leaves the selection as it is. A launches test
checks that a mission label is interactive and is the card on top for
every other layer. Docs, changelog and the dispatcher's `overlay` note
describe the merged behaviour.

Co-authored-by: Manjunath Paparaju <[email protected]>
2026-11-18 20:00:25 -08:00

182 lines
6.3 KiB
JavaScript

/**
* Test doubles for the input dispatcher: a stand-in for Cesium's
* `ScreenSpaceEventHandler`, and a dispatcher made on it ahead of the code
* under test, so that code's `inputFor(viewer)` gets the fake. Imported by
* tests only.
*
* The dispatcher's input port is the Cesium adapter's (`findInput`, over the
* fake handler), and its clicks pick through {@link sceneQuery}: the
* viewer's renderer's query port where it has one that picks, else the
* viewer double's own `scene.pick` and `drillPick`, read as the renderer's
* picks are.
*/
import * as Cesium from 'cesium';
// The adapter's unbound input port and a click's own Cesium picks.
import '../render/cesium/input.js';
import '../render/cesium/nativeClick.js';
import { rendererOf } from '../render/rendererOf.js';
import { fakeScenePicks } from '../testSupport/fakeCesiumScene.mjs';
import { provideStyleStageAlias } from '../styles/styleStages.js';
import { inputFor } from './dispatcher.js';
import { setClickCardHitTest } from '../data/pickRegistry.js';
import { ROCKET_MISSION_AMBIENT_OVERLAY_SOURCE_ID } from '../layers/launches/policy.js';
/** Cesium's own event types, the ones the input port binds. */
export const FAKE_EVENT_TYPES = Cesium.ScreenSpaceEventType;
// Tests record sensor styles on the viewer double's scene, as the code under
// them once did: the dispatcher, which looks them up by the viewer, finds
// them there too.
provideStyleStageAlias((viewer) => viewer?.scene ?? null);
/**
* A query port for a viewer double's clicks: its renderer's, where that
* picks; else one over the double's scene (`fakeScenePicks`), with its depth
* and ellipsoid picks.
* @param {object} viewer
*/
export function sceneQuery(viewer) {
const bound = () => {
const query = rendererOf(viewer)?.query;
return typeof query?.pick === 'function' ? query : null;
};
const at = (x, y) => new Cesium.Cartesian2(x, y);
const own = {
...fakeScenePicks(viewer),
pickPosition(x, y) {
const scene = viewer.scene;
if (
!scene?.pickPositionSupported ||
typeof scene.pickPosition !== 'function'
)
return null;
return scene.pickPosition(at(x, y)) ?? null;
},
pickEllipsoid(x, y) {
const camera = viewer.camera ?? viewer.scene?.camera;
return camera?.pickEllipsoid?.(at(x, y)) ?? null;
},
};
return new Proxy(own, {
get(target, key) {
const query = bound();
if (query) {
const value = query[key];
return typeof value === 'function' ? value.bind(query) : value;
}
return target[key];
},
});
}
export class FakeScreenSpaceEventHandler {
constructor() {
this.actions = new Map();
this.destroyed = false;
}
/** One action per type and modifier, as Cesium keeps them. */
static key(type, modifier) {
return modifier === undefined ? type : `${type}+${modifier}`;
}
setInputAction(fn, type, modifier) {
this.actions.set(FakeScreenSpaceEventHandler.key(type, modifier), fn);
}
getInputAction(type, modifier) {
return this.actions.get(FakeScreenSpaceEventHandler.key(type, modifier));
}
removeInputAction(type, modifier) {
this.actions.delete(FakeScreenSpaceEventHandler.key(type, modifier));
}
/** Raise an action as Cesium does for a press with no modifier held. */
fire(type, event) {
this.actions.get(type)?.(event);
}
isDestroyed() {
return this.destroyed;
}
destroy() {
this.destroyed = true;
}
}
/**
* Make `viewer`'s dispatcher on a fake handler and return ways to drive it.
* @param {object} viewer
* @param {{now?: () => number}} [options]
*/
export function fakeInput(viewer, { now } = {}) {
const handler = new FakeScreenSpaceEventHandler();
// A Cesium viewer's canvas is its scene's.
if (!('canvas' in viewer))
Object.defineProperty(viewer, 'canvas', {
configurable: true,
get: () => viewer.scene?.canvas,
});
const input = inputFor(viewer, {
createHandler: () => handler,
document: null,
query: sceneQuery(viewer),
...(now ? { now } : {}),
});
const T = FAKE_EVENT_TYPES;
return {
input,
handler,
down: (position) => handler.fire(T.LEFT_DOWN, { position }),
move: (endPosition, startPosition = endPosition) =>
handler.fire(T.MOUSE_MOVE, { startPosition, endPosition }),
up: (position) => handler.fire(T.LEFT_UP, { position }),
/** A bare LEFT_CLICK, as Cesium raises it after a tap. */
click: (position = { x: 10, y: 10 }) =>
handler.fire(T.LEFT_CLICK, { position }),
/** Down, up and click at one spot. */
tap(position = { x: 10, y: 10 }) {
handler.fire(T.LEFT_DOWN, { position });
handler.fire(T.LEFT_UP, { position });
handler.fire(T.LEFT_CLICK, { position });
},
dblclick: (position = { x: 10, y: 10 }) =>
handler.fire(T.LEFT_DOUBLE_CLICK, { position }),
/** Whether the viewer's stock `select` or `dblclickFollow` runs now. */
stockEnabled: (name) => input.stockEnabled(name),
destroy: () => input.destroy(),
};
}
/** The Space Missions label {@link fakeInputUnderLabel} paints: an
* interactive entry of the missions' world-overlay source. */
export const MISSION_LABEL = Object.freeze({
sourceId: ROCKET_MISSION_AMBIENT_OVERLAY_SOURCE_ID,
entryId: 'launch:L1',
});
/**
* `fakeInput` with a Space Missions label painted over the map: while
* `label.over` is true the label takes each click, so every click subscriber
* hears it as an overlay click (`event.overlay`) and the geometry beneath it
* is not picked, and the pick registry names it as the card on top
* (`clickCard`), as the world-overlay host's hit test names an interactive
* mission label. `destroy` also clears that hit test.
* @param {object} viewer
* @param {{now?: () => number, beneath?: () => ({sourceId: string, entryId: string}|null)}} [options]
* `beneath`: the card painted under the label, on top when the label is
* not over the click.
*/
export function fakeInputUnderLabel(
viewer,
{ beneath = () => null, ...options } = {},
) {
const fake = fakeInput(viewer, options);
const label = { over: false };
fake.input.onOverlayClick(() => label.over);
setClickCardHitTest(() => (label.over ? MISSION_LABEL : beneath()));
return {
...fake,
label,
destroy() {
setClickCardHitTest(null);
fake.destroy();
},
};
}