| index bf8f924..01ca9c0 100644 |
| --- a/chrome/chrome.test.mjs |
| +++ b/chrome/chrome.test.mjs |
| @@ -33,6 +33,11 @@ class El { |
| } |
| hasAttribute(n) { return this.attrs.has(n); } |
| getAttribute(n) { return this.attrs.has(n) ? this.attrs.get(n) : null; } |
| + setAttribute(n, v) { this.attrs.set(n, String(v)); } |
| + removeAttribute(n) { this.attrs.delete(n); } |
| + get value() { return this.attrs.get("value") ?? ""; } |
| + set value(v) { this.attrs.set("value", v); } |
| + blur() { if (doc.activeElement === this) doc.activeElement = null; } |
| get open() { return this.attrs.has("open"); } |
| set open(v) { if (v) this.attrs.set("open", ""); else this.attrs.delete("open"); } |
| get tabIndex() { return Number(this.attrs.get("tabindex") ?? 0); } |
| @@ -46,21 +51,49 @@ class El { |
| } |
| |
| let doc; |
| -function load(root) { |
| +// load runs chrome.js against root. The returned dispatch sends an event |
| +// to document's listeners; dispatch.window sends one to window's. |
| +// globals adds to (or replaces) what the script sees: fetch, |
| +// getComputedStyle, document.fonts. |
| +function load(root, globals = {}) { |
| const listeners = []; |
| + const winListeners = []; |
| doc = { |
| activeElement: null, |
| + documentElement: new El("html"), |
| + fonts: globals.fonts, |
| querySelector: (s) => root.querySelector(s), |
| + querySelectorAll: (s) => root.querySelectorAll(s), |
| getElementById: () => null, |
| addEventListener: (type, fn, capture) => listeners.push({ type, fn, capture: !!capture }), |
| }; |
| - vm.runInNewContext(src, { document: doc, HTMLButtonElement: function () {} }); |
| - return (type, target, init = {}) => { |
| + const window = { addEventListener: (type, fn) => winListeners.push({ type, fn }) }; |
| + vm.runInNewContext(src, { |
| + document: doc, |
| + window, |
| + HTMLButtonElement: function () {}, |
| + getComputedStyle: globals.getComputedStyle ?? (() => ({})), |
| + fetch: globals.fetch ?? (() => Promise.resolve()), |
| + FormData: globals.FormData ?? FakeFormData, |
| + URLSearchParams, |
| + }); |
| + const dispatch = (type, target, init = {}) => { |
| const e = { type, target, defaultPrevented: false, preventDefault() { this.defaultPrevented = true; }, ...init }; |
| for (const l of listeners.filter((l) => l.type === type && l.capture)) l.fn(e); |
| for (const l of listeners.filter((l) => l.type === type && !l.capture)) l.fn(e); |
| return e; |
| }; |
| + dispatch.window = (type) => { for (const l of winListeners.filter((l) => l.type === type)) l.fn({ type }); }; |
| + return dispatch; |
| +} |
| + |
| +// new FormData(form): the form's named inputs, as a submit sends them |
| +// (never its buttons; the submitter is the script's to add). |
| +class FakeFormData { |
| + constructor(form) { |
| + this.entries = form.querySelectorAll("input").filter((i) => i.hasAttribute("name")).map((i) => [i.getAttribute("name"), i.value]); |
| + } |
| + [Symbol.iterator]() { return this.entries[Symbol.iterator](); } |
| } |
| |
| function menubar(tabindex) { |
| @@ -101,3 +134,123 @@ test("Escape is heard before a menu that stops propagation", () => { |
| dispatch("keydown", menus[0].querySelector("button"), { key: "Escape" }); |
| assert.equal(menus[0].open, false); |
| }); |
| + |
| +// The stacked bar's title field. Its text draws 140px wide with 6px of |
| +// padding each side and 1px borders. As a field does, its scroll width |
| +// at zero width is the text plus its start padding (146), and at any |
| +// other width at least that width, so a measure that skips the zeroing |
| +// reads the old width back instead. |
| +function titleField(attrs = { size: "17" }) { |
| + const input = new El("input", { "estilo-bar-title-input": "", value: "Untitled document", ...attrs }); |
| + input.text = 140; |
| + input.style = { inlineSize: "" }; |
| + Object.defineProperty(input, "scrollWidth", { |
| + get() { |
| + const w = parseFloat(this.style.inlineSize); |
| + return Number.isNaN(w) || w > 0 ? Math.max(400, this.text + 6) : this.text + 6; |
| + }, |
| + }); |
| + return input; |
| +} |
| +const fieldStyle = () => ({ paddingInlineEnd: "6px", borderInlineStartWidth: "1px", borderInlineEndWidth: "1px" }); |
| + |
| +test("a stacked title field is fitted to its text when the script runs", () => { |
| + const fitted = titleField(); |
| + const plain = titleField({}); |
| + load(new El("", {}, [fitted, plain]), { getComputedStyle: fieldStyle }); |
| + // 146 at zero width, + 6 end padding + 2 border + 2 for the caret. |
| + assert.equal(fitted.style.inlineSize, "156px"); |
| + assert.equal(plain.style.inlineSize, "", "a one-row bar's title (no size) is left alone"); |
| +}); |
| + |
| +test("the title is fitted again as it is typed, on resize, at load and when fonts settle", async () => { |
| + const input = titleField(); |
| + let settle; |
| + const fonts = { ready: new Promise((r) => { settle = r; }) }; |
| + const dispatch = load(new El("", {}, [input]), { getComputedStyle: fieldStyle, fonts }); |
| + input.text = 200; |
| + dispatch("input", input); |
| + assert.equal(input.style.inlineSize, "216px", "input"); |
| + input.text = 100; |
| + dispatch.window("resize"); |
| + assert.equal(input.style.inlineSize, "116px", "resize"); |
| + input.text = 120; |
| + dispatch.window("load"); |
| + assert.equal(input.style.inlineSize, "136px", "load"); |
| + input.text = 130; |
| + settle(); |
| + await fonts.ready; |
| + await null; |
| + assert.equal(input.style.inlineSize, "146px", "document.fonts.ready"); |
| +}); |
| + |
| +test("Escape restores the title and fits the field to it again", () => { |
| + const input = titleField(); |
| + const dispatch = load(new El("", {}, [input]), { getComputedStyle: fieldStyle }); |
| + input.defaultValue = "Untitled document"; |
| + input.value = "Something much longer"; |
| + input.text = 300; |
| + dispatch("input", input); |
| + input.text = 140; |
| + const e = dispatch("keydown", input, { key: "Escape" }); |
| + assert.equal(input.value, "Untitled document"); |
| + assert.equal(input.style.inlineSize, "156px"); |
| + assert.equal(e.defaultPrevented, true); |
| +}); |
| + |
| +function appearance() { |
| + const buttons = ["light", "dark", "system"].map((m) => new El("button", { "estilo-menu-item": "", type: "submit", name: "mode", value: m, "aria-pressed": m === "dark" ? "true" : "false" })); |
| + const form = new El("form", { "estilo-appearance": "", method: "post", action: "/appearance", "data-busy": "false" }, [new El("input", { type: "hidden", name: "return_to", value: "/d/abc" }), ...buttons]); |
| + return { form, buttons }; |
| +} |
| + |
| +test("Appearance switches in place and saves in the background", () => { |
| + const { form, buttons } = appearance(); |
| + const sent = []; |
| + const dispatch = load(new El("", {}, [form]), { fetch: (url, init) => { sent.push({ url, init }); return Promise.resolve({}); } }); |
| + doc.documentElement.setAttribute("data-theme", "dark"); |
| + |
| + let e = dispatch("submit", form, { submitter: buttons[0] }); |
| + assert.equal(e.defaultPrevented, true, "no navigation"); |
| + assert.equal(doc.documentElement.getAttribute("data-theme"), "light"); |
| + assert.deepEqual(buttons.map((b) => b.getAttribute("aria-pressed")), ["true", "false", "false"]); |
| + assert.equal(sent.length, 1); |
| + assert.equal(sent[0].url, "/appearance"); |
| + assert.equal(sent[0].init.method, "POST"); |
| + assert.equal(sent[0].init.credentials, "same-origin"); |
| + assert.equal(String(sent[0].init.body), "return_to=%2Fd%2Fabc&mode=light", "the same form data the form would post"); |
| + |
| + // System is no attribute at all, as chrome.ThemeAttr writes it. |
| + dispatch("submit", form, { submitter: buttons[2] }); |
| + assert.equal(doc.documentElement.hasAttribute("data-theme"), false); |
| + assert.deepEqual(buttons.map((b) => b.getAttribute("aria-pressed")), ["false", "false", "true"]); |
| + assert.equal(String(sent[1].init.body), "return_to=%2Fd%2Fabc&mode=system"); |
| +}); |
| + |
| +test("a failed save leaves the page switched and says nothing", async () => { |
| + const { form, buttons } = appearance(); |
| + const dispatch = load(new El("", {}, [form]), { fetch: () => Promise.reject(new TypeError("offline")) }); |
| + let unhandled = null; |
| + const onUnhandled = (r) => { unhandled = r; }; |
| + process.on("unhandledRejection", onUnhandled); |
| + dispatch("submit", form, { submitter: buttons[1] }); |
| + await new Promise((r) => setTimeout(r, 10)); |
| + process.off("unhandledRejection", onUnhandled); |
| + assert.equal(unhandled, null, "the failure surfaced"); |
| + assert.equal(doc.documentElement.getAttribute("data-theme"), "dark"); |
| + assert.deepEqual(buttons.map((b) => b.getAttribute("aria-pressed")), ["false", "true", "false"]); |
| +}); |
| + |
| +test("any other form, or a submit already handled, posts as it always has", () => { |
| + const { form, buttons } = appearance(); |
| + const other = new El("form", { method: "post", action: "/rename" }, [new El("button", { type: "submit", name: "mode", value: "dark" })]); |
| + let fetched = 0; |
| + const dispatch = load(new El("", {}, [form, other]), { fetch: () => { fetched++; return Promise.resolve(); } }); |
| + assert.equal(dispatch("submit", other, { submitter: other.children[0] }).defaultPrevented, false); |
| + const handled = dispatch("submit", form, { submitter: buttons[0], defaultPrevented: true }); |
| + assert.equal(handled.defaultPrevented, true); |
| + assert.equal(doc.documentElement.hasAttribute("data-theme"), false); |
| + // No submitter (form.submit() or an engine without it): a plain POST. |
| + assert.equal(dispatch("submit", form, {}).defaultPrevented, false); |
| + assert.equal(fetched, 0); |
| +}); |