//go:build browser package mapatest import ( "context" "strings" "testing" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // A page's look in a real Chromium (columns and icons spec §9): LOOK7, // no shift with a banner, an image icon and chips with icons, before // and after the pictures load and when the look row shows on hover; // and LOOK10, the picker by keyboard, with axe. // lookBoxes is the title, the look row and every block of the body, // with their boxes. const lookBoxes = `JSON.stringify(Array.prototype.map.call( document.querySelectorAll("[mapa-banner], h1[mapa-title], " + "[mapa-look-row], article[mapa-body], " + "article[mapa-body] :is(p, h2, h3, li, a[mapa-chip])"), function (block) { var box = block.getBoundingClientRect(); return [block.nodeName, box.left, box.top, box.width, box.height].join(" "); }))` // lookedPage is a team page with an image icon and a banner, linking // to a page with an emoji icon, made by Bo. func lookedPage(t *testing.T, store *mapa.Store) mapa.Page { t.Helper() ctx := context.Background() other, err := store.CreateTeamPage(ctx, Bo, "Compass", "One.") if err != nil { t.Fatal(err) } if _, err := store.SetLook(ctx, Bo, other.ID, mapa.LookChange{ SetIcon: true, Icon: &mapa.Icon{Emoji: "🧭"}}); err != nil { t.Fatal(err) } page, err := store.CreateTeamPage(ctx, Bo, "Pilot", "# Plan\n\nSee [[Compass]] before the pilot.\n\n"+ strings.Repeat("A paragraph that fills the page. ", 40)) if err != nil { t.Fatal(err) } icon, band := addImage(t, store, Bo, 64, 64), addImage(t, store, Bo, 1600, 500) _, err = store.SetLook(ctx, Bo, page.ID, mapa.LookChange{ SetIcon: true, Icon: &mapa.Icon{Image: icon.PublicID}, SetBanner: true, Banner: &mapa.Banner{Image: band.PublicID, Y: 30}}) if err != nil { t.Fatal(err) } return page } func TestLook7TheLookShiftsNothing(t *testing.T) { held := &heldGets{} rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { held.Store = inner return held }) t.Cleanup(held.let) path := mapa.PagePath(lookedPage(t, store)) drive(t, rig, onNewDocument(shiftRecorder)) for _, dark := range []bool{false, true} { for _, width := range []int64{1440, 1000, 390} { for _, live := range []bool{false, true} { lookUnmoved(t, rig, held, path, width, live, dark) } } } } // lookUnmoved loads path with the pictures held, measures, lets them // come, hovers the page head, and finds nothing moved. func lookUnmoved(t *testing.T, rig *harness.Rig, held *heldGets, path string, width int64, live, dark bool) { t.Helper() held.hold() drive(t, rig, scripts(live), colorScheme(dark), chromedp.EmulateViewport(width, 900), chromedp.Evaluate[chromedp.Void](`location.href = `+ "`"+rig.Origin+path+"`"), chromedp.WaitVisible("article[mapa-body] p")) if live { drive(t, rig, chromedp.WaitVisible(mounted)) } before := read(t, rig, chromedp.Evaluate[string](lookBoxes)) held.let() eventually(t, "the banner and icon loaded", func() bool { return read(t, rig, chromedp.Evaluate[bool](`Array.prototype.every.call( document.querySelectorAll('[mapa-banner] img, h1[mapa-title] img'), function (img) { return img.complete && img.naturalWidth > 0; })`)) }) drive(t, rig, chromedp.MouseEvent( input.DispatchMouseEventTypeMouseMoved, 200, 120)) after := read(t, rig, chromedp.Evaluate[string](lookBoxes)) if after != before { t.Errorf("%dpx, scripts %v, dark %v: the page moved:\n%s\n%s", width, live, dark, before, after) } shifts := read(t, rig, chromedp.Evaluate[string]( `JSON.stringify(window.__shifts || [])`)) if shifts != "[]" { t.Errorf("%dpx: layout shifts: %s", width, shifts) } } func TestLook10ThePickerByKeyboard(t *testing.T) { rig, store := browserRig(t, 0) titledPage(t, rig, store, "Pilot", "One.") drive(t, rig, chromedp.Focus(`[data-mapa-look="icon"]`), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`[mapa-picker] [role="option"]`)) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, the picker open:\n%s", found) } drive(t, rig, chromedp.KeyEvent("compass"), chromedp.WaitVisible(`[mapa-picker] [role="option"][data-emoji="🧭"]`), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`h1[mapa-title] [mapa-icon]`)) shown := evaluate(t, rig, `document.querySelector("h1[mapa-title] [mapa-icon]").textContent`) if shown != "🧭" { t.Fatalf("the title shows %q", shown) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a page with an icon:\n%s", found) } } // LOOK10's banner half: Reposition by keyboard, then Save position. func TestLook10TheBannerByKeyboard(t *testing.T) { rig, store := browserRigWith(t, 0, nil) page := lookedPage(t, store) drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), chromedp.Focus(`[data-mapa-banner="reposition"]`), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`[mapa-banner-tools] button`)) eventually(t, "Save position has focus", func() bool { return evaluate(t, rig, `document.activeElement.textContent`) == "Save position" }) drive(t, rig, chromedp.KeyEvent(kb.ArrowDown), chromedp.KeyEvent(kb.ArrowDown)) if moved := evaluate(t, rig, `document.querySelector("[mapa-banner]")`+ `.getAttribute("data-y")`); moved != "20" { t.Fatalf("the arrows moved the banner to %s", moved) } drive(t, rig, chromedp.KeyEvent("\r")) eventually(t, "the banner moved and was saved", func() bool { return evaluate(t, rig, `(function () { var band = document.querySelector("[mapa-banner]"); return band && !band.hasAttribute("data-mapa-moving") ? band.getAttribute("data-y") : ""; })()`) == "20" }) if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a page with a banner:\n%s", found) } }