//go:build browser // The menu on a box (the quieter editor, spec §4; fixtures/box-menu.mjs), // held in a real Chromium: the round button's place, the three ways the // menu opens, what each row does to the slide (read back over the API), // the box's keys, and the keyboard alone from the top of the page. package slidestest import ( "fmt" "net/http" "strings" "testing" "amadan.net/rastrillo/rastrillo/blobs" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // boxMenuDeck: a flow slide holding a heading, a text and a shape; and a // canvas slide holding two shapes that overlap in the layer order (the // rectangle over the ellipse) and a third against the slide's top end // corner. const boxMenuDeck = `[ {"op":"add_slide","id":"boxflow00001","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"boxflow00001","id":"boxhead00001","type":"heading","region":"heading","props":{"level":1},"markdown":"A heading"}, {"op":"add_block","slide":"boxflow00001","id":"boxtext00001","type":"text","region":"body","markdown":"First words"}, {"op":"add_block","slide":"boxflow00001","id":"boxshape0001","type":"shape","region":"body","props":{"kind":"rect"}}, {"op":"add_slide","id":"boxcanvas001","layout":"canvas","template":"blank"}, {"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}, {"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}, {"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}]` // boxSel is the box with this id on the stage. func boxSel(id string) string { return `[slides-stage] [slides-block][data-id="` + id + `"]` } // selectBoxByID clicks the box (near its top start corner, clear of // any box over its middle) and waits until it is the selection with its // round button drawn. func (te *tidyEditor) selectBoxByID(t *testing.T, id string) { t.Helper() var at []float64 te.rig.Run( waitFor(`document.querySelector('`+boxSel(id)+`')`), evalInto(&at, `(r => [r.left + Math.min(20, r.width / 3), r.top + Math.min(20, r.height / 3)])(document.querySelector('`+boxSel(id)+`').getBoundingClientRect())`), ) te.rig.Run( chromedp.MouseClickXY(at[0], at[1]), waitFor(`document.querySelector('`+boxSel(id)+`')?.getAttribute('aria-selected') === 'true'`), waitFor(`document.querySelector('[slides-box-more]').checkVisibility()`), ) } // roundButtonJS reports what is wrong with the round button: not drawn, // under 24x24, outside the stage pane, or over one of the selected // box's handles (the canvas overlay's, when it is drawn). "" is the pass. const roundButtonJS = `(() => { const b = document.querySelector('[slides-box-more]') if (!b || !b.checkVisibility()) return 'the round button is not drawn' const r = b.getBoundingClientRect(), p = document.querySelector('[slides-stage-pane]').getBoundingClientRect() const out = [] if (r.width < 24 || r.height < 24) out.push('it is ' + r.width + 'x' + r.height) 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)) for (const h of document.querySelectorAll('[slides-stage] [slides-handle]')) { const q = h.getBoundingClientRect() 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') } return out.join('; ') })()` // menuOpenJS reports what is wrong with the open menu: closed, outside // the window, or without focus. "" is the pass. const menuOpenJS = `(() => { const m = document.querySelector('[slides-box-menu]') if (!m.matches(':popover-open')) return 'the menu is closed' const r = m.getBoundingClientRect(), out = [] 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) if (!document.activeElement || !document.activeElement.closest('[slides-box-menu]')) out.push('focus is not in the menu') return out.join('; ') })()` // menuRowsJS: the menu's rows drawn, by data-row, in order. const menuRowsJS = `[...document.querySelectorAll('[slides-box-menu] [data-row]')].filter(r => r.checkVisibility()).map(r => r.dataset.row)` const menuClosed = `!document.querySelector('[slides-box-menu]').matches(':popover-open')` // openBoxMenu opens the selected box's menu with its round button. func openBoxMenu() chromedp.Action[chromedp.Void] { return chromedp.Steps( waitFor(`document.querySelector('[slides-box-more]').checkVisibility()`), chromedp.Click(chromedp.CSS(`[slides-box-more]`)), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), ) } // pressBoxMenuRow opens the selected box's menu with its round button // and presses one row: a data-row, or a region's data-region. It is how // a test does what the card's arrange and delete buttons did. func pressBoxMenuRow(row string) chromedp.Action[chromedp.Void] { sel := `[slides-box-menu] [data-row="` + row + `"]` if strings.HasPrefix(row, "region:") { sel = `[slides-box-menu] [data-region="` + strings.TrimPrefix(row, "region:") + `"]` } return chromedp.Steps( openBoxMenu(), waitFor(`document.querySelector('`+sel+`').checkVisibility()`), chromedp.Click(chromedp.CSS(sel)), waitFor(menuClosed), ) } // focusOnBox is a JS expression: focus is on box id itself, the canvas // overlay over it, or its text. func focusOnBox(id string) string { return `(a => !!a && (a.matches('[slides-canvas-overlay]') ? document.querySelector('` + boxSel(id) + `')?.getAttribute('aria-selected') === 'true' : !!a.closest('` + boxSel(id) + `')))(document.activeElement)` } // blockOf is block id of slide s as the API reads it, and its place in // the slide's order; nil when it has gone. func blockOf(s map[string]any, id string) (map[string]any, int) { blocks, _ := s["blocks"].([]any) for i, b := range blocks { if m := b.(map[string]any); m["id"] == id { return m, i } } return nil, -1 } func zOf(s map[string]any, id string) float64 { b, _ := blockOf(s, id) if b == nil { return -1 } z, _ := b["z"].(float64) return z } // TestQuieterBoxMenu (spec §4.1, §4.2, §10): on a canvas shape and on a // flow heading, the round button is drawn on selection, at least 24px, // inside the stage pane and clear of every handle (a box against the // slide's edge too); the button, Shift+F10 and a right-click each open // the menu inside the window, with only the rows that apply; Esc gives // focus back to the box; and each row does what its card button or // field did, read back over the API. func TestQuieterBoxMenu(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) token := mintAgent(t, te.app, te.cookies, "box-menu") canvasRows := []string{"block-format", "block-forward", "block-back", "block-move-up", "block-move-down", "block-place", "block-delete"} flowRows := []string{"block-format", "block-move-up", "block-move-down", "region", "block-delete"} opens := func(where string, rows []string, id string) { t.Helper() var got string var drawn []string te.rig.Run(evalInto(&got, menuOpenJS), evalInto(&drawn, menuRowsJS)) if got != "" { t.Errorf("%s: %s", where, got) } if strings.Join(drawn, " ") != strings.Join(rows, " ") { t.Errorf("%s: the menu draws %v, want %v", where, drawn, rows) } te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed)) var back bool te.rig.Run(evalInto(&back, focusOnBox(id))) if !back { t.Errorf("%s: Esc left focus %+v; want it on the box", where, te.focus()) } var selected bool te.rig.Run(evalInto(&selected, `document.querySelector('`+boxSel(id)+`').getAttribute('aria-selected') === 'true'`)) if !selected { t.Errorf("%s: Esc gave the box up; want it still selected", where) } } // The canvas slide. te.openOn("boxcanvas001") for _, id := range []string{"cshape000001", "cedge0000001"} { te.selectBoxByID(t, id) var handles int te.rig.Run(evalInto(&handles, `[...document.querySelectorAll('[slides-stage] [slides-handle]')].filter(h => h.getBoundingClientRect().width > 0).length`)) if handles != 8 { t.Errorf("%s selected: %d handles drawn, want 8", id, handles) } var got string te.rig.Run(evalInto(&got, roundButtonJS)) if got != "" { t.Errorf("%s selected: %s", id, got) } } te.rig.Screen(`[slides-stage-pane]`, "the round button on a box against the slide's top end corner") te.selectBoxByID(t, "cshape000001") te.rig.Run(openBoxMenu()) te.rig.Screen(`[slides-editor]`, "the menu on a canvas shape") opens("a canvas shape, from its button", canvasRows, "cshape000001") te.rig.Run(chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift))) opens("a canvas shape, Shift+F10", canvasRows, "cshape000001") var at []float64 te.rig.Run(evalInto(&at, `(r => [r.left + r.width / 4, r.top + r.height / 4])(document.querySelector('`+boxSel("cshape000001")+`').getBoundingClientRect())`)) te.rig.Run(chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`)) opens("a canvas shape, right-click", canvasRows, "cshape000001") // Each canvas row, read back. The rectangle is over the ellipse. s := te.apiSlide(t, token, "boxcanvas001") if zOf(s, "cshape000001") <= zOf(s, "cshape000002") || zOf(s, "cshape000001") >= zOf(s, "cedge0000001") { t.Fatalf("the deck's layers are not as made: %v", s["blocks"]) } te.rig.Run(pressBoxMenuRow("block-forward")) te.untilSlide(t, token, "boxcanvas001", "the rectangle brought forward", func(s map[string]any) bool { return zOf(s, "cshape000001") > zOf(s, "cedge0000001") }) te.selectBoxByID(t, "cshape000001") te.rig.Run(pressBoxMenuRow("block-back")) te.untilSlide(t, token, "boxcanvas001", "the rectangle sent back", func(s map[string]any) bool { return zOf(s, "cshape000001") < zOf(s, "cedge0000001") }) te.selectBoxByID(t, "cshape000001") te.rig.Run(pressBoxMenuRow("block-move-down")) te.untilSlide(t, token, "boxcanvas001", "the rectangle moved down", func(s map[string]any) bool { _, i := blockOf(s, "cshape000001") return i == 1 }) te.selectBoxByID(t, "cshape000001") te.rig.Run(pressBoxMenuRow("block-move-up")) te.untilSlide(t, token, "boxcanvas001", "the rectangle moved up", func(s map[string]any) bool { _, i := blockOf(s, "cshape000001") return i == 0 }) // Position and size…: the card shown, its group open, focus on Left. te.pressFormat("closed") te.selectBoxByID(t, "cshape000001") te.rig.Run( pressBoxMenuRow("block-place"), waitFor(`document.querySelector('[slides-inspector]').dataset.state === 'open'`), waitFor(`document.querySelector('[slides-place]').open`), waitFor(`document.activeElement === document.querySelector('#rst-block-x')`), ) te.rig.Screen(`[slides-editor]`, "Position and size, opened from the menu") // Format: focus on the box's first option. te.rig.Run( chromedp.KeyEvent(kb.Escape), waitFor(focusOnBox("cshape000001")), pressBoxMenuRow("block-format"), waitFor(`!!document.activeElement.closest('[slides-inspector] form[data-section="block"]') && document.activeElement.checkVisibility()`), ) // Corners are a rectangle's: not drawn for an ellipse. te.rig.Run( waitFor(`document.querySelector('#rst-block-radius').checkVisibility()`), chromedp.Evaluate[chromedp.Void](changeJS("#rst-block-kind", "ellipse")), waitFor(`document.querySelector('`+boxSel("cshape000001")+`')?.dataset.kind === 'ellipse'`), waitFor(`!document.querySelector('#rst-block-radius').checkVisibility()`), ) te.selectBoxByID(t, "cshape000001") te.rig.Run(pressBoxMenuRow("block-delete"), waitFor(`!document.querySelector('`+boxSel("cshape000001")+`')`)) te.untilSlide(t, token, "boxcanvas001", "without the rectangle", func(s map[string]any) bool { b, _ := blockOf(s, "cshape000001") return b == nil }) // The flow slide: a heading. te.openOn("boxflow00001") te.selectBoxByID(t, "boxhead00001") var got string te.rig.Run(evalInto(&got, roundButtonJS)) if got != "" { t.Errorf("the flow heading selected: %s", got) } te.rig.Run(openBoxMenu()) te.rig.Screen(`[slides-editor]`, "the menu on a flow heading") var checked []string te.rig.Run(evalInto(&checked, `[...document.querySelectorAll('[slides-box-menu] [data-region][aria-checked="true"]')].map(b => b.dataset.region)`)) if strings.Join(checked, " ") != "heading" { t.Errorf("the heading's menu checks the regions %v, want its own, heading", checked) } opens("a flow heading, from its button", flowRows, "boxhead00001") te.rig.Run(chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift))) opens("a flow heading, Shift+F10 in its text", flowRows, "boxhead00001") // Right-click on its text with no caret in it. te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), chromedp.Evaluate[chromedp.Void](contextMenuJS(boxSel("boxhead00001"))), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), ) opens("a flow heading with no caret, right-click", flowRows, "boxhead00001") te.rig.Run(pressBoxMenuRow("region:aside")) te.untilSlide(t, token, "boxflow00001", "the heading moved to the aside", func(s map[string]any) bool { b, _ := blockOf(s, "boxhead00001") return b != nil && b["region"] == "aside" }) te.selectBoxByID(t, "boxhead00001") te.rig.Run(pressBoxMenuRow("region:heading")) te.untilSlide(t, token, "boxflow00001", "the heading back in its region", func(s map[string]any) bool { b, _ := blockOf(s, "boxhead00001") return b != nil && b["region"] == "heading" }) te.selectBoxByID(t, "boxhead00001") te.rig.Run(pressBoxMenuRow("block-move-down")) te.untilSlide(t, token, "boxflow00001", "the heading moved down", func(s map[string]any) bool { _, i := blockOf(s, "boxhead00001") return i == 1 }) te.selectBoxByID(t, "boxhead00001") te.rig.Run(pressBoxMenuRow("block-move-up")) te.untilSlide(t, token, "boxflow00001", "the heading moved up", func(s map[string]any) bool { _, i := blockOf(s, "boxhead00001") return i == 0 }) te.selectBoxByID(t, "boxhead00001") te.rig.Run(pressBoxMenuRow("block-delete"), waitFor(`!document.querySelector('`+boxSel("boxhead00001")+`')`)) te.untilSlide(t, token, "boxflow00001", "without the heading", func(s map[string]any) bool { b, _ := blockOf(s, "boxhead00001") return b == nil }) } // contextMenuJS dispatches a right-click's contextmenu event on the // first text inside sel, as the browser does, and answers whether // something took it (defaultPrevented). func contextMenuJS(sel string) string { return `(() => { const box = document.querySelector('` + sel + `') const n = document.createTreeWalker(box, NodeFilter.SHOW_TEXT).nextNode() const target = n ? n.parentElement : box, r = target.getBoundingClientRect() const ev = new MouseEvent('contextmenu', { bubbles: true, cancelable: true, button: 2, clientX: r.left + 4, clientY: r.top + 4 }) target.dispatchEvent(ev); return ev.defaultPrevented })()` } // TestQuieterBoxMenuLeavesTextAlone (spec §4.1, Review Focus 1): with the // caret in a heading's text, a right-click on that text is the // browser's (spelling, paste): nothing prevents it and the box's menu // stays closed. A real right-click on a shape opens the box's menu. func TestQuieterBoxMenuLeavesTextAlone(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) te.selectBoxByID(t, "boxhead00001") te.rig.Run(waitFor(`document.activeElement.closest('` + boxSel("boxhead00001") + `')`)) var prevented bool te.rig.Run(evalInto(&prevented, contextMenuJS(boxSel("boxhead00001")))) if prevented { t.Error("a right-click on the text being edited was prevented: the browser's menu would not open") } var open bool te.rig.Run(evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`)) if open { t.Error("a right-click on the text being edited opened the box's menu") } // A person's right-click there, through the browser's own input: the // same, heard on the window after every listener on the page. var at []float64 te.rig.Run( chromedp.Evaluate[chromedp.Void](`window.__ctx = []; addEventListener('contextmenu', e => { window.__ctx.push(e.defaultPrevented) }); true`), evalInto(&at, `(r => [r.left + 8, r.top + r.height / 2])(document.querySelector('`+boxSel("boxhead00001")+`').getBoundingClientRect())`), ) te.rig.Run(chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), waitFor(`window.__ctx.length === 1`)) var heard []bool te.rig.Run(evalInto(&heard, `window.__ctx`), evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`)) if len(heard) != 1 || heard[0] || open { t.Errorf("a right-click in the heading's text: prevented %v, the box's menu open %v; want neither", heard, open) } // On a shape (a box with no text) it is the box's. te.rig.Run(chromedp.KeyEvent(kb.Escape)) te.selectBoxByID(t, "boxshape0001") te.rig.Run(evalInto(&at, `(r => [r.left + r.width / 2, r.top + r.height / 2])(document.querySelector('`+boxSel("boxshape0001")+`').getBoundingClientRect())`)) te.rig.Run( chromedp.MouseClickXY(at[0], at[1], chromedp.ButtonType(input.MouseButtonRight)), waitFor(`window.__ctx.length === 2`), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), ) te.rig.Run(evalInto(&heard, `window.__ctx`)) if !heard[1] { t.Error("a right-click on the shape was not taken by the box's menu") } } // keyboardDeck: a canvas slide of two shapes, the first over the // second, and a heading after them (the editor is bound once a heading // is editable). const keyboardDeck = `[ {"op":"add_slide","id":"keyscanvas01","layout":"canvas","template":"blank"}, {"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}, {"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}, {"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"}]` // TestQuieterKeyboardOnlyShape (spec §0.3, §4.4, §10): a shape on a // canvas slide, the kind of box with no text to put a caret in, reached // and changed with the keyboard alone from the top of the page, with the // card showing and again with it hidden: Tab to the shape (its overlay, // selected), Shift+F10 and Format into its options, its fill, Esc back, // send it back with Ctrl+[, Position and size… from the menu, Left // typed, Esc back, Delete, and focus on the box next to it. The notes // are closed first: the caret there turns the card to the notes' marks. func TestQuieterKeyboardOnlyShape(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) token := mintAgent(t, te.app, te.cookies, "keyboard-only") te.rig.Run( chromedp.Click(chromedp.CSS(`[slides-notes-close]`)), waitFor(`document.querySelector('[slides-notes-pane]').dataset.state === 'closed'`), ) const shape = "keysshape001" onShape := func(where string) { t.Helper() var s tabStep te.rig.Run(evalInto(&s, tabStepJS)) if !s.In || !s.Overlay || s.ID != shape || !s.Selected || s.Inspect != "box" { t.Errorf("%s: %+v; want focus on the shape's overlay, the shape selected and its options in the card", where, s) } } for _, hidden := range []bool{false, true} { name := "the card showing" if hidden { name = "the card hidden at first" } te.openOn("keyscanvas01") if hidden { te.pressFormat("closed") } // From the top of the page, Tab until the shape is reached (put // back for the second pass, it is the slide's last box). te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) for i := 0; ; i++ { var s tabStep te.rig.Run(chromedp.KeyEvent(kb.Tab), evalInto(&s, tabStepJS)) if s.In && s.ID == shape { break } if i == 80 { t.Fatalf("%s: Tab never reached the shape; focus is %+v", name, te.focus()) } } onShape(name + ", reached by Tab") // Its options, from the menu: Format. te.rig.Run( chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), waitFor(`document.activeElement === document.querySelector('[slides-box-menu] [data-row="block-format"]')`), chromedp.KeyEvent(kb.Enter), waitFor(`document.querySelector('[slides-inspector]').dataset.state === 'open'`), waitFor(`!!document.activeElement.closest('[slides-inspector] form[data-section="block"]') && document.activeElement.checkVisibility()`), ) // Its fill: the colour picker itself is the browser's, so its // choice is written as the picker would. te.tabTo(t, `#rst-block-fill`, 10) te.rig.Run( chromedp.Evaluate[chromedp.Void](`(f => { f.value = '#cc3300'; f.dispatchEvent(new Event('input', {bubbles: true})); f.dispatchEvent(new Event('change', {bubbles: true})) })(document.activeElement)`), waitFor(`document.querySelector('`+boxSel(shape)+`').outerHTML.toLowerCase().includes('cc3300')`), ) te.rig.Run(chromedp.KeyEvent(kb.Escape)) onShape(name + ", Esc in its options") // Send it back, with its key: under the box that was under it. before := te.apiSlide(t, token, "keyscanvas01") var under string blocks, _ := before["blocks"].([]any) for _, b := range blocks { m := b.(map[string]any) if z, _ := m["z"].(float64); z < zOf(before, shape) && (under == "" || z > zOf(before, under)) { under, _ = m["id"].(string) } } if under == "" { t.Fatalf("%s: nothing is under the shape to send it back behind: %v", name, blocks) } te.rig.Run(chromedp.KeyEvent("[", chromedp.KeyModifiers(chromedp.ModifierCtrl))) te.untilSlide(t, token, "keyscanvas01", "the shape sent back", func(s map[string]any) bool { return zOf(s, shape) < zOf(s, under) }) te.rig.Run(waitFor(focusOnBox(shape))) // Position and size…, from the menu: Left typed. te.rig.Run( chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), waitFor(`document.activeElement?.closest('[slides-box-menu]')`), ) for range 10 { var on bool te.rig.Run(evalInto(&on, `document.activeElement === document.querySelector('[slides-box-menu] [data-row="block-place"]')`)) if on { break } te.rig.Run(chromedp.KeyEvent(kb.ArrowDown)) } te.rig.Run( chromedp.KeyEvent(kb.Enter), waitFor(`document.activeElement === document.querySelector('#rst-block-x')`), chromedp.KeyEvent("a", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.KeyEvent("40"), chromedp.KeyEvent(kb.Tab), ) te.untilSlide(t, token, "keyscanvas01", "the shape at 40% from the left", func(s map[string]any) bool { b, _ := blockOf(s, shape) box, _ := b["box"].(map[string]any) return box != nil && box["x"] == 0.4 }) te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(focusOnBox(shape))) onShape(name + ", Esc in Position and size") // Delete, with its key: focus goes on to the next box, or (the // shape last, in the second pass) the one before it, selected. next := "keysshape002" if hidden { next = "keyshead0001" } te.rig.Run( chromedp.KeyEvent(kb.Delete), waitFor(`!document.querySelector('`+boxSel(shape)+`')`), waitFor(focusOnBox(next)+` && document.querySelector('[slides-editor]').dataset.inspect === 'box'`), ) te.untilSlide(t, token, "keyscanvas01", "without the shape", func(s map[string]any) bool { b, _ := blockOf(s, shape) return b == nil }) if !hidden { apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, `{"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) } } } // TestQuieterDeleteKey (spec §4.2, §10): Delete with a shape focused // deletes it, on a flow slide and on a canvas slide; with the caret in a // heading it deletes a character; in the strip it still asks about the // slide. func TestQuieterDeleteKey(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) token := mintAgent(t, te.app, te.cookies, "delete-key") gone := func(slide, id string) { t.Helper() te.rig.Run(waitFor(`!document.querySelector('` + boxSel(id) + `')`)) te.untilSlide(t, token, slide, "without "+id, func(s map[string]any) bool { b, _ := blockOf(s, id) return b == nil }) } // A flow slide's shape, its own tab stop. te.openOn("boxflow00001") te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) te.tabTo(t, boxSel("boxshape0001"), 40) te.rig.Run(chromedp.KeyEvent(kb.Delete)) gone("boxflow00001", "boxshape0001") // The caret in a heading: a character. te.rig.Run( chromedp.Evaluate[chromedp.Void](`(() => { const h = document.querySelector('`+boxSel("boxhead00001")+`'); h.focus() const n = document.createTreeWalker(h, NodeFilter.SHOW_TEXT).nextNode() const r = document.createRange(); r.setStart(n, 0); r.collapse(true); getSelection().removeAllRanges(); getSelection().addRange(r); return true })()`), waitFor(`document.querySelector('`+boxSel("boxhead00001")+`').getAttribute('aria-selected') === 'true'`), chromedp.KeyEvent(kb.Delete), waitFor(`document.querySelector('`+boxSel("boxhead00001")+`')?.textContent.trim() === 'heading'`), ) te.untilSlide(t, token, "boxflow00001", "the heading one letter shorter", func(s map[string]any) bool { b, _ := blockOf(s, "boxhead00001") return b != nil && strings.Contains(fmt.Sprint(b), "heading") && !strings.Contains(fmt.Sprint(b), "A heading") }) // A canvas slide's shape, its overlay. te.openOn("boxcanvas001") te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) var first string te.rig.Run(evalInto(&first, `document.querySelector('[slides-stage] [slides-block][aria-selected="true"]').dataset.id`)) te.rig.Run(chromedp.KeyEvent(kb.Delete)) gone("boxcanvas001", first) // The strip: it asks about the slide, and deletes nothing. var slides int te.rig.Run(evalInto(&slides, `document.querySelectorAll('[slides-strip-item][data-slide-id]').length`)) te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-strip-item]:first-child [slides-strip-link]').focus()`), chromedp.KeyEvent(kb.Delete), waitFor(`document.querySelector('[slides-confirm]').checkVisibility()`), ) var after int te.rig.Run(evalInto(&after, `document.querySelectorAll('[slides-strip-item][data-slide-id]').length`)) if after != slides { t.Errorf("Delete in the strip left %d slides of %d; want it to ask first", after, slides) } te.rig.Run(chromedp.Click(chromedp.CSS(`[data-command="slide-delete-cancel"]`))) } // TestQuieterPlaceClosesOnReload (spec §4.3): Position and size, opened, // says its numbers are percentages of the frame, stays open while the // page is open and is closed again after a reload. func TestQuieterPlaceClosesOnReload(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) te.openOn("boxcanvas001") te.selectBoxByID(t, "cshape000001") te.rig.Run( pressBoxMenuRow("block-place"), waitFor(`document.querySelector('[slides-place]').open`), ) // The numbers' unit is in sight (review: they had none): the group's // one quiet line says they are percentages of the frame, and is // still the four fields' group name. var unit struct { Shown bool Text string } 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'))`)) if !unit.Shown || unit.Text != "Position and size, as a percentage of the frame" { t.Errorf("Position and size open: its line is %+v; want it drawn, saying the numbers are percentages of the frame", unit) } if got := axName(t, te.rig, `[slides-box-fields]`); got != unit.Text { t.Errorf("the four fields' group is named %q, want %q", got, unit.Text) } // Another box: still open. te.selectBoxByID(t, "cedge0000001") var open bool te.rig.Run(evalInto(&open, `document.querySelector('[slides-place]').open`)) if !open { t.Error("Position and size closed when another box was selected; want it as it was left") } te.openOn("boxcanvas001") te.selectBoxByID(t, "cshape000001") te.rig.Run(evalInto(&open, `document.querySelector('[slides-place]').open`)) if open { t.Error("after a reload Position and size is open; want it closed (not remembered across reloads)") } var stored []string te.rig.Run(evalInto(&stored, `Object.keys(sessionStorage).filter(k => k.includes('place'))`)) if len(stored) != 0 { t.Errorf("session storage holds %v; want nothing kept for Position and size", stored) } } // TestQuieterBlockCardImageLimits (ruling R17): an image's options show // its limits in one quiet line under the file field, describing it; a // heading's options do not show it. func TestQuieterBlockCardImageLimits(t *testing.T) { var slide string store, err := blobs.Dir(t.TempDir()) if err != nil { t.Fatal(err) } te := newTidyEditorWith(t, store, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { var path string path, slide = deckWithEveryKind(t, a, cookies) return path }) te.openOn(slide) te.rig.Run(waitFor(editorBound)) const hint = `document.querySelector('#slides-asset-hint')` te.selectBox("image") var seen struct { Shown, Describes bool Text string } te.rig.Run(evalInto(&seen, `(h => ({ Shown: h.checkVisibility() && h.getBoundingClientRect().height > 0, Text: h.textContent.trim(), Describes: document.querySelector('#rst-block-image').getAttribute('aria-describedby') === h.id }))(`+hint+`)`)) if !seen.Shown || !seen.Describes || seen.Text != "PNG, JPEG, WebP or SVG, up to 20 MB." { t.Errorf("an image selected: the limits line is %+v; want it drawn, saying the limits, describing the file field", seen) } te.selectBox("heading") var shown bool te.rig.Run(evalInto(&shown, hint+`.checkVisibility()`)) if shown { t.Error("a heading selected: the image's limits line is drawn") } }