//go:build browser package mapatest import ( "context" "fmt" "os" "path/filepath" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // A sidebar page row's ⋯ (build note 88), in real Chromium: it shows on // hover, on focus, and on a touch screen on the page on screen's row; // its menu is the page's verbs; Delete… asks first; it works with // scripts off; focus goes in and comes back; axe finds nothing; and // showing it moves nothing. // rowOf finds the first sidebar page row whose label starts with a name. const rowOf = `function (name) { return Array.prototype.find.call(document.querySelectorAll("[mapa-side-row]"), function (row) { var label = row.querySelector("[mapa-side-label]"); return label.textContent.indexOf(name) === 0; }); }` // markRow tags the first page row named name with data-row=tag, so a // selector can reach it. func markRow(name, tag string) chromedp.Action[chromedp.Void] { return chromedp.Evaluate[chromedp.Void](fmt.Sprintf( `(%s)(%q).setAttribute("data-row", %q)`, rowOf, name, tag)) } // more is the ⋯ of the row tagged tag. func more(tag string) string { return `[data-row="` + tag + `"] [mapa-row-more]` } // panel is the open menu of the row tagged tag. func panel(tag string) string { return `[data-row="` + tag + `"] [mapa-row-menu][open] [estilo-menu]` } // moreShown reports whether the ⋯ of the row tagged tag can be seen and // pressed. func moreShown(t *testing.T, rig *harness.Rig, tag string) bool { t.Helper() return read(t, rig, chromedp.Evaluate[bool](fmt.Sprintf(`(function () { var menu = document.querySelector('[data-row=%q] [mapa-row-menu]'); if (!menu || !menu.checkVisibility()) return false; var style = getComputedStyle(menu); return style.opacity === "1" && style.pointerEvents !== "none"; })()`, tag))) } // cornersOf is, for the element a selector names, each corner (3px in) // where something else is on top: "" when all of it shows. const cornersOf = `(function (menu) { var r = menu.getBoundingClientRect(), misses = []; [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3], [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3] ].forEach(function (p) { var hit = document.elementFromPoint(p[0], p[1]); if (!hit || !menu.contains(hit)) { misses.push(p.map(Math.round).join(",") + " " + (hit ? hit.tagName : "nothing")); } }); return misses.join("; "); })(document.querySelector(%q))` // openMore opens the ⋯ of the row tagged tag as a mouse does: it comes // to rest on the ⋯, which shows it, and clicks. func openMore(tag string) chromedp.Action[chromedp.Void] { return chromedp.Steps(hover(more(tag)), chromedp.Click(more(tag)), chromedp.WaitVisible(`[data-row="`+tag+`"] [mapa-row-menu][open]`)) } // pointAt moves a hovering mouse to a point. func pointAt(across, down float64) chromedp.Action[chromedp.Void] { return chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, across, down) } // sidebarAt opens path at 1400px with the sidebar open. func sidebarAt(rig *harness.Rig, live bool, path string) chromedp.Action[chromedp.Void] { return chromedp.Steps(scripts(live), sideCookie(rig, "open"), chromedp.EmulateViewport(1400, 877), chromedp.Navigate(rig.Origin+path), chromedp.WaitVisible("[mapa-side]")) } func TestARowsMoreShowsOnHoverAndOnFocus(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) writeOwnNotes(t, store) for _, live := range []bool{false, true} { drive(t, rig, sidebarAt(rig, live, "/"), markRow("Quarterly plan", "q"), pointAt(900, 400)) if moreShown(t, rig, "q") { t.Fatalf("scripts %v: the ⋯ shows with the pointer away", live) } drive(t, rig, hover(`[data-row="q"] [mapa-side-page]`)) if !moreShown(t, rig, "q") { t.Fatalf("scripts %v: the ⋯ does not show on hover", live) } drive(t, rig, pointAt(900, 400), chromedp.Focus(`[data-row="q"] [mapa-side-page]`)) if !moreShown(t, rig, "q") { t.Fatalf("scripts %v: the ⋯ does not show on focus", live) } drive(t, rig, chromedp.KeyEvent(kb.Tab)) if got := evaluate(t, rig, `document.activeElement.getAttribute( "aria-label")`); got != "More actions for Quarterly plan" { t.Fatalf("scripts %v: Tab from the row went to %q", live, got) } } // The rail hides the page rows, and so their ⋯. drive(t, rig, sideCookie(rig, "rail"), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible("[mapa-side]")) if got := evaluate(t, rig, `String(Array.prototype.filter.call( document.querySelectorAll("[mapa-row-more]"), function (el) { return el.checkVisibility(); }).length)`); got != "0" { t.Fatalf("the rail shows %s ⋯", got) } } // On a touch screen there is no hover: the page on screen's row shows // its ⋯; another row's end is still its link, so a tap opens the page; // and the ⋯ and its row are 44px tap targets. func TestARowsMoreOnATouchScreen(t *testing.T) { rig, store := browserRig(t, 0, harness.WithCoarsePointer()) notes := writeOwnNotes(t, store) drive(t, rig, sidebarAt(rig, true, notes.pinned), markRow("Pinned plan", "p"), markRow("Quarterly plan", "q")) if evaluate(t, rig, `String(matchMedia("(pointer: coarse)").matches)`) != "true" { t.Fatal("the pointer is not coarse") } if !moreShown(t, rig, "p") || moreShown(t, rig, "q") { t.Fatalf("on a touch screen the ⋯ shows on the page's row %v, "+ "on another %v", moreShown(t, rig, "p"), moreShown(t, rig, "q")) } sizes := evaluate(t, rig, `(function () { var more = document.querySelector('[data-row="p"] [mapa-row-more]'); var link = document.querySelector('[data-row="q"] [mapa-side-page]'); var a = more.getBoundingClientRect(), b = link.getBoundingClientRect(); return [a.width, a.height, b.height].map(Math.round).join("x"); })()`) if sizes != "44x44x44" { t.Fatalf("the ⋯ and a row are %s, want 44x44x44", sizes) } hit := evaluate(t, rig, `(function () { var r = document.querySelector('[data-row="q"] [mapa-row-more]') .getBoundingClientRect(); var at = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2); if (at && at.closest("a[mapa-side-page]")) return "link"; return String(at && at.tagName); })()`) if hit != "link" { t.Fatalf("a tap on another row's end lands on %s, not its link", hit) } drive(t, rig, press(more("p")), chromedp.WaitVisible(panel("p"))) short := evaluate(t, rig, `Array.prototype.filter.call( document.querySelectorAll('[data-row="p"] [estilo-menu-item]'), function (el) { return el.getBoundingClientRect().height < 44; }) .map(function (el) { return el.textContent.trim(); }).join(", ")`) if short != "" { t.Fatalf("on a touch screen these rows are under 44px: %s", short) } } // menuOf is the open or closed menu of the row tagged tag: each row's // label and its address, a divider as "—". func menuOf(t *testing.T, rig *harness.Rig, tag string) string { t.Helper() return evaluate(t, rig, fmt.Sprintf(`(function () { var menu = document.querySelector( '[data-row=%q] [mapa-row-menu] [estilo-menu]'); var page = document.querySelector('[data-row=%q] [mapa-side-page]') .getAttribute("href"); var rows = menu.querySelectorAll("hr, [estilo-menu-item]"); return Array.prototype.map.call(rows, function (el) { if (el.tagName === "HR") return "—"; var to = el.getAttribute("href") || el.form.getAttribute("action"); return el.textContent.trim() + " " + to.replace(page, "~"); }).join(" | "); })()`, tag, tag)) } func TestARowsMenuIsThePagesVerbs(t *testing.T) { rig, store := browserRig(t, 0) writeOwnNotes(t, store) drive(t, rig, sidebarAt(rig, false, "/"), markRow("Quarterly plan", "q"), markRow("Pinned plan", "p"), markRow("Pinned note", "n")) cases := map[string]string{ "q": "Rename… ~/rename | History ~/history | Pin to sidebar ~/pin" + " | — | Delete… ~/delete", "p": "Rename… ~/rename | History ~/history | Unpin from sidebar " + "~/pin | — | Delete… ~/delete", "n": "Make team page… ~/share | History ~/history | " + "Unpin from sidebar ~/pin | — | Delete… ~/delete", } for tag, want := range cases { if got := menuOf(t, rig, tag); got != want { t.Errorf("row %s: the menu is %q, want %q", tag, got, want) } } danger := evaluate(t, rig, `document.querySelector( '[data-row="q"] [mapa-row-menu] a[href$="/delete"]') .getAttribute("estilo-menu-item")`) if danger != "danger" { t.Errorf("Delete… is %q, not danger", danger) } } // Delete… from a row opens its confirm page; nothing is deleted until // that page's button is pressed. func TestDeleteFromARowAsksFirst(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) notes := writeOwnNotes(t, store) for _, live := range []bool{false, true} { drive(t, rig, sidebarAt(rig, live, "/"), markRow("Quarterly plan", "q"), openMore("q"), chromedp.Click(`[data-row="q"] a[href$="/delete"]`), chromedp.WaitVisible(`main form[action$="/delete"]`)) if got := evaluate(t, rig, `location.pathname`); got != notes.team+"/delete" { t.Fatalf("scripts %v: Delete… went to %s", live, got) } drive(t, rig, chromedp.Navigate(rig.Origin+notes.team), chromedp.WaitVisible("main h1")) if got := evaluate(t, rig, `document.querySelector("main h1") .textContent.trim()`); got != "Quarterly plan" { t.Fatalf("scripts %v: after Delete… the page reads %q", live, got) } } } // With scripts off the ⋯ opens by the mouse, the whole menu is on top // even from the sidebar's last row, and Unpin and Pin post and come // back to the screen they were sent from. func TestARowsMenuWorksWithScriptsOff(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) writeOwnNotes(t, store) drive(t, rig, sidebarAt(rig, false, "/pages"), chromedp.Evaluate[chromedp.Void](`(function (rows) { rows[rows.length - 1].setAttribute("data-row", "last"); })(document.querySelectorAll("[mapa-side-row]"))`), openMore("last"), chromedp.WaitVisible(panel("last"))) if misses := evaluate(t, rig, fmt.Sprintf(cornersOf, panel("last"))); misses != "" { t.Fatalf("the last row's menu is covered or cut off at %s", misses) } drive(t, rig, markRow("Pinned plan", "p"), openMore("p"), chromedp.Click(`[data-row="p"] button[estilo-menu-item]`), chromedp.WaitNotPresent(`[data-row="p"]`), chromedp.WaitVisible("[mapa-side]")) pinned := `(function (head) { var out = []; var el = head && head.nextElementSibling; for (; el && el.matches("[mapa-side-row]"); el = el.nextElementSibling) { out.push(el.querySelector("[mapa-side-label]").textContent.trim()); } return location.pathname + ": " + out.join(" | "); })(Array.prototype.find.call(document.querySelectorAll("[mapa-side-head]"), function (h) { return h.textContent === "Pinned"; }))` if got := evaluate(t, rig, pinned); got != "/pages: Pinned note, only you see this" { t.Fatalf("after Unpin: %q", got) } drive(t, rig, markRow("Quarterly plan", "q"), openMore("q"), chromedp.Click(`[data-row="q"] button[estilo-menu-item]`), chromedp.WaitNotPresent(`[data-row="q"]`), chromedp.WaitVisible("[mapa-side]")) if got := evaluate(t, rig, pinned); got != "/pages: Quarterly plan | Pinned note, only you see this" { t.Fatalf("after Pin: %q", got) } } // The open menu is on top of the page it hangs across: at 900px with // the sidebar open, the editor's text starts under it. func TestARowsMenuIsOverTheEditor(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) notes := writeOwnNotes(t, store) drive(t, rig, scripts(true), sideCookie(rig, "open"), chromedp.EmulateViewport(900, 877), chromedp.Navigate(rig.Origin+notes.team), chromedp.WaitVisible(mounted), markRow("Pinned plan", "p"), openMore("p")) misses := evaluate(t, rig, fmt.Sprintf(cornersOf, panel("p"))) if misses != "" { t.Fatalf("the menu is under the page at %s", misses) } } // From the keyboard: Enter on the ⋯ opens it with focus on its first // row, ↓ moves, and Esc closes it with focus back on the ⋯. func TestARowsMenuTakesFocusAndGivesItBack(t *testing.T) { rig, store := browserRig(t, 0) writeOwnNotes(t, store) focused := `(function (el) { return (el.getAttribute("aria-label") || el.textContent).trim(); })(document.activeElement)` drive(t, rig, sidebarAt(rig, true, "/"), markRow("Quarterly plan", "q"), press(more("q")), chromedp.WaitVisible(panel("q"))) eventually(t, "focus on the menu's first row", func() bool { return evaluate(t, rig, focused) == "Rename…" }) drive(t, rig, chromedp.KeyEvent(kb.ArrowDown)) if got := evaluate(t, rig, focused); got != "History" { t.Fatalf("↓ focused %q", got) } drive(t, rig, chromedp.KeyEvent(kb.ArrowUp), chromedp.KeyEvent(kb.ArrowUp)) if got := evaluate(t, rig, focused); got != "Delete…" { t.Fatalf("↑ from the first row focused %q, not the last", got) } drive(t, rig, chromedp.KeyEvent(kb.Tab)) if evaluate(t, rig, `String(document.querySelector( '[data-row="q"] [mapa-row-menu]').open)`) != "false" { t.Fatal("Tab out of the menu left it open") } drive(t, rig, press(more("q")), chromedp.WaitVisible(panel("q"))) eventually(t, "focus back in the menu", func() bool { return evaluate(t, rig, focused) == "Rename…" }) drive(t, rig, chromedp.KeyEvent(kb.Escape)) if got := evaluate(t, rig, focused); got != "More actions for Quarterly plan" { t.Fatalf("Esc left focus on %q", got) } if evaluate(t, rig, `String(document.querySelector( '[data-row="q"] [mapa-row-menu]').open)`) != "false" { t.Fatal("Esc left the menu open") } } func TestARowsMenuPassesAxe(t *testing.T) { for _, touch := range []bool{false, true} { option := harness.WithHover() if touch { option = harness.WithCoarsePointer() } rig, store := browserRig(t, 0, option) notes := writeOwnNotes(t, store) drive(t, rig, sidebarAt(rig, true, notes.pinned), markRow("Quarterly plan", "q")) if found := axeSerious(t, rig); found != "" { t.Fatalf("touch %v, closed:\n%s", touch, found) } drive(t, rig, press(more("q")), chromedp.WaitVisible(panel("q"))) if found := axeSerious(t, rig); found != "" { t.Fatalf("touch %v, open:\n%s", touch, found) } moreShot(t, rig, fmt.Sprintf("open-touch-%v", touch)) } } // Showing the ⋯ on hover or focus moves no row, no label and no icon, // and a long title ends before it. func TestShowingARowsMoreShiftsNothing(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) if _, err := store.CreateTeamPage(context.Background(), Ana, "A very long page title that cannot fit in the sidebar", "Text."); err != nil { t.Fatal(err) } writeOwnNotes(t, store) boxes := `Array.prototype.map.call(document.querySelectorAll( "[mapa-side] > *, [mapa-side-row] > a, [mapa-side-row] svg, " + "[mapa-side-label]"), function (el) { var r = el.getBoundingClientRect(); return [r.left, r.top, r.width, r.height].join(","); }).join(" ")` for _, live := range []bool{false, true} { drive(t, rig, sidebarAt(rig, live, "/"), pointAt(900, 400), markRow("A very long", "long")) before := evaluate(t, rig, boxes) moreShot(t, rig, fmt.Sprintf("rest-scripts-%v", live)) drive(t, rig, hover(`[data-row="long"] [mapa-side-page]`)) if !moreShown(t, rig, "long") { t.Fatalf("scripts %v: hovering did not show the ⋯", live) } moreShot(t, rig, fmt.Sprintf("hover-scripts-%v", live)) if after := evaluate(t, rig, boxes); after != before { t.Fatalf("scripts %v: hovering moved the sidebar:\n%s\n%s", live, before, after) } drive(t, rig, pointAt(900, 400), chromedp.Focus(`[data-row="long"] [mapa-side-page]`)) if after := evaluate(t, rig, boxes); after != before { t.Fatalf("scripts %v: focus moved the sidebar", live) } clear := evaluate(t, rig, `(function (row) { var label = row.querySelector("[mapa-side-label]").getBoundingClientRect(); var more = row.querySelector("[mapa-row-more]").getBoundingClientRect(); return String(label.right <= more.left); })(document.querySelector('[data-row="long"]'))`) if clear != "true" { t.Fatalf("scripts %v: the long title runs under the ⋯", live) } } } // moreShot saves the window to MAPA_MORE_SHOTS, when it is set, for a // look by eye. func moreShot(t *testing.T, rig *harness.Rig, name string) { dir := os.Getenv("MAPA_MORE_SHOTS") if dir == "" { return } picture := read(t, rig, chromedp.CaptureScreenshot()) if err := os.WriteFile(filepath.Join(dir, name+".png"), picture, 0o600); err != nil { t.Fatal(err) } }