//go:build browser package mapatest import ( "context" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // The ALT badge (build note I22): filled when the image is described, // outlined and always shown when it is not, named for what it does, // hidden in print; the small image's popover; the phone; and axe. // badges is each badge's state, name and whether it shows, in order. const badges = `Array.prototype.map.call( document.querySelectorAll('article[mapa-body] [mapa-image-frame]'), function (frame) { var badge = frame.querySelector('button[mapa-alt-badge]'); if (!badge) return 'none'; var style = getComputedStyle(badge); return [badge.getAttribute('mapa-alt-badge'), badge.getAttribute('aria-label'), style.display !== 'none' && style.opacity === '1' ? 'shown' : 'hidden', badge.tabIndex].join(' | '); }).join('\n')` // described is a page holding a described image and one with no // description, opened in Ana's editor. func described(t *testing.T, rig *harness.Rig, store *mapa.Store) mapa.Page { t.Helper() barn, bare := addImage(t, store, Bo, 600, 300), addImage(t, store, Bo, 600, 300) return teamPage(t, rig, store, "Pictures:\n\n"+ barn.Markdown("A red barn in the snow at dusk, with crows")+ "\n\n"+bare.Markdown("")+"\n\nThe end.") } func TestAltBadgeShowsItsState(t *testing.T) { rig, store := browserRig(t, 0) described(t, rig, store) want := "set | Alt text: A red barn in the…, edit | hidden | -1\n" + "missing | Alt text: describe this image | shown | 0" if got := read(t, rig, chromedp.Evaluate[string](badges)); got != want { t.Fatalf("the badges at rest:\n%s", got) } if got := read(t, rig, chromedp.Evaluate[string](backdrops)); got != "filled 0.92 outlined 0.8 rim true" { t.Fatalf("the badges' backdrops: %s", got) } over := box(t, rig, `article[mapa-body] img[src^="/i/"]`) drive(t, rig, chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, (over.Left+over.Right)/2, (over.Top+over.Bottom)/2)) eventually(t, "the hovered badge shows", func() bool { return strings.HasSuffix(strings.Split(read(t, rig, chromedp.Evaluate[string](badges)), "\n")[0], "shown | -1") }) describeTheBare(t, rig) printed(t, rig) } // backdrops is each badge's backdrop, by its opacity: solid enough // that its words read over a white picture or a black one. The words // are #111 on the light pill and white on the dark one. const backdrops = `(function (all) { function alpha(style) { var parts = style.backgroundColor.match(/[\d.]+/g); return parts.length > 3 ? parts[3] : '1'; } var set = getComputedStyle(all[0]), missing = getComputedStyle(all[1]); return 'filled ' + alpha(set) + ' outlined ' + alpha(missing) + ' rim ' + (missing.borderTopColor === 'rgb(255, 255, 255)'); })(document.querySelectorAll('[mapa-alt-badge]'))` // band is the open field's backdrop over the picture: its opacity. const band = `(function (style) { var parts = style.backgroundColor.match(/[\d.]+/g); return parts.length > 3 ? parts[3] : '1'; })(getComputedStyle(document.querySelector('[mapa-alt="inset"]')))` // printed opens the field again from the focused badge, then finds no // badge and no field in print. func printed(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.KeyEvent("\r"), chromedp.WaitVisible(altField), gesture(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Media: "print"})) if got := read(t, rig, chromedp.Evaluate[string](badges)); strings.Contains( got, "shown") { t.Fatalf("a badge shows in print:\n%s", got) } // Print hides the field, and a focused field hidden loses focus, // which closes it: gone is as good as hidden. Reading it as though // it stayed raced that blur (2 runs in 8 failed on main 31289b3). if read(t, rig, chromedp.Evaluate[string](`(function (field) { return field ? getComputedStyle(field).display : "none"; })(document.querySelector('[mapa-alt]'))`)) != "none" { t.Fatal("the open field shows in print") } } // describeTheBare clicks the bare image's badge, writes a description, // and finds the badge filled, named for it and focused again. func describeTheBare(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), chromedp.WaitVisible(altField), chromedp.KeyEvent("A blue door")) if got := read(t, rig, chromedp.Evaluate[string](band)); got < "0.8" { t.Fatalf("the field's band over the picture is %s opaque", got) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, the field open:\n%s", found) } drive(t, rig, enter()) saidAlt(t, rig, "A blue door") eventually(t, "the badge filled, named and shown", func() bool { return strings.HasSuffix(read(t, rig, chromedp.Evaluate[string]( badges)), "set | Alt text: A blue door, edit | shown | -1") }) focused := read(t, rig, chromedp.Evaluate[string]( `document.activeElement.getAttribute('aria-label') || ''`)) if focused != "Alt text: A blue door, edit" { t.Fatalf("focus went back to %q, not the badge", focused) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a page with badges:\n%s", found) } } // small says where a small image's field opened: a popover under the // image, its field what a point over it hits, within the screen. const small = `(function () { var field = document.querySelector('[mapa-alt] :is(input, textarea)'); var wrap = field.closest('[mapa-alt]'); var pic = document.querySelector('article[mapa-body] img[src^="/i/"]') .getBoundingClientRect(); var box = wrap.getBoundingClientRect(), at = field.getBoundingClientRect(); var hit = document.elementFromPoint(at.left + at.width / 2, at.top + at.height / 2); return JSON.stringify({ kind: wrap.getAttribute('mapa-alt'), framed: Boolean(wrap.closest('[mapa-image-frame]')), under: box.top >= pic.bottom, hit: hit === field, onScreen: box.left >= 0 && box.right <= document.documentElement.clientWidth }); })()` func TestAltOnASmallImageIsAPopover(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Icon:\n\nThe text below.\n\nMore.") upload(t, rig, page, 100, 60) open := read(t, rig, chromedp.Evaluate[string](boxes)) want := `{"kind":"popover","framed":true,"under":true,"hit":true,` + `"onScreen":true}` if got := read(t, rig, chromedp.Evaluate[string](small)); got != want { t.Fatalf("a small image's field: %s", got) } drive(t, rig, chromedp.KeyEvent("An icon"), enter()) saidAlt(t, rig, "An icon") if closed := read(t, rig, chromedp.Evaluate[string](boxes)); closed != open { t.Fatalf("the blocks moved with the popover open:\n%s\n%s", open, closed) } } // On a phone (390px, a coarse pointer): a tap on the badge opens the // field over the image with focus in it, so the keyboard comes up; the // badge is a 32px target and the field's text 16px, so iOS does not // zoom. func TestAltOnAPhone(t *testing.T) { rig, store := browserRig(t, 0, harness.WithCoarsePointer()) bare := addImage(t, store, Bo, 900, 500) page, err := store.CreateTeamPage(context.Background(), Bo, "Phone", "Photo:\n\n"+bare.Markdown("")+"\n\nBelow.") if err != nil { t.Fatal(err) } drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), chromedp.WaitVisible(`[mapa-alt-badge="missing"]`)) target := box(t, rig, `[mapa-alt-badge]`) if target.Bottom-target.Top < 32 { t.Fatalf("the badge is %vpx tall on a phone", target.Bottom-target.Top) } drive(t, rig, gesture(input.SynthesizeTapGesture, input.SynthesizeTapGestureParams{X: (target.Left + target.Right) / 2, Y: (target.Top + target.Bottom) / 2, GestureSourceType: input.GestureSourceTypeTouch}), chromedp.WaitVisible(altField)) if got := read(t, rig, chromedp.Evaluate[string](inset)); got != insetWanted { t.Fatalf("the field after a tap: %s", got) } size := read(t, rig, chromedp.Evaluate[float64](`parseFloat( getComputedStyle(document.activeElement).fontSize)`)) if size < 16 { t.Fatalf("the field's text is %vpx on a phone", size) } drive(t, rig, chromedp.KeyEvent("A harbour"), enter()) saidAlt(t, rig, "A harbour") } // pushRight types words before the page's image until it sits at the // right edge of the column, and answers whether it got there. const pushRight = `(function () { var view = mapaEditor.view, frame, room = document.documentElement.clientWidth; for (var tries = 0; tries < 400; tries++) { frame = document.querySelector('[mapa-image-frame]'); if (frame.getBoundingClientRect().right > room - 40) return true; var found = -1; view.state.doc.descendants(function (node, pos) { if (found < 0 && node.type.name === "image") found = pos; }); view.dispatch(view.state.tr.insertText("w ", found)); } return false; })()` // On a 390px phone, a small image at the column's right edge opens its // popover on the screen, not past its edge. func TestAltPopoverStaysOnAPhoneScreen(t *testing.T) { rig, store := browserRig(t, 0, harness.WithCoarsePointer()) icon := addImage(t, store, Bo, 100, 60) page, err := store.CreateTeamPage(context.Background(), Bo, "Icons", "Icon "+icon.Markdown("")+" here.\n\nBelow.") if err != nil { t.Fatal(err) } drive(t, rig, scripts(true), chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted)) if !read(t, rig, chromedp.Evaluate[bool](pushRight)) { t.Fatal("the image never reached the right edge") } drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), chromedp.WaitVisible(altField)) want := `{"kind":"popover","framed":true,"under":true,"hit":true,` + `"onScreen":true}` if got := read(t, rig, chromedp.Evaluate[string](small)); got != want { t.Fatalf("a small image's field at the right edge: %s", got) } } // The badge pressed again closes its field, the popover here, and // keeps focus on it (Fable L8). func TestAltBadgePressedAgainCloses(t *testing.T) { rig, store := browserRig(t, 0) icon := addImage(t, store, Bo, 100, 60) teamPage(t, rig, store, "Icon "+icon.Markdown("")+" here.") drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), chromedp.WaitVisible(altField), chromedp.Click(`[mapa-alt-badge="missing"]`)) eventually(t, "the field closed, the badge focused", func() bool { return read(t, rig, chromedp.Evaluate[bool]( `document.querySelector('[mapa-alt]') === null && document.activeElement.hasAttribute('mapa-alt-badge')`)) }) }