//go:build browser // Choosing how a box appears (steps and presenter spec §4, In the // editor; fixtures/inspector-steps.mjs, box-menu-appears.mjs, // step-marks.mjs): the Appears row at the head of the box's card and // the same three rows in the box's menu, After… placing a box between // two presses, the press numbers drawn while a box is selected, the // stage always finished, one undo per choice, and nothing new on a // slide without steps. Every write is read back over the API. package slidestest import ( "context" "fmt" "net/http" "strings" "testing" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // stepsEditorDeck: a flow slide whose two texts arrive on presses 1 and // 2, with a heading, a later text and a three-item list arriving with // the slide; and a slide with no steps at all. const stepsEditorDeck = `[ {"op":"add_slide","id":"stepslide01","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"stepslide01","id":"stephead0001","type":"heading","region":"heading","props":{"level":1},"markdown":"Steps"}, {"op":"add_block","slide":"stepslide01","id":"steptext0001","type":"text","region":"body","markdown":"First"}, {"op":"add_block","slide":"stepslide01","id":"steptext0002","type":"text","region":"body","markdown":"Second"}, {"op":"add_block","slide":"stepslide01","id":"steplist0001","type":"list","region":"body","props":{"style":"bullet"},"markdown":"- one\n- two\n- three"}, {"op":"add_block","slide":"stepslide01","id":"steptext0003","type":"text","region":"body","markdown":"Later"}, {"op":"set_block","slide":"stepslide01","id":"steptext0001","step":1}, {"op":"set_block","slide":"stepslide01","id":"steptext0002","step":2}, {"op":"add_slide","id":"plainslide1","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"plainslide1","id":"plainhead001","type":"heading","region":"heading","props":{"level":1},"markdown":"No steps"}, {"op":"add_block","slide":"plainslide1","id":"plaintext001","type":"text","region":"body","markdown":"Words"}, {"op":"add_block","slide":"plainslide1","id":"plainlist001","type":"list","region":"body","props":{"style":"bullet"},"markdown":"- a\n- b"}]` // openStepsEditor opens the editor on stepsEditorDeck's first slide, // and an agent's token to read the deck back. func openStepsEditor(t *testing.T) (*tidyEditor, string) { t.Helper() te := openTidyEditorOn(t, 1280, 800, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, stepsEditorDeck) }) token := mintAgent(t, te.app, te.cookies, "steps-editor") te.goToSlide("stepslide01") return te, token } // stepOf is block id's step on slide s as the API reads it, 0 for none; // itemsOf its items. func stepOf(s map[string]any, id string) float64 { b, _ := blockOf(s, id) v, _ := b["step"].(float64) return v } func itemsOf(s map[string]any, id string) string { b, _ := blockOf(s, id) v, _ := b["items"].(string) return v } // stepsAre is an untilSlide test: every id has the step given. func stepsAre(want map[string]float64) func(map[string]any) bool { return func(s map[string]any) bool { for id, v := range want { if stepOf(s, id) != v { return false } } return true } } // appearsRow is the card's Appears segment for choice, by its radio. func appearsRow(choice string) string { return `[slides-inspector-form][data-section="block"] [data-group="appears"] input[name="rst-block-appears"][value="` + choice + `"]` } // clickLabelOf clicks the middle of the label round the input sel, as // a person does: the radio itself is drawn as its segment. func clickLabelOf(sel string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx2 context.Context, t *chromedp.Target) error { var at []float64 if _, err := evalInto(&at, `(r => [r.left + r.width / 2, r.top + r.height / 2])(document.querySelector('`+sel+`').closest('label').getBoundingClientRect())`)(ctx2, t); err != nil { return err } _, err := chromedp.MouseClickXY(at[0], at[1])(ctx2, t) return err }) } // cardAppearsJS is the checked segment of the card's Appears row, and the // segments drawn, in order. const cardAppearsJS = `(() => { const inputs = [...document.querySelectorAll('` + `[data-group="appears"] input[name="rst-block-appears"]` + `')] const c = inputs.find(i => i.checked) return { Checked: c ? c.value : '', Drawn: inputs.filter(i => i.closest('label').checkVisibility()).map(i => i.value) } })()` type appearsState struct { Checked string Drawn []string } func (te *tidyEditor) cardAppears() appearsState { var s appearsState te.rig.Run(evalInto(&s, cardAppearsJS)) return s } // menuAppearsJS: the box menu's Appears rows, the checked one and the // rows drawn; with each row's role. const menuAppearsJS = `(() => { const rows = [...document.querySelectorAll('[slides-box-menu] [data-appears]')] const c = rows.find(r => r.getAttribute('aria-checked') === 'true') return { Checked: c ? c.dataset.appears : '', Drawn: rows.filter(r => r.checkVisibility()).map(r => r.dataset.appears), Roles: rows.map(r => r.getAttribute('role')).join(' ') } })()` type menuAppearsState struct { Checked string Drawn []string Roles string } func (te *tidyEditor) menuAppears() menuAppearsState { var s menuAppearsState te.rig.Run(openBoxMenu(), evalInto(&s, menuAppearsJS)) return s } // stepMarksJS: the press numbers drawn over the stage, in document order; // with where the overlay is and whether it is hidden from readers. const stepMarksJS = `(() => { const o = document.querySelector('[slides-step-marks]') if (!o) return { Numbers: [], Hidden: '', InSlide: false, Shown: false } return { Numbers: [...o.querySelectorAll('[slides-step-mark]')].filter(m => m.checkVisibility()).map(m => m.textContent), Hidden: o.getAttribute('aria-hidden') || '', InSlide: !!o.closest('[slides-slide]'), Shown: o.checkVisibility() } })()` type stepMarksState struct { Numbers []string Hidden string InSlide bool Shown bool } func (te *tidyEditor) stepMarks() stepMarksState { var m stepMarksState te.rig.Run(evalInto(&m, stepMarksJS)) return m } // deselect gives the selected box up: the card back on the slide. func (te *tidyEditor) deselect() { te.rig.Run( chromedp.KeyEvent(kb.Escape), waitFor(`document.querySelector('[slides-editor]').dataset.inspect === 'slide'`), ) } // TestAppearsRowSetsTheStep: the Appears row is the first group of the // box's card; On a press on a box with no step writes one more than the // slide's largest, and With the slide takes it away. On a press on a // list with items "each" left over clears them. func TestAppearsRowSetsTheStep(t *testing.T) { te, token := openStepsEditor(t) te.selectBoxByID(t, "steptext0003") var first string te.rig.Run(evalInto(&first, `[...document.querySelectorAll('[slides-inspector-form][data-section="block"] > [data-group]')].find(g => g.checkVisibility())?.dataset.group`)) if first != "appears" { t.Errorf("the box's card begins with the %q group, want appears", first) } if got := te.cardAppears(); got.Checked != "slide" || strings.Join(got.Drawn, " ") != "slide press" { t.Errorf("a text with no step: the Appears row is %+v, want slide checked of slide press", got) } te.rig.Run(clickLabelOf(appearsRow("press"))) te.untilSlide(t, token, "stepslide01", "with the later text on press 3", stepsAre(map[string]float64{"steptext0003": 3, "steptext0001": 1, "steptext0002": 2})) te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`)) te.rig.Screen(`[slides-inspector]`, "the Appears row, On a press") te.rig.Run(clickLabelOf(appearsRow("slide"))) te.untilSlide(t, token, "stepslide01", "with the later text back with the slide", stepsAre(map[string]float64{"steptext0003": 0})) te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`)) // A list left with items "each" and no step arrives with the slide; // On a press puts it on a press whole, items taken away with it. apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, `{"ops":[{"op":"set_block","slide":"stepslide01","id":"steplist0001","items":"each"}]}`, http.StatusOK) te.untilSlide(t, token, "stepslide01", "the list with items each", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" }) te.selectBoxByID(t, "steplist0001") te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`)) te.rig.Run(clickLabelOf(appearsRow("press"))) s := te.untilSlide(t, token, "stepslide01", "the list on a press", stepsAre(map[string]float64{"steplist0001": 3})) if itemsOf(s, "steplist0001") != "" { t.Errorf("On a press left the list's items %q; want them cleared, the list on one press", itemsOf(s, "steplist0001")) } te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`)) } // TestAppearsInTheBoxMenuMatchesTheCard: the box's menu has the same // three choices as menuitemradio rows, checked as the card is; a row // writes what the card's segment does, and the card follows. func TestAppearsInTheBoxMenuMatchesTheCard(t *testing.T) { te, token := openStepsEditor(t) te.selectBoxByID(t, "steptext0001") card, menu := te.cardAppears(), te.menuAppears() if card.Checked != "press" || menu.Checked != "press" { t.Errorf("a text on press 1: the card checks %q and the menu %q, want press in both", card.Checked, menu.Checked) } if menu.Roles != "menuitemradio menuitemradio menuitemradio" { t.Errorf("the menu's Appears rows are %q, want three menuitemradio", menu.Roles) } te.rig.Screen(`[slides-editor]`, "the box's menu with its Appears rows") te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed)) te.selectBoxByID(t, "steplist0001") menu = te.menuAppears() if menu.Checked != "slide" || strings.Join(menu.Drawn, " ") != "slide press each" { t.Errorf("the list's menu: %+v, want slide checked of slide press each", menu) } te.rig.Run( waitFor(`document.querySelector('[slides-box-menu] [data-appears="each"]')?.checkVisibility()`), chromedp.Click(chromedp.CSS(`[slides-box-menu] [data-appears="each"]`)), waitFor(menuClosed), ) s := te.untilSlide(t, token, "stepslide01", "the list item by item", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" }) if stepOf(s, "steplist0001") != 3 { t.Errorf("Item by item on a list with no step wrote step %v, want 3", stepOf(s, "steplist0001")) } te.rig.Run(waitFor(`document.querySelector('` + appearsRow("each") + `').checked`)) if menu := te.menuAppears(); menu.Checked != "each" { t.Errorf("after Item by item the menu checks %q", menu.Checked) } te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed)) // The card's With the slide, and the menu follows. te.rig.Run(clickLabelOf(appearsRow("slide"))) te.untilSlide(t, token, "stepslide01", "the list with the slide", func(s map[string]any) bool { return stepOf(s, "steplist0001") == 0 && itemsOf(s, "steplist0001") == "" }) if menu := te.menuAppears(); menu.Checked != "slide" { t.Errorf("after With the slide in the card the menu checks %q", menu.Checked) } } // TestItemByItemOnlyOnLists: Item by item is drawn, in the card and in // the menu, for a list and for nothing else. func TestItemByItemOnlyOnLists(t *testing.T) { te, _ := openStepsEditor(t) for _, c := range []struct { id string want string }{{"stephead0001", "slide press"}, {"steptext0002", "slide press"}, {"steplist0001", "slide press each"}} { te.selectBoxByID(t, c.id) if got := te.cardAppears(); strings.Join(got.Drawn, " ") != c.want { t.Errorf("%s: the card draws %v, want %s", c.id, got.Drawn, c.want) } if got := te.menuAppears(); strings.Join(got.Drawn, " ") != c.want { t.Errorf("%s: the menu draws %v, want %s", c.id, got.Drawn, c.want) } te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed)) te.deselect() } } // TestAfterPlacesABoxBetweenTwoPresses: After… lists the slide's // presses, and Esc closes it back onto After…; choosing After press 1 // writes the midpoint of presses 1 and 2, and no other box's step // changes. func TestAfterPlacesABoxBetweenTwoPresses(t *testing.T) { te, token := openStepsEditor(t) te.selectBoxByID(t, "steptext0003") after := `[slides-inspector-form][data-section="block"] [popovertarget="slides-pop-after"]` te.rig.Run( waitFor(`document.querySelector('`+after+`')?.checkVisibility()`), chromedp.Click(chromedp.CSS(after)), waitFor(`document.querySelector('#slides-pop-after').matches(':popover-open')`), ) var rows []string te.rig.Run(evalInto(&rows, `[...document.querySelectorAll('#slides-pop-after [data-after]')].map(r => r.textContent.trim())`)) if strings.Join(rows, "|") != "After press 1|After press 2" { t.Errorf("After… lists %q, want After press 1 and After press 2", rows) } te.rig.Screen(`[slides-editor]`, "After…, listing the slide's presses") // Esc closes it, focus back on After…, the box still selected. te.rig.Run( chromedp.KeyEvent(kb.Escape), waitFor(`!document.querySelector('#slides-pop-after').matches(':popover-open')`), waitFor(`document.activeElement === document.querySelector('`+after+`')`), waitFor(`document.querySelector('[slides-editor]').dataset.inspect === 'box'`), chromedp.Click(chromedp.CSS(after)), waitFor(`document.querySelector('#slides-pop-after').matches(':popover-open')`), ) te.rig.Run( waitFor(`document.querySelector('#slides-pop-after [data-after]')?.checkVisibility()`), chromedp.Click(chromedp.CSS(`#slides-pop-after [data-after]`)), waitFor(`!document.querySelector('#slides-pop-after').matches(':popover-open')`), ) te.untilSlide(t, token, "stepslide01", "the later text between presses 1 and 2", stepsAre(map[string]float64{"steptext0003": 1.5, "steptext0001": 1, "steptext0002": 2})) te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`)) // Its number is now 2, and Second's 3: numbered by press, not by value. te.rig.Run(waitFor(`(() => { const top = id => document.querySelector('[slides-block][data-id="' + id + '"]').getBoundingClientRect().top const by = n => [...document.querySelectorAll('[slides-step-marks] [slides-step-mark]')].find(m => m.textContent === n) const near = (n, id) => by(n) && Math.abs(by(n).getBoundingClientRect().top - top(id)) <= 4 return near('1', 'steptext0001') && near('2', 'steptext0003') && near('3', 'steptext0002') })()`)) } // TestPressNumbersShowOnlyWhileABoxIsSelected: with nothing selected no // number is drawn; with any box on a slide with presses selected, each // box on a press has its number, in an overlay outside the slide hidden // from readers; given up, they go. func TestPressNumbersShowOnlyWhileABoxIsSelected(t *testing.T) { te, _ := openStepsEditor(t) if m := te.stepMarks(); len(m.Numbers) != 0 || m.Shown { t.Errorf("nothing selected: %+v, want no numbers drawn", m) } te.selectBoxByID(t, "stephead0001") te.rig.Run(waitFor(`document.querySelectorAll('[slides-step-marks] [slides-step-mark]').length === 2`)) m := te.stepMarks() if strings.Join(m.Numbers, " ") != "1 2" || m.Hidden != "true" || m.InSlide || !m.Shown { t.Errorf("the heading selected: %+v, want 1 2 drawn, aria-hidden, outside the slide", m) } // Each number sits in its box's margin: at its top, before its start. var off string te.rig.Run(evalInto(&off, `(() => { const out = [] for (const [n, id] of [['1', 'steptext0001'], ['2', 'steptext0002']]) { const m = [...document.querySelectorAll('[slides-step-mark]')].find(x => x.textContent === n).getBoundingClientRect() const b = document.querySelector('[slides-block][data-id="' + id + '"]').getBoundingClientRect() if (m.right > b.left + 0.5 || Math.abs(m.top - b.top) > 4) out.push(n + ' at ' + JSON.stringify(m) + ' for its box ' + JSON.stringify(b)) } return out.join('; ') })()`)) if off != "" { t.Errorf("the numbers are not in their boxes' margins: %s", off) } te.rig.Screen(`[slides-stage-pane]`, "press numbers beside the boxes, a heading selected") te.deselect() te.rig.Run(waitFor(`!document.querySelector('[slides-step-marks]')?.checkVisibility()`)) if m := te.stepMarks(); len(m.Numbers) != 0 { t.Errorf("given up: %v still drawn", m.Numbers) } } // TestTheStageStaysFinished: the editor's stage shows the finished // slide whatever the steps: nothing on it waits for a press, before or // after a box is put on one. func TestTheStageStaysFinished(t *testing.T) { te, token := openStepsEditor(t) waiting := `document.querySelectorAll('[slides-stage] [data-waiting], [slides-stage][data-waiting]').length` var n int te.rig.Run(evalInto(&n, waiting)) if n != 0 { t.Errorf("at first %d things on the stage wait for a press", n) } te.selectBoxByID(t, "steplist0001") te.rig.Run(clickLabelOf(appearsRow("each"))) te.untilSlide(t, token, "stepslide01", "the list item by item", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" }) te.selectBoxByID(t, "stephead0001") te.rig.Run(clickLabelOf(appearsRow("press"))) te.untilSlide(t, token, "stepslide01", "the heading on a press", stepsAre(map[string]float64{"stephead0001": 4})) te.rig.Run(chromedp.Sleep(300e6), evalInto(&n, waiting)) if n != 0 { t.Errorf("with every box on a press, %d things on the stage wait for one", n) } var hidden int te.rig.Run(evalInto(&hidden, `[...document.querySelectorAll('[slides-stage] [slides-block], [slides-stage] li')].filter(e => !e.checkVisibility({ visibilityProperty: true, opacityProperty: true })).length`)) if hidden != 0 { t.Errorf("%d boxes or items on the stage are not drawn", hidden) } } // TestUndoTakesAStepBack: one undo takes one choice back, step and // items together, and the card follows. func TestUndoTakesAStepBack(t *testing.T) { te, token := openStepsEditor(t) te.selectBoxByID(t, "steplist0001") te.rig.Run(clickLabelOf(appearsRow("each"))) te.untilSlide(t, token, "stepslide01", "the list item by item on press 3", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" && stepOf(s, "steplist0001") == 3 }) te.rig.Run( chromedp.Click(chromedp.CSS(`[slides-menu="edit"] > summary`)), waitFor(`document.querySelector('[slides-menu="edit"]')?.open === true`), chromedp.Click(chromedp.CSS(`[slides-menu="edit"] [data-command="undo"]`)), ) s := te.untilSlide(t, token, "stepslide01", "the list back with the slide", func(s map[string]any) bool { return stepOf(s, "steplist0001") == 0 }) if itemsOf(s, "steplist0001") != "" { t.Errorf("one undo left items %q; want step and items taken back together", itemsOf(s, "steplist0001")) } if stepOf(s, "steptext0001") != 1 || stepOf(s, "steptext0002") != 2 { t.Errorf("one undo changed other boxes: %v, %v", stepOf(s, "steptext0001"), stepOf(s, "steptext0002")) } te.selectBoxByID(t, "steplist0001") te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`)) } // TestASlideWithoutStepsShowsNothingNew: on a slide with no steps a // selected box shows no numbers and no After…, nothing on the stage // waits, and the card's Appears row says With the slide. func TestASlideWithoutStepsShowsNothingNew(t *testing.T) { te, _ := openStepsEditor(t) te.goToSlide("plainslide1") for _, id := range []string{"plainhead001", "plaintext001", "plainlist001"} { te.selectBoxByID(t, id) var got string te.rig.Run(chromedp.Sleep(200e6), evalInto(&got, `(() => { const out = [] const after = document.querySelector('[popovertarget="slides-pop-after"]') if (after && after.checkVisibility()) out.push('After… is drawn') const marks = document.querySelector('[slides-step-marks]') if (marks && (marks.checkVisibility() || marks.children.length)) out.push('the press numbers are drawn') if (document.querySelector('[slides-stage] [data-waiting], [slides-stage] [data-press]')) out.push('the stage marks a press') return out.join('; ') })()`)) if got != "" { t.Errorf("%s selected on a slide without steps: %s", id, got) } if c := te.cardAppears(); c.Checked != "slide" { t.Errorf("%s: the Appears row checks %q, want slide", id, c.Checked) } te.deselect() } te.rig.Screen(`[slides-editor]`, fmt.Sprintf("a slide without steps, %s", "nothing selected")) }