| 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/cdp" |
| 12 | "github.com/chromedp/cdproto/input" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | "github.com/chromedp/chromedp/kb" |
| 15 | |
| 16 | "amadan.net/oficina/mapa/internal/mapa" |
| 17 | ) |
| 18 | |
| 19 | // An image's description, asked for on the image itself (images spec |
| 20 | // §4.3, build note I22): the field lies over the image's bottom edge, |
| 21 | // inside its box, and nothing below it moves; Enter and leaving keep |
| 22 | // the text, Esc puts back what was there, and focus goes back where it |
| 23 | // came from. |
| 24 | |
| 25 | // altField is the description field, whatever its kind. |
| 26 | const altField = `[mapa-alt] :is(input, textarea)` |
| 27 | |
| 28 | // inset says where the open field is: inside the picture's box, what a |
| 29 | // point over the picture's bottom edge hits, whether it has focus, and |
| 30 | // whether any part of it lies outside the image's frame. |
| 31 | const inset = `(function () { |
| 32 | var field = document.querySelector('[mapa-alt] :is(input, textarea)'); |
| 33 | var img = document.querySelector('article[mapa-body] img[src^="/i/"]'); |
| 34 | var pic = img.getBoundingClientRect(); |
| 35 | var box = field.getBoundingClientRect(); |
| 36 | var across = pic.left + pic.width / 2; |
| 37 | var hit = document.elementFromPoint(across, box.top + box.height / 2); |
| 38 | var edge = document.elementFromPoint(across, pic.bottom - 2); |
| 39 | var outside = Array.prototype.some.call( |
| 40 | document.querySelectorAll('[mapa-alt]'), function (wrap) { |
| 41 | return !wrap.closest('[mapa-image-frame]'); |
| 42 | }); |
| 43 | return JSON.stringify({ |
| 44 | inside: box.top >= pic.top && box.bottom <= pic.bottom && |
| 45 | box.left >= pic.left && box.right <= pic.right, |
| 46 | hit: hit === field, |
| 47 | edge: Boolean(edge && edge.closest('[mapa-alt]')), |
| 48 | focused: document.activeElement === field, |
| 49 | outside: outside |
| 50 | }); |
| 51 | })()` |
| 52 | |
| 53 | const insetWanted = `{"inside":true,"hit":true,"edge":true,` + |
| 54 | `"focused":true,"outside":false}` |
| 55 | |
| 56 | // upload opens page and puts a width by height picture in at the end of |
| 57 | // its first block with Insert → Image…, the system's picker stubbed. |
| 58 | func upload(t *testing.T, rig *harness.Rig, page mapa.Page, width, |
| 59 | height int) { |
| 60 | t.Helper() |
| 61 | drive(t, rig, onNewDocument(filePicker), |
| 62 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)), |
| 63 | chromedp.WaitVisible(mounted), caretAt(0), |
| 64 | press(`#mapa-menu-insert summary`), |
| 65 | chromedp.WaitVisible(`[data-mapa-command="insert-image"]`), |
| 66 | press(`[data-mapa-command="insert-image"]`), |
| 67 | choose(pictureFile(t, width, height)), |
| 68 | chromedp.WaitVisible(altField)) |
| 69 | } |
| 70 | |
| 71 | func TestAltFieldLiesInsideTheImage(t *testing.T) { |
| 72 | rig, store := browserRig(t, 0) |
| 73 | page := teamPage(t, rig, store, "Pictures:\n\nThe text below.\n\nMore.") |
| 74 | upload(t, rig, page, 600, 300) |
| 75 | if got := read(t, rig, chromedp.Evaluate[string](inset)); got != |
| 76 | insetWanted { |
| 77 | t.Fatalf("the field after an upload: %s", got) |
| 78 | } |
| 79 | open := read(t, rig, chromedp.Evaluate[string](boxes)) |
| 80 | drive(t, rig, chromedp.KeyEvent(kb.Escape)) |
| 81 | eventually(t, "the field closed", func() bool { |
| 82 | return read(t, rig, chromedp.Evaluate[bool]( |
| 83 | `document.querySelector('[mapa-alt]') === null`)) |
| 84 | }) |
| 85 | if closed := read(t, rig, chromedp.Evaluate[string](boxes)); closed != |
| 86 | open { |
| 87 | t.Fatalf("the blocks moved when the field closed:\n%s\n%s", open, |
| 88 | closed) |
| 89 | } |
| 90 | // Open again from the bubble: inside the image again, and still |
| 91 | // nothing below it moved. |
| 92 | drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), |
| 93 | chromedp.WaitVisible(`[mapa-image-bubble]`), |
| 94 | chromedp.Click(`[mapa-image-bubble] button`), |
| 95 | chromedp.WaitVisible(altField)) |
| 96 | if got := read(t, rig, chromedp.Evaluate[string](inset)); got != |
| 97 | insetWanted { |
| 98 | t.Fatalf("the field from the bubble: %s", got) |
| 99 | } |
| 100 | if again := read(t, rig, chromedp.Evaluate[string](boxes)); again != |
| 101 | open { |
| 102 | t.Fatalf("the blocks moved when the field opened:\n%s\n%s", open, |
| 103 | again) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Enter keeps the text; Shift+Enter does too, and writes no line |
| 108 | // break; Esc puts back what was there and gives the image back its |
| 109 | // selection; leaving the field keeps the text. |
| 110 | func TestAltEnterKeepsEscRestoresLeavingKeeps(t *testing.T) { |
| 111 | rig, store := browserRig(t, 0) |
| 112 | page := teamPage(t, rig, store, "Pictures:\n\nThe text below.") |
| 113 | upload(t, rig, page, 600, 300) |
| 114 | drive(t, rig, chromedp.KeyEvent("A red barn"), enter()) |
| 115 | saidAlt(t, rig, "A red barn") |
| 116 | eventually(t, "the saved description said", func() bool { |
| 117 | return text(t, rig, "[mapa-live]") == "Image: A red barn" |
| 118 | }) |
| 119 | reopen(t, rig) |
| 120 | drive(t, rig, chromedp.KeyEvent(" in snow"), |
| 121 | keyDown("Enter", 13, 8)) |
| 122 | saidAlt(t, rig, "A red barn in snow") |
| 123 | reopen(t, rig) |
| 124 | drive(t, rig, chromedp.KeyEvent(" WRONG"), chromedp.KeyEvent(kb.Escape)) |
| 125 | saidAlt(t, rig, "A red barn in snow") |
| 126 | if got := read(t, rig, chromedp.Evaluate[string]( |
| 127 | `mapaEditor.view.state.selection.node ? `+ |
| 128 | `mapaEditor.view.state.selection.node.type.name : ""`)); got != |
| 129 | "image" || !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { |
| 130 | t.Fatalf("after Esc the image is not selected and focused: %q", got) |
| 131 | } |
| 132 | reopen(t, rig) |
| 133 | drive(t, rig, chromedp.KeyEvent(" today"), keyDown("Tab", 9, 0)) |
| 134 | saidAlt(t, rig, "A red barn in snow today") |
| 135 | reopen(t, rig) |
| 136 | drive(t, rig, chromedp.KeyEvent(", at dusk"), |
| 137 | chromedp.Click(`article[mapa-body] > p:last-child`)) |
| 138 | saidAlt(t, rig, "A red barn in snow today, at dusk") |
| 139 | drive(t, rig, chromedp.KeyEvent("Typed on.")) |
| 140 | if !strings.Contains(typed(t, rig), "Typed on.") { |
| 141 | t.Fatalf("typing after leaving the field: %q", typed(t, rig)) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // reopen selects the image after the caret and opens its field from |
| 146 | // the bubble's Alt text. |
| 147 | func reopen(t *testing.T, rig *harness.Rig) { |
| 148 | t.Helper() |
| 149 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { |
| 150 | var view = mapaEditor.view, found = -1; |
| 151 | view.state.doc.descendants(function (node, pos) { |
| 152 | if (found < 0 && node.type.name === "image") found = pos; |
| 153 | }); |
| 154 | var Selection = view.state.selection.constructor; |
| 155 | view.focus(); |
| 156 | view.dispatch(view.state.tr.setSelection( |
| 157 | Selection.near(view.state.doc.resolve(found + 1)))); |
| 158 | })()`), chromedp.KeyEvent(kb.ArrowLeft), |
| 159 | chromedp.WaitVisible(`[mapa-image-bubble]`), |
| 160 | chromedp.Click(`[mapa-image-bubble] button`), |
| 161 | chromedp.WaitVisible(altField)) |
| 162 | } |
| 163 | |
| 164 | // saidAlt waits for the page's Markdown to hold the image with alt, and |
| 165 | // the field to be gone. |
| 166 | func saidAlt(t *testing.T, rig *harness.Rig, alt string) { |
| 167 | t.Helper() |
| 168 | eventually(t, "the alt "+alt, func() bool { |
| 169 | return strings.Contains(typed(t, rig), " && |
| 170 | read(t, rig, chromedp.Evaluate[bool]( |
| 171 | `document.querySelector('[mapa-alt]') === null`)) |
| 172 | }) |
| 173 | } |
| 174 | |
| 175 | // keyDown is one key as a browser sends it, as enter() is Enter: a |
| 176 | // keyDown the page may cancel, then its keyUp, and no separate char. |
| 177 | func keyDown(key string, code int64, |
| 178 | modifiers int64) chromedp.Action[chromedp.Void] { |
| 179 | return chromedp.Func(func(ctx context.Context, |
| 180 | target *chromedp.Target) error { |
| 181 | press := input.DispatchKeyEventParams{ |
| 182 | Type: input.DispatchKeyEventTypeKeyDown, Key: key, Code: key, |
| 183 | WindowsVirtualKeyCode: code, Modifiers: modifiers} |
| 184 | if key == "Enter" { |
| 185 | press.Text, press.UnmodifiedText = "\r", "\r" |
| 186 | } |
| 187 | if _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, |
| 188 | press); err != nil { |
| 189 | return err |
| 190 | } |
| 191 | press.Type, press.Text, press.UnmodifiedText = |
| 192 | input.DispatchKeyEventTypeKeyUp, "", "" |
| 193 | _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, press) |
| 194 | return err |
| 195 | }) |
| 196 | } |
| 197 | |