| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/rastrillo/rastrillo/harness" |
| 10 | "github.com/chromedp/chromedp" |
| 11 | "github.com/chromedp/chromedp/kb" |
| 12 | ) |
| 13 | |
| 14 | // sideSearch is the sidebar's Search, on screen from 800px. |
| 15 | const sideSearch = `[mapa-side] a[href="/search"]` |
| 16 | |
| 17 | // ⌘K by keyboard alone (level-up §4.6, §6.12, L10): opened from Search, |
| 18 | // a typed date's row first and active, the count announced, the arrows |
| 19 | // moving aria-activedescendant, Esc closing with focus back on Search, |
| 20 | // and ⌘↵ (Ctrl+Enter) creating. |
| 21 | func TestThePaletteByKeyboard(t *testing.T) { |
| 22 | rig, _ := browserRig(t, 0) |
| 23 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1280, 800), |
| 24 | chromedp.Navigate(rig.Origin+"/"), |
| 25 | chromedp.WaitVisible(sideSearch), press(sideSearch), |
| 26 | chromedp.WaitVisible("dialog[mapa-palette] input"), |
| 27 | chromedp.SendKeys("dialog[mapa-palette] input", "today"), |
| 28 | chromedp.WaitVisible(`[mapa-palette-results] a[href^="/d/"]`), |
| 29 | ) |
| 30 | parts := strings.Split(read(t, rig, |
| 31 | chromedp.Evaluate[string](paletteState)), "|") |
| 32 | if parts[0] != "mapa-palette-option-0" || |
| 33 | !strings.HasPrefix(parts[1], "/d/") || |
| 34 | !strings.HasPrefix(parts[2], "1 page, or create") || |
| 35 | parts[3] != "true" { |
| 36 | t.Fatalf("the date row is not first and active: %q", parts) |
| 37 | } |
| 38 | drive(t, rig, chromedp.KeyEvent(kb.ArrowDown)) |
| 39 | parts = strings.Split(read(t, rig, |
| 40 | chromedp.Evaluate[string](paletteState)), "|") |
| 41 | if parts[0] != "mapa-palette-option-1" || |
| 42 | parts[1] != "/new?title=today" { |
| 43 | t.Fatalf("ArrowDown did not move to Create: %q", parts) |
| 44 | } |
| 45 | drive(t, rig, chromedp.KeyEvent(kb.Escape), |
| 46 | chromedp.WaitNotVisible("dialog[mapa-palette]")) |
| 47 | if back := read(t, rig, chromedp.Evaluate[bool]( |
| 48 | `document.activeElement.matches('`+sideSearch+`')`)); !back { |
| 49 | t.Fatal("Esc did not return focus to Search") |
| 50 | } |
| 51 | createByKeyboard(t, rig) |
| 52 | } |
| 53 | |
| 54 | // createByKeyboard opens ⌘K with Ctrl+K and creates with Ctrl+Enter. |
| 55 | func createByKeyboard(t *testing.T, rig *harness.Rig) { |
| 56 | t.Helper() |
| 57 | drive(t, rig, |
| 58 | chromedp.KeyEvent("k", chromedp.KeyModifiers(chromedp.ModifierCtrl)), |
| 59 | chromedp.WaitVisible("dialog[mapa-palette] input"), |
| 60 | chromedp.SendKeys("dialog[mapa-palette] input", "Kelp farm"), |
| 61 | // The answer to the whole title: each keystroke asks. |
| 62 | chromedp.WaitVisible( |
| 63 | `[mapa-palette-results] a[href="/new?title=Kelp%20farm"]`), |
| 64 | chromedp.KeyEvent("\r", chromedp.KeyModifiers( |
| 65 | chromedp.ModifierCtrl)), |
| 66 | chromedp.WaitVisible(`main input[name="title"]`), |
| 67 | ) |
| 68 | title := read(t, rig, chromedp.Value(`main input[name="title"]`)) |
| 69 | if title != "Kelp farm" { |
| 70 | t.Fatalf("⌘↵ created %q", title) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // paletteState is the active option's id and href, the live count, and |
| 75 | // whether the hints' foot is on screen. |
| 76 | const paletteState = `(function () { |
| 77 | var input = document.querySelector("dialog[mapa-palette] input"); |
| 78 | var id = input.getAttribute("aria-activedescendant") || ""; |
| 79 | var option = document.getElementById(id); |
| 80 | var live = document.querySelector("dialog[mapa-palette] [role=status]"); |
| 81 | var foot = document.querySelector("[mapa-palette-foot]"); |
| 82 | return [id, option ? option.getAttribute("href") : "", live.textContent, |
| 83 | foot.getClientRects().length > 0].join("|"); |
| 84 | })()` |
| 85 | |