| 1 | //go:build browser |
| 2 | |
| 3 | // A modal dialog owns the keyboard. |
| 4 | package slidestest |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/chromedp/chromedp" |
| 13 | "github.com/chromedp/chromedp/kb" |
| 14 | ) |
| 15 | |
| 16 | // modKey presses key with a modifier held, the way a person does: |
| 17 | // keydown and keyup through the browser's own input pipeline, so the |
| 18 | // event starts at the focused element and travels the page. |
| 19 | func modKey(key string, mods chromedp.Modifier) chromedp.Action[chromedp.Void] { |
| 20 | return chromedp.KeyEvent(key, chromedp.KeyModifiers(mods)) |
| 21 | } |
| 22 | |
| 23 | // TestBrowserModalOwnsTheKeyboard (Sheets' #39 finding 1, checked here): |
| 24 | // with the link dialog open, the editor's document-level shortcuts — |
| 25 | // undo, redo, duplicate slide, new slide — do nothing. Before the fix |
| 26 | // Ctrl+D duplicated the slide behind the dialog and Ctrl+Z undid an |
| 27 | // edit nobody could see. |
| 28 | func TestBrowserModalOwnsTheKeyboard(t *testing.T) { |
| 29 | rig, _, _ := openEditor(t) |
| 30 | const heading = `[slides-stage] [data-block="heading"]` |
| 31 | const saved = `document.querySelector('[estilo-save-status]')?.dataset.state === 'saved'` |
| 32 | |
| 33 | // Something to undo, saved. |
| 34 | rig.Run( |
| 35 | chromedp.Click(chromedp.CSS(heading)), |
| 36 | chromedp.SendKeys(chromedp.CSS(heading), string(kb.End)+" undoable"), |
| 37 | waitFor(`document.querySelector('`+heading+`').textContent.includes(' undoable')`), |
| 38 | chromedp.Sleep(600*time.Millisecond), |
| 39 | waitFor(saved), |
| 40 | ) |
| 41 | var slides int |
| 42 | var title string |
| 43 | rig.Run( |
| 44 | evalInto(&slides, `document.querySelectorAll('[slides-strip-item]').length`), |
| 45 | evalInto(&title, `document.querySelector('`+heading+`').textContent`), |
| 46 | ) |
| 47 | |
| 48 | // Count every update the tab sends from here on. |
| 49 | rig.Run(chromedp.Evaluate[chromedp.Void](`(() => { |
| 50 | window.__updatePosts = 0 |
| 51 | const f = window.fetch |
| 52 | window.fetch = (u, o) => { |
| 53 | if (String(u).endsWith('/updates') && o && o.method === 'POST') window.__updatePosts++ |
| 54 | return f(u, o) |
| 55 | } |
| 56 | const b = navigator.sendBeacon.bind(navigator) |
| 57 | navigator.sendBeacon = (u, d) => { if (String(u).endsWith('/updates')) window.__updatePosts++; return b(u, d) } |
| 58 | })()`)) |
| 59 | |
| 60 | // Open the link dialog the way a person does: Mod+K in the heading. |
| 61 | var mac bool |
| 62 | rig.Run(evalInto(&mac, `/Mac|iP(hone|[oa]d)/.test(navigator.platform)`)) |
| 63 | mod := chromedp.ModifierCtrl |
| 64 | if mac { |
| 65 | mod = chromedp.ModifierMeta |
| 66 | } |
| 67 | rig.Run( |
| 68 | chromedp.Focus(chromedp.CSS(heading)), |
| 69 | modKey("k", mod), |
| 70 | waitFor(`document.querySelector('[slides-link-dialog]').open`), |
| 71 | waitFor(`document.activeElement?.closest('[slides-link-dialog]') !== null`), |
| 72 | ) |
| 73 | rig.Screen(`body`, "the link dialog is open") |
| 74 | |
| 75 | for _, m := range []chromedp.Modifier{chromedp.ModifierCtrl, chromedp.ModifierMeta} { |
| 76 | rig.Run( |
| 77 | modKey("z", m), |
| 78 | modKey("z", m|chromedp.ModifierShift), |
| 79 | modKey("y", m), |
| 80 | modKey("d", m), |
| 81 | ) |
| 82 | } |
| 83 | // Typing in the dialog's own field still works: stopping the |
| 84 | // event's propagation is not preventing its default. |
| 85 | rig.Run(chromedp.SendKeys(chromedp.CSS(`[slides-link-dialog] input[name="href"]`), "https://example.com")) |
| 86 | rig.Run(chromedp.Sleep(1500 * time.Millisecond)) |
| 87 | |
| 88 | var posts, slidesAfter int |
| 89 | var titleAfter, field string |
| 90 | var open bool |
| 91 | rig.Run( |
| 92 | evalInto(&posts, `window.__updatePosts`), |
| 93 | evalInto(&slidesAfter, `document.querySelectorAll('[slides-strip-item]').length`), |
| 94 | evalInto(&titleAfter, `document.querySelector('`+heading+`').textContent`), |
| 95 | evalInto(&open, `document.querySelector('[slides-link-dialog]').open`), |
| 96 | evalInto(&field, `document.querySelector('[slides-link-dialog] input[name="href"]').value`), |
| 97 | ) |
| 98 | if posts != 0 { |
| 99 | t.Errorf("with the link dialog open, shortcuts sent %d update(s) behind it, want 0", posts) |
| 100 | } |
| 101 | if slidesAfter != slides { |
| 102 | t.Errorf("with the link dialog open, the strip went from %d slides to %d", slides, slidesAfter) |
| 103 | } |
| 104 | if titleAfter != title { |
| 105 | t.Errorf("with the link dialog open, the heading went from %q to %q", title, titleAfter) |
| 106 | } |
| 107 | if !open { |
| 108 | t.Error("a shortcut closed the link dialog") |
| 109 | } |
| 110 | if !strings.Contains(field, "example.com") { |
| 111 | t.Errorf("typing in the dialog's field did not reach it: %q", field) |
| 112 | } |
| 113 | |
| 114 | // Escape still closes it, and the shortcuts are live again after. |
| 115 | rig.Run( |
| 116 | chromedp.KeyEvent(kb.Escape), |
| 117 | waitFor(`!document.querySelector('[slides-link-dialog]').open`), |
| 118 | chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), |
| 119 | modKey("d", chromedp.ModifierCtrl), |
| 120 | waitFor(fmt.Sprintf(`document.querySelectorAll('[slides-strip-item]').length === %d`, slides+1)), |
| 121 | ) |
| 122 | } |
| 123 | |