Files
faceclaw/tests/onboarding-layout.test.cjs
James Babcock a02b849200 Borderless modes have different padding inside the builtin apps
Most of the builtin apps had some padding between the window edge and
their content, because they expect to be displayed with a line border on
their edge. We added a customization option to remove the border, so,
that padding is not always necessary. Make the layouts dynamically
adjust, to omit the padding when it isn't needed.
2026-11-11 15:19:29 -08:00

346 lines
20 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { loader } = require('./helpers/load-typescript.cjs');
const { bordered, unbordered } = require('./helpers/viewport.cjs');
// Roboto's line heights at the onboarding sizes, and a fixed advance.
const FONTS = { title: 31, body: 24, detail: 19, hero: 52 };
const font = (role) => ({ lineHeight: FONTS[role], measureText: (text) => text.length * 8, getGlyph: () => ({ dwidthX: 8 }) });
function setting(value, values) {
return { value, values, get() { return this.value; }, set(next) { this.value = next; this.writes.push(next); },
writes: [], displayValue: (v) => v[0].toUpperCase() + v.slice(1) };
}
// The onboarding pages over G2 glasses (640x480), or a RayNeo iO's 540x220 screen.
function harness(glasses = 'g2') {
const screen = glasses === 'rayneo-io' ? { width: 540, height: 220 } : { width: 640, height: 480 };
const settings = {
glassesTypeSetting: setting(glasses, ['g2', 'rayneo-io']),
appSwitcherPositionSetting: setting('bottom', ['left', 'right', 'bottom', 'popup']),
statusBarPositionSetting: setting('bottom', ['top', 'bottom']),
uiDepthSetting: setting('48', ['-62', '-48', '-32', '-16', '0', '16', '32', '48', '62']),
};
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../ui/dashboard-settings': settings,
'../../ui/shell/geometry': { MIN_WINDOW_HEIGHT: 288, SIDEBAR_WIDTH: 64, SWITCHER_ROW_HEIGHT: 36, TOP_BAR_HEIGHT: 28,
screenWidth: () => screen.width, screenHeight: () => screen.height },
});
return { settings, screen, load };
}
/** An image that records what is drawn, checking it all lands on the image. */
function recorder(width, height) {
const calls = [];
const inside = (kind, x, y, w, h) => {
assert.ok(x >= 0 && y >= 0 && x + w <= width && y + h <= height, `${kind} out of bounds: ${[x, y, w, h]}`);
};
return {
width, height, calls,
fillRect(x, y, w, h, value) { inside('fillRect', x, y, w, h); calls.push({ kind: 'fill', x, y, w, h, value }); },
fillRoundedRect(x, y, w, h, value) { inside('fillRoundedRect', x, y, w, h); calls.push({ kind: 'fill', x, y, w, h, value }); },
drawRoundedRect(x, y, w, h, value) { inside('drawRoundedRect', x, y, w, h); calls.push({ kind: 'outline', x, y, w, h, value }); },
drawText(f, x, y, text, value) {
inside(`text "${text}"`, x, y, f.measureText(text), f.lineHeight);
calls.push({ kind: 'text', x, y, text, value });
},
};
}
test('the layout page fits four 40%-wide diagrams with captions on the immersive panel', () => {
const { load } = harness();
const { LayoutPage } = load('app/apps/onboarding/layout-page.ts');
const page = new LayoutPage();
assert.equal(page.immersive, true);
const image = recorder(640, 480);
// An immersive page's edges are the screen's: no padding.
page.paint(image, true, unbordered(640, 480).padding);
// Each diagram's outline spans the display's width at 40%.
const areas = image.calls.filter((call) => call.kind === 'outline' && call.w === 256);
assert.equal(areas.length, 4);
assert.deepEqual(areas.map((area) => area.h), [130, 130, 130, 130]);
// A 2x2 grid, the layouts with depth on top, captioned under each diagram.
const text = (label) => image.calls.filter((call) => call.kind === 'text' && call.text === label);
const [bottom, popup, left, right] = ['Bottom (Recommended)', 'Popup', 'Left', 'Right'].map((label) => text(label)[0]);
assert.equal(bottom.y, popup.y);
assert.equal(left.y, right.y);
assert.ok(left.y > bottom.y);
assert.ok(popup.x > bottom.x && right.x > left.x);
for (const [caption, area] of [[bottom, areas[0]], [popup, areas[1]], [left, areas[2]], [right, areas[3]]]) {
assert.ok(caption.y >= area.y + area.h, `${caption.text} sits under its diagram`);
assert.ok(caption.x > area.x && caption.x + caption.text.length * 8 < area.x + area.w, `${caption.text} sits under its diagram`);
}
// Customizable Depth under Bottom and Popup only.
const depth = text('Customizable Depth');
assert.equal(depth.length, 2);
for (const [subtitle, title] of [[depth[0], bottom], [depth[1], popup]]) {
assert.equal(subtitle.y, title.y + FONTS.body);
assert.ok(Math.abs(subtitle.x + 'Customizable Depth'.length * 4 - (title.x + title.text.length * 4)) <= 1, 'centred together');
}
// Bottom, recommended, is the selected cell at first, filled while focused.
const box = image.calls.find((call) => call.kind === 'fill' && call.value === 15);
assert.ok(box.x <= areas[0].x && box.y <= areas[0].y && box.y + box.h >= bottom.y);
// With nothing bordering the page, its title sits 10px in (where list rows' text starts) and 2px down.
const title = text('Layout')[0];
assert.deepEqual([title.x, title.y], [10, 2]);
// Beside border lines the heading keeps their padding, and the grid stays centred.
const framed = recorder(640, 480);
page.paint(framed, true, bordered(640, 480).padding);
const framedTitle = framed.calls.find((call) => call.kind === 'text' && call.text === 'Layout');
assert.deepEqual([framedTitle.x, framedTitle.y], [18, 10]);
const framedAreas = framed.calls.filter((call) => call.kind === 'outline' && call.w === 256);
assert.deepEqual(framedAreas.map((area) => area.x), areas.map((area) => area.x));
});
test('the layout page starts on Bottom whatever is set, swipes in reading order, and a tap saves the choice', () => {
const { load, settings } = harness();
const { LayoutPage } = load('app/apps/onboarding/layout-page.ts');
const page = new LayoutPage();
settings.appSwitcherPositionSetting.value = 'right';
page.enter?.();
let next = 0;
const nav = { next: () => next++ };
page.handleInput({ type: 'click' }, nav);
assert.deepEqual(settings.appSwitcherPositionSetting.writes, ['bottom']);
page.handleInput({ type: 'scroll-up' }, nav);
for (let i = 0; i < 2; i++) page.handleInput({ type: 'scroll-down' }, nav);
assert.deepEqual(settings.appSwitcherPositionSetting.writes, ['bottom'], 'scrolling saves nothing');
page.handleInput({ type: 'click' }, nav);
for (let i = 0; i < 5; i++) page.handleInput({ type: 'scroll-down' }, nav);
// Coming back keeps the last choice.
page.enter?.();
page.handleInput({ type: 'click' }, nav);
assert.deepEqual(settings.appSwitcherPositionSetting.writes, ['bottom', 'left', 'right']);
assert.equal(next, 3);
});
test('the text size page offers 12 to 18px, starts on the recommended 16px, and previews plain text', () => {
const menu = { selected: null, items: [], setItems(items, index) { this.items = items; this.selected = index; },
get selectedItem() { return this.items[this.selected] ?? null; }, paint(image, box) { this.box = box; },
async handleInput(event) { this.selected = Math.max(0, Math.min(this.items.length - 1, this.selected + (event.type === 'scroll-down' ? 1 : -1))); } };
let menuOptions;
let selection = { kind: 'ttf', file: 'Roboto-Light.ttf', size: 14 };
const saved = [];
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../graphics/installed-fonts': { installedFontPath: (file) => `/fonts/${file}` },
'../../graphics/ttf-font': { TtfFont: { load: (path, size) => font(size === 12 ? 'title' : 'detail') } },
'../../graphics/ui-fonts': { defaultUiFontSize: () => 16, fontSelectionLabel: () => 'Terminus', getDefaultSmallFont: () => font('detail'),
getUiFontSelection: () => selection, setUiFontSelection: (value) => { selection = value; saved.push(value.size); },
uiFontSizeAllowed: (path, size) => size !== 13 },
'../../ui/font-picker': { FONT_SIZE_CHOICES: [8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 24] },
'../../ui/menu-core': { Menu: class { constructor(options) { menuOptions = options; return menu; } } },
'../../ui/metrics': { LIST_ROW_TEXT_INSET: 4, listRowHeight: (f) => f.lineHeight + 8 },
});
const { TextSizePage } = load('app/apps/onboarding/text-size-page.ts');
const page = new TextSizePage();
page.enter();
assert.deepEqual(Array.from(menu.items), [12, 14, 15, 16, 17, 18]);
assert.equal(menu.selectedItem, 16, 'recommended, not the current 14');
// Only 16 carries the note, beside its size.
const rows = [];
for (const size of menu.items) {
const row = recorder(menu.box?.width ?? 200, 32);
menuOptions.draw({ image: row, item: size, x: 0, y: 0, width: row.width, height: 32, selected: size === 16 });
rows.push(row.calls.map((call) => call.text));
}
assert.deepEqual(rows[3], ['16 px', 'Recommended']);
assert.deepEqual(rows.filter((texts) => texts.length > 1).length, 1);
// The size column holds the note; the preview is paragraphs of text, no menu.
const image = recorder(640, 480);
page.paint(image, true, unbordered(640, 480).padding);
assert.ok(menu.box.width >= 10 + '16 px'.length * 8 + 8 + 'Recommended'.length * 8);
const preview = image.calls.filter((call) => call.kind === 'text' && call.x > menu.box.x + menu.box.width);
assert.equal(preview[0].text.startsWith('This is how text will look'), true);
assert.ok(preview.length > 3);
assert.equal(image.calls.some((call) => call.kind === 'text' && call.text === 'Timers'), false);
// Unpadded, the size list starts at the window's left edge and the preview's frame reaches its right and bottom edges.
const frame = image.calls.find((call) => call.kind === 'outline');
assert.equal(menu.box.x, 0);
assert.deepEqual([frame.x + frame.w, frame.y + frame.h], [640, 480]);
// The list scrolls, so it runs to the bottom edge whatever borders it, the edge's padding inside it.
assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 0]);
const framed = recorder(640, 480);
page.paint(framed, true, bordered(640, 480).padding);
const paddedFrame = framed.calls.find((call) => call.kind === 'outline');
assert.equal(menu.box.x, 8);
assert.deepEqual([paddedFrame.x + paddedFrame.w, paddedFrame.y + paddedFrame.h], [632, 472]);
assert.deepEqual([menu.box.y + menu.box.height, menu.box.padBottom], [480, 8]);
// A tap saves; coming back keeps the size chosen.
page.handleInput({ type: 'scroll-up' }, { next: () => {} });
page.handleInput({ type: 'click' }, { next: () => {} });
assert.deepEqual(saved, [15]);
page.enter();
assert.equal(menu.selectedItem, 15);
});
test('on a RayNeo iO the text size page recommends and starts on 14px, its default', () => {
const menu = { selected: null, items: [], setItems(items, index) { this.items = items; this.selected = index; },
get selectedItem() { return this.items[this.selected] ?? null; }, paint(image, box) { this.box = box; } };
let menuOptions;
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../graphics/installed-fonts': { installedFontPath: (file) => `/fonts/${file}` },
'../../graphics/ttf-font': { TtfFont: { load: () => font('detail') } },
'../../graphics/ui-fonts': { defaultUiFontSize: () => 14, fontSelectionLabel: () => 'Terminus', getDefaultSmallFont: () => font('detail'),
getUiFontSelection: () => ({ kind: 'ttf', file: 'Roboto-Light.ttf', size: 14 }), setUiFontSelection: () => assert.fail('nothing to save'),
uiFontSizeAllowed: () => true },
'../../ui/font-picker': { FONT_SIZE_CHOICES: [8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 24] },
'../../ui/menu-core': { Menu: class { constructor(options) { menuOptions = options; return menu; } } },
'../../ui/metrics': { LIST_ROW_TEXT_INSET: 4, listRowHeight: (f) => f.lineHeight + 8 },
});
const { TextSizePage } = load('app/apps/onboarding/text-size-page.ts');
const page = new TextSizePage();
page.enter();
assert.equal(menu.selectedItem, 14);
const notes = menu.items.filter((size) => {
const row = recorder(200, 32);
menuOptions.draw({ image: row, item: size, x: 0, y: 0, width: 200, height: 32, selected: false });
return row.calls.some((call) => call.text === 'Recommended');
});
assert.deepEqual(notes, [14]);
// Taking the default leaves the size unset, so it keeps following the glasses.
page.handleInput({ type: 'click' }, { next: () => {} });
});
test('on a RayNeo iO the layout page recommends and starts on Left, and fits the 540x220 screen', () => {
const { load, settings } = harness('rayneo-io');
const { LayoutPage, recommendedLayout } = load('app/apps/onboarding/layout-page.ts');
assert.equal(recommendedLayout(), 'left');
const page = new LayoutPage();
const image = recorder(540, 220);
page.paint(image, true, unbordered(540, 220).padding);
const captions = image.calls.filter((call) => call.kind === 'text').map((call) => call.text);
assert.ok(captions.includes('Left (Recommended)'));
assert.ok(captions.includes('Bottom'));
page.handleInput({ type: 'click' }, { next() {} });
assert.deepEqual(settings.appSwitcherPositionSetting.writes, ['left']);
// The G2 still recommends Bottom.
assert.equal(harness().load('app/apps/onboarding/layout-page.ts').recommendedLayout(), 'bottom');
});
test('the depth page follows layouts that have depth, sits in the layout, and applies depth as it scrolls', () => {
const { load, settings } = harness();
const menu = { selected: 0, items: [], select(index) { this.selected = index; },
get selectedItem() { return this.items[this.selected] ?? null; }, paint() {},
async handleInput(event) { this.selected = Math.max(0, Math.min(this.items.length - 1, this.selected + (event.type === 'scroll-down' ? 1 : -1))); } };
const mocks = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../ui/dashboard-settings': settings,
'../../ui/menu-core': { Menu: class { constructor(options) { menu.items = options.items; return menu; } } },
'../../ui/metrics': { LIST_ROW_TEXT_INSET: 4, listRowHeight: (f) => f.lineHeight + 8 },
'./layout-page': { layoutHasDepth: (position) => position === 'bottom' || position === 'popup' },
});
const { DepthPage } = mocks('app/apps/onboarding/depth-page.ts');
const page = new DepthPage();
assert.equal(page.immersive, undefined);
for (const [position, applies] of [['left', false], ['right', false], ['bottom', true], ['popup', true]]) {
settings.appSwitcherPositionSetting.value = position;
assert.equal(page.applies(), applies, position);
}
page.enter();
assert.equal(menu.selectedItem, '48');
let next = 0;
page.handleInput({ type: 'scroll-down' }, { next: () => next++ });
page.handleInput({ type: 'scroll-up' }, { next: () => next++ });
page.handleInput({ type: 'scroll-up' }, { next: () => next++ });
assert.deepEqual(settings.uiDepthSetting.writes, ['62', '48', '32']);
page.handleInput({ type: 'click' }, { next: () => next++ });
assert.equal(next, 1);
});
test('the flow switches immersive mode per page and skips pages that do not apply, both ways', async () => {
const shell = { yields: 0, yieldFocusToSidebar() { this.yields++; } };
const load = loader({}, {
'../../graphics/image': { GrayImage: class {} },
'../../ui/shell/in-process-window': {}, '../../ui/shell/shell': { shell }, '../../ui/sound-effects': {},
'./data-collection-page': {}, './depth-page': {}, './display-mode-page': {}, './layout-page': {},
'./onboarding-progress': {}, './text-size-page': {}, './welcome-page': {},
});
const { OnboardingLayer } = load('app/apps/onboarding/onboarding-app.ts');
let depthApplies = false;
const entered = [];
const page = (name, extra) => ({ ...extra, enter: () => entered.push(name),
handleInput: (event, nav) => { if (event.type === 'click') nav.next(); } });
const immersive = [];
let finished = 0;
const layer = new OnboardingLayer([
page('welcome', { immersive: true }), page('layout', { immersive: true }),
page('depth', { applies: () => depthApplies }), page('voice', {}),
], (value) => immersive.push(value), () => finished++);
const input = (type) => layer.handleInput({ type });
await input('click');
await input('click');
assert.deepEqual(entered, ['welcome', 'layout', 'voice'], 'depth skipped going forward');
assert.deepEqual(immersive, [true, false]);
await input('double-click');
assert.deepEqual(entered.at(-1), 'layout', 'and going back');
depthApplies = true;
await input('click');
await input('click');
assert.deepEqual(entered.slice(-2), ['depth', 'voice']);
assert.deepEqual(immersive, [true, false, true, false, false]);
await input('click');
assert.equal(finished, 1);
for (let i = 0; i < 4; i++) await input('double-click');
assert.equal(entered.at(-1), 'welcome');
assert.equal(shell.yields, 1, 'double-click on the first page leaves the app');
});
test('the display mode page offers Full Screen only where the layout allows it, and applies the mode as it scrolls', () => {
const values = ['band-top', 'band-high', 'band-middle', 'band-low', 'band-bottom', 'tall', 'fullscreen'];
const settings = {
appSwitcherPositionSetting: setting('bottom', ['left', 'right', 'bottom', 'popup']),
displayLayoutSetting: { ...setting('fullscreen', values), displayValue: (v) => v,
isDisabled: (v) => v === 'fullscreen' && ['left', 'right'].includes(settings.appSwitcherPositionSetting.value) },
};
const menu = { selected: null, items: [], setItems(items, index) { this.items = items; this.selected = index; },
get selectedItem() { return this.items[this.selected] ?? null; }, paint(image, box) { this.box = box; },
async handleInput(event) { this.selected = Math.max(0, Math.min(this.items.length - 1, this.selected + (event.type === 'scroll-down' ? 1 : -1))); } };
const load = loader({}, {
'./onboarding-fonts': { onboardingFont: font },
'../../ui/dashboard-settings': settings,
'../../ui/menu-core': { Menu: class { constructor() { return menu; } } },
'../../ui/metrics': { LIST_ROW_TEXT_INSET: 4, listRowHeight: (f) => f.lineHeight + 8 },
});
const { DisplayModePage } = load('app/apps/onboarding/display-mode-page.ts');
const page = new DisplayModePage();
// An ordinary window, which the mode being applied resizes.
assert.equal(page.immersive, undefined);
page.enter();
assert.deepEqual(Array.from(menu.items), values);
assert.equal(menu.selectedItem, 'fullscreen');
// The selected mode's description sits beside the list, within the window (a band's).
const image = recorder(576, 260);
page.paint(image, true, bordered(576, 260).padding);
const description = image.calls.filter((call) => call.kind === 'text' && call.x >= menu.box.x + menu.box.width);
assert.match(description.map((call) => call.text).join(' '), /Depth is turned off/);
// Beside border lines: the title at the UI-wide (18, 10), the list's rows from the padding, the list to the bottom edge.
const title = (calls) => calls.find((call) => call.kind === 'text' && call.text === 'Display mode');
assert.deepEqual([title(image.calls).x, title(image.calls).y], [18, 10]);
assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [8, 260, 8]);
assert.ok(description.every((call) => call.x + call.text.length * 8 <= 576 - 18 && call.y + FONTS.detail <= 260 - 8));
// With no border on the left or bottom, the page runs to those edges.
const open = recorder(576, 260);
page.paint(open, true, { top: 8, right: 8, bottom: 0, left: 0 });
assert.deepEqual([title(open.calls).x, title(open.calls).y], [10, 10]);
assert.deepEqual([menu.box.x, menu.box.y + menu.box.height, menu.box.padBottom], [0, 260, 0]);
let next = 0;
const nav = { next: () => next++ };
page.handleInput({ type: 'scroll-up' }, nav);
page.handleInput({ type: 'scroll-up' }, nav);
assert.deepEqual(settings.displayLayoutSetting.writes, ['tall', 'band-bottom'], 'applied as it scrolls');
page.handleInput({ type: 'click' }, nav);
assert.equal(next, 1);
assert.deepEqual(settings.displayLayoutSetting.writes, ['tall', 'band-bottom']);
// Beside a side strip, a saved Full Screen shows as Tall, and is saved as such on a tap.
settings.displayLayoutSetting.value = 'fullscreen';
settings.appSwitcherPositionSetting.value = 'left';
page.enter();
assert.deepEqual(Array.from(menu.items), values.slice(0, 6));
assert.equal(menu.selectedItem, 'tall');
page.handleInput({ type: 'click' }, nav);
assert.equal(settings.displayLayoutSetting.value, 'tall');
});