//go:build browser package mapatest import ( "context" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // An image's description, asked for on the image itself (images spec // §4.3, build note I22): the field lies over the image's bottom edge, // inside its box, and nothing below it moves; Enter and leaving keep // the text, Esc puts back what was there, and focus goes back where it // came from. // altField is the description field, whatever its kind. const altField = `[mapa-alt] :is(input, textarea)` // inset says where the open field is: inside the picture's box, what a // point over the picture's bottom edge hits, whether it has focus, and // whether any part of it lies outside the image's frame. const inset = `(function () { var field = document.querySelector('[mapa-alt] :is(input, textarea)'); var img = document.querySelector('article[mapa-body] img[src^="/i/"]'); var pic = img.getBoundingClientRect(); var box = field.getBoundingClientRect(); var across = pic.left + pic.width / 2; var hit = document.elementFromPoint(across, box.top + box.height / 2); var edge = document.elementFromPoint(across, pic.bottom - 2); var outside = Array.prototype.some.call( document.querySelectorAll('[mapa-alt]'), function (wrap) { return !wrap.closest('[mapa-image-frame]'); }); return JSON.stringify({ inside: box.top >= pic.top && box.bottom <= pic.bottom && box.left >= pic.left && box.right <= pic.right, hit: hit === field, edge: Boolean(edge && edge.closest('[mapa-alt]')), focused: document.activeElement === field, outside: outside }); })()` const insetWanted = `{"inside":true,"hit":true,"edge":true,` + `"focused":true,"outside":false}` // upload opens page and puts a width by height picture in at the end of // its first block with Insert → Image…, the system's picker stubbed. func upload(t *testing.T, rig *harness.Rig, page mapa.Page, width, height int) { t.Helper() drive(t, rig, onNewDocument(filePicker), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), caretAt(0), press(`#mapa-menu-insert summary`), chromedp.WaitVisible(`[data-mapa-command="insert-image"]`), press(`[data-mapa-command="insert-image"]`), choose(pictureFile(t, width, height)), chromedp.WaitVisible(altField)) } func TestAltFieldLiesInsideTheImage(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Pictures:\n\nThe text below.\n\nMore.") upload(t, rig, page, 600, 300) if got := read(t, rig, chromedp.Evaluate[string](inset)); got != insetWanted { t.Fatalf("the field after an upload: %s", got) } open := read(t, rig, chromedp.Evaluate[string](boxes)) drive(t, rig, chromedp.KeyEvent(kb.Escape)) eventually(t, "the field closed", func() bool { return read(t, rig, chromedp.Evaluate[bool]( `document.querySelector('[mapa-alt]') === null`)) }) if closed := read(t, rig, chromedp.Evaluate[string](boxes)); closed != open { t.Fatalf("the blocks moved when the field closed:\n%s\n%s", open, closed) } // Open again from the bubble: inside the image again, and still // nothing below it moved. drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), chromedp.WaitVisible(`[mapa-image-bubble]`), chromedp.Click(`[mapa-image-bubble] button`), chromedp.WaitVisible(altField)) if got := read(t, rig, chromedp.Evaluate[string](inset)); got != insetWanted { t.Fatalf("the field from the bubble: %s", got) } if again := read(t, rig, chromedp.Evaluate[string](boxes)); again != open { t.Fatalf("the blocks moved when the field opened:\n%s\n%s", open, again) } } // Enter keeps the text; Shift+Enter does too, and writes no line // break; Esc puts back what was there and gives the image back its // selection; leaving the field keeps the text. func TestAltEnterKeepsEscRestoresLeavingKeeps(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Pictures:\n\nThe text below.") upload(t, rig, page, 600, 300) drive(t, rig, chromedp.KeyEvent("A red barn"), enter()) saidAlt(t, rig, "A red barn") eventually(t, "the saved description said", func() bool { return text(t, rig, "[mapa-live]") == "Image: A red barn" }) reopen(t, rig) drive(t, rig, chromedp.KeyEvent(" in snow"), keyDown("Enter", 13, 8)) saidAlt(t, rig, "A red barn in snow") reopen(t, rig) drive(t, rig, chromedp.KeyEvent(" WRONG"), chromedp.KeyEvent(kb.Escape)) saidAlt(t, rig, "A red barn in snow") if got := read(t, rig, chromedp.Evaluate[string]( `mapaEditor.view.state.selection.node ? `+ `mapaEditor.view.state.selection.node.type.name : ""`)); got != "image" || !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) { t.Fatalf("after Esc the image is not selected and focused: %q", got) } reopen(t, rig) drive(t, rig, chromedp.KeyEvent(" today"), keyDown("Tab", 9, 0)) saidAlt(t, rig, "A red barn in snow today") reopen(t, rig) drive(t, rig, chromedp.KeyEvent(", at dusk"), chromedp.Click(`article[mapa-body] > p:last-child`)) saidAlt(t, rig, "A red barn in snow today, at dusk") drive(t, rig, chromedp.KeyEvent("Typed on.")) if !strings.Contains(typed(t, rig), "Typed on.") { t.Fatalf("typing after leaving the field: %q", typed(t, rig)) } } // reopen selects the image after the caret and opens its field from // the bubble's Alt text. func reopen(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { var view = mapaEditor.view, found = -1; view.state.doc.descendants(function (node, pos) { if (found < 0 && node.type.name === "image") found = pos; }); var Selection = view.state.selection.constructor; view.focus(); view.dispatch(view.state.tr.setSelection( Selection.near(view.state.doc.resolve(found + 1)))); })()`), chromedp.KeyEvent(kb.ArrowLeft), chromedp.WaitVisible(`[mapa-image-bubble]`), chromedp.Click(`[mapa-image-bubble] button`), chromedp.WaitVisible(altField)) } // saidAlt waits for the page's Markdown to hold the image with alt, and // the field to be gone. func saidAlt(t *testing.T, rig *harness.Rig, alt string) { t.Helper() eventually(t, "the alt "+alt, func() bool { return strings.Contains(typed(t, rig), "!["+alt+"](/i/") && read(t, rig, chromedp.Evaluate[bool]( `document.querySelector('[mapa-alt]') === null`)) }) } // keyDown is one key as a browser sends it, as enter() is Enter: a // keyDown the page may cancel, then its keyUp, and no separate char. func keyDown(key string, code int64, modifiers int64) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { press := input.DispatchKeyEventParams{ Type: input.DispatchKeyEventTypeKeyDown, Key: key, Code: key, WindowsVirtualKeyCode: code, Modifiers: modifiers} if key == "Enter" { press.Text, press.UnmodifiedText = "\r", "\r" } if _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, press); err != nil { return err } press.Type, press.Text, press.UnmodifiedText = input.DispatchKeyEventTypeKeyUp, "", "" _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, press) return err }) }