| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo/harness" |
| 11 | "github.com/chromedp/cdproto/emulation" |
| 12 | "github.com/chromedp/cdproto/input" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa" |
| 16 | ) |
| 17 | |
| 18 | // The ALT badge (build note I22): filled when the image is described, |
| 19 | // outlined and always shown when it is not, named for what it does, |
| 20 | // hidden in print; the small image's popover; the phone; and axe. |
| 21 | |
| 22 | // badges is each badge's state, name and whether it shows, in order. |
| 23 | const badges = `Array.prototype.map.call( |
| 24 | document.querySelectorAll('article[mapa-body] [mapa-image-frame]'), |
| 25 | function (frame) { |
| 26 | var badge = frame.querySelector('button[mapa-alt-badge]'); |
| 27 | if (!badge) return 'none'; |
| 28 | var style = getComputedStyle(badge); |
| 29 | return [badge.getAttribute('mapa-alt-badge'), |
| 30 | badge.getAttribute('aria-label'), |
| 31 | style.display !== 'none' && style.opacity === '1' ? 'shown' : 'hidden', |
| 32 | badge.tabIndex].join(' | '); |
| 33 | }).join('\n')` |
| 34 | |
| 35 | // described is a page holding a described image and one with no |
| 36 | // description, opened in Ana's editor. |
| 37 | func described(t *testing.T, rig *harness.Rig, |
| 38 | store *mapa.Store) mapa.Page { |
| 39 | t.Helper() |
| 40 | barn, bare := addImage(t, store, Bo, 600, 300), addImage(t, store, Bo, |
| 41 | 600, 300) |
| 42 | return teamPage(t, rig, store, "Pictures:\n\n"+ |
| 43 | barn.Markdown("A red barn in the snow at dusk, with crows")+ |
| 44 | "\n\n"+bare.Markdown("")+"\n\nThe end.") |
| 45 | } |
| 46 | |
| 47 | func TestAltBadgeShowsItsState(t *testing.T) { |
| 48 | rig, store := browserRig(t, 0) |
| 49 | described(t, rig, store) |
| 50 | want := "set | Alt text: A red barn in the…, edit | hidden | -1\n" + |
| 51 | "missing | Alt text: describe this image | shown | 0" |
| 52 | if got := read(t, rig, chromedp.Evaluate[string](badges)); got != want { |
| 53 | t.Fatalf("the badges at rest:\n%s", got) |
| 54 | } |
| 55 | if got := read(t, rig, chromedp.Evaluate[string](backdrops)); got != |
| 56 | "filled 0.92 outlined 0.8 rim true" { |
| 57 | t.Fatalf("the badges' backdrops: %s", got) |
| 58 | } |
| 59 | over := box(t, rig, `article[mapa-body] img[src^="/i/"]`) |
| 60 | drive(t, rig, chromedp.MouseEvent(input.DispatchMouseEventTypeMouseMoved, |
| 61 | (over.Left+over.Right)/2, (over.Top+over.Bottom)/2)) |
| 62 | eventually(t, "the hovered badge shows", func() bool { |
| 63 | return strings.HasSuffix(strings.Split(read(t, rig, |
| 64 | chromedp.Evaluate[string](badges)), "\n")[0], "shown | -1") |
| 65 | }) |
| 66 | describeTheBare(t, rig) |
| 67 | printed(t, rig) |
| 68 | } |
| 69 | |
| 70 | // backdrops is each badge's backdrop, by its opacity: solid enough |
| 71 | // that its words read over a white picture or a black one. The words |
| 72 | // are #111 on the light pill and white on the dark one. |
| 73 | const backdrops = `(function (all) { |
| 74 | function alpha(style) { |
| 75 | var parts = style.backgroundColor.match(/[\d.]+/g); |
| 76 | return parts.length > 3 ? parts[3] : '1'; |
| 77 | } |
| 78 | var set = getComputedStyle(all[0]), missing = getComputedStyle(all[1]); |
| 79 | return 'filled ' + alpha(set) + ' outlined ' + alpha(missing) + |
| 80 | ' rim ' + (missing.borderTopColor === 'rgb(255, 255, 255)'); |
| 81 | })(document.querySelectorAll('[mapa-alt-badge]'))` |
| 82 | |
| 83 | // band is the open field's backdrop over the picture: its opacity. |
| 84 | const band = `(function (style) { |
| 85 | var parts = style.backgroundColor.match(/[\d.]+/g); |
| 86 | return parts.length > 3 ? parts[3] : '1'; |
| 87 | })(getComputedStyle(document.querySelector('[mapa-alt="inset"]')))` |
| 88 | |
| 89 | // printed opens the field again from the focused badge, then finds no |
| 90 | // badge and no field in print. |
| 91 | func printed(t *testing.T, rig *harness.Rig) { |
| 92 | t.Helper() |
| 93 | drive(t, rig, chromedp.KeyEvent("\r"), chromedp.WaitVisible(altField), |
| 94 | gesture(emulation.SetEmulatedMedia, |
| 95 | emulation.SetEmulatedMediaParams{Media: "print"})) |
| 96 | if got := read(t, rig, chromedp.Evaluate[string](badges)); strings.Contains( |
| 97 | got, "shown") { |
| 98 | t.Fatalf("a badge shows in print:\n%s", got) |
| 99 | } |
| 100 | // Print hides the field, and a focused field hidden loses focus, |
| 101 | // which closes it: gone is as good as hidden. Reading it as though |
| 102 | // it stayed raced that blur (2 runs in 8 failed on main 31289b3). |
| 103 | if read(t, rig, chromedp.Evaluate[string](`(function (field) { |
| 104 | return field ? getComputedStyle(field).display : "none"; |
| 105 | })(document.querySelector('[mapa-alt]'))`)) != "none" { |
| 106 | t.Fatal("the open field shows in print") |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // describeTheBare clicks the bare image's badge, writes a description, |
| 111 | // and finds the badge filled, named for it and focused again. |
| 112 | func describeTheBare(t *testing.T, rig *harness.Rig) { |
| 113 | t.Helper() |
| 114 | drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), |
| 115 | chromedp.WaitVisible(altField), chromedp.KeyEvent("A blue door")) |
| 116 | if got := read(t, rig, chromedp.Evaluate[string](band)); got < "0.8" { |
| 117 | t.Fatalf("the field's band over the picture is %s opaque", got) |
| 118 | } |
| 119 | if found := axeSerious(t, rig); found != "" { |
| 120 | t.Fatalf("axe, the field open:\n%s", found) |
| 121 | } |
| 122 | drive(t, rig, enter()) |
| 123 | saidAlt(t, rig, "A blue door") |
| 124 | eventually(t, "the badge filled, named and shown", func() bool { |
| 125 | return strings.HasSuffix(read(t, rig, chromedp.Evaluate[string]( |
| 126 | badges)), "set | Alt text: A blue door, edit | shown | -1") |
| 127 | }) |
| 128 | focused := read(t, rig, chromedp.Evaluate[string]( |
| 129 | `document.activeElement.getAttribute('aria-label') || ''`)) |
| 130 | if focused != "Alt text: A blue door, edit" { |
| 131 | t.Fatalf("focus went back to %q, not the badge", focused) |
| 132 | } |
| 133 | if found := axeSerious(t, rig); found != "" { |
| 134 | t.Fatalf("axe, a page with badges:\n%s", found) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // small says where a small image's field opened: a popover under the |
| 139 | // image, its field what a point over it hits, within the screen. |
| 140 | const small = `(function () { |
| 141 | var field = document.querySelector('[mapa-alt] :is(input, textarea)'); |
| 142 | var wrap = field.closest('[mapa-alt]'); |
| 143 | var pic = document.querySelector('article[mapa-body] img[src^="/i/"]') |
| 144 | .getBoundingClientRect(); |
| 145 | var box = wrap.getBoundingClientRect(), at = field.getBoundingClientRect(); |
| 146 | var hit = document.elementFromPoint(at.left + at.width / 2, |
| 147 | at.top + at.height / 2); |
| 148 | return JSON.stringify({ |
| 149 | kind: wrap.getAttribute('mapa-alt'), |
| 150 | framed: Boolean(wrap.closest('[mapa-image-frame]')), |
| 151 | under: box.top >= pic.bottom, |
| 152 | hit: hit === field, |
| 153 | onScreen: box.left >= 0 && |
| 154 | box.right <= document.documentElement.clientWidth |
| 155 | }); |
| 156 | })()` |
| 157 | |
| 158 | func TestAltOnASmallImageIsAPopover(t *testing.T) { |
| 159 | rig, store := browserRig(t, 0) |
| 160 | page := teamPage(t, rig, store, "Icon:\n\nThe text below.\n\nMore.") |
| 161 | upload(t, rig, page, 100, 60) |
| 162 | open := read(t, rig, chromedp.Evaluate[string](boxes)) |
| 163 | want := `{"kind":"popover","framed":true,"under":true,"hit":true,` + |
| 164 | `"onScreen":true}` |
| 165 | if got := read(t, rig, chromedp.Evaluate[string](small)); got != want { |
| 166 | t.Fatalf("a small image's field: %s", got) |
| 167 | } |
| 168 | drive(t, rig, chromedp.KeyEvent("An icon"), enter()) |
| 169 | saidAlt(t, rig, "An icon") |
| 170 | if closed := read(t, rig, chromedp.Evaluate[string](boxes)); closed != |
| 171 | open { |
| 172 | t.Fatalf("the blocks moved with the popover open:\n%s\n%s", open, |
| 173 | closed) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // On a phone (390px, a coarse pointer): a tap on the badge opens the |
| 178 | // field over the image with focus in it, so the keyboard comes up; the |
| 179 | // badge is a 32px target and the field's text 16px, so iOS does not |
| 180 | // zoom. |
| 181 | func TestAltOnAPhone(t *testing.T) { |
| 182 | rig, store := browserRig(t, 0, harness.WithCoarsePointer()) |
| 183 | bare := addImage(t, store, Bo, 900, 500) |
| 184 | page, err := store.CreateTeamPage(context.Background(), Bo, "Phone", |
| 185 | "Photo:\n\n"+bare.Markdown("")+"\n\nBelow.") |
| 186 | if err != nil { |
| 187 | t.Fatal(err) |
| 188 | } |
| 189 | drive(t, rig, scripts(true), |
| 190 | chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), |
| 191 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)), |
| 192 | chromedp.WaitVisible(mounted), |
| 193 | chromedp.WaitVisible(`[mapa-alt-badge="missing"]`)) |
| 194 | target := box(t, rig, `[mapa-alt-badge]`) |
| 195 | if target.Bottom-target.Top < 32 { |
| 196 | t.Fatalf("the badge is %vpx tall on a phone", target.Bottom-target.Top) |
| 197 | } |
| 198 | drive(t, rig, gesture(input.SynthesizeTapGesture, |
| 199 | input.SynthesizeTapGestureParams{X: (target.Left + target.Right) / 2, |
| 200 | Y: (target.Top + target.Bottom) / 2, |
| 201 | GestureSourceType: input.GestureSourceTypeTouch}), |
| 202 | chromedp.WaitVisible(altField)) |
| 203 | if got := read(t, rig, chromedp.Evaluate[string](inset)); got != |
| 204 | insetWanted { |
| 205 | t.Fatalf("the field after a tap: %s", got) |
| 206 | } |
| 207 | size := read(t, rig, chromedp.Evaluate[float64](`parseFloat( |
| 208 | getComputedStyle(document.activeElement).fontSize)`)) |
| 209 | if size < 16 { |
| 210 | t.Fatalf("the field's text is %vpx on a phone", size) |
| 211 | } |
| 212 | drive(t, rig, chromedp.KeyEvent("A harbour"), enter()) |
| 213 | saidAlt(t, rig, "A harbour") |
| 214 | } |
| 215 | |
| 216 | // pushRight types words before the page's image until it sits at the |
| 217 | // right edge of the column, and answers whether it got there. |
| 218 | const pushRight = `(function () { |
| 219 | var view = mapaEditor.view, frame, room = |
| 220 | document.documentElement.clientWidth; |
| 221 | for (var tries = 0; tries < 400; tries++) { |
| 222 | frame = document.querySelector('[mapa-image-frame]'); |
| 223 | if (frame.getBoundingClientRect().right > room - 40) return true; |
| 224 | var found = -1; |
| 225 | view.state.doc.descendants(function (node, pos) { |
| 226 | if (found < 0 && node.type.name === "image") found = pos; |
| 227 | }); |
| 228 | view.dispatch(view.state.tr.insertText("w ", found)); |
| 229 | } |
| 230 | return false; |
| 231 | })()` |
| 232 | |
| 233 | // On a 390px phone, a small image at the column's right edge opens its |
| 234 | // popover on the screen, not past its edge. |
| 235 | func TestAltPopoverStaysOnAPhoneScreen(t *testing.T) { |
| 236 | rig, store := browserRig(t, 0, harness.WithCoarsePointer()) |
| 237 | icon := addImage(t, store, Bo, 100, 60) |
| 238 | page, err := store.CreateTeamPage(context.Background(), Bo, "Icons", |
| 239 | "Icon "+icon.Markdown("")+" here.\n\nBelow.") |
| 240 | if err != nil { |
| 241 | t.Fatal(err) |
| 242 | } |
| 243 | drive(t, rig, scripts(true), |
| 244 | chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile), |
| 245 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)), |
| 246 | chromedp.WaitVisible(mounted)) |
| 247 | if !read(t, rig, chromedp.Evaluate[bool](pushRight)) { |
| 248 | t.Fatal("the image never reached the right edge") |
| 249 | } |
| 250 | drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), |
| 251 | chromedp.WaitVisible(altField)) |
| 252 | want := `{"kind":"popover","framed":true,"under":true,"hit":true,` + |
| 253 | `"onScreen":true}` |
| 254 | if got := read(t, rig, chromedp.Evaluate[string](small)); got != want { |
| 255 | t.Fatalf("a small image's field at the right edge: %s", got) |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | // The badge pressed again closes its field, the popover here, and |
| 260 | // keeps focus on it (Fable L8). |
| 261 | func TestAltBadgePressedAgainCloses(t *testing.T) { |
| 262 | rig, store := browserRig(t, 0) |
| 263 | icon := addImage(t, store, Bo, 100, 60) |
| 264 | teamPage(t, rig, store, "Icon "+icon.Markdown("")+" here.") |
| 265 | drive(t, rig, chromedp.Click(`[mapa-alt-badge="missing"]`), |
| 266 | chromedp.WaitVisible(altField), |
| 267 | chromedp.Click(`[mapa-alt-badge="missing"]`)) |
| 268 | eventually(t, "the field closed, the badge focused", func() bool { |
| 269 | return read(t, rig, chromedp.Evaluate[bool]( |
| 270 | `document.querySelector('[mapa-alt]') === null && |
| 271 | document.activeElement.hasAttribute('mapa-alt-badge')`)) |
| 272 | }) |
| 273 | } |
| 274 | |