Files
faceclaw/tests/menu-display-list.test.cjs

144 lines
11 KiB
JavaScript

const test=require('node:test');
const assert=require('node:assert/strict');
const fs=require('node:fs');
const vm=require('node:vm');
const ts=require('typescript');
const graphics=require('../.test-build/app/graphics/image.js');
const planes=require('../.test-build/app/graphics/plane.js');
const wire=require('../.test-build/app/graphics/presentation-wire.js');
const {encodeShellScene}=require('../.test-build/app/graphics/shell-scene.js');
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 font={lineHeight:12,ascent:10,descent:2,measureText:text=>text.length*4,
drawText:(image,x,y,text,value)=>{for(let i=0;i<text.length;i++) image.fillRect(x+i*4,y+2,2,6,value);}};
const menuCore=load('app/ui/menu-core.ts',{
'../graphics/image':graphics,'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'),
});
const {MenuLayer,TextPageLayer}=load('app/ui/menu.ts',{
'./menu-animation-pref': require('../.test-build/app/ui/menu-animation-pref.js'), './animation-speed': require('../.test-build/app/ui/animation-speed.js'), '../native/settings-store': { getStringSetting: (_key, fallback) => fallback },
'../graphics/image':graphics,'../graphics/textwrap':{wrapText:(_font,text)=>text.split('\n')},'../graphics/ui-fonts':{getDefaultSmallFont:()=>font},
'../util/numeric-util':{clamp:(n,a,b)=>Math.max(a,Math.min(b,n))},'./gestures':{},
'./menu-highlight-motion': require('../.test-build/app/ui/menu-highlight-motion.js'), '../graphics/menu-scroll-list': require('../.test-build/app/graphics/menu-scroll-list.js'), './menu-scroll-motion': require('../.test-build/app/ui/menu-scroll-motion.js'), '../graphics/draw-expression': require('../.test-build/app/graphics/draw-expression.js'),
'./metrics':require('../.test-build/app/ui/metrics.js'),'./menu-core':menuCore,
});
const {prepareFrameDraws}=load('app/graphics/glyph-wire.ts',{'./presentation-wire':wire,'../native/texture-atlas':{textureAtlasAvailable:()=>false}});
function menu() {
const layer=new MenuLayer(null,[{label:'FIRST',onSelect(){}},{label:'SECOND',onSelect(){}}],{x:0,y:0,width:80,minHeight:70,maxHeight:80,squareCorners:true});
const context={stack:{getBaseSize:()=>({width:80,height:80}),isFocused:()=>true}};
return {layer,paint:()=>layer.paint(context,()=>new graphics.GrayImage(80,80)),context};
}
test('menu retains the selected text and rounded highlight on the menu plane',async()=>{
const m=menu(),image=m.paint(),row=image.draws.find(d=>d.presentation);
assert.equal(row.presentation.depth,0);assert.equal(row.presentation.radius,8);
assert.ok(row.source.pixels.some(v=>v===255));assert.ok(!image.withDrawsBaked(false).pixels.some(v=>v===255));
const flat=planes.flattenPlanesWithDraws([{image,x:0,y:0}]);
const records=wire.presentationRecords(prepareFrameDraws(flat.draws));assert.equal(records.length,1);
await m.layer.handleInput({type:'scroll-down'},m.context);
const next=m.paint().draws.find(d=>d.presentation);assert.ok(next.y>row.y);assert.equal(next.presentation.depth,0);
});
test('shell menu selection survives serialization',()=>{
const image=menu().paint(),bytes=encodeShellScene([{image,x:0,y:0,shellKey:7}]);
assert.equal(new DataView(bytes.buffer).getUint16(14,true),1);
});
test('later opaque planes add an occlusion to a replayed selection',()=>{
const image=menu().paint(),row=image.draws.find(d=>d.presentation),cover=new graphics.GrayImage(10,8,1);
const flat=planes.flattenPlanesWithDraws([{image,x:0,y:0},{image:cover,x:row.x+10,y:row.y+2}]);
const records=wire.presentationRecords(prepareFrameDraws(flat.draws));assert.ok(records[0].displayList.calls.some(c => c.op === 2 && c.resource === 65535));
});
const {LayerStack,noopLayerActions}=load('app/ui/layers.ts',{
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},'./gestures':{},
});
test('context menu surface and selected row both use +4 without changing the app screen',()=>{
const base=new graphics.GrayImage(100,90,96);
const stack=new LayerStack({paint:()=>base},noopLayerActions,{width:100,height:90});
stack.push(new MenuLayer(null,[{label:'FIRST',onSelect(){}}],{x:10,y:8,width:80,minHeight:70,maxHeight:70,depth:4}));
const frame=planes.flattenPlanesWithDraws(stack.paint());
assert.deepEqual(frame.image.pixels,base.pixels);
const records=wire.presentationRecords(prepareFrameDraws(frame.draws));
assert.deepEqual(records.map(r=>r.depth),[4,4]);assert.equal(records[0].mode,'masked-image');
stack.pop();const closed=planes.flattenPlanesWithDraws(stack.paint());
assert.deepEqual(closed.image.pixels,base.pixels);
assert.deepEqual(wire.presentationRecords(prepareFrameDraws(closed.draws)),[]);
});
test('a submenu that replaces its parent menu skips the parent plane and dims the content once',()=>{
const base=new graphics.GrayImage(100,90,200);
const stack=new LayerStack({paint:()=>base},noopLayerActions,{width:100,height:90});
const layout={x:10,y:8,width:80,minHeight:70,maxHeight:70,dimUnderneath:0.5};
class SubmenuLayer extends MenuLayer { replacesLayerBelow(below) { return below instanceof MenuLayer; } }
const parent=new MenuLayer('PARENT',[{label:'SUB',onSelect(){}}],layout);
stack.push(parent);
stack.push(new SubmenuLayer('CHILD',[{label:'ITEM',onSelect(){}}],layout));
stack.push(new SubmenuLayer('GRANDCHILD',[{label:'ITEM',onSelect(){}}],layout));
const painted=stack.paint();
assert.equal(painted.length,2);
assert.equal(painted[0].image.pixels[0],100);
assert.equal(stack.baseDim(),0.5);
stack.pop();stack.pop();
assert.equal(stack.paint().length,2);
});
test('text page paints at the stack size, not the lens size',()=>{
// An app window's stack is its viewport; a lens-sized plane is rejected by
// the compositor as larger than the window surface.
const stack=new LayerStack({paint:()=>new graphics.GrayImage(576,260)},noopLayerActions,{width:576,height:260});
stack.push(new TextPageLayer('Array status','Left: no status yet\nRight: no status yet'));
const painted=stack.paint();
assert.equal(painted.length,1);
assert.equal(painted[0].image.width,576);assert.equal(painted[0].image.height,260);
});
test('system menu bridge shifts its surface and selected row together by +4',()=>{
const image=menu().paint(),bytes=encodeShellScene([{image,x:8,y:0,shellKey:7,depth:4}]);
const view=new DataView(bytes.buffer),w=view.getUint16(8,true),h=view.getUint16(10,true);
assert.equal(view.getInt16(16,true),4);
assert.equal(wire.readPresentation(bytes,18+w*h).selection.depth,4);
});
test('unselected sidebar icons retain -2 depth through shell cropping, including attention badges',()=>{
const shellScene=require('../.test-build/app/graphics/shell-scene.js');
const {ShellChromeLayer}=load('app/ui/shell/chrome-layer.ts',{
'../../graphics/shell-scene':shellScene,'../../graphics/image':graphics,'../../graphics/display-list':require('../.test-build/app/graphics/display-list.js'),
'../../graphics/ui-fonts':{getDefaultSmallFont:()=>font,getDefaultMediumFont:()=>font},
'../../graphics/textwrap':{},'./ambient-cards':{},'../../graphics/battery':{},
'../../native/notification-icons':{},'../../native/notification-types':require('../.test-build/app/native/notification-types.js'),'../../native/phone-battery':{},'../../util/render-freshness':{},
'../../graphics/icons':{},'../dashboard-settings':{},'../clock-format':{},'../../graphics/bdffont':{},
'../layers':{LayerStack},'../menu':{scrollToKeepSelectionVisible:()=>0},'../metrics':{},
'./geometry':{MIN_WINDOW_HEIGHT:280,minWindowTop:()=>0,windowTop:()=>0,SHELL_OPAQUE_BLACK:1,SIDEBAR_WIDTH:64,TOP_BAR_HEIGHT:24,
SWITCHER_ROW_HEIGHT:36,switcherPosition:()=>'left',switcherRect:()=>({x:0,y:0,width:64,height:280})},
});
const state={selectedIndex:0,focus:'sidebar',windows:[0,1,2].map(n=>({attention:n===1,drawIcon:(image,x,y)=>image.fillRect(x,y,4,4,240)}))};
const chrome=new ShellChromeLayer(()=>state),image=new graphics.GrayImage(640,480);
chrome.drawSidebar(image,state);
assert.deepEqual(Array.from(image.draws.filter(d=>d.presentation),d=>d.presentation.depth),[-2,-2]);
const crop=shellScene.shellCrop(image,0,0,64,280,1),bytes=encodeShellScene([crop]);
const view=new DataView(bytes.buffer),w=view.getUint16(8,true),h=view.getUint16(10,true);
assert.equal(view.getUint16(14,true),2);
const first=wire.readPresentation(bytes,18+w*h),second=wire.readPresentation(bytes,first.end);
assert.deepEqual([first.selection.depth,second.selection.depth],[-2,-2]);
state.selectedIndex=1;const next=new graphics.GrayImage(640,480);chrome.drawSidebar(next,state);
assert.equal(next.draws.filter(d=>d.presentation).length,2);
});
test('shell modal carries its inner menu highlight animation instead of baking its first frame',async()=>{
const {ShellModalLayer}=load('app/ui/shell/modal-layer.ts',{
'../../graphics/image':graphics,'../../graphics/plane':planes,'../gestures':{},'../layers':load('app/ui/layers.ts',{
'../graphics/image':graphics,'../graphics/plane':planes,'../native/frame-timings':{spanCurrent:(_name,fn)=>fn()},
'./gestures':{isDirectionalInput:()=>false},
}),
'./geometry':{appViewportSize:()=>({width:120,height:110}),appViewportRect:()=>({x:10,y:20,width:120,height:110}),SHELL_OPAQUE_BLACK:1},
});
const modal=new ShellModalLayer(menu().layer,noopLayerActions),paint=()=>modal.paint({},()=>new graphics.GrayImage(200,200));
const bare=menu().paint().draws.find(d=>d.presentation),before=paint().draws.find(d=>d.presentation);
assert.equal(before.x,bare.x+28);assert.equal(before.y,bare.y+38); // translated into the box interior
await modal.handleInput({type:'scroll-down'},{});
const image=paint(),row=image.draws.find(d=>d.presentation);
assert.ok(row.y>before.y);assert.ok(row.presentation.displayList.timeline,'the slide stays a timed display list');
assert.ok(!image.withDrawsBaked(false).pixels.some(v=>v===255),'selected text is not baked into the modal surface');
});
test('shell crop widens a fractional rect (TTF-measured status bar) to whole pixels',()=>{
const {shellCrop}=require('../.test-build/app/graphics/shell-scene.js'),image=new graphics.GrayImage(576,288,7);
const crop=shellCrop(image,400.5,250,175.5,15,2);
assert.deepEqual([crop.x,crop.y,crop.image.width,crop.image.height],[400,250,176,15]);
assert.ok(crop.image.pixels.every(v=>v===7));
});