243 lines
15 KiB
JavaScript
243 lines
15 KiB
JavaScript
// Browser test of the launcher page built from MoonBit (dist/web, see scripts/build-web.sh),
|
||
// against the demo apps.json and widgets.json: the same flows legacy/tests/e2e.cjs drove.
|
||
// scripts/build-web.sh --demo && node tests/e2e.cjs
|
||
// Serves dist/web itself unless BASE points at a running copy. PLAYWRIGHT names the
|
||
// playwright module to load when it is not installed next to the repo; SHOTS=dir saves screenshots.
|
||
const assert = require("assert");
|
||
const fs = require("fs");
|
||
const http = require("http");
|
||
const path = require("path");
|
||
const { chromium } = require(process.env.PLAYWRIGHT || "playwright");
|
||
|
||
const serve = () => new Promise((resolve) => {
|
||
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 f = path.join(root, req.url.split("?")[0] === "/" ? "index.html" : req.url.split("?")[0]);
|
||
fs.readFile(f, (err, data) => {
|
||
if (err) { res.writeHead(404); res.end(); return; }
|
||
res.writeHead(200, { "content-type": types[path.extname(f)] || "application/octet-stream" });
|
||
res.end(data);
|
||
});
|
||
}).listen(0, () => resolve([srv, `http://localhost:${srv.address().port}`]));
|
||
});
|
||
|
||
(async () => {
|
||
const [srv, served] = process.env.BASE ? [null, process.env.BASE] : await serve();
|
||
const BASE = served;
|
||
const shot = async (p, name) => { if (process.env.SHOTS) await p.screenshot({ path: path.join(process.env.SHOTS, name + ".png") }); };
|
||
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; });
|
||
|
||
const settle = () => p.waitForTimeout(250); // renders land on the next frame, which can lag here
|
||
await p.emulateMedia({ reducedMotion: "reduce" }); // no sliding drawer under the pointer
|
||
await p.goto(BASE + "/");
|
||
await p.evaluate(() => localStorage.clear());
|
||
await p.reload();
|
||
await p.waitForSelector(".hint");
|
||
await shot(p, "00-blank-home");
|
||
assert.ok(!(await p.$(".clock")), "home has no built-in clock");
|
||
const demoBg = await p.evaluate(() => ({
|
||
hasDemo: document.documentElement.classList.contains("demo"),
|
||
htmlBgImage: getComputedStyle(document.documentElement).backgroundImage,
|
||
bodyBgColor: getComputedStyle(document.body).backgroundColor,
|
||
}));
|
||
assert.ok(demoBg.hasDemo, "desktop mode adds .demo class");
|
||
assert.ok(demoBg.htmlBgImage.includes("linear-gradient"), "desktop mode keeps gradient backdrop");
|
||
assert.equal(demoBg.bodyBgColor, "rgba(0, 0, 0, 0)", "body itself stays transparent");
|
||
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();
|
||
};
|
||
// the home menu's update check goes to the shell and closes the menu
|
||
await longpress(".hint");
|
||
await p.click(".sheet-item >> text=Check for updates"); await settle();
|
||
assert.equal(await p.textContent("#toast"), "Check for updates");
|
||
assert.ok(!(await p.$(".sheet")), "the menu closes");
|
||
const homeApps = () => p.$$eval(".slots .name", (ns) => ns.map((n) => n.textContent));
|
||
// long press on the blank home: add apps through the drawer's pick mode
|
||
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");
|
||
for (const i of [4, 8, 12]) await p.click(`.apps .tile:nth-child(${i})`);
|
||
await settle();
|
||
assert.equal(await p.$$eval(".apps .tile.on", (ts) => ts.length), 3);
|
||
await p.click(".drawer-head >> text=Done"); await settle();
|
||
assert.deepEqual(await homeApps(), ["Chrome", "Gmail", "Phone"]);
|
||
await shot(p, "01-home-with-apps");
|
||
// add a widget: long press on blank home, below the apps
|
||
await longpress(".home", 0.95);
|
||
await p.click(".sheet-item >> text=Add widget");
|
||
await p.waitForSelector(".widget-list li");
|
||
// search the picker by widget name and by the app it comes from
|
||
const widgetNames = () => p.$$eval(".widget-list .widget-name", (ns) => ns.map((n) => n.firstChild.textContent));
|
||
await p.fill(".widget-search", "cal"); await settle();
|
||
assert.deepEqual(await widgetNames(), ["Calendar"]);
|
||
await p.fill(".widget-search", "goo"); await settle();
|
||
assert.deepEqual(await widgetNames(), ["Weather"]);
|
||
await p.fill(".widget-search", "zzz"); await settle();
|
||
assert.equal(await p.textContent(".sheet .empty"), "No widgets match.");
|
||
await p.fill(".widget-search", ""); await settle();
|
||
await p.click(".widget-list li:first-child"); await settle();
|
||
assert.equal(await p.getAttribute(".widget-slot", "data-wid"), "100");
|
||
await shot(p, "02-widget-placeholder");
|
||
assert.equal(await p.textContent(".widget-label"), "Clock");
|
||
// hold a home app and drag it: reorder
|
||
const drag = async (from, to) => {
|
||
const a = await p.locator(from).first().boundingBox(), z = await p.locator(to).first().boundingBox();
|
||
await p.mouse.move(a.x + a.width / 2, a.y + a.height / 2);
|
||
await p.mouse.down(); await p.waitForTimeout(650); await settle();
|
||
assert.ok(await p.$(".slots > .lifted"), "a long press lifts the slot");
|
||
await p.mouse.move(z.x + z.width / 2, z.y + z.height / 2, { steps: 5 }); await settle();
|
||
await p.mouse.up(); await settle();
|
||
};
|
||
await drag(".slots .tile:nth-child(2)", ".slots .tile:nth-child(1)");
|
||
assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]);
|
||
assert.ok(!(await p.$(".sheet")), "a drag doesn't open the menu");
|
||
assert.ok(!(await p.$(".slots > .lifted")));
|
||
await drag(".slots .tile:nth-child(1)", ".slots .tile:nth-child(3)");
|
||
assert.deepEqual(await homeApps(), ["Chrome", "Phone", "Gmail"]);
|
||
// hold without moving: the menu
|
||
await longpress(".slots .tile:nth-child(3)");
|
||
await p.click(".sheet-item >> text=Move earlier"); await settle();
|
||
assert.deepEqual(await homeApps(), ["Chrome", "Gmail", "Phone"]);
|
||
await longpress(".slots .tile:nth-child(1)");
|
||
await p.click(".sheet-item >> text=Move later"); await settle();
|
||
assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]);
|
||
// the grid runs down the screen; an app dropped in an empty cell there stays in it,
|
||
// and the rest stay packed
|
||
const rows = await p.$eval(".slots", (s) => getComputedStyle(s).gridTemplateRows.split(" ").length);
|
||
assert.ok(rows >= 5, `rows fill the screen: ${rows}`);
|
||
const tileAt = (n) => p.locator(`.slots .tile:has-text("${n}")`).boundingBox();
|
||
const g0 = await tileAt("Gmail"), c0 = await tileAt("Chrome"), pitch = 64 + 8;
|
||
await p.mouse.move(g0.x + g0.width / 2, g0.y + g0.height / 2);
|
||
await p.mouse.down(); await p.waitForTimeout(650); await settle();
|
||
await p.mouse.move(g0.x + g0.width / 2 + 10, g0.y + g0.height / 2 + 4 * pitch, { steps: 6 }); await settle();
|
||
await p.mouse.up(); await settle();
|
||
const g1 = await tileAt("Gmail"), c1 = await tileAt("Chrome");
|
||
assert.ok(Math.abs(g1.y - g0.y - 4 * pitch) < 3, `left four rows down: ${g1.y - g0.y}`);
|
||
assert.equal(Math.round(c1.x), Math.round(g0.x), "Chrome moves up into the packed first cell");
|
||
await p.reload(); await p.waitForSelector(".slots .tile");
|
||
assert.ok(Math.abs((await tileAt("Gmail")).y - g1.y) < 3, "its cell is saved");
|
||
// dragging it back over an app puts it back in the packed order
|
||
const c2 = await tileAt("Chrome");
|
||
await p.mouse.move(g1.x + g1.width / 2, g1.y + g1.height / 2);
|
||
await p.mouse.down(); await p.waitForTimeout(650); await settle();
|
||
await p.mouse.move(c2.x + c2.width / 2, c2.y + c2.height / 2, { steps: 6 }); await settle();
|
||
await p.mouse.up(); await settle();
|
||
assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone"]);
|
||
assert.ok(Math.abs((await tileAt("Gmail")).y - c0.y) < 3, "back in the first row");
|
||
await p.mouse.click(5, 300); await settle(); // a tap on empty home ends the drag gesture
|
||
// swipe down opens the drawer (and takes the widget off screen), search, long press adds
|
||
const swipe = (y0, y1) => p.evaluate(([y0, y1]) => {
|
||
const at = (y) => new Touch({ identifier: 1, target: document.body, clientX: 200, clientY: y });
|
||
document.body.dispatchEvent(new TouchEvent("touchstart", { bubbles: true, touches: [at(y0)], changedTouches: [at(y0)] }));
|
||
document.body.dispatchEvent(new TouchEvent("touchend", { bubbles: true, touches: [], changedTouches: [at(y1)] }));
|
||
}, [y0, y1]);
|
||
await swipe(200, 500);
|
||
await p.waitForSelector(".drawer", { timeout: 2000 });
|
||
await shot(p, "03-drawer");
|
||
assert.equal(await p.$$eval("[data-wid]", (ws) => ws.length), 0);
|
||
assert.ok(await p.$eval(".drawer input", (i) => i === document.activeElement), "opening the drawer focuses its search");
|
||
await p.fill(".drawer input", "ma"); await settle();
|
||
assert.deepEqual(await p.$$eval(".apps .name", (ns) => ns.map((n) => n.textContent)), ["Gmail", "Maps"]);
|
||
await p.press(".drawer input", "Enter"); await settle();
|
||
assert.equal(await p.textContent("#toast"), "Open com.example.gmail/.MainActivity");
|
||
assert.ok(!(await p.$(".drawer")), "launching closes the drawer");
|
||
await swipe(200, 500);
|
||
await p.waitForSelector(".drawer .tile", { timeout: 2000 });
|
||
await longpress(".apps .tile:first-child");
|
||
assert.deepEqual(await p.$$eval(".sheet-item", (bs) => bs.map((b) => b.textContent)), ["Add to home", "App info", "Uninstall"]);
|
||
await shot(p, "04-drawer-app-menu");
|
||
await p.click(".sheet-item:has-text('Uninstall')"); await settle();
|
||
assert.equal(await p.textContent("#toast"), "Uninstall com.example.calculator/.MainActivity");
|
||
assert.ok(!(await p.$(".sheet")) && (await p.$(".drawer")), "uninstalling closes the menu, not the drawer");
|
||
await p.fill(".drawer input", "phone"); await settle();
|
||
await longpress(".apps .tile:first-child");
|
||
assert.ok(!(await p.$(".sheet-item:has-text('Uninstall')")), "system apps can't be uninstalled");
|
||
await p.click(".sheet-overlay", { position: { x: 10, y: 10 } }); await p.fill(".drawer input", ""); await settle();
|
||
await longpress(".apps .tile:first-child");
|
||
await p.click(".sheet-item:has-text('Add to home')"); await settle();
|
||
assert.equal(await p.textContent(".notice"), "Added to home");
|
||
await swipe(600, 200);
|
||
await p.waitForSelector(".drawer", { state: "detached", timeout: 2000 }); // swipe up closes it
|
||
assert.deepEqual(await homeApps(), ["Gmail", "Chrome", "Phone", "Calculator"]);
|
||
// persisted; tap launches; remove
|
||
await p.reload(); await p.waitForSelector(".slots .tile");
|
||
assert.equal(await p.$$eval(".slots > li", (ls) => ls.length), 5);
|
||
await p.click(".slots .tile:first-child"); await settle();
|
||
assert.equal(await p.textContent("#toast"), "Open com.example.gmail/.MainActivity");
|
||
// hold a widget without moving: resize handles; drag them to snap it to the grid
|
||
await longpress(".widget-slot");
|
||
assert.ok(!(await p.$(".sheet")), "a widget gets handles, not the menu");
|
||
assert.equal(await p.textContent(".resize-size"), "5 × 2");
|
||
for (const edge of ["l", "r", "t", "b", "rb"]) {
|
||
const box = await p.locator(`.resize-handle[data-edge=${edge}]`).boundingBox();
|
||
assert.ok(box, `handle ${edge} rendered`);
|
||
assert.ok(box.width >= 36 && box.height >= 36, `handle ${edge} generous target: ${box.width}x${box.height}`);
|
||
assert.ok(box.x >= 0 && box.x + box.width <= 390, `handle ${edge} stays inside viewport: [${box.x}, ${box.x + box.width}]`);
|
||
}
|
||
await shot(p, "05-resize-handles");
|
||
// resting a finger >= 500ms on a resize handle must not lift the widget or open a menu
|
||
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);
|
||
assert.ok(await p.$(".widget-slot.resizing"), "holding handle keeps resize mode");
|
||
assert.ok(!(await p.$(".widget-slot.lifted")), "holding handle does not lift widget");
|
||
assert.ok(!(await p.$(".sheet")), "holding handle does not open sheet");
|
||
await p.mouse.up(); await settle();
|
||
const size = () => p.$eval(".widget-slot", (w) => [Math.round(w.getBoundingClientRect().width), Math.round(w.getBoundingClientRect().height)]);
|
||
const [w0] = await size();
|
||
const tile = await p.locator(".slots .tile").first().boundingBox();
|
||
const hd = await p.locator(".resize-handle[data-edge=rb]").boundingBox();
|
||
await p.mouse.move(hd.x + hd.width / 2, hd.y + hd.height / 2);
|
||
await p.mouse.down();
|
||
const ws = await p.locator(".widget-slot").boundingBox();
|
||
await p.mouse.move(ws.x + tile.width * 2.1, ws.y + 140, { steps: 8 }); await settle();
|
||
await p.mouse.up(); await settle();
|
||
const [w1, h1] = await size();
|
||
assert.ok(w1 < w0 && Math.abs(w1 - (2 * tile.width + 8)) < 3, `two columns wide: ${w1} (tile ${tile.width})`);
|
||
assert.equal(h1, 2 * 64 + 8, "snapped to two whole rows of tiles");
|
||
assert.equal(await p.textContent(".resize-size"), "2 × 2");
|
||
assert.ok(await p.$(".widget-slot.resizing"), "letting go of a handle keeps the handles");
|
||
await p.click(".resize-bar >> text=Done"); await settle();
|
||
assert.ok(!(await p.$(".resize-handle")));
|
||
await p.reload(); await p.waitForSelector(".widget-slot");
|
||
assert.deepEqual(await size(), [w1, h1], "the size is saved");
|
||
// while it shows handles, dragging the widget itself moves it, without another long press
|
||
await longpress(".widget-slot");
|
||
const atStart = () => p.$eval(".widget-slot", (w) => {
|
||
const r = w.getBoundingClientRect(), s = w.parentElement.getBoundingClientRect();
|
||
return Math.abs(r.left - s.left) < 2 && Math.abs(r.top - s.top) < 2;
|
||
});
|
||
assert.ok(!(await atStart()));
|
||
const wb = await p.locator(".widget-slot").boundingBox(), first = await p.locator(".slots > li").first().boundingBox();
|
||
await p.mouse.move(wb.x + wb.width / 2, wb.y + wb.height / 2);
|
||
await p.mouse.down(); await settle();
|
||
assert.ok(await p.$(".widget-slot.lifted"), "pressing a widget with handles picks it up");
|
||
await p.mouse.move(first.x + first.width / 2, first.y + first.height / 2, { steps: 8 }); await settle();
|
||
await p.mouse.up(); await settle();
|
||
assert.ok(await atStart(), "dragged onto the top row, first cell");
|
||
// the apps flow into the cells beside it: a 2x2 widget in 5 columns leaves room for all four
|
||
const beside = await p.$eval(".widget-slot", (w) => {
|
||
const r = w.getBoundingClientRect();
|
||
return [...document.querySelectorAll(".slots .tile")].filter((t) => {
|
||
const b = t.getBoundingClientRect(); return b.top >= r.top - 1 && b.bottom <= r.bottom + 1;
|
||
}).length;
|
||
});
|
||
assert.equal(beside, 4, "apps fill the cells beside the widget");
|
||
assert.ok(await p.$(".widget-slot.resizing"), "it keeps its handles after the move");
|
||
assert.ok(!(await p.$(".sheet")));
|
||
await p.click(".resize-bar >> text=Remove"); await settle();
|
||
assert.ok(!(await p.$(".widget-slot")));
|
||
await p.evaluate(() => { window.launcher.reload(); }); await settle();
|
||
assert.equal(await p.$$eval(".slots > li", (ls) => ls.length), 4);
|
||
console.log("launcher ok");
|
||
|
||
await b.close();
|
||
if (srv) srv.close();
|
||
})().catch((e) => { console.error(e); process.exit(1); });
|