//go:build browser package mapatest import ( "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // sideSearch is the sidebar's Search, on screen from 800px. const sideSearch = `[mapa-side] a[href="/search"]` // ⌘K by keyboard alone (level-up §4.6, §6.12, L10): opened from Search, // a typed date's row first and active, the count announced, the arrows // moving aria-activedescendant, Esc closing with focus back on Search, // and ⌘↵ (Ctrl+Enter) creating. func TestThePaletteByKeyboard(t *testing.T) { rig, _ := browserRig(t, 0) drive(t, rig, scripts(true), chromedp.EmulateViewport(1280, 800), chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(sideSearch), press(sideSearch), chromedp.WaitVisible("dialog[mapa-palette] input"), chromedp.SendKeys("dialog[mapa-palette] input", "today"), chromedp.WaitVisible(`[mapa-palette-results] a[href^="/d/"]`), ) parts := strings.Split(read(t, rig, chromedp.Evaluate[string](paletteState)), "|") if parts[0] != "mapa-palette-option-0" || !strings.HasPrefix(parts[1], "/d/") || !strings.HasPrefix(parts[2], "1 page, or create") || parts[3] != "true" { t.Fatalf("the date row is not first and active: %q", parts) } drive(t, rig, chromedp.KeyEvent(kb.ArrowDown)) parts = strings.Split(read(t, rig, chromedp.Evaluate[string](paletteState)), "|") if parts[0] != "mapa-palette-option-1" || parts[1] != "/new?title=today" { t.Fatalf("ArrowDown did not move to Create: %q", parts) } drive(t, rig, chromedp.KeyEvent(kb.Escape), chromedp.WaitNotVisible("dialog[mapa-palette]")) if back := read(t, rig, chromedp.Evaluate[bool]( `document.activeElement.matches('`+sideSearch+`')`)); !back { t.Fatal("Esc did not return focus to Search") } createByKeyboard(t, rig) } // createByKeyboard opens ⌘K with Ctrl+K and creates with Ctrl+Enter. func createByKeyboard(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.KeyEvent("k", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.WaitVisible("dialog[mapa-palette] input"), chromedp.SendKeys("dialog[mapa-palette] input", "Kelp farm"), // The answer to the whole title: each keystroke asks. chromedp.WaitVisible( `[mapa-palette-results] a[href="/new?title=Kelp%20farm"]`), chromedp.KeyEvent("\r", chromedp.KeyModifiers( chromedp.ModifierCtrl)), chromedp.WaitVisible(`main input[name="title"]`), ) title := read(t, rig, chromedp.Value(`main input[name="title"]`)) if title != "Kelp farm" { t.Fatalf("⌘↵ created %q", title) } } // paletteState is the active option's id and href, the live count, and // whether the hints' foot is on screen. const paletteState = `(function () { var input = document.querySelector("dialog[mapa-palette] input"); var id = input.getAttribute("aria-activedescendant") || ""; var option = document.getElementById(id); var live = document.querySelector("dialog[mapa-palette] [role=status]"); var foot = document.querySelector("[mapa-palette-foot]"); return [id, option ? option.getAttribute("href") : "", live.textContent, foot.getClientRects().length > 0].join("|"); })()`