mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 00:20:45 +00:00
3128 lines
125 KiB
JavaScript
3128 lines
125 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert/strict");
|
|
const test = require("node:test");
|
|
|
|
const {
|
|
attachDisplayRecovery: attachDisplayRecoveryForPlatform,
|
|
boundsIntersectDisplay,
|
|
clampBoundsToDisplay,
|
|
pickDisplayRecoveryBounds,
|
|
} = require("./displayRecovery.cjs");
|
|
|
|
function attachDisplayRecovery(options) {
|
|
return attachDisplayRecoveryForPlatform({ ...options, platform: "win32" });
|
|
}
|
|
|
|
const PRIMARY = { id: 1, bounds: { x: 0, y: 0, width: 1920, height: 1080 } };
|
|
const SECONDARY = { id: 2, bounds: { x: 1920, y: 0, width: 2560, height: 1440 } };
|
|
|
|
function createMockWindow(initialBounds) {
|
|
const listeners = new Map();
|
|
const webContentsListeners = new Map();
|
|
const win = {
|
|
bounds: { ...initialBounds },
|
|
destroyed: false,
|
|
maximized: false,
|
|
fullScreen: false,
|
|
setBoundsCalls: [],
|
|
webContents: {
|
|
on(event, handler) {
|
|
if (!webContentsListeners.has(event)) webContentsListeners.set(event, []);
|
|
webContentsListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = webContentsListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event, ...args) {
|
|
for (const handler of webContentsListeners.get(event) || []) handler(...args);
|
|
},
|
|
},
|
|
isDestroyed() {
|
|
return win.destroyed;
|
|
},
|
|
isMaximized() {
|
|
return win.maximized;
|
|
},
|
|
isFullScreen() {
|
|
return win.fullScreen;
|
|
},
|
|
getBounds() {
|
|
return { ...win.bounds };
|
|
},
|
|
getNormalBounds() {
|
|
return { ...(win.normalBounds || win.bounds) };
|
|
},
|
|
unmaximize() {
|
|
win.maximized = false;
|
|
for (const handler of listeners.get("unmaximize") || []) handler();
|
|
},
|
|
setBounds(next) {
|
|
win.setBoundsCalls.push({ ...next });
|
|
win.bounds = { ...next };
|
|
for (const handler of listeners.get("move") || []) handler();
|
|
},
|
|
on(event, handler) {
|
|
if (!listeners.has(event)) listeners.set(event, []);
|
|
listeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = listeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
__listeners: listeners,
|
|
__webContentsListeners: webContentsListeners,
|
|
};
|
|
return win;
|
|
}
|
|
|
|
function createMockScreen({ primary = PRIMARY, displays = [PRIMARY, SECONDARY] } = {}) {
|
|
const listeners = new Map();
|
|
const connected = [...displays];
|
|
const mock = {
|
|
on(event, handler) {
|
|
if (!listeners.has(event)) listeners.set(event, []);
|
|
listeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = listeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event, ...args) {
|
|
// Mirror Electron: the display list changes when these events fire.
|
|
const display = args[1] || args[0];
|
|
if (event === "display-removed" && display) {
|
|
const displayIdIsTransient =
|
|
typeof display.id !== "number" || !Number.isFinite(display.id) || display.id < 0;
|
|
let index = displayIdIsTransient
|
|
? connected.indexOf(display)
|
|
: connected.findIndex((candidate) => candidate.id === display.id);
|
|
// Electron may report -1 while the durable display id is not known.
|
|
// Keep the mock topology faithful by falling back to exact geometry.
|
|
if (index < 0 && display.bounds) {
|
|
index = connected.findIndex(
|
|
(candidate) =>
|
|
candidate.bounds?.x === display.bounds.x &&
|
|
candidate.bounds?.y === display.bounds.y &&
|
|
candidate.bounds?.width === display.bounds.width &&
|
|
candidate.bounds?.height === display.bounds.height
|
|
);
|
|
}
|
|
if (index >= 0) connected.splice(index, 1);
|
|
}
|
|
if (event === "display-added" && display) {
|
|
const displayIdIsTransient =
|
|
typeof display.id !== "number" || !Number.isFinite(display.id) || display.id < 0;
|
|
if (
|
|
(displayIdIsTransient && !connected.includes(display)) ||
|
|
(!displayIdIsTransient && !connected.some((candidate) => candidate.id === display.id))
|
|
) {
|
|
connected.push(display);
|
|
}
|
|
}
|
|
// Mirror Electron: screen event listeners receive (event, display).
|
|
for (const handler of listeners.get(event) || []) handler(...args);
|
|
},
|
|
getPrimaryDisplay() {
|
|
return primary;
|
|
},
|
|
getAllDisplays() {
|
|
return [...connected];
|
|
},
|
|
getDisplayMatching(bounds) {
|
|
let best = null;
|
|
let bestArea = 0;
|
|
for (const display of connected) {
|
|
const overlap = boundsIntersectDisplay(bounds, display.bounds)
|
|
? Math.min(bounds.x + bounds.width, display.bounds.x + display.bounds.width) -
|
|
Math.max(bounds.x, display.bounds.x)
|
|
: 0;
|
|
if (overlap > bestArea) {
|
|
bestArea = overlap;
|
|
best = display;
|
|
}
|
|
}
|
|
return best || connected[0];
|
|
},
|
|
__setDisplays(nextDisplays) {
|
|
connected.splice(0, connected.length, ...nextDisplays);
|
|
},
|
|
__listeners: listeners,
|
|
};
|
|
return mock;
|
|
}
|
|
|
|
function createMockPowerMonitor() {
|
|
const listeners = new Map();
|
|
return {
|
|
on(event, handler) {
|
|
if (!listeners.has(event)) listeners.set(event, []);
|
|
listeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = listeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of listeners.get(event) || []) handler();
|
|
},
|
|
__listeners: listeners,
|
|
};
|
|
}
|
|
|
|
function moveWindowManually(win, nextBounds) {
|
|
for (const handler of win.__listeners.get("will-move") || []) {
|
|
handler({}, { ...nextBounds });
|
|
}
|
|
win.bounds = { ...nextBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
}
|
|
|
|
function resizeWindowManually(win, nextBounds) {
|
|
for (const handler of win.__listeners.get("will-resize") || []) {
|
|
handler({}, { ...nextBounds }, { edge: "bottom-right" });
|
|
}
|
|
win.bounds = { ...nextBounds };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
}
|
|
|
|
test("boundsIntersectDisplay detects overlap and rejects invalid input", () => {
|
|
assert.equal(boundsIntersectDisplay({ x: 2000, y: 100, width: 800, height: 600 }, SECONDARY.bounds), true);
|
|
assert.equal(boundsIntersectDisplay({ x: 0, y: 0, width: 800, height: 600 }, SECONDARY.bounds), false);
|
|
assert.equal(boundsIntersectDisplay(null, SECONDARY.bounds), false);
|
|
assert.equal(boundsIntersectDisplay({ x: 10, y: 10, width: 0, height: 100 }, PRIMARY.bounds), false);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds restores a remembered placement on the re-added display", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: SECONDARY,
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ x: 2000, y: 100, width: 1200, height: 800 }],
|
|
});
|
|
assert.deepEqual(restored, { x: 2000, y: 100, width: 1200, height: 800 });
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds does nothing when the window is already on the display", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: SECONDARY,
|
|
currentBounds: { x: 2000, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ x: 2100, y: 100, width: 1200, height: 800 }],
|
|
});
|
|
assert.equal(restored, null);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds ignores candidates on other displays", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: SECONDARY,
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ x: 50, y: 50, width: 1200, height: 800 }],
|
|
});
|
|
assert.equal(restored, null);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds matches a candidate by display identity when bounds changed", () => {
|
|
// The display came back with different bounds (e.g. DPI/resolution or
|
|
// topology change): the remembered geometry no longer intersects it, but
|
|
// the candidate is still valid because it was remembered for this display.
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: { id: 2, bounds: { x: 1920, y: 0, width: 1024, height: 768 } },
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ bounds: { x: 2000, y: 100, width: 1200, height: 800 }, displayId: 2 }],
|
|
});
|
|
assert.deepEqual(restored, { x: 2000, y: 100, width: 1200, height: 800 });
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds does not match a candidate remembered for another display", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: { id: 3, bounds: { x: 1920, y: 0, width: 1024, height: 768 } },
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
// Non-intersecting bounds remembered for a different display id: neither
|
|
// the identity nor the geometry matches, so nothing is restored.
|
|
candidates: [{ bounds: { x: 5000, y: 5000, width: 400, height: 300 }, displayId: 2 }],
|
|
});
|
|
assert.equal(restored, null);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds does not match a tagged candidate by geometry alone", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
// Display 3 re-appears first with bounds overlapping the geometry that
|
|
// was remembered for display 2: the identity mismatch must win over the
|
|
// geometry match, or the window would be dragged onto the wrong display
|
|
// before its real owner returns.
|
|
addedDisplay: { id: 3, bounds: { x: 1920, y: 0, width: 2560, height: 1440 } },
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ bounds: { x: 2000, y: 100, width: 1400, height: 900 }, displayId: 2 }],
|
|
});
|
|
assert.equal(restored, null);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds treats an unknown negative display id as untagged", () => {
|
|
const rememberedBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: SECONDARY,
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
// Electron uses -1 when the correct display id is not known yet. The
|
|
// returning display can later have its stable positive id.
|
|
candidates: [{ bounds: rememberedBounds, displayId: -1 }],
|
|
});
|
|
|
|
assert.deepEqual(restored, rememberedBounds);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds falls back to geometry while the added display id is unknown", () => {
|
|
const rememberedBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: { id: -1, bounds: SECONDARY.bounds },
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
candidates: [{ bounds: rememberedBounds, displayId: SECONDARY.id }],
|
|
});
|
|
|
|
assert.deepEqual(restored, rememberedBounds);
|
|
});
|
|
|
|
test("pickDisplayRecoveryBounds does not use the unified-desktop sentinel as identity", () => {
|
|
const restored = pickDisplayRecoveryBounds({
|
|
addedDisplay: { id: -10, bounds: { x: 1920, y: 0, width: 2560, height: 1440 } },
|
|
currentBounds: { x: 100, y: 100, width: 1200, height: 800 },
|
|
// Electron assigns -10 to a unified virtual desktop. It is not a unique
|
|
// display identity, so unrelated geometry must not match on id alone.
|
|
candidates: [
|
|
{
|
|
bounds: { x: 5000, y: 5000, width: 400, height: 300 },
|
|
displayId: -10,
|
|
},
|
|
],
|
|
});
|
|
|
|
assert.equal(restored, null);
|
|
});
|
|
|
|
test("clampBoundsToDisplay keeps the restored window fully visible", () => {
|
|
const clamped = clampBoundsToDisplay(
|
|
{ x: 3000, y: -200, width: 3000, height: 2000 },
|
|
SECONDARY.bounds
|
|
);
|
|
assert.deepEqual(clamped, { x: 1920, y: 0, width: 2560, height: 1440 });
|
|
});
|
|
|
|
test("attachDisplayRecovery moves the window back after lock/unlock display churn", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// User is working with the window on the secondary display: placement gets tracked.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Lock: the secondary display disappears.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// Windows relocates the window to the primary display.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Unlock: the display comes back.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery restores the placement when the display returns with changed bounds", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// The window is tracked on the secondary display (never moved, so only the
|
|
// seeded placement exists).
|
|
// Lock: the secondary display disappears.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Unlock: the display comes back with different bounds (DPI/resolution or
|
|
// topology change), and the window still sits on the secondary's old
|
|
// coordinates, which no longer intersect the new ones. The remembered
|
|
// placement must still be restored (clamped into the new bounds).
|
|
const changedSecondary = { id: SECONDARY.id, bounds: { x: -1024, y: 0, width: 1024, height: 768 } };
|
|
screen.emit("display-added", {}, changedSecondary);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: -1024, y: 0, width: 1024, height: 768 });
|
|
});
|
|
|
|
test("attachDisplayRecovery clears the remembered placement when the user moves to the primary", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
// A deliberate user move happens well before any display churn, so the
|
|
// teardown grace window is disabled for this scenario.
|
|
attachDisplayRecovery({ win, screen, teardownGraceMs: 0 });
|
|
|
|
// The user deliberately moves the window to the primary display while the
|
|
// secondary display is still connected.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Later the secondary display is torn down and re-added (lock cycle or
|
|
// unplug/replug): the stale secondary placement must not be restored.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery preserves the fallback when the OS relocates the window before display-removed", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// User is working with the window on the secondary display: placement gets tracked.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Teardown: Windows relocates the window to the primary display BEFORE
|
|
// Electron emits "display-removed" (the secondary is still connected when
|
|
// the relocation fires, so the move looks like a user move).
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Unlock: the display comes back and the window must be restored.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the promoted snapshot through trailing teardown-burst events", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// User is working with the window on the secondary display: placement gets tracked.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Teardown: Windows relocates the window to the primary BEFORE
|
|
// "display-removed" fires (the secondary is still connected, so the move
|
|
// is stashed as a pending teardown move).
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
// The removal promotes the pending move to the removal-time snapshot.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// A trailing "resize" of the same relocation burst fires after the
|
|
// removal: it must not clear the promoted snapshot.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 898 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
// Unlock: the display comes back and the window must be restored.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the promoted snapshot through a delayed lock-screen teardown burst", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_500_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// During Win+L teardown, Windows relocates the window before Electron
|
|
// reports the display removal. The removal promotes the secondary bounds.
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// The session remains locked while a delayed resize from the same OS
|
|
// relocation arrives well after the ordinary teardown grace window.
|
|
now += 60_000;
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the removal snapshot through a delayed lock-screen relocation burst", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_750_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// In the opposite valid ordering, Electron reports the display removal
|
|
// while the window still has its secondary placement, then Windows moves
|
|
// it onto the primary display.
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// A delayed resize is still part of the OS relocation because the session
|
|
// remains locked, even though the normal grace window has elapsed.
|
|
now += 60_000;
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery lets an immediate post-unlock user edit invalidate recovery", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_900_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Once the session unlocks, the user can deliberately re-place the
|
|
// relocated window. That edit must win even when it happens immediately
|
|
// after unlock and before the monitor returns.
|
|
powerMonitor.emit("unlock-screen");
|
|
moveWindowManually(win, { x: 300, y: 200, width: 1200, height: 800 });
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 300, y: 200, width: 1200, height: 800 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery does not promote an immediate post-unlock user move", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_950_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The interruption ends before the display is removed. An immediate move
|
|
// onto the primary is therefore a real user edit, not an OS relocation
|
|
// racing ahead of display-removed, even though it falls inside the usual
|
|
// teardown grace window.
|
|
powerMonitor.emit("lock-screen");
|
|
powerMonitor.emit("unlock-screen");
|
|
moveWindowManually(win, { x: 300, y: 200, width: 1200, height: 800 });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 300, y: 200, width: 1200, height: 800 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery lets an immediate post-unlock manual resize cancel recovery", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
resizeWindowManually(win, { x: 100, y: 100, width: 1200, height: 800 });
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 100, y: 100, width: 1200, height: 800 });
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps a removal snapshot through a queued post-unlock OS move", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// This regular move has no matching will-move because Windows queued it as
|
|
// part of the display teardown rather than the user moving the window.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], {
|
|
x: 2000,
|
|
y: 100,
|
|
width: 1400,
|
|
height: 900,
|
|
});
|
|
});
|
|
|
|
test("attachDisplayRecovery promotes a queued post-unlock OS move when removal follows", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// Windows can deliver the queued relocation before Electron reports that
|
|
// the display disappeared. It is still a system move, not manual intent.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], {
|
|
x: 2000,
|
|
y: 100,
|
|
width: 1400,
|
|
height: 900,
|
|
});
|
|
});
|
|
|
|
test("attachDisplayRecovery matches a pending move to a removal with an unknown id", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, { ...SECONDARY, id: -1 });
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery matches an unknown pending id to a stable removal", () => {
|
|
const unknownSecondary = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, unknownSecondary] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery restores when display-added precedes a queued OS move", () => {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// The display returns while the window still has its old secondary
|
|
// coordinates. Windows then delivers the queued teardown relocation only
|
|
// after unlock, so there is no later display-added event to trigger restore.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery restores a delayed queued OS move after display-added", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// Windows may not deliver the queued system relocation until well after
|
|
// the short teardown grace window has elapsed.
|
|
now += 5_000;
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery respects a manual move after display-added", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
now += 5_000;
|
|
|
|
// Unlike the queued OS relocation, a real user move emits will-move first
|
|
// and must cancel the recently returned recovery target even after the
|
|
// teardown grace window has elapsed.
|
|
moveWindowManually(win, { x: 300, y: 200, width: 1200, height: 800 });
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 300, y: 200, width: 1200, height: 800 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery protects a new returned-display placement from a late OS move", () => {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const newSecondaryBounds = { x: 2300, y: 180, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// The user deliberately refines the window position on the returned target
|
|
// before Windows delivers its queued teardown relocation. That newer target
|
|
// must replace, rather than discard, the one-shot late recovery.
|
|
moveWindowManually(win, newSecondaryBounds);
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], newSecondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery protects a new placement on a transient returned display", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const newDisplayBounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
const manualBounds = { x: -2300, y: 160, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = newDisplayBounds;
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
moveWindowManually(win, manualBounds);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], manualBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery protects a manual placement on another secondary from a late OS move", () => {
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
moveWindowManually(win, tertiaryBounds);
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], tertiaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery restores a late OS move to another secondary after the unlock grace", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_000_000;
|
|
Date.now = () => now;
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
now += 5_000;
|
|
|
|
// A queued Windows relocation can arrive well after the unlock signal and
|
|
// can choose another still-connected secondary instead of the primary.
|
|
win.bounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps a removal snapshot when locked Windows relocates to another secondary", () => {
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// The session is still locked, so this ordinary move onto the remaining
|
|
// secondary can only be Windows topology recovery, not user placement.
|
|
win.bounds = { x: -1800, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery retries after a transient returning display stabilizes", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
const returningDisplay = {
|
|
id: -1,
|
|
bounds: { x: -2560, y: 0, width: 2560, height: 1440 },
|
|
};
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
const stableReturningDisplay = {
|
|
id: SECONDARY.id,
|
|
bounds: { ...returningDisplay.bounds },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableReturningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableReturningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery ignores unrelated metrics while a removed display id is unknown", () => {
|
|
const unknownSecondary = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 1850, y: 100, width: 800, height: 600 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, unknownSecondary] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, unknownSecondary);
|
|
win.bounds = { x: 100, y: 100, width: 800, height: 600 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The saved bounds overlap the primary display, but this event is only a
|
|
// primary-display metrics change. It must not consume the unknown-id
|
|
// snapshot before the actual secondary display returns.
|
|
screen.emit("display-metrics-changed", {}, PRIMARY, ["bounds"]);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(
|
|
win.setBoundsCalls[0],
|
|
clampBoundsToDisplay(secondaryBounds, SECONDARY.bounds)
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery follows an unknown returned display when its id and bounds stabilize", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
// Once the transient association exists, an unrelated primary-display
|
|
// metrics event still must not claim it.
|
|
screen.emit("display-metrics-changed", {}, PRIMARY, ["bounds"]);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[1], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery follows a stabilized unknown display while maximized", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery associates a sole unknown return after its bounds changed", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery associates a moved unknown return while maximized", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery clears a transient association when its display is removed again", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const thirdDisplay = {
|
|
id: -1,
|
|
bounds: { x: 0, y: -1200, width: 1920, height: 1200 },
|
|
};
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
screen.emit("display-added", {}, thirdDisplay);
|
|
thirdDisplay.id = 3;
|
|
screen.emit("display-metrics-changed", {}, thirdDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(boundsIntersectDisplay(win.bounds, thirdDisplay.bounds), false);
|
|
});
|
|
|
|
test("attachDisplayRecovery ignores removal of a known same-label secondary", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1, label: "Same Model" };
|
|
const thirdDisplay = {
|
|
id: 3,
|
|
label: "Same Model",
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({
|
|
displays: [PRIMARY, returningDisplay, thirdDisplay],
|
|
});
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
screen.emit("display-removed", {}, thirdDisplay);
|
|
returningDisplay.id = SECONDARY.id;
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[1], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps recovery through a second transient disconnect", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
// The same physical display drops again before Electron learns its durable
|
|
// id. Windows relocates the window first, so the last trusted placement has
|
|
// to survive the second removal event instead of being cleared with the
|
|
// transient identity.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[1], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps deferred recovery through a second transient disconnect", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// The display drops again before the deferred recovery can be applied and
|
|
// before its durable id is known. The queued target must remain available
|
|
// for the next add event.
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps deferred recovery when a transient id stabilizes at removal", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// Electron may expose the durable id for the first time in the next removal
|
|
// payload. Preserve that identity before ending the transient association so
|
|
// the deferred placement can follow a changed-geometry return.
|
|
const stableRemoval = { ...returningDisplay, id: SECONDARY.id };
|
|
screen.emit("display-removed", {}, stableRemoval);
|
|
stableRemoval.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, stableRemoval);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableRemoval.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery ignores removal of a known unrelated transient neighbor", () => {
|
|
const realNow = Date.now;
|
|
let now = 2_500_000;
|
|
Date.now = () => now;
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const thirdDisplay = {
|
|
id: 3,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const originalThirdBounds = { ...thirdDisplay.bounds };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({
|
|
displays: [PRIMARY, returningDisplay, thirdDisplay],
|
|
});
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// Consume the one late relocation record, then let its bounded burst tail
|
|
// expire. A later pre-removal move creates a fresh untagged target snapshot
|
|
// while the transient association is still live.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
now += 3_000;
|
|
win.bounds = { x: 120, y: 110, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// A known third display moves across that old geometry and is removed. It
|
|
// must not claim the untagged target snapshot via geometry fallback.
|
|
thirdDisplay.bounds = { ...returningDisplay.bounds };
|
|
screen.emit("display-removed", {}, thirdDisplay);
|
|
|
|
const stableRemoval = { ...returningDisplay, id: SECONDARY.id };
|
|
screen.emit("display-removed", {}, stableRemoval);
|
|
stableRemoval.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, stableRemoval);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[1], stableRemoval.bounds),
|
|
true
|
|
);
|
|
assert.equal(boundsIntersectDisplay(win.bounds, originalThirdBounds), false);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery rejects a second unknown display before either identity stabilizes", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
unknownNeighbor.id = 4;
|
|
unknownNeighbor.bounds = { x: -2500, y: 0, width: 1920, height: 1080 };
|
|
screen.emit("display-metrics-changed", {}, unknownNeighbor, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// Once the unrelated unknown display is no longer geometrically ambiguous,
|
|
// the real target can still stabilize and recover the window.
|
|
unknownNeighbor.bounds = { x: 4480, y: 0, width: 1920, height: 1080 };
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery recovers after an unrelated unknown display disappears", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The target returns at new coordinates without a stable id, followed by a
|
|
// second unknown display. The second display disappears before either id
|
|
// stabilizes, making the target unambiguous again.
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
screen.emit("display-removed", {}, unknownNeighbor);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery recovers when an unrelated unknown display changes identity and bounds before removal", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// Electron may replace the transient Display object when the neighbor gets
|
|
// its stable id, while also reporting new topology coordinates.
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { x: 7040, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
screen.emit("display-removed", {}, stableNeighbor);
|
|
|
|
const stableReturningDisplay = {
|
|
id: SECONDARY.id,
|
|
bounds: { ...returningDisplay.bounds },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableReturningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableReturningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery follows a moved target after an unrelated unknown display stabilizes with a fresh payload", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// Both displays receive fresh payload objects and move when their ids
|
|
// stabilize. The still-connected unknown display at the target's return
|
|
// geometry proves the first stable event belongs to the neighbor.
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { x: 7040, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
|
|
const stableReturningDisplay = {
|
|
id: SECONDARY.id,
|
|
bounds: { x: -5120, y: 0, width: 2560, height: 1440 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableReturningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableReturningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery follows a moved target when it stabilizes before a fresh unrelated payload", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// The target stabilizes first, but both displays use replacement payloads
|
|
// and move from their transient positions. Correlation must not depend on
|
|
// which metrics event happens to arrive first.
|
|
const stableReturningDisplay = {
|
|
id: SECONDARY.id,
|
|
bounds: { x: -5120, y: 0, width: 2560, height: 1440 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableReturningDisplay, ["bounds"]);
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { x: 7040, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableReturningDisplay.bounds),
|
|
true
|
|
);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], stableNeighbor.bounds),
|
|
false
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery does not guess when fresh stable displays cross transient positions", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// Fresh payload objects cross past each other's transient positions while
|
|
// retaining their physical resolutions. Position and size now disagree,
|
|
// so fail closed instead of restoring onto the wrong physical display.
|
|
const stableReturningDisplay = {
|
|
id: SECONDARY.id,
|
|
bounds: { x: 6000, y: 0, width: 2560, height: 1440 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableReturningDisplay, ["bounds"]);
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { x: -5000, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery does not let a neighbor claim a fresh target removal", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// The real target disappears again, revealing its stable id only in a fresh
|
|
// removal payload at changed coordinates.
|
|
const stableTargetRemoval = {
|
|
id: SECONDARY.id,
|
|
bounds: { x: -5120, y: 0, width: 2560, height: 1440 },
|
|
};
|
|
screen.emit("display-removed", {}, stableTargetRemoval);
|
|
|
|
// The surviving neighbor later moves over the target's transient return
|
|
// geometry. It must not inherit the target's recovery record.
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { ...returningDisplay.bounds },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery does not let a swapped neighbor claim a removed target", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
const unknownNeighbor = {
|
|
id: -1,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
screen.emit("display-added", {}, unknownNeighbor);
|
|
|
|
// The target disappears, but its fresh removal payload lands at the
|
|
// neighbor's transient position. Reflect the already-updated topology
|
|
// before delivering the event, as Electron does.
|
|
const stableTargetRemoval = {
|
|
id: SECONDARY.id,
|
|
bounds: { ...unknownNeighbor.bounds },
|
|
};
|
|
screen.__setDisplays([PRIMARY, unknownNeighbor]);
|
|
for (const handler of screen.__listeners.get("display-removed") || []) {
|
|
handler({}, stableTargetRemoval);
|
|
}
|
|
|
|
const stableNeighbor = {
|
|
id: 4,
|
|
bounds: { ...returningDisplay.bounds },
|
|
};
|
|
screen.emit("display-metrics-changed", {}, stableNeighbor, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery associates an unknown moved return beside a stable third display", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const thirdDisplay = {
|
|
id: 3,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({
|
|
displays: [PRIMARY, returningDisplay, thirdDisplay],
|
|
});
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery associates a maximized unknown return beside a stable third display", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const thirdDisplay = {
|
|
id: 3,
|
|
bounds: { x: 4480, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen({
|
|
displays: [PRIMARY, returningDisplay, thirdDisplay],
|
|
});
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery rejects a newly added stable display while a transient target stabilizes", () => {
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, returningDisplay] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
screen.emit("display-removed", {}, returningDisplay);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
|
|
const overlappingThirdDisplay = {
|
|
id: 3,
|
|
bounds: { ...returningDisplay.bounds },
|
|
};
|
|
screen.emit("display-added", {}, overlappingThirdDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
returningDisplay.id = SECONDARY.id;
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery drops the promoted snapshot for user edits after the grace window", () => {
|
|
const realNow = Date.now;
|
|
let now = 2_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// Teardown relocation before "display-removed": the pending move is
|
|
// promoted to the removal-time snapshot.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Well after the grace window, the user deliberately re-places the
|
|
// window on the primary: the stale snapshot must be dropped.
|
|
now += 10_000;
|
|
win.bounds = { x: 300, y: 300, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the snapshot when unrelated secondary displays are removed later", () => {
|
|
const TERTIARY = { id: 3, bounds: { x: -1920, y: 0, width: 1920, height: 1080 } };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// User is working with the window on the secondary display: placement gets tracked.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Teardown: Windows relocates the window to the primary BEFORE the
|
|
// "display-removed" event for the window's own display fires.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// A second secondary display is removed during the same teardown: this
|
|
// unrelated removal must not overwrite the snapshot for SECONDARY.
|
|
screen.emit("display-removed", {}, TERTIARY);
|
|
|
|
// Unlock: the window's display comes back and the window must be restored.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery drops the stale recovery when the user edits the relocated window later", () => {
|
|
const realNow = Date.now;
|
|
let now = 2_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// The window lives on the secondary display, then teardown removes the
|
|
// display from the list and relocates the window to the primary.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// While the monitor stays disconnected, the user deliberately moves the
|
|
// relocated window well past the teardown grace window: the stale
|
|
// secondary placement must not be restored when the display re-appears.
|
|
now += 60_000;
|
|
win.bounds = { x: 150, y: 150, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
now += 60_000;
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the snapshot when the teardown relocation also resizes", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// The window lives on the secondary display, then teardown removes the
|
|
// display from the list and relocates the window to the primary.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The same relocation burst also emits a "resize" (well within the
|
|
// teardown grace window): it must not be mistaken for a user edit and
|
|
// must not invalidate the pre-teardown placement.
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
// Unlock: the display comes back and the window must be restored.
|
|
now += 60_000;
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery clears the removal-time snapshot once it is consumed", () => {
|
|
const realNow = Date.now;
|
|
let now = 1_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// The window lives on the secondary display, then teardown relocates it to
|
|
// the primary before "display-removed" fires (within the grace window).
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Recovery: the display returns and the window is restored.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
// The user later deliberately moves the window back to the primary
|
|
// display, well past the teardown grace window, and the display is torn
|
|
// down and re-added again: the already-consumed snapshot must not restore
|
|
// the old placement against the user's latest move.
|
|
now += 60_000;
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
now += 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending teardown snapshot across a suspension delay", () => {
|
|
const realNow = Date.now;
|
|
let now = 2_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
__listeners: powerListeners,
|
|
};
|
|
|
|
const detach = attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The window lives on the secondary display, then the OS relocates it to
|
|
// the primary before "display-removed" fires (within the grace window).
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The machine suspends right after the relocation. On wake the wall
|
|
// clock has advanced far past the teardown grace window — but the jump
|
|
// happened while the machine was asleep, so the pending snapshot must
|
|
// not be expired because of it.
|
|
powerMonitor.emit("suspend");
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Unlock: the display returns and the pre-relocation placement must
|
|
// still be restored.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
// The tracked placement was last updated by the pre-teardown move that
|
|
// still happened on the secondary display (2100,120), not by the OS
|
|
// relocation to the primary.
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
// Detach must also unsubscribe the suspend listener.
|
|
detach();
|
|
assert.equal((powerListeners.get("suspend") || []).length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending teardown snapshot across a lock-screen delay (Win+L without suspend)", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
};
|
|
|
|
const detach = attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The window lives on the secondary display, then the OS relocates it to
|
|
// the primary before "display-removed" fires (within the grace window).
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Locking the session (Win+L) tears the display down without necessarily
|
|
// emitting "suspend". The lock must count as a session interruption just
|
|
// like a suspension: on unlock the wall clock is far past the grace
|
|
// window, but the pending snapshot must not be expired because of it.
|
|
powerMonitor.emit("lock-screen");
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
detach();
|
|
assert.equal((powerListeners.get("lock-screen") || []).length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending teardown snapshot when the relocation follows the lock-screen event", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_200_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
};
|
|
|
|
const detach = attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The window lives on the secondary display.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Windows may lock the session BEFORE relocating the window: the
|
|
// lock-screen event fires first and the OS teardown relocation to the
|
|
// primary lands afterwards. The pending move is then stamped as part of
|
|
// the interruption even though its timestamp postdates the lock.
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The "display-removed" event is delivered long after the relocation
|
|
// (e.g. the machine went to sleep right after the lock), so the wall
|
|
// clock is far past the teardown grace window. The snapshot must still
|
|
// be promoted: the move belonged to the interruption, not to the user.
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
detach();
|
|
assert.equal((powerListeners.get("lock-screen") || []).length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending teardown snapshot when a suspend follows the lock in the same interruption", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_400_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
__listeners: powerListeners,
|
|
};
|
|
|
|
const detach = attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The window lives on the secondary display, then the OS relocates it to
|
|
// the primary right after the lock (Win+L).
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
powerMonitor.emit("lock-screen");
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// More than the grace window after the relocation (but within the same
|
|
// lock/sleep cycle) the machine actually suspends. The repeated
|
|
// interruption signal must not clear the pending move recorded for this
|
|
// cycle — the session was locked, so the user cannot have moved the
|
|
// window in between.
|
|
now += 60 * 60_000;
|
|
powerMonitor.emit("suspend");
|
|
|
|
// "display-removed" is delivered after the suspend, sees only the
|
|
// relocated primary bounds, and must still promote the pending snapshot.
|
|
now += 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
// After a resume the interruption is over: a later lock starts a new one
|
|
// and must again expire pending moves that went stale while the session
|
|
// was running.
|
|
powerMonitor.emit("resume");
|
|
assert.equal((powerListeners.get("suspend") || []).length, 1);
|
|
|
|
detach();
|
|
assert.equal((powerListeners.get("suspend") || []).length, 0);
|
|
assert.equal((powerListeners.get("lock-screen") || []).length, 0);
|
|
assert.equal((powerListeners.get("resume") || []).length, 0);
|
|
assert.equal((powerListeners.get("unlock-screen") || []).length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending teardown snapshot when resume fires while the session is still locked", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_600_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
__listeners: powerListeners,
|
|
};
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// Win+L locks the session and the machine then suspends. On wake the
|
|
// screen is still locked: "resume" fires while "unlock-screen" has not.
|
|
powerMonitor.emit("lock-screen");
|
|
powerMonitor.emit("suspend");
|
|
powerMonitor.emit("resume");
|
|
|
|
// The OS teardown relocation lands after the resume, while the session
|
|
// is still locked — the user cannot have moved the window.
|
|
now += 60_000;
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The matching "display-removed" arrives outside the grace window. The
|
|
// interruption must still count as active (the session is locked), so
|
|
// the pre-relocation placement is promoted and restored on re-add.
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery drops a pending teardown move that predates the interruption", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_500_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
};
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The user deliberately moves the window from the secondary display to
|
|
// the primary, well before any session interruption: this supersedes the
|
|
// remembered secondary placement.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Much later the user locks the session (Win+L). The pending move is
|
|
// stale by now — it was a deliberate user edit, not the OS's pre-teardown
|
|
// relocation — so it must not be promoted by the "display-removed" event
|
|
// that fires during the lock.
|
|
now += 60_000;
|
|
powerMonitor.emit("lock-screen");
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
// The user's primary-display placement must survive the teardown and
|
|
// re-addition of the secondary display.
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 100, y: 100, width: 1400, height: 900 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps the pending snapshot for a relocation that lands long after the lock", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_600_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
};
|
|
|
|
const detach = attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The window lives on the secondary display.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// Win+L locks the session; the OS teardown relocation to the primary
|
|
// lands long after the lock event (the teardown itself takes longer
|
|
// than the grace window) while the session is still locked.
|
|
powerMonitor.emit("lock-screen");
|
|
now += 60_000;
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The matching "display-removed" is delivered much later still (the
|
|
// machine went to sleep right after the lock): every elapsed-time check
|
|
// fails, but the move was recorded while the interruption was active —
|
|
// the session was locked, so the user cannot have moved the window —
|
|
// and the pending snapshot must still be promoted.
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2100, y: 120, width: 1400, height: 900 });
|
|
|
|
detach();
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery drops the pending teardown move when the interruption ends without a removal", () => {
|
|
const realNow = Date.now;
|
|
let now = 3_700_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerListeners = new Map();
|
|
const powerMonitor = {
|
|
on(event, handler) {
|
|
if (!powerListeners.has(event)) powerListeners.set(event, []);
|
|
powerListeners.get(event).push(handler);
|
|
},
|
|
removeListener(event, handler) {
|
|
const list = powerListeners.get(event) || [];
|
|
const index = list.indexOf(handler);
|
|
if (index >= 0) list.splice(index, 1);
|
|
},
|
|
emit(event) {
|
|
for (const handler of powerListeners.get(event) || []) handler();
|
|
},
|
|
};
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
|
|
// The user deliberately moves the window from the secondary display to
|
|
// the primary, then locks the session within the grace window — the
|
|
// pending move is indistinguishable from a teardown relocation at this
|
|
// point, so it is kept.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
powerMonitor.emit("lock-screen");
|
|
|
|
// The display never disappears: the interruption ends without the
|
|
// pending move ever being claimed by a "display-removed" event.
|
|
now += 60_000;
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// Much later the secondary display is unplugged ordinarily. The stale
|
|
// pending move must not be promoted by the "interruption started after
|
|
// the move" ordering check, and the re-added display must not drag the
|
|
// window back to the superseded placement.
|
|
now += 60 * 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
assert.deepEqual(win.bounds, { x: 100, y: 100, width: 1400, height: 900 });
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery drops the removal snapshot when the returning display already holds the window", () => {
|
|
const realNow = Date.now;
|
|
let now = 4_000_000;
|
|
Date.now = () => now;
|
|
try {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
|
|
// The window lives on the secondary display, then the display disappears
|
|
// (a removal-time snapshot is captured) and re-appears before the window
|
|
// coordinates stop intersecting it: recovery is a no-op, but the stale
|
|
// removal snapshot must still be invalidated.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// The user then deliberately moves the window to the primary display
|
|
// (still inside the snapshot's teardown grace window, so the grace
|
|
// protection alone cannot clear it), and that display is later torn down
|
|
// and re-added again: the stale snapshot must not restore the old
|
|
// placement over the user's newer choice.
|
|
moveWindowManually(win, { x: 100, y: 100, width: 1400, height: 900 });
|
|
now += 60_000;
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery drops the removal snapshot after the user moves to another secondary display", () => {
|
|
const TERTIARY = { id: 3, bounds: { x: -1920, y: 0, width: 1920, height: 1080 } };
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
|
|
attachDisplayRecovery({ win, screen, teardownGraceMs: 0 });
|
|
|
|
// User is working with the window on the secondary display: placement gets tracked.
|
|
win.bounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The secondary display disappears and the OS relocates the window to the
|
|
// primary display, capturing a removal-time snapshot for SECONDARY.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The user then deliberately moves the relocated window onto the
|
|
// still-connected third display: the stale SECONDARY snapshot must be
|
|
// dropped so a later re-add of SECONDARY cannot yank the window back.
|
|
moveWindowManually(win, { x: -1800, y: 100, width: 1400, height: 900 });
|
|
|
|
// The removed display comes back: the user's placement on TERTIARY stands.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery defers recovery while maximized and applies it on unmaximize", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// The window stays maximized while the display is missing and returns:
|
|
// recovery must not be lost, only deferred.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// Leaving the maximized state applies the deferred recovery.
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2000, y: 100, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery applies a deferred recovery after a transient added id stabilizes", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
win.normalBounds = { ...win.bounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
const returningDisplay = { ...SECONDARY, id: -1 };
|
|
screen.emit("display-added", {}, returningDisplay);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// Electron can learn the durable id and report changed topology before the
|
|
// maximized window returns to normal. The queued recovery must follow the
|
|
// same display by its now-stable identity even though geometry also changed.
|
|
returningDisplay.id = SECONDARY.id;
|
|
returningDisplay.bounds = { x: -2560, y: 0, width: 2560, height: 1440 };
|
|
screen.emit("display-metrics-changed", {}, returningDisplay, ["bounds"]);
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.equal(
|
|
boundsIntersectDisplay(win.setBoundsCalls[0], returningDisplay.bounds),
|
|
true
|
|
);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps a deferred recovery queued when its display vanishes again", () => {
|
|
// The deferred recovery is the only remaining candidate: the removal-time
|
|
// snapshot was consumed when the recovery was deferred. If the display
|
|
// vanishes again before the window leaves the maximized state and the user
|
|
// unmaximizes while it is still gone, the next re-add must still restore.
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen, teardownGraceMs: 0 });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// The OS relocates the maximized window to the primary display; the
|
|
// re-added display defers the recovery because the window stays maximized.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// The display disappears once more while the window is still maximized.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// A later edit while the display stays disconnected supersedes the
|
|
// tracked placement (past the grace window), leaving the deferred
|
|
// recovery as the only remaining candidate.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
// The user unmaximizes while the display is absent: the deferred recovery
|
|
// must survive instead of being discarded.
|
|
win.unmaximize();
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// When the display returns, the deferred recovery is finally applied.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2000, y: 100, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery drops a deferred recovery when the user re-placed the window", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen, teardownGraceMs: 0 });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.unmaximize();
|
|
|
|
// While the display is absent the user deliberately moves the window
|
|
// somewhere else on the primary display: the stale deferred recovery must
|
|
// not drag it back when the display re-appears.
|
|
win.bounds = { x: 40, y: 60, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery drops a deferred recovery when the maximized window is moved to another monitor", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen, teardownGraceMs: 0 });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
// The OS relocates the maximized window to the primary display; the
|
|
// re-added display defers the recovery because the window stays maximized.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// While still maximized, the user moves the window to another monitor
|
|
// (e.g. Win+Shift+Arrow): getNormalBounds() changes but the deferred
|
|
// recovery stays queued. Unmaximizing must not overwrite the newer
|
|
// placement with the stale deferred recovery.
|
|
win.normalBounds = { x: 40, y: 60, width: 1400, height: 900 };
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery respects a maximized keyboard move after an early display return", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// The display returns before Windows moves the maximized window, so the
|
|
// recovery target is retained for a possible late OS relocation.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
// Win+Shift+Arrow changes the normal placement and emits a regular move,
|
|
// but not Electron's manual-only will-move event. This is a user choice and
|
|
// must not be mistaken for the late OS relocation. before-input-event is
|
|
// the reliable intent signal that distinguishes the otherwise identical
|
|
// window event sequence.
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
win.normalBounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery restores a maximized OS relocation delivered after unlock", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
// The same normal-bounds change can arrive after unlock without any user
|
|
// monitor-transfer input. It remains part of the queued OS relocation.
|
|
win.normalBounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery respects a maximized keyboard move from a returned display to another secondary", () => {
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
win.normalBounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
win.maximized = false;
|
|
win.bounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("unmaximize") || []) handler();
|
|
|
|
screen.emit("display-removed", {}, TERTIARY);
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery waits for a maximized shortcut destination when resize fires first", () => {
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const resizedSourceBounds = { x: 2200, y: 80, width: 1000, height: 700 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
// Windows can emit the resize half while the normal bounds still belong to
|
|
// the source display. Keep waiting for the paired move event to reveal the
|
|
// actual destination instead of arming recovery back to the source.
|
|
win.normalBounds = { ...resizedSourceBounds };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
win.normalBounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery protects a maximized keyboard transfer from a trailing OS relocation", () => {
|
|
const realNow = Date.now;
|
|
let now = 4_800_000;
|
|
Date.now = () => now;
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
win.normalBounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The user's transfer to B wins even if a trailing event from the old
|
|
// Windows relocation arrives well after the ordinary grace window.
|
|
now += 3_000;
|
|
win.normalBounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], tertiaryBounds);
|
|
|
|
// A second half of the same stale relocation can arrive outside the
|
|
// ordinary burst window too; it still must not undo the explicit choice.
|
|
now += 3_000;
|
|
win.normalBounds = { x: 120, y: 120, width: 1200, height: 800 };
|
|
win.bounds = { ...win.normalBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.deepEqual(win.setBoundsCalls[1], tertiaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery protects an unmaximized keyboard transfer from a late OS relocation", () => {
|
|
const realNow = Date.now;
|
|
let now = 5_100_000;
|
|
Date.now = () => now;
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
win.normalBounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The user can leave maximized mode before Windows delivers its old
|
|
// relocation. The explicit destination still wins when that system event
|
|
// arrives later without a manual-placement signal.
|
|
win.maximized = false;
|
|
win.bounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("unmaximize") || []) handler();
|
|
now += 3_000;
|
|
win.normalBounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
win.bounds = { ...win.normalBounds };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], tertiaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery respects a maximized keyboard move while the old display is absent", () => {
|
|
const TERTIARY = {
|
|
id: 3,
|
|
bounds: { x: -1920, y: 0, width: 1920, height: 1080 },
|
|
};
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const tertiaryBounds = { x: -1700, y: 100, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen({ displays: [PRIMARY, SECONDARY, TERTIARY] });
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.webContents.emit("before-input-event", {}, {
|
|
type: "keyDown",
|
|
key: "ArrowLeft",
|
|
meta: true,
|
|
shift: true,
|
|
});
|
|
win.normalBounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
win.maximized = false;
|
|
win.bounds = { ...tertiaryBounds };
|
|
for (const handler of win.__listeners.get("unmaximize") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps a maximized OS relocation during an active interruption", () => {
|
|
const secondaryBounds = { x: 2000, y: 100, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
// The normal placement changes while the session is still locked. The user
|
|
// cannot have initiated this move, so it remains a recoverable OS action.
|
|
win.normalBounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
powerMonitor.emit("unlock-screen");
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps recovery through a paired late move and resize", () => {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
// A single Windows relocation can emit the complementary resize after its
|
|
// move. The short recovery tail must cover that second event too.
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.deepEqual(win.bounds, secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps recovery when display return splits a relocation burst", () => {
|
|
const realNow = Date.now;
|
|
let now = 5_000_000;
|
|
Date.now = () => now;
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
// Windows emits the move half of one relocation, then reports the display
|
|
// return before delivering the complementary resize half.
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
// While the session remains locked, elapsed wall time cannot turn a later
|
|
// system event into user intent: the user cannot have moved the window.
|
|
now += 3_000;
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.deepEqual(win.bounds, secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps maximized recovery when display return splits a relocation burst", () => {
|
|
const realNow = Date.now;
|
|
let now = 5_100_000;
|
|
Date.now = () => now;
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
win.maximized = true;
|
|
win.normalBounds = { ...secondaryBounds };
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
|
|
win.normalBounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
|
|
// The trailing resize is the second half of the same OS relocation, not a
|
|
// user placement, even when it arrives after the ordinary grace window.
|
|
now += 3_000;
|
|
win.normalBounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
powerMonitor.emit("unlock-screen");
|
|
win.unmaximize();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
assert.deepEqual(win.setBoundsCalls[0], secondaryBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery keeps recovery through repeated late move events", () => {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
win.bounds = { x: 120, y: 110, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.deepEqual(win.bounds, secondaryBounds);
|
|
});
|
|
|
|
test("attachDisplayRecovery protects a manual adjustment between late move and resize events", () => {
|
|
const realNow = Date.now;
|
|
let now = 5_200_000;
|
|
Date.now = () => now;
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const manualBounds = { x: 2300, y: 180, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
const powerMonitor = createMockPowerMonitor();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen, powerMonitor });
|
|
powerMonitor.emit("lock-screen");
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
powerMonitor.emit("unlock-screen");
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
now += 1_900;
|
|
moveWindowManually(win, manualBounds);
|
|
now += 200;
|
|
win.bounds = { x: 100, y: 100, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("resize") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 2);
|
|
assert.deepEqual(win.setBoundsCalls[1], manualBounds);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery respects a manual move after recovering a late relocation", () => {
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
|
|
moveWindowManually(win, { x: 200, y: 160, width: 1200, height: 800 });
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
});
|
|
|
|
test("attachDisplayRecovery does not rearm late recovery after the burst expires", () => {
|
|
const realNow = Date.now;
|
|
let now = 4_000_000;
|
|
Date.now = () => now;
|
|
const secondaryBounds = { x: 2100, y: 120, width: 1400, height: 900 };
|
|
const manualBounds = { x: 2300, y: 180, width: 1200, height: 800 };
|
|
const win = createMockWindow({ ...secondaryBounds });
|
|
const screen = createMockScreen();
|
|
|
|
try {
|
|
attachDisplayRecovery({ win, screen });
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
now += 3_000;
|
|
|
|
// Once the queued relocation burst has expired, a manual adjustment on the
|
|
// target is ordinary user placement and must not create a new indefinite
|
|
// late-relocation record.
|
|
moveWindowManually(win, manualBounds);
|
|
win.bounds = { x: 200, y: 160, width: 1200, height: 800 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
} finally {
|
|
Date.now = realNow;
|
|
}
|
|
});
|
|
|
|
test("attachDisplayRecovery clamps restored windows to the display work area", () => {
|
|
// The re-added secondary display reserves a 40px top taskbar: restoring
|
|
// with the full display bounds would place the window partly beneath it.
|
|
const DOCKED = {
|
|
id: 2,
|
|
bounds: { x: 1920, y: 0, width: 2560, height: 1440 },
|
|
workArea: { x: 1920, y: 40, width: 2560, height: 1400 },
|
|
};
|
|
const win = createMockWindow({ x: 2000, y: 20, width: 1400, height: 900 });
|
|
const screen = createMockScreen({ displays: [PRIMARY, DOCKED] });
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, DOCKED);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, DOCKED);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 1);
|
|
// The y coordinate is pulled down to the top of the work area instead of
|
|
// the display bounds.
|
|
assert.deepEqual(win.setBoundsCalls[0], { x: 2000, y: 40, width: 1400, height: 900 });
|
|
});
|
|
|
|
test("attachDisplayRecovery does not claim a window split evenly across the removed display", () => {
|
|
// Exactly half of the window sits on the primary display and half on the
|
|
// removed secondary one (the 1920 boundary splits it down the middle): the
|
|
// ownership tie must be treated as ambiguous, so no snapshot is taken and
|
|
// re-adding the display does not move the window.
|
|
const win = createMockWindow({ x: 1720, y: 100, width: 400, height: 300 });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 400, height: 300 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery does nothing when the window never left the primary display", () => {
|
|
const win = createMockWindow({ x: 100, y: 100, width: 1200, height: 800 });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery does not claim a primary-display window that merely overlaps the removed display", () => {
|
|
// The window lives primarily on the primary display but its right edge
|
|
// overlaps the secondary display.
|
|
const overlappingBounds = { x: 1700, y: 100, width: 400, height: 300 };
|
|
const win = createMockWindow({ ...overlappingBounds });
|
|
const screen = createMockScreen();
|
|
|
|
attachDisplayRecovery({ win, screen });
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// The secondary display is torn down and the OS moves the window fully onto
|
|
// the primary display: the removal-time snapshot must not claim the window
|
|
// for the secondary display, since it primarily lived on the primary.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 400, height: 300 };
|
|
for (const handler of win.__listeners.get("move") || []) handler();
|
|
|
|
// When the secondary display returns, the user's primary-display placement
|
|
// must stand: no recovery move.
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("detach removes all listeners and stops recovery", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
|
|
const detach = attachDisplayRecovery({ win, screen });
|
|
detach();
|
|
|
|
assert.equal((screen.__listeners.get("display-removed") || []).length, 0);
|
|
assert.equal((screen.__listeners.get("display-added") || []).length, 0);
|
|
assert.equal((screen.__listeners.get("display-metrics-changed") || []).length, 0);
|
|
assert.equal((win.__listeners.get("will-move") || []).length, 0);
|
|
assert.equal((win.__listeners.get("will-resize") || []).length, 0);
|
|
assert.equal((win.__listeners.get("move") || []).length, 0);
|
|
assert.equal((win.__listeners.get("resize") || []).length, 0);
|
|
assert.equal((win.__listeners.get("unmaximize") || []).length, 0);
|
|
assert.equal((win.__listeners.get("leave-full-screen") || []).length, 0);
|
|
assert.equal(
|
|
(win.__webContentsListeners.get("before-input-event") || []).length,
|
|
0
|
|
);
|
|
|
|
// Events after detach must not move the window.
|
|
screen.emit("display-removed", {}, SECONDARY);
|
|
win.bounds = { x: 100, y: 100, width: 1400, height: 900 };
|
|
screen.emit("display-added", {}, SECONDARY);
|
|
assert.equal(win.setBoundsCalls.length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery stays disabled outside Windows", () => {
|
|
const win = createMockWindow({ x: 2000, y: 100, width: 1400, height: 900 });
|
|
const screen = createMockScreen();
|
|
|
|
const detach = attachDisplayRecoveryForPlatform({ win, screen, platform: "linux" });
|
|
|
|
assert.equal(typeof detach, "function");
|
|
assert.equal((screen.__listeners.get("display-removed") || []).length, 0);
|
|
assert.equal((screen.__listeners.get("display-added") || []).length, 0);
|
|
assert.equal((win.__listeners.get("will-move") || []).length, 0);
|
|
assert.equal((win.__listeners.get("move") || []).length, 0);
|
|
});
|
|
|
|
test("attachDisplayRecovery tolerates a missing screen module", () => {
|
|
const win = createMockWindow({ x: 100, y: 100, width: 1200, height: 800 });
|
|
const detach = attachDisplayRecovery({ win, screen: null });
|
|
assert.equal(typeof detach, "function");
|
|
detach();
|
|
});
|