oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "fmt"
8 "os"
9 "path/filepath"
10 "testing"
11
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/cdproto/input"
14 "github.com/chromedp/chromedp"
15 "github.com/chromedp/chromedp/kb"
16)
17
18// A sidebar page row's ⋯ (build note 88), in real Chromium: it shows on
19// hover, on focus, and on a touch screen on the page on screen's row;
20// its menu is the page's verbs; Delete… asks first; it works with
21// scripts off; focus goes in and comes back; axe finds nothing; and
22// showing it moves nothing.
23
24// rowOf finds the first sidebar page row whose label starts with a name.
25const rowOf = `function (name) {
26 return Array.prototype.find.call(document.querySelectorAll("[mapa-side-row]"),
27 function (row) {
28 var label = row.querySelector("[mapa-side-label]");
29 return label.textContent.indexOf(name) === 0;
30 });
31}`
32
33// markRow tags the first page row named name with data-row=tag, so a
34// selector can reach it.
35func markRow(name, tag string) chromedp.Action[chromedp.Void] {
36 return chromedp.Evaluate[chromedp.Void](fmt.Sprintf(
37 `(%s)(%q).setAttribute("data-row", %q)`, rowOf, name, tag))
38}
39
40// more is the ⋯ of the row tagged tag.
41func more(tag string) string {
42 return `[data-row="` + tag + `"] [mapa-row-more]`
43}
44
45// panel is the open menu of the row tagged tag.
46func panel(tag string) string {
47 return `[data-row="` + tag + `"] [mapa-row-menu][open] [estilo-menu]`
48}
49
50// moreShown reports whether the ⋯ of the row tagged tag can be seen and
51// pressed.
52func moreShown(t *testing.T, rig *harness.Rig, tag string) bool {
53 t.Helper()
54 return read(t, rig, chromedp.Evaluate[bool](fmt.Sprintf(`(function () {
55 var menu = document.querySelector('[data-row=%q] [mapa-row-menu]');
56 if (!menu || !menu.checkVisibility()) return false;
57 var style = getComputedStyle(menu);
58 return style.opacity === "1" && style.pointerEvents !== "none";
59})()`, tag)))
60}
61
62// cornersOf is, for the element a selector names, each corner (3px in)
63// where something else is on top: "" when all of it shows.
64const cornersOf = `(function (menu) {
65 var r = menu.getBoundingClientRect(), misses = [];
66 [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3],
67 [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3]
68 ].forEach(function (p) {
69 var hit = document.elementFromPoint(p[0], p[1]);
70 if (!hit || !menu.contains(hit)) {
71 misses.push(p.map(Math.round).join(",") + " " +
72 (hit ? hit.tagName : "nothing"));
73 }
74 });
75 return misses.join("; ");
76})(document.querySelector(%q))`
77
78// openMore opens the ⋯ of the row tagged tag as a mouse does: it comes
79// to rest on the ⋯, which shows it, and clicks.
80func openMore(tag string) chromedp.Action[chromedp.Void] {
81 return chromedp.Steps(hover(more(tag)), chromedp.Click(more(tag)),
82 chromedp.WaitVisible(`[data-row="`+tag+`"] [mapa-row-menu][open]`))
83}
84
85// pointAt moves a hovering mouse to a point.
86func pointAt(across, down float64) chromedp.Action[chromedp.Void] {
87 return chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved,
88 across, down)
89}
90
91// sidebarAt opens path at 1400px with the sidebar open.
92func sidebarAt(rig *harness.Rig, live bool,
93 path string) chromedp.Action[chromedp.Void] {
94 return chromedp.Steps(scripts(live), sideCookie(rig, "open"),
95 chromedp.EmulateViewport(1400, 877),
96 chromedp.Navigate(rig.Origin+path),
97 chromedp.WaitVisible("[mapa-side]"))
98}
99
100func TestARowsMoreShowsOnHoverAndOnFocus(t *testing.T) {
101 rig, store := browserRig(t, 0, harness.WithHover())
102 writeOwnNotes(t, store)
103 for _, live := range []bool{false, true} {
104 drive(t, rig, sidebarAt(rig, live, "/"),
105 markRow("Quarterly plan", "q"), pointAt(900, 400))
106 if moreShown(t, rig, "q") {
107 t.Fatalf("scripts %v: the ⋯ shows with the pointer away", live)
108 }
109 drive(t, rig, hover(`[data-row="q"] [mapa-side-page]`))
110 if !moreShown(t, rig, "q") {
111 t.Fatalf("scripts %v: the ⋯ does not show on hover", live)
112 }
113 drive(t, rig, pointAt(900, 400),
114 chromedp.Focus(`[data-row="q"] [mapa-side-page]`))
115 if !moreShown(t, rig, "q") {
116 t.Fatalf("scripts %v: the ⋯ does not show on focus", live)
117 }
118 drive(t, rig, chromedp.KeyEvent(kb.Tab))
119 if got := evaluate(t, rig, `document.activeElement.getAttribute(
120 "aria-label")`); got != "More actions for Quarterly plan" {
121 t.Fatalf("scripts %v: Tab from the row went to %q", live, got)
122 }
123 }
124 // The rail hides the page rows, and so their ⋯.
125 drive(t, rig, sideCookie(rig, "rail"), chromedp.Navigate(rig.Origin+"/"),
126 chromedp.WaitVisible("[mapa-side]"))
127 if got := evaluate(t, rig, `String(Array.prototype.filter.call(
128 document.querySelectorAll("[mapa-row-more]"),
129 function (el) { return el.checkVisibility(); }).length)`); got != "0" {
130 t.Fatalf("the rail shows %s ⋯", got)
131 }
132}
133
134// On a touch screen there is no hover: the page on screen's row shows
135// its ⋯; another row's end is still its link, so a tap opens the page;
136// and the ⋯ and its row are 44px tap targets.
137func TestARowsMoreOnATouchScreen(t *testing.T) {
138 rig, store := browserRig(t, 0, harness.WithCoarsePointer())
139 notes := writeOwnNotes(t, store)
140 drive(t, rig, sidebarAt(rig, true, notes.pinned),
141 markRow("Pinned plan", "p"), markRow("Quarterly plan", "q"))
142 if evaluate(t, rig, `String(matchMedia("(pointer: coarse)").matches)`) !=
143 "true" {
144 t.Fatal("the pointer is not coarse")
145 }
146 if !moreShown(t, rig, "p") || moreShown(t, rig, "q") {
147 t.Fatalf("on a touch screen the ⋯ shows on the page's row %v, "+
148 "on another %v", moreShown(t, rig, "p"), moreShown(t, rig, "q"))
149 }
150 sizes := evaluate(t, rig, `(function () {
151 var more = document.querySelector('[data-row="p"] [mapa-row-more]');
152 var link = document.querySelector('[data-row="q"] [mapa-side-page]');
153 var a = more.getBoundingClientRect(), b = link.getBoundingClientRect();
154 return [a.width, a.height, b.height].map(Math.round).join("x");
155})()`)
156 if sizes != "44x44x44" {
157 t.Fatalf("the ⋯ and a row are %s, want 44x44x44", sizes)
158 }
159 hit := evaluate(t, rig, `(function () {
160 var r = document.querySelector('[data-row="q"] [mapa-row-more]')
161 .getBoundingClientRect();
162 var at = document.elementFromPoint(r.left + r.width / 2,
163 r.top + r.height / 2);
164 if (at && at.closest("a[mapa-side-page]")) return "link";
165 return String(at && at.tagName);
166})()`)
167 if hit != "link" {
168 t.Fatalf("a tap on another row's end lands on %s, not its link", hit)
169 }
170 drive(t, rig, press(more("p")), chromedp.WaitVisible(panel("p")))
171 short := evaluate(t, rig, `Array.prototype.filter.call(
172 document.querySelectorAll('[data-row="p"] [estilo-menu-item]'),
173 function (el) { return el.getBoundingClientRect().height < 44; })
174 .map(function (el) { return el.textContent.trim(); }).join(", ")`)
175 if short != "" {
176 t.Fatalf("on a touch screen these rows are under 44px: %s", short)
177 }
178}
179
180// menuOf is the open or closed menu of the row tagged tag: each row's
181// label and its address, a divider as "—".
182func menuOf(t *testing.T, rig *harness.Rig, tag string) string {
183 t.Helper()
184 return evaluate(t, rig, fmt.Sprintf(`(function () {
185 var menu = document.querySelector(
186 '[data-row=%q] [mapa-row-menu] [estilo-menu]');
187 var page = document.querySelector('[data-row=%q] [mapa-side-page]')
188 .getAttribute("href");
189 var rows = menu.querySelectorAll("hr, [estilo-menu-item]");
190 return Array.prototype.map.call(rows,
191 function (el) {
192 if (el.tagName === "HR") return "—";
193 var to = el.getAttribute("href") || el.form.getAttribute("action");
194 return el.textContent.trim() + " " + to.replace(page, "~");
195 }).join(" | ");
196})()`, tag, tag))
197}
198
199func TestARowsMenuIsThePagesVerbs(t *testing.T) {
200 rig, store := browserRig(t, 0)
201 writeOwnNotes(t, store)
202 drive(t, rig, sidebarAt(rig, false, "/"),
203 markRow("Quarterly plan", "q"), markRow("Pinned plan", "p"),
204 markRow("Pinned note", "n"))
205 cases := map[string]string{
206 "q": "Rename… ~/rename | History ~/history | Pin to sidebar ~/pin" +
207 " | — | Delete… ~/delete",
208 "p": "Rename… ~/rename | History ~/history | Unpin from sidebar " +
209 "~/pin | — | Delete… ~/delete",
210 "n": "Make team page… ~/share | History ~/history | " +
211 "Unpin from sidebar ~/pin | — | Delete… ~/delete",
212 }
213 for tag, want := range cases {
214 if got := menuOf(t, rig, tag); got != want {
215 t.Errorf("row %s: the menu is %q, want %q", tag, got, want)
216 }
217 }
218 danger := evaluate(t, rig, `document.querySelector(
219 '[data-row="q"] [mapa-row-menu] a[href$="/delete"]')
220 .getAttribute("estilo-menu-item")`)
221 if danger != "danger" {
222 t.Errorf("Delete… is %q, not danger", danger)
223 }
224}
225
226// Delete… from a row opens its confirm page; nothing is deleted until
227// that page's button is pressed.
228func TestDeleteFromARowAsksFirst(t *testing.T) {
229 rig, store := browserRig(t, 0, harness.WithHover())
230 notes := writeOwnNotes(t, store)
231 for _, live := range []bool{false, true} {
232 drive(t, rig, sidebarAt(rig, live, "/"),
233 markRow("Quarterly plan", "q"),
234 openMore("q"),
235 chromedp.Click(`[data-row="q"] a[href$="/delete"]`),
236 chromedp.WaitVisible(`main form[action$="/delete"]`))
237 if got := evaluate(t, rig, `location.pathname`); got !=
238 notes.team+"/delete" {
239 t.Fatalf("scripts %v: Delete… went to %s", live, got)
240 }
241 drive(t, rig, chromedp.Navigate(rig.Origin+notes.team),
242 chromedp.WaitVisible("main h1"))
243 if got := evaluate(t, rig, `document.querySelector("main h1")
244 .textContent.trim()`); got != "Quarterly plan" {
245 t.Fatalf("scripts %v: after Delete… the page reads %q", live, got)
246 }
247 }
248}
249
250// With scripts off the ⋯ opens by the mouse, the whole menu is on top
251// even from the sidebar's last row, and Unpin and Pin post and come
252// back to the screen they were sent from.
253func TestARowsMenuWorksWithScriptsOff(t *testing.T) {
254 rig, store := browserRig(t, 0, harness.WithHover())
255 writeOwnNotes(t, store)
256 drive(t, rig, sidebarAt(rig, false, "/pages"),
257 chromedp.Evaluate[chromedp.Void](`(function (rows) {
258 rows[rows.length - 1].setAttribute("data-row", "last");
259 })(document.querySelectorAll("[mapa-side-row]"))`),
260 openMore("last"),
261 chromedp.WaitVisible(panel("last")))
262 if misses := evaluate(t, rig, fmt.Sprintf(cornersOf,
263 panel("last"))); misses != "" {
264 t.Fatalf("the last row's menu is covered or cut off at %s", misses)
265 }
266 drive(t, rig, markRow("Pinned plan", "p"), openMore("p"),
267 chromedp.Click(`[data-row="p"] button[estilo-menu-item]`),
268 chromedp.WaitNotPresent(`[data-row="p"]`),
269 chromedp.WaitVisible("[mapa-side]"))
270 pinned := `(function (head) {
271 var out = [];
272 var el = head && head.nextElementSibling;
273 for (; el && el.matches("[mapa-side-row]"); el = el.nextElementSibling) {
274 out.push(el.querySelector("[mapa-side-label]").textContent.trim());
275 }
276 return location.pathname + ": " + out.join(" | ");
277})(Array.prototype.find.call(document.querySelectorAll("[mapa-side-head]"),
278 function (h) { return h.textContent === "Pinned"; }))`
279 if got := evaluate(t, rig, pinned); got !=
280 "/pages: Pinned note, only you see this" {
281 t.Fatalf("after Unpin: %q", got)
282 }
283 drive(t, rig, markRow("Quarterly plan", "q"), openMore("q"),
284 chromedp.Click(`[data-row="q"] button[estilo-menu-item]`),
285 chromedp.WaitNotPresent(`[data-row="q"]`),
286 chromedp.WaitVisible("[mapa-side]"))
287 if got := evaluate(t, rig, pinned); got !=
288 "/pages: Quarterly plan | Pinned note, only you see this" {
289 t.Fatalf("after Pin: %q", got)
290 }
291}
292
293// The open menu is on top of the page it hangs across: at 900px with
294// the sidebar open, the editor's text starts under it.
295func TestARowsMenuIsOverTheEditor(t *testing.T) {
296 rig, store := browserRig(t, 0, harness.WithHover())
297 notes := writeOwnNotes(t, store)
298 drive(t, rig, scripts(true), sideCookie(rig, "open"),
299 chromedp.EmulateViewport(900, 877),
300 chromedp.Navigate(rig.Origin+notes.team),
301 chromedp.WaitVisible(mounted), markRow("Pinned plan", "p"),
302 openMore("p"))
303 misses := evaluate(t, rig, fmt.Sprintf(cornersOf, panel("p")))
304 if misses != "" {
305 t.Fatalf("the menu is under the page at %s", misses)
306 }
307}
308
309// From the keyboard: Enter on the ⋯ opens it with focus on its first
310// row, ↓ moves, and Esc closes it with focus back on the ⋯.
311func TestARowsMenuTakesFocusAndGivesItBack(t *testing.T) {
312 rig, store := browserRig(t, 0)
313 writeOwnNotes(t, store)
314 focused := `(function (el) {
315 return (el.getAttribute("aria-label") || el.textContent).trim();
316})(document.activeElement)`
317 drive(t, rig, sidebarAt(rig, true, "/"), markRow("Quarterly plan", "q"),
318 press(more("q")), chromedp.WaitVisible(panel("q")))
319 eventually(t, "focus on the menu's first row", func() bool {
320 return evaluate(t, rig, focused) == "Rename…"
321 })
322 drive(t, rig, chromedp.KeyEvent(kb.ArrowDown))
323 if got := evaluate(t, rig, focused); got != "History" {
324 t.Fatalf("↓ focused %q", got)
325 }
326 drive(t, rig, chromedp.KeyEvent(kb.ArrowUp),
327 chromedp.KeyEvent(kb.ArrowUp))
328 if got := evaluate(t, rig, focused); got != "Delete…" {
329 t.Fatalf("↑ from the first row focused %q, not the last", got)
330 }
331 drive(t, rig, chromedp.KeyEvent(kb.Tab))
332 if evaluate(t, rig, `String(document.querySelector(
333 '[data-row="q"] [mapa-row-menu]').open)`) != "false" {
334 t.Fatal("Tab out of the menu left it open")
335 }
336 drive(t, rig, press(more("q")), chromedp.WaitVisible(panel("q")))
337 eventually(t, "focus back in the menu", func() bool {
338 return evaluate(t, rig, focused) == "Rename…"
339 })
340 drive(t, rig, chromedp.KeyEvent(kb.Escape))
341 if got := evaluate(t, rig, focused); got !=
342 "More actions for Quarterly plan" {
343 t.Fatalf("Esc left focus on %q", got)
344 }
345 if evaluate(t, rig, `String(document.querySelector(
346 '[data-row="q"] [mapa-row-menu]').open)`) != "false" {
347 t.Fatal("Esc left the menu open")
348 }
349}
350
351func TestARowsMenuPassesAxe(t *testing.T) {
352 for _, touch := range []bool{false, true} {
353 option := harness.WithHover()
354 if touch {
355 option = harness.WithCoarsePointer()
356 }
357 rig, store := browserRig(t, 0, option)
358 notes := writeOwnNotes(t, store)
359 drive(t, rig, sidebarAt(rig, true, notes.pinned),
360 markRow("Quarterly plan", "q"))
361 if found := axeSerious(t, rig); found != "" {
362 t.Fatalf("touch %v, closed:\n%s", touch, found)
363 }
364 drive(t, rig, press(more("q")),
365 chromedp.WaitVisible(panel("q")))
366 if found := axeSerious(t, rig); found != "" {
367 t.Fatalf("touch %v, open:\n%s", touch, found)
368 }
369 moreShot(t, rig, fmt.Sprintf("open-touch-%v", touch))
370 }
371}
372
373// Showing the ⋯ on hover or focus moves no row, no label and no icon,
374// and a long title ends before it.
375func TestShowingARowsMoreShiftsNothing(t *testing.T) {
376 rig, store := browserRig(t, 0, harness.WithHover())
377 if _, err := store.CreateTeamPage(context.Background(), Ana,
378 "A very long page title that cannot fit in the sidebar",
379 "Text."); err != nil {
380 t.Fatal(err)
381 }
382 writeOwnNotes(t, store)
383 boxes := `Array.prototype.map.call(document.querySelectorAll(
384 "[mapa-side] > *, [mapa-side-row] > a, [mapa-side-row] svg, " +
385 "[mapa-side-label]"),
386 function (el) {
387 var r = el.getBoundingClientRect();
388 return [r.left, r.top, r.width, r.height].join(",");
389 }).join(" ")`
390 for _, live := range []bool{false, true} {
391 drive(t, rig, sidebarAt(rig, live, "/"), pointAt(900, 400),
392 markRow("A very long", "long"))
393 before := evaluate(t, rig, boxes)
394 moreShot(t, rig, fmt.Sprintf("rest-scripts-%v", live))
395 drive(t, rig, hover(`[data-row="long"] [mapa-side-page]`))
396 if !moreShown(t, rig, "long") {
397 t.Fatalf("scripts %v: hovering did not show the ⋯", live)
398 }
399 moreShot(t, rig, fmt.Sprintf("hover-scripts-%v", live))
400 if after := evaluate(t, rig, boxes); after != before {
401 t.Fatalf("scripts %v: hovering moved the sidebar:\n%s\n%s",
402 live, before, after)
403 }
404 drive(t, rig, pointAt(900, 400),
405 chromedp.Focus(`[data-row="long"] [mapa-side-page]`))
406 if after := evaluate(t, rig, boxes); after != before {
407 t.Fatalf("scripts %v: focus moved the sidebar", live)
408 }
409 clear := evaluate(t, rig, `(function (row) {
410 var label = row.querySelector("[mapa-side-label]").getBoundingClientRect();
411 var more = row.querySelector("[mapa-row-more]").getBoundingClientRect();
412 return String(label.right <= more.left);
413})(document.querySelector('[data-row="long"]'))`)
414 if clear != "true" {
415 t.Fatalf("scripts %v: the long title runs under the ⋯", live)
416 }
417 }
418}
419
420// moreShot saves the window to MAPA_MORE_SHOTS, when it is set, for a
421// look by eye.
422func moreShot(t *testing.T, rig *harness.Rig, name string) {
423 dir := os.Getenv("MAPA_MORE_SHOTS")
424 if dir == "" {
425 return
426 }
427 picture := read(t, rig, chromedp.CaptureScreenshot())
428 if err := os.WriteFile(filepath.Join(dir, name+".png"), picture,
429 0o600); err != nil {
430 t.Fatal(err)
431 }
432}
433