| 1 | //go:build browser |
| 2 | |
| 3 | // What fell between the quieter editor's tasks (the whole-branch review, |
| 4 | // items A1–A4 and A7), held in a real Chromium: a nudge that Tab leaves |
| 5 | // behind is still written; Tab gets past a box hidden on the phone; the |
| 6 | // box's menu closes when a colleague changes the slide's layout; the |
| 7 | // round button follows the phone frame's scroll; and the keyboard has |
| 8 | // somewhere to be after a box is deleted or moved from its menu. |
| 9 | package slidestest |
| 10 | |
| 11 | import ( |
| 12 | "math" |
| 13 | "net/http" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/chromedp/chromedp" |
| 19 | "github.com/chromedp/chromedp/kb" |
| 20 | ) |
| 21 | |
| 22 | // boxX is block id's left edge on slide s as the API reads it, or -1. |
| 23 | func boxX(s map[string]any, id string) float64 { |
| 24 | b, _ := blockOf(s, id) |
| 25 | if b == nil { |
| 26 | return -1 |
| 27 | } |
| 28 | box, _ := b["box"].(map[string]any) |
| 29 | x, ok := box["x"].(float64) |
| 30 | if !ok { |
| 31 | return -1 |
| 32 | } |
| 33 | return x |
| 34 | } |
| 35 | |
| 36 | // TestQuieterNudgeThenTabKeepsBoth (A1): a nudge is written once the |
| 37 | // burst settles, 300ms on. Nudge one shape, Tab to the next and nudge it |
| 38 | // with no wait between the keys: both moves reach the document. |
| 39 | func TestQuieterNudgeThenTabKeepsBoth(t *testing.T) { |
| 40 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) |
| 41 | token := mintAgent(t, te.app, te.cookies, "nudge-tab") |
| 42 | te.openOn("keyscanvas01") |
| 43 | const a, b = "keysshape001", "keysshape002" |
| 44 | // From the top of the page, Tab to the first shape: its overlay. |
| 45 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) |
| 46 | te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) |
| 47 | te.rig.Run( |
| 48 | waitFor(focusOnBox(a)), |
| 49 | chromedp.KeyEvent(kb.ArrowRight), |
| 50 | chromedp.KeyEvent(kb.Tab), |
| 51 | chromedp.KeyEvent(kb.ArrowRight), |
| 52 | waitFor(focusOnBox(b)), |
| 53 | ) |
| 54 | deadline := time.Now().Add(10 * time.Second) |
| 55 | for { |
| 56 | s := te.apiSlide(t, token, "keyscanvas01") |
| 57 | if math.Abs(boxX(s, a)-0.105) < 0.002 && math.Abs(boxX(s, b)-0.305) < 0.002 { |
| 58 | return |
| 59 | } |
| 60 | if time.Now().After(deadline) { |
| 61 | t.Fatalf("after a nudge right, Tab, a nudge right: the first shape is at x %v (want 0.105), the second at %v (want 0.305)", boxX(s, a), boxX(s, b)) |
| 62 | } |
| 63 | time.Sleep(100 * time.Millisecond) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // phoneHiddenDeck: keyboardDeck with its second shape hidden on a phone. |
| 68 | const phoneHiddenDeck = `[ |
| 69 | {"op":"add_slide","id":"keyscanvas01","layout":"canvas","template":"blank"}, |
| 70 | {"op":"add_block","slide":"keyscanvas01","id":"keysshape001","type":"shape","props":{"kind":"rect"},"box":{"x":0.1,"y":0.1,"w":0.4,"h":0.3},"z":1}, |
| 71 | {"op":"add_block","slide":"keyscanvas01","id":"keysshape002","type":"shape","props":{"kind":"ellipse"},"box":{"x":0.3,"y":0.2,"w":0.4,"h":0.3},"z":0}, |
| 72 | {"op":"add_block","slide":"keyscanvas01","id":"keyshead0001","type":"heading","props":{"level":1},"box":{"x":0.1,"y":0.7,"w":0.8,"h":0.2},"z":2,"markdown":"After the shapes"}, |
| 73 | {"op":"set_phone","slide":"keyscanvas01","id":"keysshape002","phone":{"hidden":true}}]` |
| 74 | |
| 75 | // TestQuieterTabSkipsAPhoneHiddenBox (A2): in Phone view a box hidden on |
| 76 | // the phone is not drawn, so it is not a stop: Tab from the box before |
| 77 | // it reaches the box after it, and Shift+Tab comes back. The hidden box |
| 78 | // is still in the Outline. |
| 79 | func TestQuieterTabSkipsAPhoneHiddenBox(t *testing.T) { |
| 80 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, phoneHiddenDeck) }) |
| 81 | te.openOn("keyscanvas01") |
| 82 | te.showIn("keyscanvas01", "phone") |
| 83 | // slides.css hides a box hidden on a phone (display: none), but on a |
| 84 | // canvas slide its rule loses to the canvas box's own display (a |
| 85 | // renderer stylesheet matter, outside this test): the rule is put |
| 86 | // back here as slides.css means it. |
| 87 | te.rig.Run( |
| 88 | chromedp.Evaluate[chromedp.Void](`document.head.insertAdjacentHTML('beforeend', '<style>[slides-stage] [slides-slide] > [slides-block][data-phone-hidden] { display: none !important }</style>')`), |
| 89 | waitFor(`!document.querySelector('`+boxSel("keysshape002")+`').checkVisibility()`), |
| 90 | chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), |
| 91 | ) |
| 92 | te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) |
| 93 | te.rig.Run(waitFor(focusOnBox("keysshape001"))) |
| 94 | var s tabStep |
| 95 | te.rig.Run(chromedp.KeyEvent(kb.Tab), evalInto(&s, tabStepJS)) |
| 96 | if s.ID != "keyshead0001" { |
| 97 | t.Errorf("Tab from the first shape in Phone view: %+v; want the heading, past the shape hidden on a phone", s) |
| 98 | } |
| 99 | te.rig.Run(chromedp.KeyEvent(kb.Tab, chromedp.KeyModifiers(chromedp.ModifierShift)), evalInto(&s, tabStepJS)) |
| 100 | if s.ID != "keysshape001" || !s.Overlay { |
| 101 | t.Errorf("Shift+Tab from the heading in Phone view: %+v; want the first shape's overlay", s) |
| 102 | } |
| 103 | var rows []string |
| 104 | te.rig.Run(openOutline(), evalInto(&rows, `[...document.querySelectorAll('#slides-outline [data-select-block]')].map(b => b.dataset.selectBlock)`)) |
| 105 | if !slicesHas(rows, "keysshape002") { |
| 106 | t.Errorf("the Outline lists %v; want the box hidden on a phone there too", rows) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // TestQuieterBoxMenuClosesOnALayoutChange (A3): the menu is open on a |
| 111 | // canvas shape and a colleague makes the slide a flow slide: the menu |
| 112 | // closes (its rows were the canvas slide's), and opened again it holds |
| 113 | // the flow slide's rows. |
| 114 | func TestQuieterBoxMenuClosesOnALayoutChange(t *testing.T) { |
| 115 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 116 | token := mintAgent(t, te.app, te.cookies, "colleague") |
| 117 | te.openOn("boxcanvas001") |
| 118 | te.selectBoxByID(t, "cshape000001") |
| 119 | te.rig.Run(openBoxMenu()) |
| 120 | apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, |
| 121 | `{"ops":[{"op":"set_slide","id":"boxcanvas001","layout":"flow"}]}`, http.StatusOK) |
| 122 | te.rig.Run(waitFor(`document.querySelector('[slides-stage] [slides-slide]')?.dataset.layout === 'flow'`)) |
| 123 | var open bool |
| 124 | var place bool |
| 125 | te.rig.Run( |
| 126 | evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`), |
| 127 | evalInto(&place, `document.querySelector('[slides-box-menu] [data-row="block-place"]').checkVisibility()`), |
| 128 | ) |
| 129 | if open || place { |
| 130 | t.Errorf("after a colleague made the slide flow, the box's menu is open %v, Position and size drawn %v; want it closed", open, place) |
| 131 | } |
| 132 | te.selectBoxByID(t, "cshape000001") |
| 133 | var rows []string |
| 134 | te.rig.Run(openBoxMenu(), evalInto(&rows, menuRowsJS)) |
| 135 | if want := "block-format block-move-up block-move-down region block-delete"; strings.Join(rows, " ") != want { |
| 136 | t.Errorf("opened again on the flow slide the menu draws %v, want %s", rows, want) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // longPhoneDeck: a flow slide whose heading is followed by forty |
| 141 | // paragraphs, far taller than a phone. |
| 142 | func longPhoneDeck() string { |
| 143 | paras := "" |
| 144 | for i := range 40 { |
| 145 | if i > 0 { |
| 146 | paras += `\n\n` |
| 147 | } |
| 148 | paras += "A paragraph of a slide far longer than a phone is tall." |
| 149 | } |
| 150 | return `[ |
| 151 | {"op":"add_slide","id":"longphone001","layout":"flow","template":"title-body"}, |
| 152 | {"op":"add_block","slide":"longphone001","id":"longhead0001","type":"heading","region":"heading","props":{"level":1},"markdown":"At the top"}, |
| 153 | {"op":"add_block","slide":"longphone001","id":"longtext0001","type":"text","region":"body","markdown":"` + paras + `"}]` |
| 154 | } |
| 155 | |
| 156 | // buttonOffJS is the round button's place against the selected box's, |
| 157 | // as "dx,dy" in whole px, or "hidden". |
| 158 | const buttonOffJS = `(() => { |
| 159 | const b = document.querySelector('[slides-box-more]'), box = document.querySelector('[slides-stage] [slides-block][aria-selected="true"]') |
| 160 | if (!b.checkVisibility() || !box) return 'hidden' |
| 161 | const r = b.getBoundingClientRect(), q = box.getBoundingClientRect() |
| 162 | return Math.round(r.left - q.left) + ',' + Math.round(r.top - q.top) |
| 163 | })()` |
| 164 | |
| 165 | // TestQuieterRoundButtonFollowsThePhoneScroll (A4): in Phone view the |
| 166 | // phone's screen scrolls, not the stage: the round button keeps its |
| 167 | // place on the selected box as the screen scrolls, and is not drawn once |
| 168 | // the box has scrolled out of the screen. |
| 169 | func TestQuieterRoundButtonFollowsThePhoneScroll(t *testing.T) { |
| 170 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, longPhoneDeck()) }) |
| 171 | te.showIn("longphone001", "phone") |
| 172 | te.selectBoxByID(t, "longhead0001") |
| 173 | var at string |
| 174 | te.rig.Run(evalInto(&at, buttonOffJS)) |
| 175 | if at == "hidden" { |
| 176 | t.Fatal("the heading selected in Phone view: the round button is not drawn") |
| 177 | } |
| 178 | te.rig.Run( |
| 179 | chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 40`), |
| 180 | waitFor(`(`+buttonOffJS+`) === '`+at+`'`), |
| 181 | ) |
| 182 | te.rig.Run( |
| 183 | chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 1e6`), |
| 184 | waitFor(`(f => { const q = document.querySelector('`+boxSel("longhead0001")+`').getBoundingClientRect(), r = f.getBoundingClientRect(); return q.bottom < r.top })(document.querySelector('[slides-stage-frame]'))`), |
| 185 | waitFor(`!document.querySelector('[slides-box-more]').checkVisibility()`), |
| 186 | ) |
| 187 | te.rig.Run( |
| 188 | chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 0`), |
| 189 | waitFor(`(`+buttonOffJS+`) === '`+at+`'`), |
| 190 | ) |
| 191 | } |
| 192 | |
| 193 | // TestQuieterFocusAfterTheBoxMenu (A7): the keyboard has somewhere to be |
| 194 | // after the menu changes the slide. Delete from the menu puts focus on |
| 195 | // the next box, so a second Delete deletes that one; a heading moved |
| 196 | // down from the menu keeps focus, so Shift+F10 opens its menu. |
| 197 | func TestQuieterFocusAfterTheBoxMenu(t *testing.T) { |
| 198 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) |
| 199 | token := mintAgent(t, te.app, te.cookies, "after-menu") |
| 200 | te.openOn("keyscanvas01") |
| 201 | te.selectBoxByID(t, "keysshape001") |
| 202 | te.rig.Run( |
| 203 | pressBoxMenuRow("block-delete"), |
| 204 | waitFor(`!document.querySelector('`+boxSel("keysshape001")+`')`), |
| 205 | waitFor(focusOnBox("keysshape002")), |
| 206 | chromedp.KeyEvent(kb.Delete), |
| 207 | waitFor(`!document.querySelector('`+boxSel("keysshape002")+`')`), |
| 208 | ) |
| 209 | te.untilSlide(t, token, "keyscanvas01", "without both shapes", func(s map[string]any) bool { |
| 210 | a, _ := blockOf(s, "keysshape001") |
| 211 | b, _ := blockOf(s, "keysshape002") |
| 212 | return a == nil && b == nil |
| 213 | }) |
| 214 | |
| 215 | // Moved from the menu, a heading on a flow slide keeps focus. |
| 216 | te2 := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 217 | token2 := mintAgent(t, te2.app, te2.cookies, "after-move") |
| 218 | te2.openOn("boxflow00001") |
| 219 | te2.selectBoxByID(t, "boxhead00001") |
| 220 | // The stage's DOM is replaced once the move is saved: the heading's |
| 221 | // element now is marked, to know the new one when it comes. (In a |
| 222 | // flow slide the heading stays first on the stage, in its region.) |
| 223 | te2.rig.Run( |
| 224 | chromedp.Evaluate[chromedp.Void](`document.querySelector('`+boxSel("boxhead00001")+`').dataset.before = 'yes'`), |
| 225 | pressBoxMenuRow("block-move-down"), |
| 226 | ) |
| 227 | te2.untilSlide(t, token2, "boxflow00001", "the heading moved down", func(s map[string]any) bool { |
| 228 | _, i := blockOf(s, "boxhead00001") |
| 229 | return i == 1 |
| 230 | }) |
| 231 | te2.rig.Run( |
| 232 | waitFor(`!document.querySelector('`+boxSel("boxhead00001")+`').dataset.before`), |
| 233 | waitFor(focusOnBox("boxhead00001")), |
| 234 | chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), |
| 235 | waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), |
| 236 | ) |
| 237 | var on bool |
| 238 | te2.rig.Run(evalInto(&on, `document.querySelector('`+boxSel("boxhead00001")+`').getAttribute('aria-selected') === 'true'`)) |
| 239 | if !on { |
| 240 | t.Error("Shift+F10 after moving the heading opened a menu, but not the heading's") |
| 241 | } |
| 242 | } |
| 243 | |