oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3// Choosing how a box appears (steps and presenter spec §4, In the
4// editor; fixtures/inspector-steps.mjs, box-menu-appears.mjs,
5// step-marks.mjs): the Appears row at the head of the box's card and
6// the same three rows in the box's menu, After… placing a box between
7// two presses, the press numbers drawn while a box is selected, the
8// stage always finished, one undo per choice, and nothing new on a
9// slide without steps. Every write is read back over the API.
10package slidestest
11
12import (
13 "context"
14 "fmt"
15 "net/http"
16 "strings"
17 "testing"
18
19 "github.com/chromedp/chromedp"
20 "github.com/chromedp/chromedp/kb"
21)
22
23// stepsEditorDeck: a flow slide whose two texts arrive on presses 1 and
24// 2, with a heading, a later text and a three-item list arriving with
25// the slide; and a slide with no steps at all.
26const stepsEditorDeck = `[
27 {"op":"add_slide","id":"stepslide01","layout":"flow","template":"title-body"},
28 {"op":"add_block","slide":"stepslide01","id":"stephead0001","type":"heading","region":"heading","props":{"level":1},"markdown":"Steps"},
29 {"op":"add_block","slide":"stepslide01","id":"steptext0001","type":"text","region":"body","markdown":"First"},
30 {"op":"add_block","slide":"stepslide01","id":"steptext0002","type":"text","region":"body","markdown":"Second"},
31 {"op":"add_block","slide":"stepslide01","id":"steplist0001","type":"list","region":"body","props":{"style":"bullet"},"markdown":"- one\n- two\n- three"},
32 {"op":"add_block","slide":"stepslide01","id":"steptext0003","type":"text","region":"body","markdown":"Later"},
33 {"op":"set_block","slide":"stepslide01","id":"steptext0001","step":1},
34 {"op":"set_block","slide":"stepslide01","id":"steptext0002","step":2},
35 {"op":"add_slide","id":"plainslide1","layout":"flow","template":"title-body"},
36 {"op":"add_block","slide":"plainslide1","id":"plainhead001","type":"heading","region":"heading","props":{"level":1},"markdown":"No steps"},
37 {"op":"add_block","slide":"plainslide1","id":"plaintext001","type":"text","region":"body","markdown":"Words"},
38 {"op":"add_block","slide":"plainslide1","id":"plainlist001","type":"list","region":"body","props":{"style":"bullet"},"markdown":"- a\n- b"}]`
39
40// openStepsEditor opens the editor on stepsEditorDeck's first slide,
41// and an agent's token to read the deck back.
42func openStepsEditor(t *testing.T) (*tidyEditor, string) {
43 t.Helper()
44 te := openTidyEditorOn(t, 1280, 800, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, stepsEditorDeck) })
45 token := mintAgent(t, te.app, te.cookies, "steps-editor")
46 te.goToSlide("stepslide01")
47 return te, token
48}
49
50// stepOf is block id's step on slide s as the API reads it, 0 for none;
51// itemsOf its items.
52func stepOf(s map[string]any, id string) float64 {
53 b, _ := blockOf(s, id)
54 v, _ := b["step"].(float64)
55 return v
56}
57func itemsOf(s map[string]any, id string) string {
58 b, _ := blockOf(s, id)
59 v, _ := b["items"].(string)
60 return v
61}
62
63// stepsAre is an untilSlide test: every id has the step given.
64func stepsAre(want map[string]float64) func(map[string]any) bool {
65 return func(s map[string]any) bool {
66 for id, v := range want {
67 if stepOf(s, id) != v {
68 return false
69 }
70 }
71 return true
72 }
73}
74
75// appearsRow is the card's Appears segment for choice, by its radio.
76func appearsRow(choice string) string {
77 return `[slides-inspector-form][data-section="block"] [data-group="appears"] input[name="rst-block-appears"][value="` + choice + `"]`
78}
79
80// clickLabelOf clicks the middle of the label round the input sel, as
81// a person does: the radio itself is drawn as its segment.
82func clickLabelOf(sel string) chromedp.Action[chromedp.Void] {
83 return chromedp.Func(func(ctx2 context.Context, t *chromedp.Target) error {
84 var at []float64
85 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 {
86 return err
87 }
88 _, err := chromedp.MouseClickXY(at[0], at[1])(ctx2, t)
89 return err
90 })
91}
92
93// cardAppearsJS is the checked segment of the card's Appears row, and the
94// segments drawn, in order.
95const cardAppearsJS = `(() => {
96 const inputs = [...document.querySelectorAll('` + `[data-group="appears"] input[name="rst-block-appears"]` + `')]
97 const c = inputs.find(i => i.checked)
98 return { Checked: c ? c.value : '', Drawn: inputs.filter(i => i.closest('label').checkVisibility()).map(i => i.value) }
99})()`
100
101type appearsState struct {
102 Checked string
103 Drawn []string
104}
105
106func (te *tidyEditor) cardAppears() appearsState {
107 var s appearsState
108 te.rig.Run(evalInto(&s, cardAppearsJS))
109 return s
110}
111
112// menuAppearsJS: the box menu's Appears rows, the checked one and the
113// rows drawn; with each row's role.
114const menuAppearsJS = `(() => {
115 const rows = [...document.querySelectorAll('[slides-box-menu] [data-appears]')]
116 const c = rows.find(r => r.getAttribute('aria-checked') === 'true')
117 return { Checked: c ? c.dataset.appears : '', Drawn: rows.filter(r => r.checkVisibility()).map(r => r.dataset.appears),
118 Roles: rows.map(r => r.getAttribute('role')).join(' ') }
119})()`
120
121type menuAppearsState struct {
122 Checked string
123 Drawn []string
124 Roles string
125}
126
127func (te *tidyEditor) menuAppears() menuAppearsState {
128 var s menuAppearsState
129 te.rig.Run(openBoxMenu(), evalInto(&s, menuAppearsJS))
130 return s
131}
132
133// stepMarksJS: the press numbers drawn over the stage, in document order;
134// with where the overlay is and whether it is hidden from readers.
135const stepMarksJS = `(() => {
136 const o = document.querySelector('[slides-step-marks]')
137 if (!o) return { Numbers: [], Hidden: '', InSlide: false, Shown: false }
138 return { Numbers: [...o.querySelectorAll('[slides-step-mark]')].filter(m => m.checkVisibility()).map(m => m.textContent),
139 Hidden: o.getAttribute('aria-hidden') || '', InSlide: !!o.closest('[slides-slide]'), Shown: o.checkVisibility() }
140})()`
141
142type stepMarksState struct {
143 Numbers []string
144 Hidden string
145 InSlide bool
146 Shown bool
147}
148
149func (te *tidyEditor) stepMarks() stepMarksState {
150 var m stepMarksState
151 te.rig.Run(evalInto(&m, stepMarksJS))
152 return m
153}
154
155// deselect gives the selected box up: the card back on the slide.
156func (te *tidyEditor) deselect() {
157 te.rig.Run(
158 chromedp.KeyEvent(kb.Escape),
159 waitFor(`document.querySelector('[slides-editor]').dataset.inspect === 'slide'`),
160 )
161}
162
163// TestAppearsRowSetsTheStep: the Appears row is the first group of the
164// box's card; On a press on a box with no step writes one more than the
165// slide's largest, and With the slide takes it away. On a press on a
166// list with items "each" left over clears them.
167func TestAppearsRowSetsTheStep(t *testing.T) {
168 te, token := openStepsEditor(t)
169 te.selectBoxByID(t, "steptext0003")
170 var first string
171 te.rig.Run(evalInto(&first, `[...document.querySelectorAll('[slides-inspector-form][data-section="block"] > [data-group]')].find(g => g.checkVisibility())?.dataset.group`))
172 if first != "appears" {
173 t.Errorf("the box's card begins with the %q group, want appears", first)
174 }
175 if got := te.cardAppears(); got.Checked != "slide" || strings.Join(got.Drawn, " ") != "slide press" {
176 t.Errorf("a text with no step: the Appears row is %+v, want slide checked of slide press", got)
177 }
178 te.rig.Run(clickLabelOf(appearsRow("press")))
179 te.untilSlide(t, token, "stepslide01", "with the later text on press 3", stepsAre(map[string]float64{"steptext0003": 3, "steptext0001": 1, "steptext0002": 2}))
180 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`))
181 te.rig.Screen(`[slides-inspector]`, "the Appears row, On a press")
182
183 te.rig.Run(clickLabelOf(appearsRow("slide")))
184 te.untilSlide(t, token, "stepslide01", "with the later text back with the slide", stepsAre(map[string]float64{"steptext0003": 0}))
185 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`))
186
187 // A list left with items "each" and no step arrives with the slide;
188 // On a press puts it on a press whole, items taken away with it.
189 apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token,
190 `{"ops":[{"op":"set_block","slide":"stepslide01","id":"steplist0001","items":"each"}]}`, http.StatusOK)
191 te.untilSlide(t, token, "stepslide01", "the list with items each", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" })
192 te.selectBoxByID(t, "steplist0001")
193 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`))
194 te.rig.Run(clickLabelOf(appearsRow("press")))
195 s := te.untilSlide(t, token, "stepslide01", "the list on a press", stepsAre(map[string]float64{"steplist0001": 3}))
196 if itemsOf(s, "steplist0001") != "" {
197 t.Errorf("On a press left the list's items %q; want them cleared, the list on one press", itemsOf(s, "steplist0001"))
198 }
199 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`))
200}
201
202// TestAppearsInTheBoxMenuMatchesTheCard: the box's menu has the same
203// three choices as menuitemradio rows, checked as the card is; a row
204// writes what the card's segment does, and the card follows.
205func TestAppearsInTheBoxMenuMatchesTheCard(t *testing.T) {
206 te, token := openStepsEditor(t)
207 te.selectBoxByID(t, "steptext0001")
208 card, menu := te.cardAppears(), te.menuAppears()
209 if card.Checked != "press" || menu.Checked != "press" {
210 t.Errorf("a text on press 1: the card checks %q and the menu %q, want press in both", card.Checked, menu.Checked)
211 }
212 if menu.Roles != "menuitemradio menuitemradio menuitemradio" {
213 t.Errorf("the menu's Appears rows are %q, want three menuitemradio", menu.Roles)
214 }
215 te.rig.Screen(`[slides-editor]`, "the box's menu with its Appears rows")
216 te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed))
217
218 te.selectBoxByID(t, "steplist0001")
219 menu = te.menuAppears()
220 if menu.Checked != "slide" || strings.Join(menu.Drawn, " ") != "slide press each" {
221 t.Errorf("the list's menu: %+v, want slide checked of slide press each", menu)
222 }
223 te.rig.Run(
224 waitFor(`document.querySelector('[slides-box-menu] [data-appears="each"]')?.checkVisibility()`),
225 chromedp.Click(chromedp.CSS(`[slides-box-menu] [data-appears="each"]`)),
226 waitFor(menuClosed),
227 )
228 s := te.untilSlide(t, token, "stepslide01", "the list item by item", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" })
229 if stepOf(s, "steplist0001") != 3 {
230 t.Errorf("Item by item on a list with no step wrote step %v, want 3", stepOf(s, "steplist0001"))
231 }
232 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("each") + `').checked`))
233 if menu := te.menuAppears(); menu.Checked != "each" {
234 t.Errorf("after Item by item the menu checks %q", menu.Checked)
235 }
236 te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed))
237 // The card's With the slide, and the menu follows.
238 te.rig.Run(clickLabelOf(appearsRow("slide")))
239 te.untilSlide(t, token, "stepslide01", "the list with the slide", func(s map[string]any) bool {
240 return stepOf(s, "steplist0001") == 0 && itemsOf(s, "steplist0001") == ""
241 })
242 if menu := te.menuAppears(); menu.Checked != "slide" {
243 t.Errorf("after With the slide in the card the menu checks %q", menu.Checked)
244 }
245}
246
247// TestItemByItemOnlyOnLists: Item by item is drawn, in the card and in
248// the menu, for a list and for nothing else.
249func TestItemByItemOnlyOnLists(t *testing.T) {
250 te, _ := openStepsEditor(t)
251 for _, c := range []struct {
252 id string
253 want string
254 }{{"stephead0001", "slide press"}, {"steptext0002", "slide press"}, {"steplist0001", "slide press each"}} {
255 te.selectBoxByID(t, c.id)
256 if got := te.cardAppears(); strings.Join(got.Drawn, " ") != c.want {
257 t.Errorf("%s: the card draws %v, want %s", c.id, got.Drawn, c.want)
258 }
259 if got := te.menuAppears(); strings.Join(got.Drawn, " ") != c.want {
260 t.Errorf("%s: the menu draws %v, want %s", c.id, got.Drawn, c.want)
261 }
262 te.rig.Run(chromedp.KeyEvent(kb.Escape), waitFor(menuClosed))
263 te.deselect()
264 }
265}
266
267// TestAfterPlacesABoxBetweenTwoPresses: After… lists the slide's
268// presses, and Esc closes it back onto After…; choosing After press 1
269// writes the midpoint of presses 1 and 2, and no other box's step
270// changes.
271func TestAfterPlacesABoxBetweenTwoPresses(t *testing.T) {
272 te, token := openStepsEditor(t)
273 te.selectBoxByID(t, "steptext0003")
274 after := `[slides-inspector-form][data-section="block"] [popovertarget="slides-pop-after"]`
275 te.rig.Run(
276 waitFor(`document.querySelector('`+after+`')?.checkVisibility()`),
277 chromedp.Click(chromedp.CSS(after)),
278 waitFor(`document.querySelector('#slides-pop-after').matches(':popover-open')`),
279 )
280 var rows []string
281 te.rig.Run(evalInto(&rows, `[...document.querySelectorAll('#slides-pop-after [data-after]')].map(r => r.textContent.trim())`))
282 if strings.Join(rows, "|") != "After press 1|After press 2" {
283 t.Errorf("After… lists %q, want After press 1 and After press 2", rows)
284 }
285 te.rig.Screen(`[slides-editor]`, "After…, listing the slide's presses")
286 // Esc closes it, focus back on After…, the box still selected.
287 te.rig.Run(
288 chromedp.KeyEvent(kb.Escape),
289 waitFor(`!document.querySelector('#slides-pop-after').matches(':popover-open')`),
290 waitFor(`document.activeElement === document.querySelector('`+after+`')`),
291 waitFor(`document.querySelector('[slides-editor]').dataset.inspect === 'box'`),
292 chromedp.Click(chromedp.CSS(after)),
293 waitFor(`document.querySelector('#slides-pop-after').matches(':popover-open')`),
294 )
295 te.rig.Run(
296 waitFor(`document.querySelector('#slides-pop-after [data-after]')?.checkVisibility()`),
297 chromedp.Click(chromedp.CSS(`#slides-pop-after [data-after]`)),
298 waitFor(`!document.querySelector('#slides-pop-after').matches(':popover-open')`),
299 )
300 te.untilSlide(t, token, "stepslide01", "the later text between presses 1 and 2",
301 stepsAre(map[string]float64{"steptext0003": 1.5, "steptext0001": 1, "steptext0002": 2}))
302 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("press") + `').checked`))
303 // Its number is now 2, and Second's 3: numbered by press, not by value.
304 te.rig.Run(waitFor(`(() => {
305 const top = id => document.querySelector('[slides-block][data-id="' + id + '"]').getBoundingClientRect().top
306 const by = n => [...document.querySelectorAll('[slides-step-marks] [slides-step-mark]')].find(m => m.textContent === n)
307 const near = (n, id) => by(n) && Math.abs(by(n).getBoundingClientRect().top - top(id)) <= 4
308 return near('1', 'steptext0001') && near('2', 'steptext0003') && near('3', 'steptext0002')
309 })()`))
310}
311
312// TestPressNumbersShowOnlyWhileABoxIsSelected: with nothing selected no
313// number is drawn; with any box on a slide with presses selected, each
314// box on a press has its number, in an overlay outside the slide hidden
315// from readers; given up, they go.
316func TestPressNumbersShowOnlyWhileABoxIsSelected(t *testing.T) {
317 te, _ := openStepsEditor(t)
318 if m := te.stepMarks(); len(m.Numbers) != 0 || m.Shown {
319 t.Errorf("nothing selected: %+v, want no numbers drawn", m)
320 }
321 te.selectBoxByID(t, "stephead0001")
322 te.rig.Run(waitFor(`document.querySelectorAll('[slides-step-marks] [slides-step-mark]').length === 2`))
323 m := te.stepMarks()
324 if strings.Join(m.Numbers, " ") != "1 2" || m.Hidden != "true" || m.InSlide || !m.Shown {
325 t.Errorf("the heading selected: %+v, want 1 2 drawn, aria-hidden, outside the slide", m)
326 }
327 // Each number sits in its box's margin: at its top, before its start.
328 var off string
329 te.rig.Run(evalInto(&off, `(() => {
330 const out = []
331 for (const [n, id] of [['1', 'steptext0001'], ['2', 'steptext0002']]) {
332 const m = [...document.querySelectorAll('[slides-step-mark]')].find(x => x.textContent === n).getBoundingClientRect()
333 const b = document.querySelector('[slides-block][data-id="' + id + '"]').getBoundingClientRect()
334 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))
335 }
336 return out.join('; ')
337 })()`))
338 if off != "" {
339 t.Errorf("the numbers are not in their boxes' margins: %s", off)
340 }
341 te.rig.Screen(`[slides-stage-pane]`, "press numbers beside the boxes, a heading selected")
342 te.deselect()
343 te.rig.Run(waitFor(`!document.querySelector('[slides-step-marks]')?.checkVisibility()`))
344 if m := te.stepMarks(); len(m.Numbers) != 0 {
345 t.Errorf("given up: %v still drawn", m.Numbers)
346 }
347}
348
349// TestTheStageStaysFinished: the editor's stage shows the finished
350// slide whatever the steps: nothing on it waits for a press, before or
351// after a box is put on one.
352func TestTheStageStaysFinished(t *testing.T) {
353 te, token := openStepsEditor(t)
354 waiting := `document.querySelectorAll('[slides-stage] [data-waiting], [slides-stage][data-waiting]').length`
355 var n int
356 te.rig.Run(evalInto(&n, waiting))
357 if n != 0 {
358 t.Errorf("at first %d things on the stage wait for a press", n)
359 }
360 te.selectBoxByID(t, "steplist0001")
361 te.rig.Run(clickLabelOf(appearsRow("each")))
362 te.untilSlide(t, token, "stepslide01", "the list item by item", func(s map[string]any) bool { return itemsOf(s, "steplist0001") == "each" })
363 te.selectBoxByID(t, "stephead0001")
364 te.rig.Run(clickLabelOf(appearsRow("press")))
365 te.untilSlide(t, token, "stepslide01", "the heading on a press", stepsAre(map[string]float64{"stephead0001": 4}))
366 te.rig.Run(chromedp.Sleep(300e6), evalInto(&n, waiting))
367 if n != 0 {
368 t.Errorf("with every box on a press, %d things on the stage wait for one", n)
369 }
370 var hidden int
371 te.rig.Run(evalInto(&hidden, `[...document.querySelectorAll('[slides-stage] [slides-block], [slides-stage] li')].filter(e => !e.checkVisibility({ visibilityProperty: true, opacityProperty: true })).length`))
372 if hidden != 0 {
373 t.Errorf("%d boxes or items on the stage are not drawn", hidden)
374 }
375}
376
377// TestUndoTakesAStepBack: one undo takes one choice back, step and
378// items together, and the card follows.
379func TestUndoTakesAStepBack(t *testing.T) {
380 te, token := openStepsEditor(t)
381 te.selectBoxByID(t, "steplist0001")
382 te.rig.Run(clickLabelOf(appearsRow("each")))
383 te.untilSlide(t, token, "stepslide01", "the list item by item on press 3", func(s map[string]any) bool {
384 return itemsOf(s, "steplist0001") == "each" && stepOf(s, "steplist0001") == 3
385 })
386 te.rig.Run(
387 chromedp.Click(chromedp.CSS(`[slides-menu="edit"] > summary`)),
388 waitFor(`document.querySelector('[slides-menu="edit"]')?.open === true`),
389 chromedp.Click(chromedp.CSS(`[slides-menu="edit"] [data-command="undo"]`)),
390 )
391 s := te.untilSlide(t, token, "stepslide01", "the list back with the slide", func(s map[string]any) bool {
392 return stepOf(s, "steplist0001") == 0
393 })
394 if itemsOf(s, "steplist0001") != "" {
395 t.Errorf("one undo left items %q; want step and items taken back together", itemsOf(s, "steplist0001"))
396 }
397 if stepOf(s, "steptext0001") != 1 || stepOf(s, "steptext0002") != 2 {
398 t.Errorf("one undo changed other boxes: %v, %v", stepOf(s, "steptext0001"), stepOf(s, "steptext0002"))
399 }
400 te.selectBoxByID(t, "steplist0001")
401 te.rig.Run(waitFor(`document.querySelector('` + appearsRow("slide") + `').checked`))
402}
403
404// TestASlideWithoutStepsShowsNothingNew: on a slide with no steps a
405// selected box shows no numbers and no After…, nothing on the stage
406// waits, and the card's Appears row says With the slide.
407func TestASlideWithoutStepsShowsNothingNew(t *testing.T) {
408 te, _ := openStepsEditor(t)
409 te.goToSlide("plainslide1")
410 for _, id := range []string{"plainhead001", "plaintext001", "plainlist001"} {
411 te.selectBoxByID(t, id)
412 var got string
413 te.rig.Run(chromedp.Sleep(200e6), evalInto(&got, `(() => {
414 const out = []
415 const after = document.querySelector('[popovertarget="slides-pop-after"]')
416 if (after && after.checkVisibility()) out.push('After… is drawn')
417 const marks = document.querySelector('[slides-step-marks]')
418 if (marks && (marks.checkVisibility() || marks.children.length)) out.push('the press numbers are drawn')
419 if (document.querySelector('[slides-stage] [data-waiting], [slides-stage] [data-press]')) out.push('the stage marks a press')
420 return out.join('; ')
421 })()`))
422 if got != "" {
423 t.Errorf("%s selected on a slide without steps: %s", id, got)
424 }
425 if c := te.cardAppears(); c.Checked != "slide" {
426 t.Errorf("%s: the Appears row checks %q, want slide", id, c.Checked)
427 }
428 te.deselect()
429 }
430 te.rig.Screen(`[slides-editor]`, fmt.Sprintf("a slide without steps, %s", "nothing selected"))
431}
432