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// The presenter window's slide picker (Task 12, spec §5.3): the
4// position opens a popover with every slide of the talk, narrowed by a
5// number or by words, at 105 slides and beyond.
6package slidestest
7
8import (
9 "strconv"
10 "strings"
11 "testing"
12
13 "github.com/chromedp/cdproto/emulation"
14 "github.com/chromedp/chromedp"
15 "github.com/chromedp/chromedp/kb"
16)
17
18// bigTalk is a session on a deck of 105 slides in the talk (the 105th
19// full-only, and a parked one besides), with "The Software Factory" the
20// 62nd.
21func newBigRig(t *testing.T) notesRig {
22 t.Helper()
23 rig, app := newRigApp(t, nil)
24 cookies := signedIn(t, app, allowedAddress)
25 token := mintAgent(t, app, cookies, "big-deck")
26 sqid := apiDeck(t, app, token, "Big")
27 view := apiJSON(t, app, "GET", "/api/v1/decks/"+sqid, token, "", 200)
28 var ops []string
29 slides, _ := view["slides"].([]any)
30 for _, s := range slides {
31 id, _ := s.(map[string]any)["id"].(string)
32 ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`)
33 }
34 for n := 1; n <= 105; n++ {
35 id, title := "n"+strconv.Itoa(n), "Topic "+strconv.Itoa(n)
36 if n == 62 {
37 title = "The Software Factory"
38 }
39 ops = append(ops,
40 `{"op":"add_slide","id":`+quote(id)+`,"layout":"flow","template":"title-body"}`,
41 `{"op":"add_block","slide":`+quote(id)+`,"id":`+quote("h"+strconv.Itoa(n))+`,"type":"heading","region":"heading","markdown":`+quote(title)+`}`)
42 }
43 ops = append(ops,
44 `{"op":"set_slide","id":"n105","talk":"full"}`,
45 `{"op":"add_slide","id":"hidden","layout":"flow","template":"title-body"}`,
46 `{"op":"set_slide","id":"hidden","talk":"parked"}`)
47 apiJSON(t, app, "POST", "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, 200)
48 session, tok := startPresenting(t, app, "/d/"+sqid, cookies)
49 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1, Mobile: false}))
50 return notesRig{rig: rig, pt: &presenterTalk{app: app, cookies: cookies, deck: "/d/" + sqid, session: session, token: tok}}
51}
52
53// pickerRig is the four-slide talk: steps, plain, extra (full-only),
54// last, and a parked slide that must not be listed.
55func newSmallRig(t *testing.T) notesRig {
56 t.Helper()
57 nr := newNotesRig(t, map[string]string{"plain": "Plain."})
58 nr.goTo(t, 1)
59 return nr
60}
61
62const pickerOpenJS = `document.querySelector('[presenter-picker]') && !document.querySelector('[presenter-picker]').hidden`
63
64// loaded waits until the talk is in memory, which is what the picker lists.
65func (nr notesRig) loaded() chromedp.Action[chromedp.Void] {
66 return waitFor(`document.querySelector('[presenter]').getAttribute('data-loaded') === 'all'`)
67}
68
69func (nr notesRig) openPicker(t *testing.T) {
70 t.Helper()
71 nr.rig.Run(nr.loaded(), chromedp.Evaluate[chromedp.Void](`window.__before = performance.getEntriesByType('resource').filter(r => /\.svg(\?|$)/.test(r.name)).length`), chromedp.Click(chromedp.CSS(`[presenter-pos]`)), waitFor(pickerOpenJS))
72}
73
74type pickerState struct {
75 Count string
76 Tiles int
77 First string
78 IDs []string
79 Current string
80 CurInView bool
81 ScrollTop float64
82 DocScrolls bool
83 InWindow bool
84 Pics int
85 None bool
86 NoneText string
87 Expanded string
88 Focus string
89 Full []string
90 Roles string
91 // Pictures of the picker alone: in view, in view and loaded, asked
92 // for, asked for beyond the grid's foot by more than a row, above it.
93 InView, InViewLoaded, Asked, TooFar, Above int
94}
95
96const pickerStateJS = `(() => {
97 const box = document.querySelector('[presenter-picker]');
98 const grid = box.querySelector('[presenter-picker-grid]');
99 const tiles = [...grid.querySelectorAll('[presenter-pick-item]')];
100 const cur = grid.querySelector('[aria-current]');
101 const g = grid.getBoundingClientRect(), b = box.getBoundingClientRect();
102 let curIn = false;
103 if (cur) { const c = cur.getBoundingClientRect(); curIn = c.top >= g.top - 1 && c.bottom <= g.bottom + 1; }
104 const de = document.documentElement;
105 const pos = document.querySelector('[presenter-pos]');
106 const none = box.querySelector('[presenter-picker-none]');
107 return {
108 Count: box.querySelector('[presenter-picker-count]').textContent,
109 Tiles: tiles.length,
110 First: tiles[0] ? tiles[0].querySelector('b').textContent : '',
111 IDs: tiles.map(t => t.getAttribute('data-id')),
112 Current: cur ? cur.querySelector('b').textContent : '',
113 CurInView: curIn,
114 ScrollTop: grid.scrollTop,
115 DocScrolls: de.scrollHeight > innerHeight || de.scrollWidth > innerWidth || scrollY > 0,
116 InWindow: b.bottom <= innerHeight && b.right <= innerWidth && b.left >= 0,
117 Pics: performance.getEntriesByType('resource').filter(r => /\.svg(\?|$)/.test(r.name)).length,
118 None: !!none.textContent,
119 Roles: box.querySelector('[presenter-picker-count]').getAttribute('role') + ',' + none.getAttribute('role'),
120 ...(() => {
121 const row = tiles.length ? tiles[0].getBoundingClientRect().height + 12 : 0;
122 const o = { InView: 0, InViewLoaded: 0, Asked: 0, TooFar: 0, Above: 0 };
123 for (const t of tiles) {
124 const r = t.getBoundingClientRect(), img = t.querySelector('img');
125 const seen = r.bottom > g.top && r.top < g.bottom;
126 const asked = !!img.getAttribute('src');
127 if (seen) { o.InView++; if (asked && img.complete && img.naturalWidth > 0) o.InViewLoaded++; }
128 if (asked) {
129 o.Asked++;
130 if (r.top >= g.bottom + row) o.TooFar++;
131 if (r.bottom <= g.top) o.Above++;
132 }
133 }
134 return o;
135 })(),
136 NoneText: none.textContent,
137 Expanded: pos.getAttribute('aria-expanded'),
138 Focus: document.activeElement === pos ? 'pos' : (document.activeElement.tagName || ''),
139 Full: tiles.filter(t => t.querySelector('em')).map(t => t.getAttribute('data-id'))
140 };
141})()`
142
143func (nr notesRig) picker(t *testing.T) pickerState {
144 t.Helper()
145 var s pickerState
146 nr.rig.Run(evalInto(&s, pickerStateJS))
147 return s
148}
149
150// type types into the picker's field, from empty.
151func (nr notesRig) typeInPicker(t *testing.T, text string) {
152 t.Helper()
153 nr.rig.Run(chromedp.Evaluate[chromedp.Void](`(() => { const i = document.querySelector('[presenter-picker] input'); i.value = ''; i.dispatchEvent(new Event('input', { bubbles: true })); })()`),
154 chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), text))
155}
156
157func TestPickerHolds105Slides(t *testing.T) {
158 nr := newBigRig(t)
159 nr.goTo(t, 62)
160 nr.openPicker(t)
161 // Let the pictures that are going to load, load.
162 nr.rig.Run(chromedp.Sleep(800 * 1000 * 1000))
163 s := nr.picker(t)
164 if s.Count != "105 slides" || s.Tiles != 105 {
165 t.Errorf("opened with count %q and %d tiles, want %q and 105 (the parked slide is not one)", s.Count, s.Tiles, "105 slides")
166 }
167 if s.Current != "62" || !s.CurInView || s.ScrollTop <= 0 {
168 t.Errorf("slide 62 is current %q, in view %v, grid scrolled %v; want 62, in view, scrolled to it", s.Current, s.CurInView, s.ScrollTop)
169 }
170 if s.Expanded != "true" || s.DocScrolls || !s.InWindow {
171 t.Errorf("expanded %q, window scrolls %v, picker inside the window %v; want true, no, yes", s.Expanded, s.DocScrolls, s.InWindow)
172 }
173 var before, src int
174 nr.rig.Run(evalInto(&before, `window.__before`), evalInto(&src, `document.querySelectorAll('[presenter-picker] img[src]').length`))
175 t.Logf("%d svg requests on opening (%d before it), %d picker pictures asked, %d tiles in view", s.Pics, before, src, s.InView)
176 if s.InView == 0 || s.InViewLoaded != s.InView {
177 t.Errorf("%d tiles in view, %d with their picture loaded; want every one", s.InView, s.InViewLoaded)
178 }
179 if s.TooFar != 0 || s.Above != 0 || s.Asked > s.InView+12 {
180 t.Errorf("pictures asked for: %d (in view %d), %d beyond one row past the foot, %d above the top; want only the tiles in view and at most one row beyond", s.Asked, s.InView, s.TooFar, s.Above)
181 }
182 if s.Roles != "status,status" {
183 t.Errorf("count and no-match roles %q, want status,status (polite live regions)", s.Roles)
184 }
185
186 nr.typeInPicker(t, "software")
187 if s = nr.picker(t); s.Count != "1 of 105" || s.Tiles != 1 || s.First != "62" {
188 t.Errorf("words: count %q, %d tiles, first %q; want 1 of 105, one, 62", s.Count, s.Tiles, s.First)
189 }
190 nr.typeInPicker(t, "6")
191 if s = nr.picker(t); s.Count != "11 of 105" || s.First != "6" || s.Tiles != 11 {
192 t.Errorf("number 6: count %q, %d tiles, first %q; want 11 of 105, 11, 6", s.Count, s.Tiles, s.First)
193 }
194 nr.typeInPicker(t, "62")
195 if s = nr.picker(t); s.First != "62" || s.Tiles != 1 {
196 t.Errorf("number 62: %d tiles, first %q; want one, 62", s.Tiles, s.First)
197 }
198
199 // Enter goes to the first match; the picker closes, the window
200 // stays one page, and the focus is back on the position.
201 nr.typeInPicker(t, "7")
202 nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Enter),
203 waitFor(`document.querySelector('[presenter]').getAttribute('data-slide') === '7' && document.querySelector('[presenter-picker]').hidden`))
204 if s = nr.picker(t); s.DocScrolls || s.Focus != "pos" || s.Expanded != "false" {
205 t.Errorf("after Enter: window scrolls %v, focus %q, expanded %q; want no, pos, false", s.DocScrolls, s.Focus, s.Expanded)
206 }
207}
208
209func TestPickerEscapeClosesAndReturnsFocus(t *testing.T) {
210 nr := newSmallRig(t)
211 nr.openPicker(t)
212 nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Escape), waitFor(`document.querySelector('[presenter-picker]').hidden`))
213 if s := nr.picker(t); s.Focus != "pos" || s.Expanded != "false" {
214 t.Errorf("after Esc: focus %q, expanded %q; want pos, false", s.Focus, s.Expanded)
215 }
216}
217
218// Space on the position opens the picker, as Enter does; it does not
219// press Next.
220func TestPickerSpaceOnThePositionOpensIt(t *testing.T) {
221 nr := newSmallRig(t)
222 nr.rig.Run(nr.loaded(), chromedp.Evaluate[chromedp.Void](`document.querySelector('[presenter-pos]').focus()`), chromedp.KeyEvent(" "), waitFor(pickerOpenJS))
223 var slide string
224 nr.rig.Run(evalInto(&slide, `document.querySelector('[presenter]').getAttribute('data-slide')`))
225 if slide != "1" {
226 t.Errorf("Space on the position moved the talk to slide %s, want it to stay on 1", slide)
227 }
228}
229
230// Esc closes the picker and leaves the focus on the position, from
231// outside the dialog as well as from its field.
232func TestPickerEscapeFromOutsideReturnsFocus(t *testing.T) {
233 nr := newSmallRig(t)
234 nr.openPicker(t)
235 nr.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[presenter-press="next"]').focus()`), chromedp.KeyEvent(kb.Escape), waitFor(`document.querySelector('[presenter-picker]').hidden`))
236 if s := nr.picker(t); s.Focus != "pos" {
237 t.Errorf("after Esc from outside the dialog the focus is on %q, want pos", s.Focus)
238 }
239}
240
241func TestPickerListsNoParkedSlide(t *testing.T) {
242 nr := newSmallRig(t)
243 nr.openPicker(t)
244 s := nr.picker(t)
245 if s.Tiles != 4 || s.Count != "4 slides" {
246 t.Errorf("%d tiles, count %q; want the talk's four, 4 slides", s.Tiles, s.Count)
247 }
248 for _, id := range s.IDs {
249 if id == "parked" {
250 t.Errorf("the picker lists the parked slide: %v", s.IDs)
251 }
252 }
253 if len(s.Full) != 1 || s.Full[0] != "extra" {
254 t.Errorf("full-only slides marked: %v, want just extra", s.Full)
255 }
256}
257
258func TestPickerFullOnlySwitchesToFull(t *testing.T) {
259 nr := newSmallRig(t)
260 if mode := nr.state(t).Mode; mode == "" {
261 t.Fatal("no window")
262 }
263 nr.openPicker(t)
264 nr.rig.Run(chromedp.Click(chromedp.CSS(`[presenter-picker] [data-id="extra"]`)),
265 waitFor(`document.querySelector('[presenter]').getAttribute('data-slide') === '3' && document.querySelector('[presenter]').getAttribute('data-mode') === 'full'`))
266 var mode string
267 nr.rig.Run(evalInto(&mode, `document.querySelector('[presenter-mode]').textContent`))
268 if mode != "Full" {
269 t.Errorf("after choosing a full-only slide in the short version the badge says %q, want Full", mode)
270 }
271}
272
273func TestPickerSaysWhenNothingMatches(t *testing.T) {
274 nr := newSmallRig(t)
275 nr.openPicker(t)
276 nr.typeInPicker(t, "zzzz")
277 s := nr.picker(t)
278 if !s.None || s.NoneText != "No slide matches that." || s.Tiles != 0 || s.Count != "0 of 4" {
279 t.Errorf("none: shown %v %q, %d tiles, count %q; want the sentence, no tiles, 0 of 4", s.None, s.NoneText, s.Tiles, s.Count)
280 }
281 nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Enter), chromedp.Sleep(300*1000*1000))
282 var slide string
283 nr.rig.Run(evalInto(&slide, `document.querySelector('[presenter]').getAttribute('data-slide')`))
284 if slide != "1" || !nr.pickerOpen() {
285 t.Errorf("Enter with nothing found moved the talk to %q or closed the picker", slide)
286 }
287}
288
289func (nr notesRig) pickerOpen() bool {
290 var open bool
291 nr.rig.Run(evalInto(&open, pickerOpenJS))
292 return open
293}
294
295// pickTilesJS is every tile the picker has made: its number, its words,
296// whether it is marked full-only, and the picture it asks for.
297const pickTilesJS = `Object.fromEntries([...document.querySelectorAll('[presenter-pick-item]')].map(t => [t.getAttribute('data-id'), {
298 Number: t.querySelector('b').textContent, Text: t.querySelector('span').textContent,
299 Full: !!t.querySelector('em'), Src: t.querySelector('img').getAttribute('src') || t.querySelector('img').getAttribute('data-src') }]))`
300
301type pickTile struct {
302 Number, Text string
303 Full bool
304 Src string
305}
306
307// TestPickerTakesANewVersionOfTheDeck: tiles made before the deck
308// changed are kept, and once the window takes the new version each
309// says the slide as it is now: its number after a reorder, its title,
310// whether it is full-only, and its picture.
311func TestPickerTakesANewVersionOfTheDeck(t *testing.T) {
312 nr := newSmallRig(t)
313 nr.openPicker(t)
314 var before map[string]pickTile
315 nr.rig.Run(evalInto(&before, pickTilesJS), chromedp.KeyEvent(kb.Escape), waitFor(`!(`+pickerOpenJS+`)`))
316
317 agent := mintAgent(t, nr.pt.app, nr.pt.cookies, "picker-edit")
318 sqid := strings.TrimPrefix(nr.pt.deck, "/d/")
319 apiJSON(t, nr.pt.app, "POST", "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[
320 {"op":"add_block","slide":"plain","id":"plainhead","type":"heading","region":"heading","markdown":"Renamed"},
321 {"op":"move_slide","id":"last","after":"steps"},
322 {"op":"set_slide","id":"plain","talk":"full"},
323 {"op":"set_slide","id":"extra","talk":null}]}`, 200)
324 // The window hears of the new version (on focus, as when the
325 // speaker comes back from the editor) and takes it at its next
326 // change of slide.
327 nr.rig.Run(chromedp.Evaluate[chromedp.Void](`dispatchEvent(new Event('focus'))`),
328 waitFor(`document.querySelector('[presenter]').hasAttribute('data-deck-pending')`),
329 chromedp.KeyEvent(kb.End),
330 waitFor(`!document.querySelector('[presenter]').hasAttribute('data-deck-pending')`))
331
332 nr.rig.Run(chromedp.Click(chromedp.CSS(`[presenter-pos]`)), waitFor(pickerOpenJS))
333 var after map[string]pickTile
334 nr.rig.Run(evalInto(&after, pickTilesJS))
335 if got := after["last"]; got.Number != "2" {
336 t.Errorf("last, moved second, is numbered %q", got.Number)
337 }
338 if got := after["plain"]; got.Number != "3" || !strings.Contains(got.Text, "Renamed") || !got.Full {
339 t.Errorf("plain now says %+v; want number 3, titled Renamed, full-only", got)
340 }
341 if got := after["extra"]; got.Full {
342 t.Errorf("extra, back in the talk, is still marked full-only: %+v", got)
343 }
344 if after["plain"].Src == before["plain"].Src {
345 t.Errorf("plain's picture is still %q, from before it changed", after["plain"].Src)
346 }
347 if after["steps"].Src != before["steps"].Src {
348 t.Errorf("steps did not change, but its picture went from %q to %q", before["steps"].Src, after["steps"].Src)
349 }
350}
351