Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
159 lines
4.1 KiB
TypeScript
159 lines
4.1 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { test } from "node:test";
|
|
import {
|
|
type MediaQueryListLike,
|
|
watchDevicePixelRatio,
|
|
} from "./rendererDprWatch";
|
|
|
|
class FakeMediaQueryList implements MediaQueryListLike {
|
|
readonly query: string;
|
|
modernListeners: Array<() => void> = [];
|
|
legacyListeners: Array<() => void> = [];
|
|
private readonly supportsModern: boolean;
|
|
|
|
constructor(query: string, supportsModern = true) {
|
|
this.query = query;
|
|
this.supportsModern = supportsModern;
|
|
if (!supportsModern) {
|
|
// Strip the modern API to emulate legacy environments.
|
|
this.addEventListener = undefined;
|
|
this.removeEventListener = undefined;
|
|
}
|
|
}
|
|
|
|
addEventListener? = (_type: "change", listener: () => void) => {
|
|
this.modernListeners.push(listener);
|
|
};
|
|
|
|
removeEventListener? = (_type: "change", listener: () => void) => {
|
|
this.modernListeners = this.modernListeners.filter((l) => l !== listener);
|
|
};
|
|
|
|
addListener = (listener: () => void) => {
|
|
this.legacyListeners.push(listener);
|
|
};
|
|
|
|
removeListener = (listener: () => void) => {
|
|
this.legacyListeners = this.legacyListeners.filter((l) => l !== listener);
|
|
};
|
|
|
|
trigger() {
|
|
for (const l of [...this.modernListeners, ...this.legacyListeners]) l();
|
|
}
|
|
|
|
get listenerCount() {
|
|
return this.modernListeners.length + this.legacyListeners.length;
|
|
}
|
|
}
|
|
|
|
function makeEnv(initialDpr: number, supportsModern = true) {
|
|
let dpr = initialDpr;
|
|
const created: FakeMediaQueryList[] = [];
|
|
return {
|
|
created,
|
|
getDevicePixelRatio: () => dpr,
|
|
matchMedia: (query: string) => {
|
|
const mql = new FakeMediaQueryList(query, supportsModern);
|
|
created.push(mql);
|
|
return mql;
|
|
},
|
|
setDpr: (value: number) => {
|
|
dpr = value;
|
|
},
|
|
};
|
|
}
|
|
|
|
test("registers a change listener for the current devicePixelRatio", () => {
|
|
const env = makeEnv(1);
|
|
watchDevicePixelRatio({
|
|
getDevicePixelRatio: env.getDevicePixelRatio,
|
|
matchMedia: env.matchMedia,
|
|
onChange: () => {},
|
|
});
|
|
|
|
assert.equal(env.created.length, 1);
|
|
assert.equal(env.created[0].query, "(resolution: 1dppx)");
|
|
assert.equal(env.created[0].listenerCount, 1);
|
|
});
|
|
|
|
test("invokes onChange when the media query reports a change", () => {
|
|
const env = makeEnv(1);
|
|
let calls = 0;
|
|
watchDevicePixelRatio({
|
|
getDevicePixelRatio: env.getDevicePixelRatio,
|
|
matchMedia: env.matchMedia,
|
|
onChange: () => {
|
|
calls += 1;
|
|
},
|
|
});
|
|
|
|
env.setDpr(2);
|
|
env.created[0].trigger();
|
|
|
|
assert.equal(calls, 1);
|
|
});
|
|
|
|
test("re-registers for the new ratio so subsequent changes still fire", () => {
|
|
const env = makeEnv(1);
|
|
let calls = 0;
|
|
watchDevicePixelRatio({
|
|
getDevicePixelRatio: env.getDevicePixelRatio,
|
|
matchMedia: env.matchMedia,
|
|
onChange: () => {
|
|
calls += 1;
|
|
},
|
|
});
|
|
|
|
env.setDpr(2);
|
|
env.created[0].trigger();
|
|
|
|
assert.equal(env.created.length, 2);
|
|
assert.equal(env.created[1].query, "(resolution: 2dppx)");
|
|
// The stale listener must be detached so it cannot double-fire.
|
|
assert.equal(env.created[0].listenerCount, 0);
|
|
|
|
env.setDpr(3);
|
|
env.created[1].trigger();
|
|
|
|
assert.equal(calls, 2);
|
|
});
|
|
|
|
test("cleanup stops further onChange callbacks", () => {
|
|
const env = makeEnv(1);
|
|
let calls = 0;
|
|
const stop = watchDevicePixelRatio({
|
|
getDevicePixelRatio: env.getDevicePixelRatio,
|
|
matchMedia: env.matchMedia,
|
|
onChange: () => {
|
|
calls += 1;
|
|
},
|
|
});
|
|
|
|
stop();
|
|
|
|
assert.equal(env.created[0].listenerCount, 0);
|
|
env.created[0].trigger();
|
|
assert.equal(calls, 0);
|
|
});
|
|
|
|
test("falls back to addListener/removeListener when addEventListener is unavailable", () => {
|
|
const env = makeEnv(1, /* supportsModern */ false);
|
|
let calls = 0;
|
|
const stop = watchDevicePixelRatio({
|
|
getDevicePixelRatio: env.getDevicePixelRatio,
|
|
matchMedia: env.matchMedia,
|
|
onChange: () => {
|
|
calls += 1;
|
|
},
|
|
});
|
|
|
|
assert.equal(env.created[0].legacyListeners.length, 1);
|
|
env.created[0].trigger();
|
|
assert.equal(calls, 1);
|
|
|
|
stop();
|
|
// After cleanup the most recently registered query has no listeners.
|
|
const latest = env.created[env.created.length - 1];
|
|
assert.equal(latest.listenerCount, 0);
|
|
});
|