338 lines
17 KiB
TypeScript
338 lines
17 KiB
TypeScript
import { expect, it, vi } from "vitest";
|
|
import { RemoteTouchpad } from "../src/client/remote-control/touchpad";
|
|
import type { RemoteInputEvent } from "../src/client/remote-control/input-session";
|
|
import { picturePoint, pictureRect, rotatePicturePoint, type VideoRotation } from "../src/client/media/video-geometry";
|
|
|
|
it("targets the touched picture directly, ignores letterboxing and retires gestures when mouse mode changes", () => {
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
try {
|
|
const video = { videoWidth: 400, videoHeight: 200, style: { objectFit: "contain" }, dataset: {},
|
|
getBoundingClientRect: () => ({ left: 0, top: 0, width: 400, height: 300 }) } as unknown as HTMLVideoElement;
|
|
const events: RemoteInputEvent[] = [];
|
|
const pad = new RemoteTouchpad(() => {
|
|
const picture = pictureRect(video);
|
|
return picture && { ...picture, rotation: 0 };
|
|
}, event => events.push(event), () => {}, () => {},
|
|
{ scale: 1, transform: () => {} });
|
|
pad.down(1, 320, 10, picturePoint(video, 320, 10)); pad.move(1, 350, 100); pad.up(1);
|
|
expect(events).toEqual([]); expect(vi.getTimerCount()).toBe(0);
|
|
|
|
pad.down(1, 320, 100, picturePoint(video, 320, 100)); pad.up(1);
|
|
expect(events).toEqual([{ type: "move", x: .8, y: .25 },
|
|
{ type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
|
|
events.length = 0;
|
|
pad.down(2, 80, 150, picturePoint(video, 80, 150)); vi.advanceTimersByTime(500);
|
|
pad.move(2, 160, 190); pad.up(2);
|
|
expect(events).toEqual([{ type: "move", x: .2, y: .5 }, { type: "button", button: 0, down: true },
|
|
{ type: "move", x: .4, y: .7 }, { type: "button", button: 0, down: false }]);
|
|
|
|
events.length = 0;
|
|
pad.down(3, 100, 150, picturePoint(video, 100, 150)); vi.advanceTimersByTime(500);
|
|
pad.setMode("trackpad"); pad.up(3);
|
|
expect(events.filter(event => event.type === "button")).toEqual([
|
|
{ type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
|
|
expect(events.at(-1)).toEqual({ type: "move", x: .25, y: .5 });
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
events.length = 0;
|
|
pad.down(4, 100, 150, { x: .9, y: .9 }); pad.move(4, 140, 150); pad.up(4);
|
|
expect(events).toEqual([{ type: "move", x: .35, y: .5 }]);
|
|
pad.setMode("direct");
|
|
pad.down(5, 320, 100, picturePoint(video, 320, 100)); pad.up(5);
|
|
expect(events.at(-3)).toEqual({ type: "move", x: .8, y: .25 });
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("anchors expansion in source coordinates and refreshes geometry without input", () => {
|
|
const video = { videoWidth: 400, videoHeight: 200, style: { objectFit: "contain" }, dataset: {},
|
|
getBoundingClientRect: () => ({ left: 0, top: 0, width: 400, height: 300 }) } as unknown as HTMLVideoElement;
|
|
const events: RemoteInputEvent[] = [], changed = vi.fn();
|
|
const pad = new RemoteTouchpad(() => {
|
|
const picture = pictureRect(video);
|
|
return picture && { ...picture, rotation: 0 };
|
|
}, event => events.push(event), () => {}, changed, { scale: 1, transform: () => {} });
|
|
pad.setMode("trackpad", picturePoint(video, 320, 100));
|
|
expect(pad.position).toEqual({ x: .8, y: .25 });
|
|
expect(events).toEqual([{ type: "move", x: .8, y: .25 }]);
|
|
expect(changed.mock.calls).toEqual([[pad.position]]);
|
|
pad.moveBy(-40, 20);
|
|
expect(pad.position.x).toBeCloseTo(.7); expect(pad.position.y).toBeCloseTo(.35);
|
|
expect(events.at(-1)).toEqual({ type: "move", ...pad.position });
|
|
expect(changed).toHaveBeenLastCalledWith(pad.position);
|
|
pad.setMode("direct");
|
|
events.length = 0; changed.mockClear();
|
|
pad.setMode("trackpad", picturePoint(video, 320, 10));
|
|
expect(events).toEqual([{ type: "move", ...pad.position }]);
|
|
expect(changed.mock.calls).toEqual([[pad.position]]);
|
|
pad.setMode("trackpad", { x: .1, y: .9 });
|
|
expect(pad.position).toEqual({ x: .1, y: .9 });
|
|
expect(events.at(-1)).toEqual({ type: "move", x: .1, y: .9 });
|
|
events.length = 0; changed.mockClear();
|
|
pad.setMode("trackpad");
|
|
expect(changed).not.toHaveBeenCalled();
|
|
const position = pad.position;
|
|
pad.refresh();
|
|
expect(changed.mock.calls).toEqual([[position]]);
|
|
expect(pad.position).toBe(position);
|
|
expect(events).toEqual([]);
|
|
pad.reset();
|
|
});
|
|
|
|
it("shares virtual buttons with picture gestures until their final owner releases", () => {
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
try {
|
|
const events: RemoteInputEvent[] = [];
|
|
const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }), event => events.push(event),
|
|
() => {}, () => {}, { scale: 1, transform: () => {} });
|
|
pad.setMode("trackpad");
|
|
events.length = 0;
|
|
const buttons = () => events.filter(event => event.type === "button");
|
|
pad.button(0, true);
|
|
pad.down(1, 20, 20, null); pad.move(1, 24, 22); pad.up(1);
|
|
pad.down(2, 20, 20, null); vi.advanceTimersByTime(600); pad.move(2, 70, 45); pad.up(2);
|
|
expect(buttons()).toEqual([{ type: "button", button: 0, down: true }]);
|
|
expect(events.at(-1)).toEqual({ type: "move", x: .77, y: .77 });
|
|
pad.button(0, false);
|
|
expect(buttons().at(-1)).toEqual({ type: "button", button: 0, down: false });
|
|
|
|
events.length = 0;
|
|
pad.down(3, 20, 20, null); vi.advanceTimersByTime(500);
|
|
pad.button(0, true); pad.up(3);
|
|
expect(buttons()).toEqual([{ type: "button", button: 0, down: true }]);
|
|
pad.button(0, false);
|
|
expect(buttons()).toHaveLength(2);
|
|
|
|
events.length = 0;
|
|
pad.down(4, 20, 20, null); pad.button(2, true); pad.button(2, false);
|
|
vi.advanceTimersByTime(600); pad.up(4);
|
|
expect(buttons()).toEqual([{ type: "button", button: 2, down: true }, { type: "button", button: 2, down: false }]);
|
|
pad.down(5, 20, 20, null); vi.advanceTimersByTime(500); pad.button(0, true); pad.button(1, true);
|
|
events.length = 0;
|
|
pad.moveBy(20, 10);
|
|
expect(events).toEqual([{ type: "move", ...pad.position }]);
|
|
pad.cancel(5); pad.reset(); pad.up(5); pad.button(0, false); pad.button(1, false); vi.advanceTimersByTime(600);
|
|
expect(buttons()).toEqual([{ type: "button", button: 0, down: false }, { type: "button", button: 1, down: false }]);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("keeps taps, dragging, two-finger gestures and grant retirement distinct", () => {
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
try {
|
|
const events: RemoteInputEvent[] = [], scroll: number[] = [];
|
|
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
|
|
vi.stubGlobal("cancelAnimationFrame", clearTimeout);
|
|
const touchpad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }),
|
|
event => events.push(event), delta => scroll.push(delta), () => {}, { scale: 1, transform: () => {} });
|
|
touchpad.setMode("trackpad");
|
|
events.length = 0;
|
|
touchpad.down(1, 20, 20, null); touchpad.up(1);
|
|
expect(events).toEqual([{ type: "move", x: .5, y: .5 },
|
|
{ type: "button", button: 0, down: true }, { type: "button", button: 0, down: false }]);
|
|
events.length = 0;
|
|
touchpad.down(1, 20, 20, null);
|
|
touchpad.move(1, 24, 22); // Below the tap threshold: retain, don't lose motion.
|
|
touchpad.move(1, 26, 23);
|
|
touchpad.move(1, 70, 45); touchpad.up(1);
|
|
expect(events.at(-1)).toEqual({ type: "move", x: .75, y: .75 });
|
|
expect(events.some(event => event.type === "button")).toBe(false);
|
|
|
|
events.length = 0;
|
|
touchpad.down(1, 20, 20, null); vi.advanceTimersByTime(500); touchpad.move(1, 500, 45);
|
|
expect(touchpad.position).toEqual({ x: 1, y: 1 });
|
|
touchpad.reset(); touchpad.up(1); vi.advanceTimersByTime(1000);
|
|
expect(events.filter(event => event.type === "button")).toEqual([
|
|
{ type: "button", button: 0, down: true }, { type: "button", button: 0, down: false },
|
|
]);
|
|
|
|
events.length = 0;
|
|
touchpad.down(1, 20, 20, null); touchpad.down(2, 40, 20, null); touchpad.up(2);
|
|
touchpad.cancel(2); // Normal pointer-up also emits lostpointercapture.
|
|
touchpad.up(1);
|
|
expect(events.slice(-2)).toEqual([
|
|
{ type: "button", button: 2, down: true }, { type: "button", button: 2, down: false },
|
|
]);
|
|
events.length = 0;
|
|
touchpad.down(1, 20, 20, null); touchpad.down(2, 40, 20, null);
|
|
touchpad.move(1, 20, 50); touchpad.move(2, 40, 50);
|
|
touchpad.up(1); touchpad.move(2, 40, 80); touchpad.up(2);
|
|
expect(scroll.reduce((sum, delta) => sum + delta, 0)).toBe(-30);
|
|
expect(events.some(event => event.type === "button")).toBe(false);
|
|
|
|
touchpad.down(1, 20, 20, null); touchpad.reset(); vi.advanceTimersByTime(600);
|
|
expect(events.some(event => event.type === "button")).toBe(false);
|
|
} finally { vi.useRealTimers(); vi.unstubAllGlobals(); }
|
|
});
|
|
|
|
it("keeps pinch/pan local and never turns the remaining finger into a remote click", () => {
|
|
vi.useFakeTimers();
|
|
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
|
|
vi.stubGlobal("cancelAnimationFrame", clearTimeout);
|
|
try {
|
|
const input = vi.fn(), scroll = vi.fn(), transform = vi.fn();
|
|
const viewport = { scale: 1, transform };
|
|
const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: 0 }), input, scroll, () => {}, viewport);
|
|
pad.setMode("trackpad");
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
input.mockClear();
|
|
pad.move(1, 10, 50); pad.move(2, 190, 50);
|
|
vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 100, y: 50 }, 1.5);
|
|
pad.up(1); pad.move(2, 180, 60); pad.up(2);
|
|
expect(input).not.toHaveBeenCalled();
|
|
expect(scroll).not.toHaveBeenCalled();
|
|
|
|
viewport.scale = 1.5;
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 60, 70); pad.move(2, 180, 70);
|
|
vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
|
|
expect(scroll).not.toHaveBeenCalled();
|
|
pad.up(1); pad.up(2);
|
|
|
|
viewport.scale = .5;
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 60, 70); pad.move(2, 180, 70);
|
|
vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
|
|
expect(scroll).not.toHaveBeenCalled();
|
|
pad.up(1); pad.up(2);
|
|
|
|
// A zoomed two-finger tap remains a right-click, and cancellation discards
|
|
// pending local movement instead of replaying it after the grant ends.
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.up(1); pad.up(2);
|
|
expect(input.mock.calls.slice(-2)).toEqual([
|
|
[{ type: "button", button: 2, down: true }], [{ type: "button", button: 2, down: false }],
|
|
]);
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 10, 50); transform.mockClear(); pad.reset();
|
|
vi.advanceTimersByTime(600);
|
|
expect(transform).not.toHaveBeenCalled();
|
|
} finally { vi.useRealTimers(); vi.unstubAllGlobals(); }
|
|
});
|
|
|
|
it.each([0, 90, 180, 270] as const)("keeps the full mouse fixed while picture pan and pinch reanchor its local target (%s degrees)", rotation => {
|
|
vi.useFakeTimers();
|
|
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => setTimeout(callback, 16));
|
|
vi.stubGlobal("cancelAnimationFrame", clearTimeout);
|
|
const input = vi.fn(), scroll = vi.fn(), changed = vi.fn();
|
|
let picture = { left: 0, top: 0, width: 200, height: 100, rotation };
|
|
const transform = vi.fn((from: { x: number; y: number }, to: { x: number; y: number }, ratio: number) => {
|
|
picture = { ...picture, left: to.x + (picture.left - from.x) * ratio,
|
|
top: to.y + (picture.top - from.y) * ratio, width: picture.width * ratio, height: picture.height * ratio };
|
|
});
|
|
const pad = new RemoteTouchpad(() => picture,
|
|
input, scroll, changed, { scale: 1, transform });
|
|
const clientTarget = () => {
|
|
const point = rotatePicturePoint(pad.position, rotation);
|
|
return { x: picture.left + point.x * picture.width, y: picture.top + point.y * picture.height };
|
|
};
|
|
try {
|
|
pad.setMode("mouse"); input.mockClear(); changed.mockClear();
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
expect(changed).not.toHaveBeenCalled();
|
|
pad.move(1, 60, 70); pad.move(2, 180, 70); vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
|
|
expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
|
|
expect(pad.position).toEqual(rotatePicturePoint({ x: .4, y: .3 }, rotation, true));
|
|
pad.up(1); pad.up(2);
|
|
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 10, 50); pad.move(2, 190, 50); vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 100, y: 50 }, 1.5);
|
|
expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
|
|
pad.move(1, 30, 70); pad.move(2, 210, 70); vi.advanceTimersByTime(16);
|
|
expect(transform).toHaveBeenLastCalledWith({ x: 100, y: 50 }, { x: 120, y: 70 }, 1);
|
|
expect(clientTarget().x).toBeCloseTo(100); expect(clientTarget().y).toBeCloseTo(50);
|
|
pad.up(1); pad.move(2, 220, 80); pad.up(2);
|
|
expect(input).not.toHaveBeenCalled(); expect(scroll).not.toHaveBeenCalled();
|
|
expect(changed).toHaveBeenLastCalledWith(pad.position);
|
|
|
|
// Past the source edge the target stays on that edge instead of inventing
|
|
// coordinates in the gutter; rendering/follow owns its final reachability.
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 440, 50); pad.move(2, 560, 50); vi.advanceTimersByTime(16);
|
|
expect(rotatePicturePoint(pad.position, rotation).x).toBeCloseTo(0);
|
|
expect(input).not.toHaveBeenCalled(); expect(scroll).not.toHaveBeenCalled();
|
|
pad.up(1); pad.up(2);
|
|
|
|
pad.button(0, true); pad.button(2, true);
|
|
pad.down(1, 40, 50, null); pad.down(2, 160, 50, null);
|
|
pad.move(1, 20, 50); transform.mockClear(); input.mockClear();
|
|
pad.cancel(1); pad.reset(); pad.up(2); vi.advanceTimersByTime(600);
|
|
expect(transform).not.toHaveBeenCalled();
|
|
expect(input.mock.calls).toEqual([
|
|
[{ type: "button", button: 0, down: false }], [{ type: "button", button: 2, down: false }],
|
|
]);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally { pad.reset(); vi.useRealTimers(); vi.unstubAllGlobals(); }
|
|
});
|
|
|
|
it("moves across the full source through the rendered zoom without a delayed click", () => {
|
|
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout", "performance"] });
|
|
try {
|
|
const events: RemoteInputEvent[] = [];
|
|
const changed = vi.fn(), scroll = vi.fn(), transform = vi.fn();
|
|
// pictureRect already includes the local zoom; do not divide by scale again.
|
|
const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 400, height: 200, rotation: 0 }),
|
|
event => events.push(event), scroll, changed, { scale: 2, transform });
|
|
pad.setMode("trackpad");
|
|
events.length = 0;
|
|
pad.down(1, 20, 20, null);
|
|
pad.moveBy(100, 50);
|
|
expect(pad.position).toEqual({ x: .75, y: .75 });
|
|
expect(changed).toHaveBeenLastCalledWith(pad.position);
|
|
expect(events).toEqual([{ type: "move", x: .75, y: .75 }]);
|
|
vi.advanceTimersByTime(600); pad.up(1);
|
|
expect(events).toHaveLength(1);
|
|
pad.moveBy(400, 200);
|
|
expect(events.at(-1)).toEqual({ type: "move", x: 1, y: 1 });
|
|
expect(scroll).not.toHaveBeenCalled();
|
|
expect(transform).not.toHaveBeenCalled();
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it.each([
|
|
[0, .75, .5], [90, .5, .25], [180, .25, .5], [270, .5, .75],
|
|
] as const)("aligns picture and widget motion with rotation and releases a held drag (%s°)", (rotation, x, y) => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
const events: RemoteInputEvent[] = [];
|
|
const pad = new RemoteTouchpad(() => ({ left: 0, top: 0, width: 200, height: 100, rotation: rotation as VideoRotation }),
|
|
event => events.push(event), () => {}, () => {}, { scale: 1, transform: () => {} });
|
|
pad.setMode("trackpad");
|
|
events.length = 0;
|
|
pad.down(1, 20, 20, null);
|
|
vi.advanceTimersByTime(500);
|
|
pad.move(1, 70, 20);
|
|
expect(events.at(-1)).toEqual({ type: "move", x, y });
|
|
pad.reset();
|
|
expect(events.at(-1)).toEqual({ type: "button", button: 0, down: false });
|
|
pad.up(1);
|
|
expect(events.filter(event => event.type === "button")).toHaveLength(2);
|
|
events.length = 0;
|
|
pad.position = { x: .5, y: .5 };
|
|
pad.moveBy(50, 0);
|
|
vi.advanceTimersByTime(600);
|
|
expect(events).toEqual([{ type: "move", x, y }]);
|
|
} finally { vi.useRealTimers(); }
|
|
});
|
|
|
|
it("holds diagonal WASD alongside a mouse tap and releases only changed directions", () => {
|
|
const events: RemoteInputEvent[] = [];
|
|
const pad = new RemoteTouchpad(() => null, event => events.push(event), () => {}, () => {}, { scale: 1, transform: () => {} });
|
|
pad.direction(0, -1);
|
|
pad.direction(1, -1);
|
|
pad.button(0, true); pad.button(0, false);
|
|
pad.direction(1, 0);
|
|
pad.direction(0.05, 0.05);
|
|
pad.direction(-1, 0);
|
|
pad.reset();
|
|
expect(events.filter(event => event.type === "key")).toEqual([
|
|
{ type: "key", code: "KeyW", down: true }, { type: "key", code: "KeyD", down: true },
|
|
{ type: "key", code: "KeyW", down: false }, { type: "key", code: "KeyD", down: false },
|
|
{ type: "key", code: "KeyA", down: true }, { type: "key", code: "KeyA", down: false },
|
|
]);
|
|
});
|