| 1 | //go:build browser |
| 2 | |
| 3 | // The menu on a box (the quieter editor, spec §4; fixtures/box-menu.mjs), |
| 4 | // held in a real Chromium: the round button's place, the three ways the |
| 5 | // menu opens, what each row does to the slide (read back over the API), |
| 6 | // the box's keys, and the keyboard alone from the top of the page. |
| 7 | package slidestest |
| 8 | |
| 9 | import ( |
| 10 | "fmt" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/blobs" |
| 16 | "github.com/chromedp/cdproto/input" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | "github.com/chromedp/chromedp/kb" |
| 19 | ) |
| 20 | |
| 21 | // boxMenuDeck: a flow slide holding a heading, a text and a shape; and a |
| 22 | // canvas slide holding two shapes that overlap in the layer order (the |
| 23 | // rectangle over the ellipse) and a third against the slide's top end |
| 24 | // corner. |
| 25 | const boxMenuDeck = `[ |
| 26 | {"op":"add_slide","id":"boxflow00001","layout":"flow","template":"title-body"}, |
| 27 | {"op":"add_block","slide":"boxflow00001","id":"boxhead00001","type":"heading","region":"heading","props":{"level":1},"markdown":"A heading"}, |
| 28 | {"op":"add_block","slide":"boxflow00001","id":"boxtext00001","type":"text","region":"body","markdown":"First words"}, |
| 29 | {"op":"add_block","slide":"boxflow00001","id":"boxshape0001","type":"shape","region":"body","props":{"kind":"rect"}}, |
| 30 | {"op":"add_slide","id":"boxcanvas001","layout":"canvas","template":"blank"}, |
| 31 | {"op":"add_block","slide":"boxcanvas001","id":"cshape000001","type":"shape","props":{"kind":"rect"},"box":{"x":0.2,"y":0.3,"w":0.3,"h":0.3},"z":1}, |
| 32 | {"op":"add_block","slide":"boxcanvas001","id":"cshape000002","type":"shape","props":{"kind":"ellipse"},"box":{"x":0.35,"y":0.4,"w":0.3,"h":0.3},"z":0}, |
| 33 | {"op":"add_block","slide":"boxcanvas001","id":"cedge0000001","type":"shape","props":{"kind":"rect"},"box":{"x":0.7,"y":0,"w":0.3,"h":0.25},"z":2}]` |
| 34 | |
| 35 | // boxSel is the box with this id on the stage. |
| 36 | func boxSel(id string) string { return `[slides-stage] [slides-block][data-id="` + id + `"]` } |
| 37 | |
| 38 | // selectBoxByID clicks the box (near its top start corner, clear of |
| 39 | // any box over its middle) and waits until it is the selection with its |
| 40 | // round button drawn. |
| 41 | func (te *tidyEditor) selectBoxByID(t *testing.T, id string) { |
| 42 | t.Helper() |
| 43 | var at []float64 |
| 44 | te.rig.Run( |
| 45 | waitFor(`document.querySelector('`+boxSel(id)+`')`), |
| 46 | evalInto(&at, `(r => [r.left + Math.min(20, r.width / 3), r.top + Math.min(20, r.height / 3)])(document.querySelector('`+boxSel(id)+`').getBoundingClientRect())`), |
| 47 | ) |
| 48 | te.rig.Run( |
| 49 | chromedp.MouseClickXY(at[0], at[1]), |
| 50 | waitFor(`document.querySelector('`+boxSel(id)+`')?.getAttribute('aria-selected') === 'true'`), |
| 51 | waitFor(`document.querySelector('[slides-box-more]').checkVisibility()`), |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | // roundButtonJS reports what is wrong with the round button: not drawn, |
| 56 | // under 24x24, outside the stage pane, or over one of the selected |
| 57 | // box's handles (the canvas overlay's, when it is drawn). "" is the pass. |
| 58 | const roundButtonJS = `(() => { |
| 59 | const b = document.querySelector('[slides-box-more]') |
| 60 | if (!b || !b.checkVisibility()) return 'the round button is not drawn' |
| 61 | const r = b.getBoundingClientRect(), p = document.querySelector('[slides-stage-pane]').getBoundingClientRect() |
| 62 | const out = [] |
| 63 | if (r.width < 24 || r.height < 24) out.push('it is ' + r.width + 'x' + r.height) |
| 64 | if (r.left < p.left - 0.5 || r.top < p.top - 0.5 || r.right > p.right + 0.5 || r.bottom > p.bottom + 0.5) out.push('it ' + JSON.stringify(r) + ' is outside the stage pane ' + JSON.stringify(p)) |
| 65 | for (const h of document.querySelectorAll('[slides-stage] [slides-handle]')) { |
| 66 | const q = h.getBoundingClientRect() |
| 67 | if (q.width && r.left < q.right && q.left < r.right && r.top < q.bottom && q.top < r.bottom) out.push('it covers the ' + h.dataset.handle + ' handle') |
| 68 | } |
| 69 | return out.join('; ') |
| 70 | })()` |
| 71 | |
| 72 | // menuOpenJS reports what is wrong with the open menu: closed, outside |
| 73 | // the window, or without focus. "" is the pass. |
| 74 | const menuOpenJS = `(() => { |
| 75 | const m = document.querySelector('[slides-box-menu]') |
| 76 | if (!m.matches(':popover-open')) return 'the menu is closed' |
| 77 | const r = m.getBoundingClientRect(), out = [] |
| 78 | if (r.left < 0 || r.top < 0 || r.right > innerWidth || r.bottom > innerHeight) out.push('the menu ' + JSON.stringify(r) + ' is outside the window ' + innerWidth + 'x' + innerHeight) |
| 79 | if (!document.activeElement || !document.activeElement.closest('[slides-box-menu]')) out.push('focus is not in the menu') |
| 80 | return out.join('; ') |
| 81 | })()` |
| 82 | |
| 83 | // menuRowsJS: the menu's rows drawn, by data-row, in order. |
| 84 | const menuRowsJS = `[...document.querySelectorAll('[slides-box-menu] [data-row]')].filter(r => r.checkVisibility()).map(r => r.dataset.row)` |
| 85 | |
| 86 | const menuClosed = `!document.querySelector('[slides-box-menu]').matches(':popover-open')` |
| 87 | |
| 88 | // openBoxMenu opens the selected box's menu with its round button. |
| 89 | func openBoxMenu() chromedp.Action[chromedp.Void] { |
| 90 | return chromedp.Steps( |
| 91 | waitFor(`document.querySelector('[slides-box-more]').checkVisibility()`), |
| 92 | chromedp.Click(chromedp.CSS(`[slides-box-more]`)), |
| 93 | waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), |
| 94 | ) |
| 95 | } |
| 96 | |
| 97 | // pressBoxMenuRow opens the selected box's menu with its round button |
| 98 | // and presses one row: a data-row, or a region's data-region. It is how |
| 99 | // a test does what the card's arrange and delete buttons did. |
| 100 | func pressBoxMenuRow(row string) chromedp.Action[chromedp.Void] { |
| 101 | sel := `[slides-box-menu] [data-row="` + row + `"]` |
| 102 | if strings.HasPrefix(row, "region:") { |
| 103 | sel = `[slides-box-menu] [data-region="` + strings.TrimPrefix(row, "region:") + `"]` |
| 104 | } |
| 105 | return chromedp.Steps( |
| 106 | openBoxMenu(), |
| 107 | waitFor(`document.querySelector('`+sel+`').checkVisibility()`), |
| 108 | chromedp.Click(chromedp.CSS(sel)), |
| 109 | waitFor(menuClosed), |
| 110 | ) |
| 111 | } |
| 112 | |
| 113 | // focusOnBox is a JS expression: focus is on box id itself, the canvas |
| 114 | // overlay over it, or its text. |
| 115 | func focusOnBox(id string) string { |
| 116 | return `(a => !!a && (a.matches('[slides-canvas-overlay]') ? document.querySelector('` + boxSel(id) + `')?.getAttribute('aria-selected') === 'true' : !!a.closest('` + boxSel(id) + `')))(document.activeElement)` |
| 117 | } |
| 118 | |
| 119 | // blockOf is block id of slide s as the API reads it, and its place in |
| 120 | // the slide's order; nil when it has gone. |
| 121 | func blockOf(s map[string]any, id string) (map[string]any, int) { |
| 122 | blocks, _ := s["blocks"].([]any) |
| 123 | for i, b := range blocks { |
| 124 | if m := b.(map[string]any); m["id"] == id { |
| 125 | return m, i |
| 126 | } |
| 127 | } |
| 128 | return nil, -1 |
| 129 | } |
| 130 | |
| 131 | func zOf(s map[string]any, id string) float64 { |
| 132 | b, _ := blockOf(s, id) |
| 133 | if b == nil { |
| 134 | return -1 |
| 135 | } |
| 136 | z, _ := b["z"].(float64) |
| 137 | return z |
| 138 | } |
| 139 | |
| 140 | // TestQuieterBoxMenu (spec §4.1, §4.2, §10): on a canvas shape and on a |
| 141 | // flow heading, the round button is drawn on selection, at least 24px, |
| 142 | // inside the stage pane and clear of every handle (a box against the |
| 143 | // slide's edge too); the button, Shift+F10 and a right-click each open |
| 144 | // the menu inside the window, with only the rows that apply; Esc gives |
| 145 | // focus back to the box; and each row does what its card button or |
| 146 | // field did, read back over the API. |
| 147 | func TestQuieterBoxMenu(t *testing.T) { |
| 148 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 149 | token := mintAgent(t, te.app, te.cookies, "box-menu") |
| 150 | |
| 151 | canvasRows := []string{"block-format", "block-forward", "block-back", "block-move-up", "block-move-down", "block-place", "block-delete"} |
| 152 | flowRows := []string{"block-format", "block-move-up", "block-move-down", "region", "block-delete"} |
| 153 | opens := func(where string, rows []string, id string) { |
| 154 | t.Helper() |
| 155 | var got string |
| 156 | var drawn []string |
| 157 | te.rig.Run(evalInto(&got, menuOpenJS), evalInto(&drawn, menuRowsJS)) |
| 158 | if got != "" { |
| 159 | t.Errorf("%s: %s", where, got) |
| 160 | } |
| 161 | if strings.Join(drawn, " ") != strings.Join(rows, " ") { |
| 162 | t.Errorf("%s: the menu draws %v, want %v", where, drawn, rows) |
| 163 | } |
| 164 | te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed)) |
| 165 | var back bool |
| 166 | te.rig.Run(evalInto(&back, focusOnBox(id))) |
| 167 | if !back { |
| 168 | t.Errorf("%s: Esc left focus %+v; want it on the box", where, te.focus()) |
| 169 | } |
| 170 | var selected bool |
| 171 | te.rig.Run(evalInto(&selected, `document.querySelector('`+boxSel(id)+`').getAttribute('aria-selected') === 'true'`)) |
| 172 | if !selected { |
| 173 | t.Errorf("%s: Esc gave the box up; want it still selected", where) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | // The canvas slide. |
| 178 | te.openOn("boxcanvas001") |
| 179 | for _, id := range []string{"cshape000001", "cedge0000001"} { |
| 180 | te.selectBoxByID(t, id) |
| 181 | var handles int |
| 182 | te.rig.Run(evalInto(&handles, `[...document.querySelectorAll('[slides-stage] [slides-handle]')].filter(h => h.getBoundingClientRect().width > 0).length`)) |
| 183 | if handles != 8 { |
| 184 | t.Errorf("%s selected: %d handles drawn, want 8", id, handles) |
| 185 | } |
| 186 | var got string |
| 187 | te.rig.Run(evalInto(&got, roundButtonJS)) |
| 188 | if got != "" { |
| 189 | t.Errorf("%s selected: %s", id, got) |
| 190 | } |
| 191 | } |
| 192 | te.rig.Screen(`[slides-stage-pane]`, "the round button on a box against the slide's top end corner") |
| 193 | te.selectBoxByID(t, "cshape000001") |
| 194 | te.rig.Run(openBoxMenu()) |
| 195 | te.rig.Screen(`[slides-editor]`, "the menu on a canvas shape") |
| 196 | opens("a canvas shape, from its button", canvasRows, "cshape000001") |
| 197 | te.rig.Run(chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift))) |
| 198 | opens("a canvas shape, Shift+F10", canvasRows, "cshape000001") |
| 199 | var at []float64 |
| 200 | te.rig.Run(evalInto(&at, `(r => [r.left + r.width / 4, r.top + r.height / 4])(document.querySelector('`+boxSel("cshape000001")+`').getBoundingClientRect())`)) |
| 201 | te.rig.Run(chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`)) |
| 202 | opens("a canvas shape, right-click", canvasRows, "cshape000001") |
| 203 | |
| 204 | // Each canvas row, read back. The rectangle is over the ellipse. |
| 205 | s := te.apiSlide(t, token, "boxcanvas001") |
| 206 | if zOf(s, "cshape000001") <= zOf(s, "cshape000002") || zOf(s, "cshape000001") >= zOf(s, "cedge0000001") { |
| 207 | t.Fatalf("the deck's layers are not as made: %v", s["blocks"]) |
| 208 | } |
| 209 | te.rig.Run(pressBoxMenuRow("block-forward")) |
| 210 | te.untilSlide(t, token, "boxcanvas001", "the rectangle brought forward", func(s map[string]any) bool { |
| 211 | return zOf(s, "cshape000001") > zOf(s, "cedge0000001") |
| 212 | }) |
| 213 | te.selectBoxByID(t, "cshape000001") |
| 214 | te.rig.Run(pressBoxMenuRow("block-back")) |
| 215 | te.untilSlide(t, token, "boxcanvas001", "the rectangle sent back", func(s map[string]any) bool { |
| 216 | return zOf(s, "cshape000001") < zOf(s, "cedge0000001") |
| 217 | }) |
| 218 | te.selectBoxByID(t, "cshape000001") |
| 219 | te.rig.Run(pressBoxMenuRow("block-move-down")) |
| 220 | te.untilSlide(t, token, "boxcanvas001", "the rectangle moved down", func(s map[string]any) bool { |
| 221 | _, i := blockOf(s, "cshape000001") |
| 222 | return i == 1 |
| 223 | }) |
| 224 | te.selectBoxByID(t, "cshape000001") |
| 225 | te.rig.Run(pressBoxMenuRow("block-move-up")) |
| 226 | te.untilSlide(t, token, "boxcanvas001", "the rectangle moved up", func(s map[string]any) bool { |
| 227 | _, i := blockOf(s, "cshape000001") |
| 228 | return i == 0 |
| 229 | }) |
| 230 | // Position and size…: the card shown, its group open, focus on Left. |
| 231 | te.pressFormat("closed") |
| 232 | te.selectBoxByID(t, "cshape000001") |
| 233 | te.rig.Run( |
| 234 | pressBoxMenuRow("block-place"), |
| 235 | waitFor(`document.querySelector('[slides-inspector]').dataset.state === 'open'`), |
| 236 | waitFor(`document.querySelector('[slides-place]').open`), |
| 237 | waitFor(`document.activeElement === document.querySelector('#rst-block-x')`), |
| 238 | ) |
| 239 | te.rig.Screen(`[slides-editor]`, "Position and size, opened from the menu") |
| 240 | // Format: focus on the box's first option. |
| 241 | te.rig.Run( |
| 242 | chromedp.KeyEvent(kb.Escape), |
| 243 | waitFor(focusOnBox("cshape000001")), |
| 244 | pressBoxMenuRow("block-format"), |
| 245 | waitFor(`!!document.activeElement.closest('[slides-inspector] form[data-section="block"]') && document.activeElement.checkVisibility()`), |
| 246 | ) |
| 247 | // Corners are a rectangle's: not drawn for an ellipse. |
| 248 | te.rig.Run( |
| 249 | waitFor(`document.querySelector('#rst-block-radius').checkVisibility()`), |
| 250 | chromedp.Evaluate[chromedp.Void](changeJS("#rst-block-kind", "ellipse")), |
| 251 | waitFor(`document.querySelector('`+boxSel("cshape000001")+`')?.dataset.kind === 'ellipse'`), |
| 252 | waitFor(`!document.querySelector('#rst-block-radius').checkVisibility()`), |
| 253 | ) |
| 254 | te.selectBoxByID(t, "cshape000001") |
| 255 | te.rig.Run(pressBoxMenuRow("block-delete"), waitFor(`!document.querySelector('`+boxSel("cshape000001")+`')`)) |
| 256 | te.untilSlide(t, token, "boxcanvas001", "without the rectangle", func(s map[string]any) bool { |
| 257 | b, _ := blockOf(s, "cshape000001") |
| 258 | return b == nil |
| 259 | }) |
| 260 | |
| 261 | // The flow slide: a heading. |
| 262 | te.openOn("boxflow00001") |
| 263 | te.selectBoxByID(t, "boxhead00001") |
| 264 | var got string |
| 265 | te.rig.Run(evalInto(&got, roundButtonJS)) |
| 266 | if got != "" { |
| 267 | t.Errorf("the flow heading selected: %s", got) |
| 268 | } |
| 269 | te.rig.Run(openBoxMenu()) |
| 270 | te.rig.Screen(`[slides-editor]`, "the menu on a flow heading") |
| 271 | var checked []string |
| 272 | te.rig.Run(evalInto(&checked, `[...document.querySelectorAll('[slides-box-menu] [data-region][aria-checked="true"]')].map(b => b.dataset.region)`)) |
| 273 | if strings.Join(checked, " ") != "heading" { |
| 274 | t.Errorf("the heading's menu checks the regions %v, want its own, heading", checked) |
| 275 | } |
| 276 | opens("a flow heading, from its button", flowRows, "boxhead00001") |
| 277 | te.rig.Run(chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift))) |
| 278 | opens("a flow heading, Shift+F10 in its text", flowRows, "boxhead00001") |
| 279 | // Right-click on its text with no caret in it. |
| 280 | te.rig.Run( |
| 281 | chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), |
| 282 | chromedp.Evaluate[chromedp.Void](contextMenuJS(boxSel("boxhead00001"))), |
| 283 | waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), |
| 284 | ) |
| 285 | opens("a flow heading with no caret, right-click", flowRows, "boxhead00001") |
| 286 | |
| 287 | te.rig.Run(pressBoxMenuRow("region:aside")) |
| 288 | te.untilSlide(t, token, "boxflow00001", "the heading moved to the aside", func(s map[string]any) bool { |
| 289 | b, _ := blockOf(s, "boxhead00001") |
| 290 | return b != nil && b["region"] == "aside" |
| 291 | }) |
| 292 | te.selectBoxByID(t, "boxhead00001") |
| 293 | te.rig.Run(pressBoxMenuRow("region:heading")) |
| 294 | te.untilSlide(t, token, "boxflow00001", "the heading back in its region", func(s map[string]any) bool { |
| 295 | b, _ := blockOf(s, "boxhead00001") |
| 296 | return b != nil && b["region"] == "heading" |
| 297 | }) |
| 298 | te.selectBoxByID(t, "boxhead00001") |
| 299 | te.rig.Run(pressBoxMenuRow("block-move-down")) |
| 300 | te.untilSlide(t, token, "boxflow00001", "the heading moved down", func(s map[string]any) bool { |
| 301 | _, i := blockOf(s, "boxhead00001") |
| 302 | return i == 1 |
| 303 | }) |
| 304 | te.selectBoxByID(t, "boxhead00001") |
| 305 | te.rig.Run(pressBoxMenuRow("block-move-up")) |
| 306 | te.untilSlide(t, token, "boxflow00001", "the heading moved up", func(s map[string]any) bool { |
| 307 | _, i := blockOf(s, "boxhead00001") |
| 308 | return i == 0 |
| 309 | }) |
| 310 | te.selectBoxByID(t, "boxhead00001") |
| 311 | te.rig.Run(pressBoxMenuRow("block-delete"), waitFor(`!document.querySelector('`+boxSel("boxhead00001")+`')`)) |
| 312 | te.untilSlide(t, token, "boxflow00001", "without the heading", func(s map[string]any) bool { |
| 313 | b, _ := blockOf(s, "boxhead00001") |
| 314 | return b == nil |
| 315 | }) |
| 316 | } |
| 317 | |
| 318 | // contextMenuJS dispatches a right-click's contextmenu event on the |
| 319 | // first text inside sel, as the browser does, and answers whether |
| 320 | // something took it (defaultPrevented). |
| 321 | func contextMenuJS(sel string) string { |
| 322 | return `(() => { const box = document.querySelector('` + sel + `') |
| 323 | const n = document.createTreeWalker(box, NodeFilter.SHOW_TEXT).nextNode() |
| 324 | const target = n ? n.parentElement : box, r = target.getBoundingClientRect() |
| 325 | const ev = new MouseEvent('contextmenu', { bubbles: true, cancelable: true, button: 2, clientX: r.left + 4, clientY: r.top + 4 }) |
| 326 | target.dispatchEvent(ev); return ev.defaultPrevented })()` |
| 327 | } |
| 328 | |
| 329 | // TestQuieterBoxMenuLeavesTextAlone (spec §4.1, Review Focus 1): with the |
| 330 | // caret in a heading's text, a right-click on that text is the |
| 331 | // browser's (spelling, paste): nothing prevents it and the box's menu |
| 332 | // stays closed. A real right-click on a shape opens the box's menu. |
| 333 | func TestQuieterBoxMenuLeavesTextAlone(t *testing.T) { |
| 334 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 335 | te.selectBoxByID(t, "boxhead00001") |
| 336 | te.rig.Run(waitFor(`document.activeElement.closest('` + boxSel("boxhead00001") + `')`)) |
| 337 | var prevented bool |
| 338 | te.rig.Run(evalInto(&prevented, contextMenuJS(boxSel("boxhead00001")))) |
| 339 | if prevented { |
| 340 | t.Error("a right-click on the text being edited was prevented: the browser's menu would not open") |
| 341 | } |
| 342 | var open bool |
| 343 | te.rig.Run(evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`)) |
| 344 | if open { |
| 345 | t.Error("a right-click on the text being edited opened the box's menu") |
| 346 | } |
| 347 | // A person's right-click there, through the browser's own input: the |
| 348 | // same, heard on the window after every listener on the page. |
| 349 | var at []float64 |
| 350 | te.rig.Run( |
| 351 | chromedp.Evaluate[chromedp.Void](`window.__ctx = []; addEventListener('contextmenu', e => { window.__ctx.push(e.defaultPrevented) }); true`), |
| 352 | evalInto(&at, `(r => [r.left + 8, r.top + r.height / 2])(document.querySelector('`+boxSel("boxhead00001")+`').getBoundingClientRect())`), |
| 353 | ) |
| 354 | te.rig.Run(chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), waitFor(`window.__ctx.length === 1`)) |
| 355 | var heard []bool |
| 356 | te.rig.Run(evalInto(&heard, `window.__ctx`), evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`)) |
| 357 | if len(heard) != 1 || heard[0] || open { |
| 358 | t.Errorf("a right-click in the heading's text: prevented %v, the box's menu open %v; want neither", heard, open) |
| 359 | } |
| 360 | |
| 361 | // On a shape (a box with no text) it is the box's. |
| 362 | te.rig.Run(chromedp.KeyEvent(kb.Escape)) |
| 363 | te.selectBoxByID(t, "boxshape0001") |
| 364 | te.rig.Run(evalInto(&at, `(r => [r.left + r.width / 2, r.top + r.height / 2])(document.querySelector('`+boxSel("boxshape0001")+`').getBoundingClientRect())`)) |
| 365 | te.rig.Run( |
| 366 | chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), |
| 367 | waitFor(`window.__ctx.length === 2`), |
| 368 | waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), |
| 369 | ) |
| 370 | te.rig.Run(evalInto(&heard, `window.__ctx`)) |
| 371 | if !heard[1] { |
| 372 | t.Error("a right-click on the shape was not taken by the box's menu") |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // keyboardDeck: a canvas slide of two shapes, the first over the |
| 377 | // second, and a heading after them (the editor is bound once a heading |
| 378 | // is editable). |
| 379 | const keyboardDeck = `[ |
| 380 | {"op":"add_slide","id":"keyscanvas01","layout":"canvas","template":"blank"}, |
| 381 | {"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}, |
| 382 | {"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}, |
| 383 | {"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"}]` |
| 384 | |
| 385 | // TestQuieterKeyboardOnlyShape (spec §0.3, §4.4, §10): a shape on a |
| 386 | // canvas slide, the kind of box with no text to put a caret in, reached |
| 387 | // and changed with the keyboard alone from the top of the page, with the |
| 388 | // card showing and again with it hidden: Tab to the shape (its overlay, |
| 389 | // selected), Shift+F10 and Format into its options, its fill, Esc back, |
| 390 | // send it back with Ctrl+[, Position and size… from the menu, Left |
| 391 | // typed, Esc back, Delete, and focus on the box next to it. The notes |
| 392 | // are closed first: the caret there turns the card to the notes' marks. |
| 393 | func TestQuieterKeyboardOnlyShape(t *testing.T) { |
| 394 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) |
| 395 | token := mintAgent(t, te.app, te.cookies, "keyboard-only") |
| 396 | te.rig.Run( |
| 397 | chromedp.Click(chromedp.CSS(`[slides-notes-close]`)), |
| 398 | waitFor(`document.querySelector('[slides-notes-pane]').dataset.state === 'closed'`), |
| 399 | ) |
| 400 | const shape = "keysshape001" |
| 401 | onShape := func(where string) { |
| 402 | t.Helper() |
| 403 | var s tabStep |
| 404 | te.rig.Run(evalInto(&s, tabStepJS)) |
| 405 | if !s.In || !s.Overlay || s.ID != shape || !s.Selected || s.Inspect != "box" { |
| 406 | t.Errorf("%s: %+v; want focus on the shape's overlay, the shape selected and its options in the card", where, s) |
| 407 | } |
| 408 | } |
| 409 | for _, hidden := range []bool{false, true} { |
| 410 | name := "the card showing" |
| 411 | if hidden { |
| 412 | name = "the card hidden at first" |
| 413 | } |
| 414 | te.openOn("keyscanvas01") |
| 415 | if hidden { |
| 416 | te.pressFormat("closed") |
| 417 | } |
| 418 | // From the top of the page, Tab until the shape is reached (put |
| 419 | // back for the second pass, it is the slide's last box). |
| 420 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) |
| 421 | for i := 0; ; i++ { |
| 422 | var s tabStep |
| 423 | te.rig.Run(chromedp.KeyEvent(kb.Tab), evalInto(&s, tabStepJS)) |
| 424 | if s.In && s.ID == shape { |
| 425 | break |
| 426 | } |
| 427 | if i == 80 { |
| 428 | t.Fatalf("%s: Tab never reached the shape; focus is %+v", name, te.focus()) |
| 429 | } |
| 430 | } |
| 431 | onShape(name + ", reached by Tab") |
| 432 | |
| 433 | // Its options, from the menu: Format. |
| 434 | te.rig.Run( |
| 435 | chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), |
| 436 | waitFor(`document.activeElement === document.querySelector('[slides-box-menu] [data-row="block-format"]')`), |
| 437 | chromedp.KeyEvent(kb.Enter), |
| 438 | waitFor(`document.querySelector('[slides-inspector]').dataset.state === 'open'`), |
| 439 | waitFor(`!!document.activeElement.closest('[slides-inspector] form[data-section="block"]') && document.activeElement.checkVisibility()`), |
| 440 | ) |
| 441 | // Its fill: the colour picker itself is the browser's, so its |
| 442 | // choice is written as the picker would. |
| 443 | te.tabTo(t, `#rst-block-fill`, 10) |
| 444 | te.rig.Run( |
| 445 | chromedp.Evaluate[chromedp.Void](`(f => { f.value = '#cc3300'; f.dispatchEvent(new Event('input', {bubbles: true})); f.dispatchEvent(new Event('change', {bubbles: true})) })(document.activeElement)`), |
| 446 | waitFor(`document.querySelector('`+boxSel(shape)+`').outerHTML.toLowerCase().includes('cc3300')`), |
| 447 | ) |
| 448 | te.rig.Run(chromedp.KeyEvent(kb.Escape)) |
| 449 | onShape(name + ", Esc in its options") |
| 450 | |
| 451 | // Send it back, with its key: under the box that was under it. |
| 452 | before := te.apiSlide(t, token, "keyscanvas01") |
| 453 | var under string |
| 454 | blocks, _ := before["blocks"].([]any) |
| 455 | for _, b := range blocks { |
| 456 | m := b.(map[string]any) |
| 457 | if z, _ := m["z"].(float64); z < zOf(before, shape) && (under == "" || z > zOf(before, under)) { |
| 458 | under, _ = m["id"].(string) |
| 459 | } |
| 460 | } |
| 461 | if under == "" { |
| 462 | t.Fatalf("%s: nothing is under the shape to send it back behind: %v", name, blocks) |
| 463 | } |
| 464 | te.rig.Run(chromedp.KeyEvent("[", chromedp.KeyModifiers(chromedp.ModifierCtrl))) |
| 465 | te.untilSlide(t, token, "keyscanvas01", "the shape sent back", func(s map[string]any) bool { |
| 466 | return zOf(s, shape) < zOf(s, under) |
| 467 | }) |
| 468 | te.rig.Run(waitFor(focusOnBox(shape))) |
| 469 | |
| 470 | // Position and size…, from the menu: Left typed. |
| 471 | te.rig.Run( |
| 472 | chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), |
| 473 | waitFor(`document.activeElement?.closest('[slides-box-menu]')`), |
| 474 | ) |
| 475 | for range 10 { |
| 476 | var on bool |
| 477 | te.rig.Run(evalInto(&on, `document.activeElement === document.querySelector('[slides-box-menu] [data-row="block-place"]')`)) |
| 478 | if on { |
| 479 | break |
| 480 | } |
| 481 | te.rig.Run(chromedp.KeyEvent(kb.ArrowDown)) |
| 482 | } |
| 483 | te.rig.Run( |
| 484 | chromedp.KeyEvent(kb.Enter), |
| 485 | waitFor(`document.activeElement === document.querySelector('#rst-block-x')`), |
| 486 | chromedp.KeyEvent("a", chromedp.KeyModifiers(chromedp.ModifierCtrl)), |
| 487 | chromedp.KeyEvent("40"), |
| 488 | chromedp.KeyEvent(kb.Tab), |
| 489 | ) |
| 490 | te.untilSlide(t, token, "keyscanvas01", "the shape at 40% from the left", func(s map[string]any) bool { |
| 491 | b, _ := blockOf(s, shape) |
| 492 | box, _ := b["box"].(map[string]any) |
| 493 | return box != nil && box["x"] == 0.4 |
| 494 | }) |
| 495 | te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(focusOnBox(shape))) |
| 496 | onShape(name + ", Esc in Position and size") |
| 497 | |
| 498 | // Delete, with its key: focus goes on to the next box, or (the |
| 499 | // shape last, in the second pass) the one before it, selected. |
| 500 | next := "keysshape002" |
| 501 | if hidden { |
| 502 | next = "keyshead0001" |
| 503 | } |
| 504 | te.rig.Run( |
| 505 | chromedp.KeyEvent(kb.Delete), |
| 506 | waitFor(`!document.querySelector('`+boxSel(shape)+`')`), |
| 507 | waitFor(focusOnBox(next)+` && document.querySelector('[slides-editor]').dataset.inspect === 'box'`), |
| 508 | ) |
| 509 | te.untilSlide(t, token, "keyscanvas01", "without the shape", func(s map[string]any) bool { |
| 510 | b, _ := blockOf(s, shape) |
| 511 | return b == nil |
| 512 | }) |
| 513 | if !hidden { |
| 514 | apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, |
| 515 | `{"ops":[{"op":"add_block","slide":"keyscanvas01","id":"`+shape+`","type":"shape","props":{"kind":"rect"},"box":{"x":0.1,"y":0.1,"w":0.4,"h":0.3},"z":5}]}`, http.StatusOK) |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | // TestQuieterDeleteKey (spec §4.2, §10): Delete with a shape focused |
| 521 | // deletes it, on a flow slide and on a canvas slide; with the caret in a |
| 522 | // heading it deletes a character; in the strip it still asks about the |
| 523 | // slide. |
| 524 | func TestQuieterDeleteKey(t *testing.T) { |
| 525 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 526 | token := mintAgent(t, te.app, te.cookies, "delete-key") |
| 527 | gone := func(slide, id string) { |
| 528 | t.Helper() |
| 529 | te.rig.Run(waitFor(`!document.querySelector('` + boxSel(id) + `')`)) |
| 530 | te.untilSlide(t, token, slide, "without "+id, func(s map[string]any) bool { |
| 531 | b, _ := blockOf(s, id) |
| 532 | return b == nil |
| 533 | }) |
| 534 | } |
| 535 | |
| 536 | // A flow slide's shape, its own tab stop. |
| 537 | te.openOn("boxflow00001") |
| 538 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) |
| 539 | te.tabTo(t, boxSel("boxshape0001"), 40) |
| 540 | te.rig.Run(chromedp.KeyEvent(kb.Delete)) |
| 541 | gone("boxflow00001", "boxshape0001") |
| 542 | |
| 543 | // The caret in a heading: a character. |
| 544 | te.rig.Run( |
| 545 | chromedp.Evaluate[chromedp.Void](`(() => { const h = document.querySelector('`+boxSel("boxhead00001")+`'); h.focus() |
| 546 | const n = document.createTreeWalker(h, NodeFilter.SHOW_TEXT).nextNode() |
| 547 | const r = document.createRange(); r.setStart(n, 0); r.collapse(true); getSelection().removeAllRanges(); getSelection().addRange(r); return true })()`), |
| 548 | waitFor(`document.querySelector('`+boxSel("boxhead00001")+`').getAttribute('aria-selected') === 'true'`), |
| 549 | chromedp.KeyEvent(kb.Delete), |
| 550 | waitFor(`document.querySelector('`+boxSel("boxhead00001")+`')?.textContent.trim() === 'heading'`), |
| 551 | ) |
| 552 | te.untilSlide(t, token, "boxflow00001", "the heading one letter shorter", func(s map[string]any) bool { |
| 553 | b, _ := blockOf(s, "boxhead00001") |
| 554 | return b != nil && strings.Contains(fmt.Sprint(b), "heading") && !strings.Contains(fmt.Sprint(b), "A heading") |
| 555 | }) |
| 556 | |
| 557 | // A canvas slide's shape, its overlay. |
| 558 | te.openOn("boxcanvas001") |
| 559 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) |
| 560 | te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) |
| 561 | var first string |
| 562 | te.rig.Run(evalInto(&first, `document.querySelector('[slides-stage] [slides-block][aria-selected="true"]').dataset.id`)) |
| 563 | te.rig.Run(chromedp.KeyEvent(kb.Delete)) |
| 564 | gone("boxcanvas001", first) |
| 565 | |
| 566 | // The strip: it asks about the slide, and deletes nothing. |
| 567 | var slides int |
| 568 | te.rig.Run(evalInto(&slides, `document.querySelectorAll('[slides-strip-item][data-slide-id]').length`)) |
| 569 | te.rig.Run( |
| 570 | chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-strip-item]:first-child [slides-strip-link]').focus()`), |
| 571 | chromedp.KeyEvent(kb.Delete), |
| 572 | waitFor(`document.querySelector('[slides-confirm]').checkVisibility()`), |
| 573 | ) |
| 574 | var after int |
| 575 | te.rig.Run(evalInto(&after, `document.querySelectorAll('[slides-strip-item][data-slide-id]').length`)) |
| 576 | if after != slides { |
| 577 | t.Errorf("Delete in the strip left %d slides of %d; want it to ask first", after, slides) |
| 578 | } |
| 579 | te.rig.Run(chromedp.Click(chromedp.CSS(`[data-command="slide-delete-cancel"]`))) |
| 580 | } |
| 581 | |
| 582 | // TestQuieterPlaceClosesOnReload (spec §4.3): Position and size, opened, |
| 583 | // says its numbers are percentages of the frame, stays open while the |
| 584 | // page is open and is closed again after a reload. |
| 585 | func TestQuieterPlaceClosesOnReload(t *testing.T) { |
| 586 | te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) |
| 587 | te.openOn("boxcanvas001") |
| 588 | te.selectBoxByID(t, "cshape000001") |
| 589 | te.rig.Run( |
| 590 | pressBoxMenuRow("block-place"), |
| 591 | waitFor(`document.querySelector('[slides-place]').open`), |
| 592 | ) |
| 593 | // The numbers' unit is in sight (review: they had none): the group's |
| 594 | // one quiet line says they are percentages of the frame, and is |
| 595 | // still the four fields' group name. |
| 596 | var unit struct { |
| 597 | Shown bool |
| 598 | Text string |
| 599 | } |
| 600 | te.rig.Run(evalInto(&unit, `(l => ({ Shown: l.checkVisibility() && l.getBoundingClientRect().height >= 8 && getComputedStyle(l).clipPath === 'none' && getComputedStyle(l).position !== 'absolute', Text: l.textContent.trim() }))(document.querySelector('[slides-box-fields] > legend'))`)) |
| 601 | if !unit.Shown || unit.Text != "Position and size, as a percentage of the frame" { |
| 602 | t.Errorf("Position and size open: its line is %+v; want it drawn, saying the numbers are percentages of the frame", unit) |
| 603 | } |
| 604 | if got := axName(t, te.rig, `[slides-box-fields]`); got != unit.Text { |
| 605 | t.Errorf("the four fields' group is named %q, want %q", got, unit.Text) |
| 606 | } |
| 607 | // Another box: still open. |
| 608 | te.selectBoxByID(t, "cedge0000001") |
| 609 | var open bool |
| 610 | te.rig.Run(evalInto(&open, `document.querySelector('[slides-place]').open`)) |
| 611 | if !open { |
| 612 | t.Error("Position and size closed when another box was selected; want it as it was left") |
| 613 | } |
| 614 | te.openOn("boxcanvas001") |
| 615 | te.selectBoxByID(t, "cshape000001") |
| 616 | te.rig.Run(evalInto(&open, `document.querySelector('[slides-place]').open`)) |
| 617 | if open { |
| 618 | t.Error("after a reload Position and size is open; want it closed (not remembered across reloads)") |
| 619 | } |
| 620 | var stored []string |
| 621 | te.rig.Run(evalInto(&stored, `Object.keys(sessionStorage).filter(k => k.includes('place'))`)) |
| 622 | if len(stored) != 0 { |
| 623 | t.Errorf("session storage holds %v; want nothing kept for Position and size", stored) |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | // TestQuieterBlockCardImageLimits (ruling R17): an image's options show |
| 628 | // its limits in one quiet line under the file field, describing it; a |
| 629 | // heading's options do not show it. |
| 630 | func TestQuieterBlockCardImageLimits(t *testing.T) { |
| 631 | var slide string |
| 632 | store, err := blobs.Dir(t.TempDir()) |
| 633 | if err != nil { |
| 634 | t.Fatal(err) |
| 635 | } |
| 636 | te := newTidyEditorWith(t, store, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { |
| 637 | var path string |
| 638 | path, slide = deckWithEveryKind(t, a, cookies) |
| 639 | return path |
| 640 | }) |
| 641 | te.openOn(slide) |
| 642 | te.rig.Run(waitFor(editorBound)) |
| 643 | const hint = `document.querySelector('#slides-asset-hint')` |
| 644 | te.selectBox("image") |
| 645 | var seen struct { |
| 646 | Shown, Describes bool |
| 647 | Text string |
| 648 | } |
| 649 | te.rig.Run(evalInto(&seen, `(h => ({ Shown: h.checkVisibility() && h.getBoundingClientRect().height > 0, Text: h.textContent.trim(), |
| 650 | Describes: document.querySelector('#rst-block-image').getAttribute('aria-describedby') === h.id }))(`+hint+`)`)) |
| 651 | if !seen.Shown || !seen.Describes || seen.Text != "PNG, JPEG, WebP or SVG, up to 20 MB." { |
| 652 | t.Errorf("an image selected: the limits line is %+v; want it drawn, saying the limits, describing the file field", seen) |
| 653 | } |
| 654 | te.selectBox("heading") |
| 655 | var shown bool |
| 656 | te.rig.Run(evalInto(&shown, hint+`.checkVisibility()`)) |
| 657 | if shown { |
| 658 | t.Error("a heading selected: the image's limits line is drawn") |
| 659 | } |
| 660 | } |
| 661 | |