Files
faceclaw/tests/menu-label-marquee.test.cjs
T

219 lines
11 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { GrayImage } = require('../.test-build/app/graphics/image.js');
const { Menu } = require('../.test-build/app/ui/menu-core.js');
const { paintDisplayList } = require('../.test-build/app/graphics/display-list.js');
const pref = require('../.test-build/app/ui/menu-animation-pref.js');
// Distinctive ink at the tail proves the full label, not an ellipsis, is rasterized.
const font = { lineHeight: 8, measureText: s => s.length * 4,
drawText(image, x, y, text, value) {
for (let i = 0; i < text.length; i++) image.fillRect(x + i * 4, y, text[i] === 'Z' ? 3 : 1, 4, value);
} };
function fixture() {
const labels = ['abcdefghijkZ', 'short'];
const menu = new Menu({ items: labels, getHeight: () => 12,
draw: ({ image, item, x, y, width, drawLabel }) => {
if (drawLabel) drawLabel(font, x + 3, y + 2, width - 6, item, 240);
else image.drawText(font, x + 3, y + 2, item, 240);
} });
return { menu, labels, paint(focused = true, width = 30) {
const image = new GrayImage(50, 30); menu.paint(image, { x: 2, y: 1, width, height: 24 }, focused); return image;
} };
}
const fs = require('node:fs');
const vm = require('node:vm');
const ts = require('typescript');
function load(file, modules) {
const context = { exports: {}, require: name => { assert.ok(name in modules, name); return modules[name]; } };
vm.runInNewContext(ts.transpileModule(fs.readFileSync(file, 'utf8'), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 },
}).outputText, context);
return context.exports;
}
const menuModule = load('app/ui/menu.ts', {
'../graphics/image': { GrayImage }, '../graphics/screen-size': { screenHeight: () => 480 },
'../graphics/textwrap': {}, '../graphics/ui-fonts': { getDefaultSmallFont: () => font },
'../native/settings-store': { getStringSetting: (_key, fallback) => fallback },
'../util/numeric-util': { clamp: (n, lo, hi) => Math.max(lo, Math.min(hi, n)) },
'./animation-speed': require('../.test-build/app/ui/animation-speed.js'), './layers': {}, './gestures': {},
'./menu-animation-pref': pref, './menu-core': require('../.test-build/app/ui/menu-core.js'),
'./metrics': require('../.test-build/app/ui/metrics.js'),
});
const lists = image => image.draws.filter(d => d.presentation?.displayList?.timeline);
test('selected overflowing label is retained, initially paused, then reveals its tail inside its clip', () => {
const f = fixture();
const image = f.paint();
const moving = lists(image);
assert.equal(moving.length, 1);
const d = moving[0], list = d.presentation.displayList;
const render = ms => {
const pixels = new Uint8Array(50 * 30).fill(64);
paintDisplayList(pixels, pixels.slice(), 50, 30,
{ displayList: { ...list, presentedAt: list.timeline.startedAt }, x: d.x, y: d.y, width: d.source.width, height: d.source.height, depth: 0 }, false, list.timeline.startedAt + ms);
return pixels;
};
assert.deepEqual(render(0), render(700));
assert.notDeepEqual(render(0), render(1800));
assert.equal(render(1800)[3 * 50 + 27], 240, 'tail Z reaches the right edge');
assert.equal(render(1800)[3 * 50 + 29], 64, 'right inset is untouched');
assert.equal(render(1800)[13 * 50 + 8], 64, 'next row is untouched');
});
test('short, unselected, unfocused and disabled-animation labels stay clipped and static', () => {
const f = fixture();
assert.equal(lists(f.paint(false)).length, 0);
f.menu.select(1);
assert.equal(lists(f.paint()).length, 0);
pref.setMenuAnimationReader(() => 'disabled');
try {
f.menu.select(0);
const image = f.paint();
assert.equal(lists(image).length, 0);
const row = image.draws.find(d => d.presentation).source;
assert.ok(row.pixels.some(v => v === 240), 'static label remains readable');
for (let y = 0; y < row.height; y++) assert.equal(row.getPixel(28, y), 0, 'right inset stays clear');
} finally { pref.setMenuAnimationReader(() => 'normal'); }
});
test('same retained presentation keeps its clock across paints and resets on content, width and focus', () => {
const original = Date.now;
let now = 1000;
Date.now = () => now;
try {
const f = fixture();
const timeline = () => lists(f.paint())[0].presentation.displayList.timeline;
const first = timeline();
now = 1500;
assert.deepEqual(timeline(), first);
f.labels[0] += 'Z';
const content = timeline();
assert.equal(content.startedAt, 1500);
assert.notEqual(content.token, first.token);
const resized = lists(f.paint(true, 28))[0].presentation.displayList.timeline;
assert.notEqual(resized.token, content.token);
f.paint(false);
assert.notEqual(timeline().token, resized.token);
} finally { Date.now = original; }
});
test('retained label pauses at the end, returns, stops scheduling, and survives a later PRESENT', () => {
const { evaluate } = require('../.test-build/app/graphics/draw-expression.js');
const { encodeDisplayList, readDisplayList } = require('../.test-build/app/graphics/display-list.js');
const d = lists(fixture().paint())[0];
const list = d.presentation.displayList;
const offset = ms => evaluate(list.calls[0].dx, 0, ms);
assert.equal(offset(1700).value, -24);
assert.equal(offset(2400).value, -24);
assert.equal(offset(3400).value, 0);
assert.equal(offset(100000).pending, false, 'native refresh becomes idle after the bounded pass');
const placed = { displayList: list, x: d.x, y: d.y, width: d.source.width, height: d.source.height, depth: 0 };
const start = list.timeline.startedAt;
const bytes = encodeDisplayList(placed, start + 1400);
const replay = readDisplayList(bytes, 0, 10000).placed;
assert.equal(evaluate(replay.displayList.calls[0].dx, 1400, 0).value, offset(1400).value);
});
test('very long labels are tiled into firmware-valid resources rather than giant images', () => {
const { encodeDisplayList } = require('../.test-build/app/graphics/display-list.js');
const f = fixture(); f.labels[0] = 'a'.repeat(1000) + 'Z';
const d = lists(f.paint())[0];
const list = d.presentation.displayList;
assert.ok(list.resources.length > 1);
assert.equal(list.resources.reduce((n, r) => n + r.width, 0), 4004, 'the entire label is retained');
for (const r of list.resources) {
assert.ok(r.width <= 640);
assert.ok(5 + Math.ceil(r.width / 2) * r.height <= 65536);
}
assert.doesNotThrow(() => encodeDisplayList({ displayList: list, x: d.x, y: d.y, width: d.source.width, height: d.source.height, depth: 0 }));
});
test('selected marquee clips to a short menu viewport rather than painting below it', () => {
const f = fixture();
const image = new GrayImage(50, 30);
f.menu.paint(image, { x: 2, y: 1, width: 30, height: 5 }, true);
const d = lists(image)[0];
assert.ok(d);
const clip = d.presentation.displayList.calls[0].clip;
assert.ok(d.y + clip.y + clip.height <= 6);
});
test('setItems changing selection restarts the pass without restarting unchanged settings repaints', () => {
const f = fixture();
const first = lists(f.paint())[0].presentation.displayList.timeline.token;
f.menu.setItems(f.labels);
assert.equal(lists(f.paint())[0].presentation.displayList.timeline.token, first);
f.menu.setItems(f.labels, 1);
f.menu.setItems(f.labels, 0);
assert.notEqual(lists(f.paint())[0].presentation.displayList.timeline.token, first);
});
test('selection away and back restarts the reading pass even without an intermediate paint', () => {
const f = fixture();
const before = lists(f.paint())[0].presentation.displayList.timeline;
f.menu.select(1);
f.menu.select(0);
const after = lists(f.paint())[0].presentation.displayList.timeline;
assert.notEqual(after.token, before.token);
});
test('common row builders scroll only the label and reserve fixed right decorations', () => {
const label = 'abcdefghijklmnopqrstuvwxyzZ';
const items = [
menuModule.submenuItem(label, () => {}),
{ label, onSelect() {}, render: a => menuModule.drawToggleMenuItem(a.image, font, a.x, a.y, a.width, a.text, true, a.selected) },
{ label, onSelect() {}, render: a => menuModule.drawRightValueMenuItem(a.image, font, a.x, a.y, a.width, a.text, '42') },
];
for (const [i, item] of items.entries()) {
const layer = new menuModule.MenuLayer(null, [item], { x: 0, y: 0, width: 140, minHeight: 40, maxHeight: 50 });
const image = layer.paint({ stack: { getBaseSize: () => ({ width: 140, height: 60 }), isFocused: () => true } }, () => new GrayImage(140, 60));
const scrolling = lists(image);
assert.equal(scrolling.length, 1, `builder ${i}`);
assert.ok(scrolling[0].source.width < 140 - 44, 'right decoration has reserved space');
const render = ms => {
const pixels = image.withDrawsBaked(false).pixels.slice();
for (const d of image.draws) {
const list = d.presentation?.displayList;
if (!list) continue;
const origin = list.timeline?.startedAt ?? 0;
paintDisplayList(pixels, pixels.slice(), 140, 60,
{ displayList: { ...list, presentedAt: origin }, x: d.x, y: d.y,
width: d.source.width, height: d.source.height, depth: 0 }, false, origin + ms);
}
return pixels;
};
const before = render(0), during = render(1800);
assert.notDeepEqual(before, during, 'the selected label actually moves');
const decorationX = [110, 82, 108][i];
const decoration = pixels => Array.from({ length: 60 }, (_, y) =>
Array.from(pixels.slice(y * 140 + decorationX, y * 140 + 119))).flat();
assert.deepEqual(decoration(before), decoration(during), 'right decoration never moves or gets overwritten');
assert.ok(decoration(before).some(v => v > 100), 'the fixed decoration is visible');
}
});
test('SettingsPanel labels animate only in the focused column', async () => {
const { SettingsPanelLayer } = load('app/ui/dashboard/settings-panel.ts', {
'../../graphics/ui-fonts': { getDefaultSmallFont: () => font }, '../../graphics/image': { GrayImage },
'../../graphics/textwrap': { wrapText: () => [] }, '../gestures': {}, '../layers': {},
'../menu': menuModule, '../menu-core': require('../.test-build/app/ui/menu-core.js'),
'../metrics': require('../.test-build/app/ui/metrics.js'), '../shell/shell': { shell: {} },
});
const layer = new SettingsPanelLayer([{ label: 'very long section label beyond column width',
items: [{ label: 'very long option label beyond the right column width', onSelect() {} }] }]);
const ctx = { stack: { getBaseSize: () => require('./helpers/viewport.cjs').bordered(360, 100), isFocused: () => true } };
const left = lists(layer.paint(ctx));
assert.equal(left.length, 1);
assert.ok(left[0].x < 150);
await layer.handleInput({ type: 'click' }, ctx);
const right = lists(layer.paint(ctx));
assert.equal(right.length, 1);
assert.ok(right[0].x > 150);
});
test('common MenuLayer default row retains the full overflowing label', () => {
const layer = new menuModule.MenuLayer(null, [{ label: 'abcdefghijklmnopZ', onSelect() {} }],
{ x: 0, y: 0, width: 80, minHeight: 40, maxHeight: 50 });
const image = layer.paint({ stack: { getBaseSize: () => ({ width: 80, height: 60 }), isFocused: () => true } }, () => new GrayImage(80, 60));
assert.equal(lists(image).length, 1);
});