'use strict' const assert = require('node:assert/strict') const Module = require('node:module') const test = require('node:test') const { EventEmitter } = require('node:events') // displays.js 顶层 require('electron'),而开发机上的 electron 包没有下载 dist,直接 require 会抛。 // 这里在加载它之前把 electron 换成桩:本文件只验证「窗口模式 ↔ URL 里的 windowed」这条不变式, // 不需要真窗口,也跑不了真的窗口级全屏(那只能在 Windows 实机上验)。 class FakeWebContents extends EventEmitter { constructor() { super() this.url = '' this.sent = [] this.loadCount = 0 } send(channel, payload) { this.sent.push([channel, payload]) } loadURL(url) { this.url = url this.loadCount += 1 this.emit('did-finish-load') return Promise.resolve() } getURL() { return this.url } reload() { this.loadCount += 1 this.emit('did-finish-load') } } let created = [] class FakeBrowserWindow extends EventEmitter { constructor(options) { super() this.options = options this.webContents = new FakeWebContents() this.destroyed = false this.kiosk = options.kiosk === true this.fullScreen = false // Electron 里 setIgnoreMouseEvents / setAlwaysOnTop 的默认态:不穿透、不置顶 this.ignoreMouse = false this.alwaysOnTop = false this.alwaysOnTopLevel = null // 调用流水。坐标与窗口态的**先后顺序**本身就是被测行为(见 assertKioskOrder): // 只看最终状态会漏掉「先进 kiosk 再 setBounds」这种被改回去的回归 this.calls = [] // 模拟真实 Electron 在切换瞬间回读到的旧值:设了它 isFullScreen() 就返回这个 this.staleFullScreen = undefined this.bounds = { x: options.x, y: options.y, width: options.width, height: options.height } this.shown = false created.push(this) } // Electron 的 BrowserWindow.loadURL 是 webContents.loadURL 的快捷方式,displays.js 用的是前者 loadURL(url) { return this.webContents.loadURL(url) } isDestroyed() { return this.destroyed } destroy() { this.destroyed = true this.emit('closed') } isFullScreen() { return this.staleFullScreen === undefined ? this.fullScreen : this.staleFullScreen } setFullScreen(value) { this.fullScreen = value this.calls.push(['setFullScreen', value]) } isKiosk() { return this.kiosk } // 真实 Electron 里 kiosk 就是窗口级全屏,两者同进同退,桩也照这个来, // 否则「kiosk 窗口其实是全屏」这层关系测不出来 setKiosk(value) { this.kiosk = value this.fullScreen = value this.calls.push(['setKiosk', value]) } getBounds() { return this.bounds } setBounds(bounds) { this.bounds = bounds this.calls.push(['setBounds', { ...bounds }]) } isAlwaysOnTop() { return this.alwaysOnTop } setAlwaysOnTop(value, level) { this.alwaysOnTop = value this.alwaysOnTopLevel = value ? level || 'floating' : null this.calls.push(['setAlwaysOnTop', value, level]) } setIgnoreMouseEvents(value) { this.ignoreMouse = value this.calls.push(['setIgnoreMouseEvents', value]) } show() { this.shown = true } focus() {} isMinimized() { return false } restore() {} } // screen 桩要让「挂监听 / 摘监听」可观察,否则 P1(18) 那类 // 「监听被永久摘掉」的回归测不出来 class FakeScreen extends EventEmitter { constructor() { super() this.displays = [] this.primaryId = 1 } getAllDisplays() { return this.displays } getPrimaryDisplay() { return this.displays.find((item) => item.id === this.primaryId) || this.displays[0] } } const fakeScreen = new FakeScreen() const electronStub = { BrowserWindow: FakeBrowserWindow, screen: fakeScreen } const originalLoad = Module._load Module._load = function (request, parent, isMain) { if (request === 'electron') return electronStub return originalLoad.call(this, request, parent, isMain) } const { DisplayManager } = require('./displays') Module._load = originalLoad // 随包发布的内置默认值:用例要验「多屏 kiosk 默认不穿透」,就得拿**真的默认值**去跑, // 在测试里自己写死一份 false 是验不到 DEFAULTS/config.json 被改回 true 的 const { DEFAULTS } = require('./config') // 随包的 config.json 模板(打包态首次启动复制到用户数据目录)是现场真正的默认值, // 只改代码里的 DEFAULTS 而漏了它,现场升级后仍然是穿透 const shippedConfig = require('../config.json') const WIDTH = 1920 const HEIGHT = 1080 function fakeDisplay(id, { internal = false } = {}) { return { id, label: `显示器 ${id}`, internal, bounds: { x: 0, y: 0, width: WIDTH, height: HEIGHT }, scaleFactor: 1 } } // allowClickThrough 默认直接取 config.js 里的内置默认值(false = 不穿透), // 这样「默认值被改回 true」这件事本身会被用例抓住;要穿透的用例显式传 { allowClickThrough: true }。 // legacy 用来注入旧机器配置文件里留下的老键(如 ignoreMouse: true),验「老键已失效」 function makeManager(t, { selection = 'all', kiosk = true, allowClickThrough = DEFAULTS.display.allowClickThrough, legacy = {} } = {}) { fakeScreen.displays = [] fakeScreen.primaryId = 1 // 上一个用例若忘了收摊,残留的监听会串到这一个用例里 fakeScreen.removeAllListeners() created = [] const log = { lines: [], push(_scope, text) { this.lines.push(text) } } const manager = new DisplayManager({ values: { display: { selection, displays: [], pages: ['standings', 'medals'], pageParam: 'page', kiosk, allowClickThrough, reloadEveryHours: 0, backgroundColor: '#000000', ...legacy } }, server: { host: '0.0.0.0', port: 19540 }, log }) t.after(() => manager.closeAll()) return manager } /** * 唯一不变式:窗口的几何/模式、页面 URL 里的 windowed 参数必须一致。 * 这条断言在每个用例里都跑,任何一种脱钩都会在这里炸出来。 */ function assertModeConsistent(manager, displayId, expectedSingle) { const entry = manager.windows.get(displayId) assert.ok(entry, `display=${displayId} 上应该有窗口`) assert.equal(entry.single, expectedSingle, `entry.single 应为 ${expectedSingle}`) assert.equal(entry.win.isKiosk(), expectedSingle === false, 'kiosk 标记应与 entry.single 一致') const url = new URL(entry.win.webContents.getURL()) assert.equal(url.pathname, '/display') assert.equal( url.searchParams.get('windowed'), expectedSingle ? '1' : null, `URL 的 windowed 应与 entry.single 一致:${url.href}` ) const bounds = entry.win.getBounds() assert.equal(bounds.width, expectedSingle ? Math.round(WIDTH * 0.8) : WIDTH, '窗口宽度应与模式一致') assert.equal(bounds.height, expectedSingle ? Math.round(HEIGHT * 0.8) : HEIGHT, '窗口高度应与模式一致') return entry } test('单屏排布:窗口化窗口的 URL 带 windowed=1', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true })] manager.enable() const entry = assertModeConsistent(manager, 1, true) assert.equal(entry.manual, false) }) test('多屏排布:kiosk 窗口的 URL 不带 windowed', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() assertModeConsistent(manager, 1, false) assertModeConsistent(manager, 2, false) }) test('回归:窗口化窗口被排布改成 kiosk 时,URL 一并换成不带 windowed 并重载', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true })] manager.enable() const before = assertModeConsistent(manager, 1, true) const loadsBefore = before.win.webContents.loadCount assert.match(before.win.webContents.getURL(), /windowed=1/) // 插上第二块屏,整机从「单屏窗口化」变成「多屏 kiosk」 fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.layout() const after = assertModeConsistent(manager, 1, false) assert.equal(after.win, before.win, '同一个窗口应当就地改模式,而不是重建') assert.equal(after.win.webContents.loadCount, loadsBefore + 1, '模式变了必须重载一次 URL') assert.doesNotMatch(after.win.webContents.getURL(), /windowed=1/) }) test('回归反向:kiosk 窗口被排布改成窗口化时,URL 补上 windowed=1', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() const before = assertModeConsistent(manager, 1, false) const loadsBefore = before.win.webContents.loadCount // 拔掉扩展屏,只剩主屏 → 退化成窗口化 fakeScreen.displays = [fakeDisplay(1, { internal: true })] manager.layout() const after = assertModeConsistent(manager, 1, true) assert.equal(after.win, before.win, '同一个窗口应当就地改模式,而不是重建') assert.equal(after.win.webContents.loadCount, loadsBefore + 1, '模式变了必须重载一次 URL') assert.match(after.win.webContents.getURL(), /windowed=1/) }) test('管理面板手动开的窗口保持自己的模式,不被自动排布改成 kiosk', (t) => { const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary] const opened = manager.openOn(primary, { page: 'standings', single: true }) assert.deepEqual(opened, { created: true, mode: 'windowed' }) assertModeConsistent(manager, 1, true) // 之后整机变成多屏,自动排布也不许把这个手动开的小窗顶成 kiosk fakeScreen.displays = [primary, fakeDisplay(2)] manager.layout() assertModeConsistent(manager, 1, true) assertModeConsistent(manager, 2, false) }) test('模式上报以 entry 为准:窗口化窗口被 F11 全屏后仍报 windowed', (t) => { const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary] manager.openOn(primary, { single: true }) const win = manager.windows.get(1).win win.setFullScreen(true) // 只聚焦不重建时,模式必须还说窗口化:拿 isFullScreen() 反推会误报成 kiosk assert.deepEqual(manager.openOn(primary, { single: true }), { created: false, mode: 'windowed' }) assert.equal(manager.listDisplays().displays.find((item) => item.id === 1).mode, 'windowed') assert.equal(manager.modeOf(manager.windows.get(1)), 'windowed') // kiosk 窗口不受 F11 影响,仍报 kiosk fakeScreen.displays = [primary, fakeDisplay(2)] manager.layout() assert.equal(manager.listDisplays().displays.find((item) => item.id === 2).mode, 'kiosk') }) test('F11 下发的是算出来的确定值,不回读窗口', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true })] manager.enable() const win = manager.windows.get(1).win const [handler] = win.webContents.listeners('before-input-event') assert.ok(handler, '窗口化窗口必须挂 F11 处理') let prevented = 0 const pressF11 = () => handler({ preventDefault: () => { prevented += 1 } }, { type: 'keyDown', key: 'F11' }) win.webContents.sent.length = 0 pressF11() assert.equal(prevented, 1) assert.equal(win.isFullScreen(), true) assert.deepEqual(win.webContents.sent, [['window:fullscreen-changed', true]]) win.webContents.sent.length = 0 pressF11() assert.equal(win.isFullScreen(), false) assert.deepEqual(win.webContents.sent, [['window:fullscreen-changed', false]]) // 切换瞬间回读可能拿到旧值:enter/leave 必须下发事件本身的确定值 win.webContents.sent.length = 0 win.staleFullScreen = false win.emit('enter-full-screen') assert.deepEqual(win.webContents.sent, [['window:fullscreen-changed', true]]) win.webContents.sent.length = 0 win.staleFullScreen = true win.emit('leave-full-screen') assert.deepEqual(win.webContents.sent, [['window:fullscreen-changed', false]]) win.staleFullScreen = undefined // 加载完成那次窗口状态已经稳定,继续用「读一次」 win.webContents.sent.length = 0 win.fullScreen = true win.webContents.emit('did-finish-load') assert.deepEqual(win.webContents.sent, [['window:fullscreen-changed', true]]) }) test('kiosk 窗口不挂 F11 处理', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() assert.equal(manager.windows.get(2).win.webContents.listeners('before-input-event').length, 0) }) // —— 窗口层行为:能不能点穿、压不压得住任务栏 —— // 现场回归:① 大屏窗口默认 setIgnoreMouseEvents(true),点击穿透到后面的窗口/桌面, // 「防误触」变成了「误点别的东西」,被穿透的窗口还点不活; // ② kiosk 窗口先 setKiosk(true) 再 setBounds,被 Windows 从真正的全屏态拽回 // 「恰好铺满该屏的无边框窗口」,不是 topmost,任务栏与拖过来的窗口都能盖住它。 // ①的开关键从 ignoreMouse 改名成了 allowClickThrough:旧键在老机器的用户配置里被写死着, // 只改默认值救不了它们,用例里的「老机器」就是带 ignoreMouse: true 进来的那份配置。 // 窗口调用流水里「最后一次」满足条件的调用下标;找不到返回 -1 function lastCallIndex(win, name, match = () => true) { return win.calls.reduce((found, [called, ...args], index) => ( called === name && match(...args) ? index : found ), -1) } // kiosk 窗口必须:先 setBounds 定位 → 再 setKiosk(true) → 最后置顶到 screen-saver。 // 顺序反了就是上面 ② 那个事故;这里断言的是顺序而不是最终状态,正为此。 function assertKioskOrder(win) { const boundsAt = lastCallIndex(win, 'setBounds') const kioskAt = lastCallIndex(win, 'setKiosk', (value) => value === true) assert.notEqual(kioskAt, -1, 'kiosk 窗口必须真的进过 kiosk') assert.ok( boundsAt !== -1 && boundsAt < kioskAt, `setBounds(#${boundsAt}) 必须在 setKiosk(true)(#${kioskAt}) 之前,实际调用流水:${JSON.stringify(win.calls)}` ) const topAt = lastCallIndex(win, 'setAlwaysOnTop', (value) => value === true) assert.ok(topAt > kioskAt, `进 kiosk 之后必须置顶,实际调用流水:${JSON.stringify(win.calls)}`) assert.equal(win.alwaysOnTopLevel, 'screen-saver', 'Windows 上只有 pop-up-menu 及以上才画在任务栏之上') } test('新键缺省 = 不穿透:多屏 kiosk 不调 setIgnoreMouseEvents(true)', (t) => { // 先把「默认值本身」钉住:这份默认值改成 true,就等于把点击穿透又放回现场 assert.equal(DEFAULTS.display.allowClickThrough, false, '内置默认值必须是「不穿透」') assert.equal(shippedConfig.display.allowClickThrough, false, '随包的 config.json 模板也必须是「不穿透」') assert.equal('ignoreMouse' in DEFAULTS.display, false, '旧键必须从 DEFAULTS 里消失,否则等于还在读它') const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() for (const id of [1, 2]) { const win = manager.windows.get(id).win assert.equal(win.ignoreMouse, false, `display=${id} 默认必须吞掉鼠标:穿透会把点击送给后面的窗口/桌面`) assert.equal(lastCallIndex(win, 'setIgnoreMouseEvents', (value) => value === true), -1) } }) test('只有显式配 display.allowClickThrough=true 才穿透', (t) => { const manager = makeManager(t, { allowClickThrough: true }) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() assert.deepEqual( manager.windows.get(2).win.calls.filter(([name]) => name === 'setIgnoreMouseEvents'), [['setIgnoreMouseEvents', true]] ) }) test('回归钉子:老机器配置里留着 ignoreMouse: true 也不穿透(旧键已失效)', (t) => { // 这就是本次修复的核心:旧版模板把 ignoreMouse: true 写进了现场机器的用户配置, // 而 userpaths.js 不会覆盖用户文件——只要还读旧键,那台机器升级后照样穿透 const manager = makeManager(t, { legacy: { ignoreMouse: true } }) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() for (const id of [1, 2]) { const win = manager.windows.get(id).win assert.equal(win.ignoreMouse, false, `display=${id}:旧键 ignoreMouse 已废弃,不能让它把穿透带回来`) assert.equal(lastCallIndex(win, 'setIgnoreMouseEvents', (value) => value === true), -1) } // 用例本身要确保旧键真的塞进了配置里,否则这条测试等于没测 assert.equal('ignoreMouse' in manager.display, true) assert.equal(manager.display.allowClickThrough, false, '新键没被显式写成 true') }) test('单屏窗口化不受穿透开关影响:配了穿透也不穿透、也不置顶', (t) => { const manager = makeManager(t, { allowClickThrough: true }) fakeScreen.displays = [fakeDisplay(1, { internal: true })] manager.enable() const win = manager.windows.get(1).win assert.equal(win.ignoreMouse, false, '穿透这一行从来只管多屏 kiosk') assert.equal(lastCallIndex(win, 'setAlwaysOnTop', (value) => value === true), -1, '窗口化的 F11 全屏归用户管,不能置顶') assert.equal(win.isKiosk(), false) }) test('kiosk 窗口:先 setBounds 再 setKiosk(true),最后置顶 screen-saver', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() for (const id of [1, 2]) { const win = manager.windows.get(id).win assertKioskOrder(win) assert.equal(win.isKiosk(), true) assert.equal(win.alwaysOnTop, true, '被别的窗口盖住后要能点回最前') } }) test('面板手动开的 kiosk 窗口同样置顶、不穿透', (t) => { const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) const external = fakeDisplay(2) fakeScreen.displays = [primary, external] assert.deepEqual(manager.openOn(external, { page: 'medals', single: false }), { created: true, mode: 'kiosk' }) const win = manager.windows.get(2).win assertKioskOrder(win) assert.equal(win.ignoreMouse, false) }) test('kiosk 已是目标态时不重复进出 kiosk(重排不该让大屏白闪)', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() const win = manager.windows.get(2).win const callsBefore = JSON.stringify(win.calls) // 一次「屏幕没变」的重排:窗口态已经是目标态,就一个窗口调用都不该发生 manager.layout() assert.equal(JSON.stringify(win.calls), callsBefore) assertKioskOrder(win) }) test('插拔屏幕就地换模式:置顶跟着 kiosk 来回,顺序不变', (t) => { const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary] manager.enable() const win = manager.windows.get(1).win assert.equal(win.alwaysOnTop, false, '单屏窗口化不置顶') // 插上电视:同一块屏上的窗口就地转 kiosk,必须补上置顶 fakeScreen.displays = [primary, fakeDisplay(2)] manager.layout() assertModeConsistent(manager, 1, false) assertKioskOrder(win) // 电视拔了:转回窗口化,置顶必须摘掉,否则窗口化态会继续盖住一切 fakeScreen.displays = [primary] manager.layout() assertModeConsistent(manager, 1, true) assert.equal(win.alwaysOnTop, false) assert.equal(win.alwaysOnTopLevel, null) }) test('收窗路径不被置顶破坏:closeDisplay / closeAll 仍收得掉 kiosk 窗口', (t) => { const manager = makeManager(t) fakeScreen.displays = [fakeDisplay(1, { internal: true }), fakeDisplay(2)] manager.enable() const external = manager.windows.get(2).win const primary = manager.windows.get(1).win assert.equal(manager.closeDisplay(2), true) assert.equal(external.isDestroyed(), true) assert.equal(manager.windows.has(2), false) assert.equal(manager.closeDisplay(2), false, '已经收掉的窗口不能再报成功') manager.closeAll() assert.equal(primary.isDestroyed(), true) assert.equal(manager.windows.size, 0) assert.equal(manager.enabled, false) }) // 屏幕监听的三条不变式,对应审查日志模块13 [1] P1: // ① 任何一次「屏幕数量变化」都不能把监听永久摘掉(只有 closeAll 摘) // ② 事件到达后 600ms 防抖重排,从 1 块恢复 2 块要自动回到 kiosk,从 2 块变 1 块要收回窗口化 // ③ 单屏态只冻结「分辨率/缩放」,不冻结「插拔」 const SCREEN_EVENTS = ['display-metrics-changed', 'display-added', 'display-removed'] function screenListenerCounts() { return SCREEN_EVENTS.map((event) => fakeScreen.listenerCount(event)) } // 触发屏幕事件并跑完防抖定时器 function emitScreen(t, event, display) { fakeScreen.emit(event, {}, display) t.mock.timers.tick(600) } test('回归 P1(18):出现过一次「只剩一块屏」之后,插回扩展屏仍会自动重排成 kiosk', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) const external = fakeDisplay(2) fakeScreen.displays = [primary, external] manager.enable() assertModeConsistent(manager, 2, false) // 电视待机 / HDMI 松动:系统报 display-removed,整机退化成单屏 fakeScreen.displays = [primary] emitScreen(t, 'display-removed', external) assert.equal(manager.windows.has(2), false, '只剩一块屏时扩展屏窗口要收掉,不能留在已消失的坐标上') assertModeConsistent(manager, 1, true) assert.deepEqual(screenListenerCounts(), [1, 1, 1], '退化成单屏也不许摘监听:这是唯一能听见电视插回来的通道') // 电视再开机:没有任何人工干预,必须自己排布回去 fakeScreen.displays = [primary, external] emitScreen(t, 'display-added', external) assertModeConsistent(manager, 1, false) assertModeConsistent(manager, 2, false) assert.deepEqual(screenListenerCounts(), [1, 1, 1], '重排之后监听仍然只挂一份') }) test('反复插拔:监听既不被摘掉也不叠加', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) const external = fakeDisplay(2) fakeScreen.displays = [primary, external] manager.enable() for (let i = 0; i < 3; i += 1) { fakeScreen.displays = [primary] emitScreen(t, 'display-removed', external) fakeScreen.displays = [primary, external] emitScreen(t, 'display-added', external) } assert.deepEqual(screenListenerCounts(), [1, 1, 1]) assertModeConsistent(manager, 1, false) assertModeConsistent(manager, 2, false) }) test('单屏态冻结分辨率/缩放变化,但不冻结屏幕数量变化', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary] manager.enable() const entry = assertModeConsistent(manager, 1, true) const loadsBefore = entry.win.webContents.loadCount // 单屏调试态下调分辨率/缩放:不跟着重排,现场调窗口时不会被打断 const resized = { ...primary, bounds: { x: 0, y: 0, width: 1280, height: 720 } } emitScreen(t, 'display-metrics-changed', resized) assert.equal(entry.win.webContents.loadCount, loadsBefore, '单屏态下分辨率变化不应触发重排') assert.deepEqual(screenListenerCounts(), [1, 1, 1]) // 但插上第二块屏必须重排——冻结分辨率不等于冻结插拔 fakeScreen.displays = [primary, fakeDisplay(2)] emitScreen(t, 'display-added', fakeDisplay(2)) assertModeConsistent(manager, 1, false) assertModeConsistent(manager, 2, false) }) test('从 2 块变 1 块:主屏窗口收回窗口化,不卡在 kiosk 全屏', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) const external = fakeDisplay(2) fakeScreen.displays = [primary, external] manager.enable() const entry = assertModeConsistent(manager, 1, false) // 真实 Electron 里 kiosk 窗口 isFullScreen() 也是 true entry.win.fullScreen = true fakeScreen.displays = [primary] emitScreen(t, 'display-removed', external) const after = assertModeConsistent(manager, 1, true) assert.equal(after.win, entry.win, '同一块屏上的窗口就地改模式,不重建') assert.equal(after.win.isFullScreen(), false, '必须退出全屏,否则窗口化模式会卡在黑屏全屏里') assert.deepEqual(after.win.getBounds(), { x: 192, y: 108, width: WIDTH - 2 * 192, height: HEIGHT - 2 * 108 }) }) test('只有 closeAll(服务停止 / 应用退出)才摘监听,重新启用后自动挂回', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary, fakeDisplay(2)] manager.enable() assert.deepEqual(screenListenerCounts(), [1, 1, 1]) // 服务停止:窗口全收、监听全摘(此时没有大屏要维护了) manager.closeAll() assert.deepEqual(screenListenerCounts(), [0, 0, 0]) assert.equal(manager.windows.size, 0) // 服务再起来:enable() 必须把监听挂回来,否则后续拔插又没人听 fakeScreen.displays = [primary, fakeDisplay(2)] manager.enable() assert.deepEqual(screenListenerCounts(), [1, 1, 1]) assertModeConsistent(manager, 2, false) // 重复 enable() 不能叠加监听 manager.enable() assert.deepEqual(screenListenerCounts(), [1, 1, 1]) }) test('面板手动开窗同样挂上监听:之后拔插屏幕不会失联', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t) const primary = fakeDisplay(1, { internal: true }) fakeScreen.displays = [primary] manager.openOn(primary, { single: true }) assert.deepEqual(screenListenerCounts(), [1, 1, 1], '手动开窗也是「管理已激活」,必须能听见屏幕变化') // 服务端一直没起来的情况下插上电视,也要自动排布; // 手动开的那块屏保持自己的窗口化模式,新插进来的屏按 kiosk 开 fakeScreen.displays = [primary, fakeDisplay(2)] emitScreen(t, 'display-added', fakeDisplay(2)) assertModeConsistent(manager, 1, true) assertModeConsistent(manager, 2, false) }) test('电视回来后 kiosk 仍开在 display.selection=specified 指定的那块屏上', (t) => { t.mock.timers.enable({ apis: ['setTimeout'] }) const manager = makeManager(t, { selection: 'specified' }) manager.display.displays = [2] const primary = fakeDisplay(1, { internal: true }) const external = fakeDisplay(2) fakeScreen.displays = [primary, external] manager.enable() assertModeConsistent(manager, 2, false) assert.equal(manager.windows.has(1), false, 'specified 模式下只该在指定的那块屏上开窗') // 电视待机:只剩主屏,先退化成窗口化 fakeScreen.displays = [primary] emitScreen(t, 'display-removed', external) assert.equal(manager.windows.has(2), false) assertModeConsistent(manager, 1, true) // 电视回来:必须重新只开在 display=2 上,而不是把主屏也铺成 kiosk fakeScreen.displays = [primary, external] emitScreen(t, 'display-added', external) assertModeConsistent(manager, 2, false) assert.equal(manager.windows.has(1), false) })