//go:build browser package browsertest import ( "testing" "github.com/chromedp/chromedp" "amadan.net/rastrillo/rastrillo/harness" ) // settingsLayoutJS describes what is out of place in Settings, "" if // nothing: its sections in order under their headings; every row one // height, its name on the left and its control on the right, centred on // the row; Appearance's pill as tall as a button; signing out last, its // explanation right under it. const settingsLayoutJS = `(() => { const problems = []; const heads = [...document.querySelectorAll(".settings .group h3")].map(h => h.textContent).join(", "); if (heads !== "You, This Base, Ask Base") problems.push("headings: " + heads); const rows = [...document.querySelectorAll(".settings .rows li")]; const names = rows.map(li => li.querySelector(".who").firstChild.textContent).join(", "); if (names !== "Signed in as, Appearance, Passkeys, Base name, Connection") problems.push("rows: " + names); const heights = new Set(rows.map(li => Math.round(li.getBoundingClientRect().height))); if (heights.size !== 1) problems.push("rows of heights " + [...heights].join(", ")); const btn = document.querySelector(".settings .rows .btn.ghost:not([hidden])").getBoundingClientRect(); const pill = document.querySelector("form.segmented").getBoundingClientRect(); if (Math.round(pill.height) !== Math.round(btn.height)) problems.push("Appearance is " + pill.height + "px tall, a button " + btn.height); for (const li of rows) { const r = li.getBoundingClientRect(), who = li.querySelector(".who").getBoundingClientRect(); const mid = (r.top + r.bottom) / 2; const control = [...li.children].find(c => c !== li.querySelector(".who") && c.checkVisibility()); if (Math.abs((who.top + who.bottom) / 2 - mid) > 1.5) problems.push(li.textContent.slice(0, 20) + ": name not centred"); if (!control) continue; const c = control.getBoundingClientRect(); if (Math.abs((c.top + c.bottom) / 2 - mid) > 1.5) problems.push(li.textContent.slice(0, 20) + ": control not centred"); if (Math.abs(c.right - r.right) > 1) problems.push(li.textContent.slice(0, 20) + ": control " + (r.right - c.right) + "px off the right"); if (c.left < who.right) problems.push(li.textContent.slice(0, 20) + ": control overlaps the name"); } const last = [...document.querySelector(".settings").children].pop(); if (!last.matches(".signout") || !last.querySelector('form[action="/auth/signout"]')) problems.push("signing out is not last"); const out = last.querySelector("button").getBoundingClientRect(), note = last.querySelector(".note").getBoundingClientRect(); if (note.top < out.bottom || note.top - out.bottom > 16 || Math.abs(note.left - out.left) > 1) problems.push("the explanation is not under Sign out"); return problems.join("; "); })()` // Settings is in tidy sections at a phone's width and a laptop's, light // and dark. With BASE_BROWSER_ARTIFACTS set it saves each. func TestSettingsLayout(t *testing.T) { rig, _, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) for _, v := range []struct { name string view chromedp.Action[chromedp.Void] }{ {"390", chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))}, {"1280", chromedp.EmulateViewport(1280, 800)}, } { for _, scheme := range []string{"light", "dark"} { drive(t, rig, v.view, reducedMotion(scheme, true), chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS(`form.segmented`))) // The add-a-passkey button shows once its script has looked. drive(t, rig, chromedp.Poll[chromedp.Void](`!document.querySelector("[data-passkey-add]").hidden`)) noOverflow(t, rig, "Settings at "+v.name) if got := harness.Evaluate[string](rig, settingsLayoutJS); got != "" { t.Errorf("Settings at %s, %s: %s", v.name, scheme, got) } if got := harness.Evaluate[string](rig, closeJS); got != "" { t.Errorf("Settings' close at %s: %s", v.name, got) } mobileShot(t, rig, "settings-"+v.name+"-"+scheme) } } // The Store and People pages' close, on a phone. drive(t, rig, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3)), reducedMotion("dark", true)) for _, p := range []string{"store", "people"} { drive(t, rig, chromedp.Navigate(rig.Origin+"/"+p), chromedp.WaitVisible(chromedp.CSS(`.crumb a.close`))) mobileShot(t, rig, p+"-390-dark") } }