Files
launcher/tests/android-sim.cjs
T
nandi 3070db4fb4
Launcher / test (push) Successful in 1m21s
Launcher / apk (push) Successful in 17s
Fix wallpaper transparency, Recents setup guide, drawer animation, and widget resizing (#2)
2026-11-23 05:36:23 +00:00

283 lines
17 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Drives the page against a stub of the Java shell's window.Android bridge, to check the seam
// between the MoonBit page and the shell (what the page asks of Android, and how it reacts when
// Android calls window.launcher.*). The Java side itself needs a device; this is the page half.
// scripts/build-web.sh --demo && node tests/android-sim.cjs
const assert = require("assert");
const fs = require("fs");
const http = require("http");
const path = require("path");
const { chromium } = require(process.env.PLAYWRIGHT || "playwright");
const RELEASE = JSON.stringify({ tag_name: "build-9", assets: [
{ name: "version.json", browser_download_url: "https://x/version.json" },
{ name: "ultra-launcher.apk", browser_download_url: "https://x/a.apk" }] });
(async () => {
const root = path.join(__dirname, "..", "dist", "web");
const types = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".json": "application/json" };
const srv = http.createServer((req, res) => {
const u = req.url.split("?")[0] === "/" ? "/index.html" : req.url.split("?")[0];
fs.readFile(path.join(root, u), (err, data) => {
if (err) { res.writeHead(404); res.end(); return; }
res.writeHead(200, { "content-type": types[path.extname(u)] || "application/octet-stream" }); res.end(data);
});
}).listen(0);
const BASE = `http://localhost:${srv.address().port}`;
const b = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH || undefined });
const p = await b.newPage({ viewport: { width: 390, height: 780 } });
p.on("pageerror", (e) => { console.error("page error:", e.message); process.exitCode = 1; });
await p.addInitScript((release) => {
window.__calls = [];
window.__recentsEnabled = true;
window.__widgetLimits = { w: 110, minW: 64, minH: 64, maxW: 0, maxH: 0, h: true, v: true };
const rec = (n) => (...a) => { window.__calls.push([n, ...a]); };
const disc = "data:image/svg+xml," + encodeURIComponent("<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'><circle cx='4' cy='4' r='4' fill='red'/></svg>");
window.Android = {
launch: rec("launch"), appInfo: rec("appInfo"), uninstall: rec("uninstall"),
addWidget: (prov) => { rec("addWidget")(prov); setTimeout(() => window.launcher.widgetAdded(77), 0); },
removeWidget: rec("removeWidget"), placeWidgets: rec("placeWidgets"), coverWidgets: rec("coverWidgets"),
excludeGestures: rec("excludeGestures"),
widgetLimits: () => JSON.stringify(window.__widgetLimits),
loadIcons: (json) => { rec("loadIcons")(json); const o = {}; for (const k of JSON.parse(json)) o[k] = disc; setTimeout(() => window.launcher.icons(o), 0); },
haptic: rec("haptic"), showKeyboard: rec("showKeyboard"), hideKeyboard: rec("hideKeyboard"),
openSettings: rec("openSettings"), pickWallpaper: rec("pickWallpaper"),
recentsEnabled: () => !!window.__recentsEnabled,
openRecents: () => { rec("openRecents")(); return !!window.__recentsEnabled; },
openAccessibilitySettings: rec("openAccessibilitySettings"),
openSelfAppInfo: rec("openSelfAppInfo"),
httpGet: (url, tag) => { rec("httpGet")(url, tag);
setTimeout(() => window.launcher.httpResult(tag, true, url.endsWith("/latest") ? release : JSON.stringify({ versionCode: 9, commit: "abc" })), 0); },
installApk: rec("installApk"), installedVersion: () => 5, declinedVersion: () => -1,
};
}, RELEASE);
const calls = (n) => p.evaluate((n) => window.__calls.filter((c) => c[0] === n), n);
const settle = () => p.waitForTimeout(250);
const longpress = async (sel, fy = 0.5) => {
const r = await p.locator(sel).first().boundingBox();
await p.mouse.move(r.x + r.width / 2, r.y + r.height * fy);
await p.mouse.down(); await p.waitForTimeout(650); await p.mouse.up(); await settle();
};
await p.goto(BASE + "/");
await p.evaluate(() => localStorage.clear());
await p.reload();
await p.waitForSelector(".hint");
assert.ok(!(await p.evaluate(() => document.documentElement.classList.contains("demo"))), "no demo backdrop on a phone");
const bg = await p.evaluate(() => ({
htmlColor: getComputedStyle(document.documentElement).backgroundColor,
htmlImage: getComputedStyle(document.documentElement).backgroundImage,
bodyColor: getComputedStyle(document.body).backgroundColor,
}));
assert.equal(bg.htmlColor, "rgba(0, 0, 0, 0)", "html background is transparent on Android");
assert.equal(bg.bodyColor, "rgba(0, 0, 0, 0)", "body background is transparent on Android");
assert.equal(bg.htmlImage, "none", "no demo gradient on Android");
// on start the page checks for an update: release API, then version.json, then installs build 9 over 5
await p.waitForFunction(() => window.__calls.some((c) => c[0] === "installApk"));
assert.deepEqual((await calls("httpGet")).map((c) => c[1]), [
"https://git.site.latha.org/api/v1/repos/experiments/launcher/releases/latest", "https://x/version.json"]);
assert.deepEqual((await calls("installApk"))[0].slice(1), ["https://x/a.apk", 9]);
assert.equal(await p.textContent(".notice"), "Downloading launcher update (build 9)…");
// app icons are pre-warmed right after apps.json loads, before opening the drawer
await p.waitForFunction(() => window.__calls.some((c) => c[0] === "loadIcons"));
assert.ok((await calls("loadIcons")).length > 0, "icons are pre-warmed before opening the drawer");
// the drawer asks the shell for icons (batched, by key) and fills the images from its answer
await p.evaluate(() => document.body.dispatchEvent(Object.assign(new Event("x"), {})));
await longpress(".hint");
await p.evaluate(() => { document.querySelector(".sheet-overlay").__tag = "overlay"; });
await p.click(".sheet-item >> text=Add apps"); await settle();
assert.equal(await p.$eval(".drawer", (d) => d.__tag), undefined, "drawer does not recycle sheet-overlay");
assert.equal(await p.$eval(".drawer", (d) => getComputedStyle(d).backdropFilter), "none", "no full-screen backdrop blur");
assert.ok((await calls("loadIcons")).length > 0, "icons are asked for");
assert.ok(!(await calls("showKeyboard")).length, "no keyboard in pick mode");
await p.click(".apps .tile:nth-child(4)"); await p.click(".drawer-head >> text=Done"); await settle();
assert.ok((await p.getAttribute(".slots .icon", "src")).startsWith("data:image/svg+xml"), "the icon the shell drew");
await p.click(".slots .tile"); await settle();
assert.deepEqual((await calls("launch"))[0].slice(1), ["com.example.chrome/.MainActivity"]);
// Windows asks the shell for Recents when enabled, and opens setup sheet when disabled
await p.click(".panel-btn"); await settle();
assert.equal((await calls("openRecents")).length, 1);
assert.ok(!(await p.$(".sheet")), "no setup sheet when RecentsService is running");
await p.evaluate(() => { window.__recentsEnabled = false; });
await p.click(".panel-btn");
await p.waitForSelector(".sheet");
assert.equal((await calls("openRecents")).length, 2);
assert.equal(await p.textContent(".sheet-title"), "Enable window list");
const setupText = await p.textContent(".sheet");
assert.ok(setupText.includes("Restricted setting"), "explains Android 13+ Restricted setting");
assert.ok(setupText.includes("Allow restricted settings"), "explains Allow restricted settings step");
await p.click(".sheet button:has-text('Open Accessibility settings')"); await settle();
assert.equal((await calls("openAccessibilitySettings")).length, 1);
assert.ok(await p.$(".sheet"), "setup sheet stays open while user checks Settings");
await p.click(".sheet button:has-text('Open App info')"); await settle();
assert.equal((await calls("openSelfAppInfo")).length, 1);
assert.ok(await p.$(".sheet"), "setup sheet stays open while user checks App info");
await p.click(".sheet button:has-text('Close')"); await settle();
assert.ok(!(await p.$(".sheet")), "Close dismisses setup sheet");
await p.evaluate(() => { window.__recentsEnabled = true; });
// add a widget: the shell binds it and the page places it over its slot (wid, x, y, w, h, editing)
await longpress(".home", 0.95);
await p.click(".sheet-item >> text=Add widget");
await p.waitForSelector(".widget-list li");
await p.click(".widget-list li:first-child"); await settle();
assert.equal((await calls("addWidget")).length, 1);
await p.waitForSelector(".widget-slot[data-wid='77']");
await settle();
const placed = (await calls("placeWidgets")).pop()[1];
const [[wid, x, y, w, h, editing]] = JSON.parse(placed);
assert.equal(wid, 77); assert.ok(w > 100 && h >= 64 && x >= 0 && y > 100, placed); assert.equal(editing, 0);
const box = await p.locator(".widget-slot").boundingBox();
assert.ok(Math.abs(box.x - x) < 2 && Math.abs(box.width - w) < 2, "placed where the slot is");
assert.ok(w < 358, "starts at its natural width, not the whole row"); // 110px natural = 2 columns
// opening the drawer: the shell is told to cover the widgets, the keyboard comes up, widgets are unmarked
await p.click(".search-pill");
await p.waitForSelector(".drawer");
await settle();
assert.equal((await calls("coverWidgets")).pop()[1], -1, "the shell hides widgets under the drawer");
assert.ok((await calls("showKeyboard")).length > 0, "the search takes the keyboard");
assert.equal(await p.$$eval("[data-wid]", (ws) => ws.length), 0);
await p.click(".drawer-head >> text=Close");
await p.waitForSelector(".drawer.closing", { timeout: 1000 });
assert.ok((await calls("hideKeyboard")).length > 0);
assert.equal((await calls("coverWidgets")).pop()[1], 0, "the widgets are shown again as drawer closes");
await p.waitForSelector(".drawer", { state: "detached", timeout: 2000 });
// a long press on the native widget view reaches the page; letting go without moving shows handles,
// and the shell is told to keep its edge swipes off them
await p.evaluate(() => { window.launcher.widgetMenu(77); window.launcher.widgetDrop(false); }); await settle();
assert.ok(await p.$(".widget-slot.resizing .resize-handle"), "handles");
assert.equal(await p.textContent(".resize-size"), "2 × 2");
assert.equal(JSON.parse((await calls("excludeGestures")).pop()[1]).length, 5, "five handles excluded from system gestures");
assert.equal(JSON.parse((await calls("placeWidgets")).pop()[1])[0][5], 1, "the shell is told which widget is being edited");
// dragging it from the native view moves it to a cell (widgetDrag reports page px)
const tileBox = await p.locator(".slots .tile").first().boundingBox();
const pitchX = Math.round(tileBox.width + 8);
const pitchY = Math.round(tileBox.height + 8);
await p.evaluate(([dx, dy]) => {
window.launcher.widgetGrab(77);
window.launcher.widgetDrag(150, 200);
window.launcher.widgetDrag(150 + dx, 200 + dy);
window.launcher.widgetDrop(true);
}, [pitchX, 2 * pitchY]);
await settle();
const moved = await p.$eval(".widget-slot", (s) => s.style.gridRow);
assert.ok(!moved.startsWith("1 "), `moved off the first row: ${moved}`);
// Left and Top resize handles grow without shifting the anchored right/bottom edges
const b0 = await p.locator(".widget-slot").boundingBox();
const hl = await p.locator(".resize-handle[data-edge=l]").boundingBox();
await p.mouse.move(hl.x + hl.width / 2, hl.y + hl.height / 2);
await p.mouse.down();
await p.mouse.move(hl.x + hl.width / 2 - pitchX, hl.y + hl.height / 2, { steps: 6 }); await settle();
await p.mouse.up(); await settle();
const b1 = await p.locator(".widget-slot").boundingBox();
assert.ok(Math.abs((b0.x + b0.width) - (b1.x + b1.width)) < 2, "left resize keeps right edge anchored");
assert.ok(Math.abs((b1.width - b0.width) - pitchX) < 3, `left resize grew by 1 col: ${b1.width - b0.width}`);
assert.equal(await p.textContent(".resize-size"), "3 × 2");
const ht = await p.locator(".resize-handle[data-edge=t]").boundingBox();
await p.mouse.move(ht.x + ht.width / 2, ht.y + ht.height / 2);
await p.mouse.down();
await p.mouse.move(ht.x + ht.width / 2, ht.y + ht.height / 2 - pitchY, { steps: 6 }); await settle();
await p.mouse.up(); await settle();
const b2 = await p.locator(".widget-slot").boundingBox();
assert.ok(Math.abs((b1.y + b1.height) - (b2.y + b2.height)) < 2, "top resize keeps bottom edge anchored");
assert.ok(Math.abs((b2.height - b1.height) - pitchY) < 3, `top resize grew by 1 row: ${b2.height - b1.height}`);
assert.equal(await p.textContent(".resize-size"), "3 × 3");
// holding a resize handle >= 500ms does not lift widget or corrupt resize state
const hr = await p.locator(".resize-handle[data-edge=r]").boundingBox();
await p.mouse.move(hr.x + hr.width / 2, hr.y + hr.height / 2);
await p.mouse.down(); await p.waitForTimeout(600);
await p.mouse.move(hr.x + hr.width / 2 + pitchX, hr.y + hr.height / 2, { steps: 6 }); await settle();
await p.mouse.up(); await settle();
assert.ok(await p.$(".widget-slot.resizing"), "still resizing after 600ms hold");
assert.ok(!(await p.$(".widget-slot.lifted")), "not lifted after holding handle");
assert.ok(!(await p.$(".sheet")), "no sheet opened after holding handle");
assert.equal(await p.textContent(".resize-size"), "4 × 3");
// scrolled resize: ensure page scrolls, scroll down, then resize bottom by +1 row
await p.evaluate(() => {
document.querySelector(".home").style.minHeight = "1200px";
window.scrollTo(0, 120);
});
await settle();
assert.ok((await p.evaluate(() => window.scrollY)) > 50, "page scrolled for scrolled resize test");
const bs0 = await p.locator(".widget-slot").boundingBox();
const hb = await p.locator(".resize-handle[data-edge=b]").boundingBox();
await p.mouse.move(hb.x + hb.width / 2, hb.y + hb.height / 2);
await p.mouse.down();
await p.mouse.move(hb.x + hb.width / 2, hb.y + hb.height / 2 + pitchY, { steps: 6 }); await settle();
await p.mouse.up(); await settle();
const bs1 = await p.locator(".widget-slot").boundingBox();
assert.ok(Math.abs((bs1.height - bs0.height) - pitchY) < 3, `scrolled bottom resize grew by 1 row: ${bs1.height - bs0.height}`);
assert.equal(await p.textContent(".resize-size"), "4 × 4");
await p.evaluate(() => {
document.querySelector(".home").style.minHeight = "";
window.scrollTo(0, 0);
});
await settle();
// axis-conditional handles: h-only, v-only, and non-resizable
const edges = () => p.$$eval(".resize-handle", (hs) => hs.map((h) => h.dataset.edge));
await p.click(".resize-bar >> text=Done"); await settle();
await p.evaluate(() => {
window.__widgetLimits = { w: 110, minW: 64, minH: 64, maxW: 0, maxH: 0, h: true, v: false };
window.launcher.widgetMenu(77); window.launcher.widgetDrop(false);
});
await settle();
assert.deepEqual(await edges(), ["l", "r"]);
assert.equal(JSON.parse((await calls("excludeGestures")).pop()[1]).length, 2);
await p.click(".resize-bar >> text=Done"); await settle();
await p.evaluate(() => {
window.__widgetLimits = { w: 110, minW: 64, minH: 64, maxW: 0, maxH: 0, h: false, v: true };
window.launcher.widgetMenu(77); window.launcher.widgetDrop(false);
});
await settle();
assert.deepEqual(await edges(), ["t", "b"]);
assert.equal(JSON.parse((await calls("excludeGestures")).pop()[1]).length, 2);
await p.click(".resize-bar >> text=Done"); await settle();
await p.evaluate(() => {
window.__widgetLimits = { w: 110, minW: 64, minH: 64, maxW: 0, maxH: 0, h: false, v: false };
window.launcher.widgetMenu(77); window.launcher.widgetDrop(false);
});
await settle();
assert.deepEqual(await edges(), []);
assert.equal(JSON.parse((await calls("excludeGestures")).pop()[1]).length, 0);
await p.evaluate(() => { window.__widgetLimits = { w: 110, minW: 64, minH: 64, maxW: 0, maxH: 0, h: true, v: true }; });
await p.click(".resize-bar >> text=Remove"); await settle();
assert.equal((await calls("removeWidget"))[0][1], 77);
// opening the drawer while a notice is visible does not recycle .notice as .drawer
await longpress(".home", 0.95);
await p.click(".sheet-item >> text=Check for updates");
await p.waitForSelector(".notice");
await p.evaluate(() => { document.querySelector(".notice").__tag = "notice"; });
await p.click(".search-pill");
await p.waitForSelector(".drawer");
assert.equal(await p.$eval(".drawer", (d) => d.__tag), undefined, "drawer does not recycle .notice");
assert.equal(await p.$eval(".notice", (n) => n.__tag), "notice", ".notice keeps its DOM element");
await p.click(".drawer-head >> text=Close");
await p.waitForSelector(".drawer", { state: "detached", timeout: 2000 });
// a package change: the shell calls reload(), which fetches the app list again and re-asks for icons
const before = (await calls("loadIcons")).length;
await p.evaluate(() => window.launcher.reload()); await settle();
assert.ok((await calls("loadIcons")).length > before, "icons are asked for again after a reload");
// back and home close the drawer
await p.click(".search-pill"); await p.waitForSelector(".drawer");
await p.evaluate(() => window.launcher.back());
await p.waitForSelector(".drawer", { state: "detached", timeout: 2000 });
assert.ok(!(await p.$(".drawer")));
console.log("android bridge ok");
await b.close(); srv.close();
})().catch((e) => { console.error(e); process.exit(1); });