//go:build browser // The presenter window's slide picker (Task 12, spec ยง5.3): the // position opens a popover with every slide of the talk, narrowed by a // number or by words, at 105 slides and beyond. package slidestest import ( "strconv" "strings" "testing" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // bigTalk is a session on a deck of 105 slides in the talk (the 105th // full-only, and a parked one besides), with "The Software Factory" the // 62nd. func newBigRig(t *testing.T) notesRig { t.Helper() rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) token := mintAgent(t, app, cookies, "big-deck") sqid := apiDeck(t, app, token, "Big") view := apiJSON(t, app, "GET", "/api/v1/decks/"+sqid, token, "", 200) var ops []string slides, _ := view["slides"].([]any) for _, s := range slides { id, _ := s.(map[string]any)["id"].(string) ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`) } for n := 1; n <= 105; n++ { id, title := "n"+strconv.Itoa(n), "Topic "+strconv.Itoa(n) if n == 62 { title = "The Software Factory" } ops = append(ops, `{"op":"add_slide","id":`+quote(id)+`,"layout":"flow","template":"title-body"}`, `{"op":"add_block","slide":`+quote(id)+`,"id":`+quote("h"+strconv.Itoa(n))+`,"type":"heading","region":"heading","markdown":`+quote(title)+`}`) } ops = append(ops, `{"op":"set_slide","id":"n105","talk":"full"}`, `{"op":"add_slide","id":"hidden","layout":"flow","template":"title-body"}`, `{"op":"set_slide","id":"hidden","talk":"parked"}`) apiJSON(t, app, "POST", "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, 200) session, tok := startPresenting(t, app, "/d/"+sqid, cookies) rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1, Mobile: false})) return notesRig{rig: rig, pt: &presenterTalk{app: app, cookies: cookies, deck: "/d/" + sqid, session: session, token: tok}} } // pickerRig is the four-slide talk: steps, plain, extra (full-only), // last, and a parked slide that must not be listed. func newSmallRig(t *testing.T) notesRig { t.Helper() nr := newNotesRig(t, map[string]string{"plain": "Plain."}) nr.goTo(t, 1) return nr } const pickerOpenJS = `document.querySelector('[presenter-picker]') && !document.querySelector('[presenter-picker]').hidden` // loaded waits until the talk is in memory, which is what the picker lists. func (nr notesRig) loaded() chromedp.Action[chromedp.Void] { return waitFor(`document.querySelector('[presenter]').getAttribute('data-loaded') === 'all'`) } func (nr notesRig) openPicker(t *testing.T) { t.Helper() 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)) } type pickerState struct { Count string Tiles int First string IDs []string Current string CurInView bool ScrollTop float64 DocScrolls bool InWindow bool Pics int None bool NoneText string Expanded string Focus string Full []string Roles string // Pictures of the picker alone: in view, in view and loaded, asked // for, asked for beyond the grid's foot by more than a row, above it. InView, InViewLoaded, Asked, TooFar, Above int } const pickerStateJS = `(() => { const box = document.querySelector('[presenter-picker]'); const grid = box.querySelector('[presenter-picker-grid]'); const tiles = [...grid.querySelectorAll('[presenter-pick-item]')]; const cur = grid.querySelector('[aria-current]'); const g = grid.getBoundingClientRect(), b = box.getBoundingClientRect(); let curIn = false; if (cur) { const c = cur.getBoundingClientRect(); curIn = c.top >= g.top - 1 && c.bottom <= g.bottom + 1; } const de = document.documentElement; const pos = document.querySelector('[presenter-pos]'); const none = box.querySelector('[presenter-picker-none]'); return { Count: box.querySelector('[presenter-picker-count]').textContent, Tiles: tiles.length, First: tiles[0] ? tiles[0].querySelector('b').textContent : '', IDs: tiles.map(t => t.getAttribute('data-id')), Current: cur ? cur.querySelector('b').textContent : '', CurInView: curIn, ScrollTop: grid.scrollTop, DocScrolls: de.scrollHeight > innerHeight || de.scrollWidth > innerWidth || scrollY > 0, InWindow: b.bottom <= innerHeight && b.right <= innerWidth && b.left >= 0, Pics: performance.getEntriesByType('resource').filter(r => /\.svg(\?|$)/.test(r.name)).length, None: !!none.textContent, Roles: box.querySelector('[presenter-picker-count]').getAttribute('role') + ',' + none.getAttribute('role'), ...(() => { const row = tiles.length ? tiles[0].getBoundingClientRect().height + 12 : 0; const o = { InView: 0, InViewLoaded: 0, Asked: 0, TooFar: 0, Above: 0 }; for (const t of tiles) { const r = t.getBoundingClientRect(), img = t.querySelector('img'); const seen = r.bottom > g.top && r.top < g.bottom; const asked = !!img.getAttribute('src'); if (seen) { o.InView++; if (asked && img.complete && img.naturalWidth > 0) o.InViewLoaded++; } if (asked) { o.Asked++; if (r.top >= g.bottom + row) o.TooFar++; if (r.bottom <= g.top) o.Above++; } } return o; })(), NoneText: none.textContent, Expanded: pos.getAttribute('aria-expanded'), Focus: document.activeElement === pos ? 'pos' : (document.activeElement.tagName || ''), Full: tiles.filter(t => t.querySelector('em')).map(t => t.getAttribute('data-id')) }; })()` func (nr notesRig) picker(t *testing.T) pickerState { t.Helper() var s pickerState nr.rig.Run(evalInto(&s, pickerStateJS)) return s } // type types into the picker's field, from empty. func (nr notesRig) typeInPicker(t *testing.T, text string) { t.Helper() nr.rig.Run(chromedp.Evaluate[chromedp.Void](`(() => { const i = document.querySelector('[presenter-picker] input'); i.value = ''; i.dispatchEvent(new Event('input', { bubbles: true })); })()`), chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), text)) } func TestPickerHolds105Slides(t *testing.T) { nr := newBigRig(t) nr.goTo(t, 62) nr.openPicker(t) // Let the pictures that are going to load, load. nr.rig.Run(chromedp.Sleep(800 * 1000 * 1000)) s := nr.picker(t) if s.Count != "105 slides" || s.Tiles != 105 { 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") } if s.Current != "62" || !s.CurInView || s.ScrollTop <= 0 { 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) } if s.Expanded != "true" || s.DocScrolls || !s.InWindow { t.Errorf("expanded %q, window scrolls %v, picker inside the window %v; want true, no, yes", s.Expanded, s.DocScrolls, s.InWindow) } var before, src int nr.rig.Run(evalInto(&before, `window.__before`), evalInto(&src, `document.querySelectorAll('[presenter-picker] img[src]').length`)) t.Logf("%d svg requests on opening (%d before it), %d picker pictures asked, %d tiles in view", s.Pics, before, src, s.InView) if s.InView == 0 || s.InViewLoaded != s.InView { t.Errorf("%d tiles in view, %d with their picture loaded; want every one", s.InView, s.InViewLoaded) } if s.TooFar != 0 || s.Above != 0 || s.Asked > s.InView+12 { 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) } if s.Roles != "status,status" { t.Errorf("count and no-match roles %q, want status,status (polite live regions)", s.Roles) } nr.typeInPicker(t, "software") if s = nr.picker(t); s.Count != "1 of 105" || s.Tiles != 1 || s.First != "62" { t.Errorf("words: count %q, %d tiles, first %q; want 1 of 105, one, 62", s.Count, s.Tiles, s.First) } nr.typeInPicker(t, "6") if s = nr.picker(t); s.Count != "11 of 105" || s.First != "6" || s.Tiles != 11 { t.Errorf("number 6: count %q, %d tiles, first %q; want 11 of 105, 11, 6", s.Count, s.Tiles, s.First) } nr.typeInPicker(t, "62") if s = nr.picker(t); s.First != "62" || s.Tiles != 1 { t.Errorf("number 62: %d tiles, first %q; want one, 62", s.Tiles, s.First) } // Enter goes to the first match; the picker closes, the window // stays one page, and the focus is back on the position. nr.typeInPicker(t, "7") nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Enter), waitFor(`document.querySelector('[presenter]').getAttribute('data-slide') === '7' && document.querySelector('[presenter-picker]').hidden`)) if s = nr.picker(t); s.DocScrolls || s.Focus != "pos" || s.Expanded != "false" { t.Errorf("after Enter: window scrolls %v, focus %q, expanded %q; want no, pos, false", s.DocScrolls, s.Focus, s.Expanded) } } func TestPickerEscapeClosesAndReturnsFocus(t *testing.T) { nr := newSmallRig(t) nr.openPicker(t) nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Escape), waitFor(`document.querySelector('[presenter-picker]').hidden`)) if s := nr.picker(t); s.Focus != "pos" || s.Expanded != "false" { t.Errorf("after Esc: focus %q, expanded %q; want pos, false", s.Focus, s.Expanded) } } // Space on the position opens the picker, as Enter does; it does not // press Next. func TestPickerSpaceOnThePositionOpensIt(t *testing.T) { nr := newSmallRig(t) nr.rig.Run(nr.loaded(), chromedp.Evaluate[chromedp.Void](`document.querySelector('[presenter-pos]').focus()`), chromedp.KeyEvent(" "), waitFor(pickerOpenJS)) var slide string nr.rig.Run(evalInto(&slide, `document.querySelector('[presenter]').getAttribute('data-slide')`)) if slide != "1" { t.Errorf("Space on the position moved the talk to slide %s, want it to stay on 1", slide) } } // Esc closes the picker and leaves the focus on the position, from // outside the dialog as well as from its field. func TestPickerEscapeFromOutsideReturnsFocus(t *testing.T) { nr := newSmallRig(t) nr.openPicker(t) nr.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[presenter-press="next"]').focus()`), chromedp.KeyEvent(kb.Escape), waitFor(`document.querySelector('[presenter-picker]').hidden`)) if s := nr.picker(t); s.Focus != "pos" { t.Errorf("after Esc from outside the dialog the focus is on %q, want pos", s.Focus) } } func TestPickerListsNoParkedSlide(t *testing.T) { nr := newSmallRig(t) nr.openPicker(t) s := nr.picker(t) if s.Tiles != 4 || s.Count != "4 slides" { t.Errorf("%d tiles, count %q; want the talk's four, 4 slides", s.Tiles, s.Count) } for _, id := range s.IDs { if id == "parked" { t.Errorf("the picker lists the parked slide: %v", s.IDs) } } if len(s.Full) != 1 || s.Full[0] != "extra" { t.Errorf("full-only slides marked: %v, want just extra", s.Full) } } func TestPickerFullOnlySwitchesToFull(t *testing.T) { nr := newSmallRig(t) if mode := nr.state(t).Mode; mode == "" { t.Fatal("no window") } nr.openPicker(t) nr.rig.Run(chromedp.Click(chromedp.CSS(`[presenter-picker] [data-id="extra"]`)), waitFor(`document.querySelector('[presenter]').getAttribute('data-slide') === '3' && document.querySelector('[presenter]').getAttribute('data-mode') === 'full'`)) var mode string nr.rig.Run(evalInto(&mode, `document.querySelector('[presenter-mode]').textContent`)) if mode != "Full" { t.Errorf("after choosing a full-only slide in the short version the badge says %q, want Full", mode) } } func TestPickerSaysWhenNothingMatches(t *testing.T) { nr := newSmallRig(t) nr.openPicker(t) nr.typeInPicker(t, "zzzz") s := nr.picker(t) if !s.None || s.NoneText != "No slide matches that." || s.Tiles != 0 || s.Count != "0 of 4" { 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) } nr.rig.Run(chromedp.SendKeys(chromedp.CSS(`[presenter-picker] input`), kb.Enter), chromedp.Sleep(300*1000*1000)) var slide string nr.rig.Run(evalInto(&slide, `document.querySelector('[presenter]').getAttribute('data-slide')`)) if slide != "1" || !nr.pickerOpen() { t.Errorf("Enter with nothing found moved the talk to %q or closed the picker", slide) } } func (nr notesRig) pickerOpen() bool { var open bool nr.rig.Run(evalInto(&open, pickerOpenJS)) return open } // pickTilesJS is every tile the picker has made: its number, its words, // whether it is marked full-only, and the picture it asks for. const pickTilesJS = `Object.fromEntries([...document.querySelectorAll('[presenter-pick-item]')].map(t => [t.getAttribute('data-id'), { Number: t.querySelector('b').textContent, Text: t.querySelector('span').textContent, Full: !!t.querySelector('em'), Src: t.querySelector('img').getAttribute('src') || t.querySelector('img').getAttribute('data-src') }]))` type pickTile struct { Number, Text string Full bool Src string } // TestPickerTakesANewVersionOfTheDeck: tiles made before the deck // changed are kept, and once the window takes the new version each // says the slide as it is now: its number after a reorder, its title, // whether it is full-only, and its picture. func TestPickerTakesANewVersionOfTheDeck(t *testing.T) { nr := newSmallRig(t) nr.openPicker(t) var before map[string]pickTile nr.rig.Run(evalInto(&before, pickTilesJS), chromedp.KeyEvent(kb.Escape), waitFor(`!(`+pickerOpenJS+`)`)) agent := mintAgent(t, nr.pt.app, nr.pt.cookies, "picker-edit") sqid := strings.TrimPrefix(nr.pt.deck, "/d/") apiJSON(t, nr.pt.app, "POST", "/api/v1/decks/"+sqid+"/ops", agent, `{"ops":[ {"op":"add_block","slide":"plain","id":"plainhead","type":"heading","region":"heading","markdown":"Renamed"}, {"op":"move_slide","id":"last","after":"steps"}, {"op":"set_slide","id":"plain","talk":"full"}, {"op":"set_slide","id":"extra","talk":null}]}`, 200) // The window hears of the new version (on focus, as when the // speaker comes back from the editor) and takes it at its next // change of slide. nr.rig.Run(chromedp.Evaluate[chromedp.Void](`dispatchEvent(new Event('focus'))`), waitFor(`document.querySelector('[presenter]').hasAttribute('data-deck-pending')`), chromedp.KeyEvent(kb.End), waitFor(`!document.querySelector('[presenter]').hasAttribute('data-deck-pending')`)) nr.rig.Run(chromedp.Click(chromedp.CSS(`[presenter-pos]`)), waitFor(pickerOpenJS)) var after map[string]pickTile nr.rig.Run(evalInto(&after, pickTilesJS)) if got := after["last"]; got.Number != "2" { t.Errorf("last, moved second, is numbered %q", got.Number) } if got := after["plain"]; got.Number != "3" || !strings.Contains(got.Text, "Renamed") || !got.Full { t.Errorf("plain now says %+v; want number 3, titled Renamed, full-only", got) } if got := after["extra"]; got.Full { t.Errorf("extra, back in the talk, is still marked full-only: %+v", got) } if after["plain"].Src == before["plain"].Src { t.Errorf("plain's picture is still %q, from before it changed", after["plain"].Src) } if after["steps"].Src != before["steps"].Src { t.Errorf("steps did not change, but its picture went from %q to %q", before["steps"].Src, after["steps"].Src) } }