forked from Blendman974/faceclaw
149 lines
7.1 KiB
JavaScript
149 lines
7.1 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const vm = require('node:vm');
|
|
const ts = require('typescript');
|
|
|
|
function fixture(options = {}) {
|
|
const source = ts.createSourceFile('shell.ts', fs.readFileSync(path.join(__dirname, '../app/ui/shell/shell.ts'), 'utf8'), ts.ScriptTarget.Latest, true);
|
|
const shellClass = source.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'Shell');
|
|
const names = new Set(['routeInput', 'openEscapeMenu', 'closeAllWindows', 'closeWindow', 'removeWindow',
|
|
'foregroundWindow', 'noteWindowVisible', 'mostRecentWindowIndex', 'yieldFocusToSidebar']);
|
|
const methods = shellClass.members.filter(node => names.has(node.name?.getText(source)));
|
|
assert.equal(methods.length, names.size);
|
|
const overlay = source.statements.find(node => ts.isClassDeclaration(node) && node.name.text === 'ShellOverlayMenuLayer');
|
|
class MenuLayer {
|
|
constructor(title, items) { this.title = title; this.items = items; }
|
|
selectItem(index) { this.selectedIndex = index; }
|
|
}
|
|
const context = {
|
|
exports: {}, MenuLayer, console: { warn: () => {} },
|
|
appViewportRect: () => ({ x: 0, y: 0, width: 576 }), CONTEXT_MENU_DIM: 0.5,
|
|
brightnessSetting: { get: () => 'auto' }, gestureHints: () => 'App menu',
|
|
GESTURE_SHORT_THEN_LONG_PRESS: 'tap-hold', makeInputEvent: event => event,
|
|
isWatchInput: event => event.source === 'watch', isDirectionalInput: () => false,
|
|
};
|
|
vm.runInNewContext(ts.transpileModule(`${overlay.getText(source)}\nexport class Shell { ${methods.map(node => node.getText(source)).join('\n')} }`, {
|
|
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
|
|
}).outputText, context);
|
|
const s = new context.exports.Shell();
|
|
const closed = [], delivered = [], warnings = [], renders = [];
|
|
context.console.warn = message => warnings.push(message);
|
|
const window = (id, closeable = true, extra = {}) => ({
|
|
appId: id, windowId: id, closeable,
|
|
handleInput: async event => delivered.push([id, event.type]),
|
|
requestRender: () => renders.push(id),
|
|
close: () => closed.push(id),
|
|
...extra,
|
|
});
|
|
const launcher = window('launcher', false);
|
|
const app = window('current', true, options.app);
|
|
const background = window('background');
|
|
const layers = [];
|
|
s.stack = {
|
|
isAtBase: () => !layers.length,
|
|
push: layer => layers.push(layer),
|
|
pop: () => layers.pop()?.onRemoved?.(),
|
|
popIfTop: predicate => { if (!layers.length || !predicate(layers.at(-1))) return false; s.stack.pop(); return true; },
|
|
handleInput: async event => { if (event.type === 'double-click') s.stack.pop(); },
|
|
};
|
|
Object.assign(s, {
|
|
windows: [launcher, app, background], selectedIndex: 1, focus: 'sidebar', screenOn: true,
|
|
mruWindowIds: ['current', 'background', 'launcher'], attention: new Map(),
|
|
config: { voiceInputEnabled: false, requestShellRender() {}, onWindowsChanged() {} },
|
|
cancelEscapeMenuTimer() {}, startEscapeMenuTimer() { s.escapeStarted = true; },
|
|
lastInput: null,
|
|
});
|
|
const input = type => s.routeInput({ type, source: 'ring', timestampMs: 100 }, 1);
|
|
const menu = () => layers.at(-1);
|
|
const choose = label => {
|
|
const item = menu().items.find(item => item.label === label);
|
|
assert.ok(item, label);
|
|
item.onSelect({ stack: s.stack });
|
|
};
|
|
return { s, input, menu, choose, closed, delivered, warnings, renders, launcher, app, background, window };
|
|
}
|
|
|
|
test('switcher opens its menu on hold or tap-hold even for an app claiming holds or hold-to-talk', async () => {
|
|
for (const type of ['long-press', 'short-then-long-press']) {
|
|
for (const app of [{}, { claimsLongPress: () => true }, { holdToTalk: true }]) {
|
|
const h = fixture({ app });
|
|
await h.input(type);
|
|
assert.ok(h.menu());
|
|
assert.ok(h.menu().items.some(item => item.label === 'Close current app'));
|
|
assert.ok(h.menu().items.some(item => item.label === 'Close all apps'));
|
|
assert.equal(h.menu().items[h.menu().selectedIndex].label, 'Focus app switcher');
|
|
assert.deepEqual(h.delivered, [['current', 'system-menu-opened']]);
|
|
assert.equal(h.s.escapeStarted, undefined);
|
|
assert.equal(h.s.focus, 'sidebar');
|
|
}
|
|
}
|
|
});
|
|
|
|
test('close current app closes the captured window and preserves the launcher and background windows', async () => {
|
|
const h = fixture();
|
|
await h.input('long-press');
|
|
h.s.selectedIndex = 2; // An asynchronous focus change must not redirect the close action.
|
|
h.choose('Close current app');
|
|
assert.deepEqual(h.closed, ['current']);
|
|
assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher', 'background']);
|
|
assert.equal(h.s.focus, 'sidebar');
|
|
assert.equal(h.menu(), undefined);
|
|
});
|
|
|
|
test('launcher offers close all but never close current; close all leaves only the launcher', async () => {
|
|
const h = fixture();
|
|
h.s.selectedIndex = 0;
|
|
await h.input('short-then-long-press');
|
|
assert.ok(!h.menu().items.some(item => item.label === 'Close current app'));
|
|
h.choose('Close all apps');
|
|
assert.deepEqual(h.closed, ['current', 'background']);
|
|
assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher']);
|
|
assert.equal(h.s.selectedIndex, 0);
|
|
assert.equal(h.s.focus, 'sidebar');
|
|
await h.input('long-press');
|
|
assert.equal(h.menu().items.find(item => item.label === 'Close all apps').disabled(), true);
|
|
});
|
|
|
|
test('close all uses normal cleanup despite reentrant removal, multiple windows per app and throwing callbacks', () => {
|
|
const h = fixture();
|
|
h.s.windows.push(h.window('another', true, { appId: 'current' }), h.window('pinned', false));
|
|
h.app.close = () => { h.closed.push('current'); h.s.removeWindow('current'); };
|
|
h.background.close = () => { h.closed.push('background'); throw new Error('failed cleanup'); };
|
|
h.s.closeAllWindows();
|
|
assert.deepEqual(h.closed, ['current', 'background', 'another']);
|
|
assert.deepEqual(h.s.windows.map(w => w.windowId), ['launcher', 'pinned']);
|
|
assert.equal(h.warnings.length, 1);
|
|
assert.equal(h.s.foregroundWindow().windowId, 'launcher');
|
|
});
|
|
|
|
test('default selection and cancel do not close apps; an open overlay never receives a second menu', async () => {
|
|
const h = fixture();
|
|
await h.input('long-press');
|
|
h.choose(h.menu().items[h.menu().selectedIndex].label);
|
|
assert.deepEqual(h.closed, []);
|
|
await h.input('long-press');
|
|
const menu = h.menu();
|
|
await h.input('long-press');
|
|
assert.equal(h.menu(), menu);
|
|
await h.input('double-click');
|
|
assert.equal(h.menu(), undefined);
|
|
assert.deepEqual(h.closed, []);
|
|
});
|
|
|
|
test('in-app hold actions and app menus keep their existing routing; asleep switcher stays asleep', async () => {
|
|
for (const [app, type] of [[{ claimsLongPress: () => true }, 'long-press'], [{ holdToTalk: true }, 'long-press'], [{}, 'short-then-long-press']]) {
|
|
const h = fixture({ app });
|
|
h.s.focus = 'window';
|
|
await h.input(type);
|
|
assert.deepEqual(h.delivered, [['current', type]]);
|
|
assert.equal(h.menu(), undefined);
|
|
}
|
|
const h = fixture();
|
|
h.s.screenOn = false;
|
|
await h.input('long-press');
|
|
assert.equal(h.menu(), undefined);
|
|
assert.deepEqual(h.closed, []);
|
|
});
|