//go:build browser // What a presenting window takes from the server beside the position // (spec ยง6): the talk's timer, which every window shows whoever // pressed, and the empty talk, which an open window draws as the empty // state, Now, Next and the notes cleared. package slidestest import ( "context" "net/http" "strings" "testing" "time" "github.com/chromedp/cdproto/page" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // timerJS is the presenter window's timer: its start, and its words. const timerJS = `(() => { const e = document.querySelector('[presenter-elapsed]'); return { Since: e.getAttribute('data-timer-since') || '', Text: e.textContent.trim() } })()` type timerState struct{ Since, Text string } // TestPresenterTimerFollowsOtherWindows: the presenter window opened // before the talk starts its timer when the phone or the slide window // makes the first press, and goes back to 0:00 when another window // resets it. func TestPresenterTimerFollowsOtherWindows(t *testing.T) { mt := newMovesTalk(t) presenter := mt.fr.rig.Context() slides, cancel := chromedp.NewContext(presenter) defer cancel() open(t, presenter, mt.fr.rig.Origin, mt.presenter()) open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) var got timerState run(t, presenter, evalInto(&got, timerJS)) if got.Since != "" || got.Text != "0:00" { t.Fatalf("before any press the timer is %+v, want 0:00 and not running", got) } // The slide window presses first. run(t, slides, key(kb.ArrowRight)) run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').textContent.trim() !== '0:00'`)) // The phone resets it. mt.phone(t, "timer-reset") run(t, presenter, waitFor(`!document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) run(t, presenter, chromedp.Sleep(1200*time.Millisecond), evalInto(&got, timerJS)) if got.Text != "0:00" { t.Errorf("after the phone reset it the timer says %q, want 0:00", got.Text) } // And the phone's next starts it again, though the presenter // window is where it was told to be. mt.phone(t, "next") run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) } // emptyJS is what each window shows of the talk: the empty state, and // whatever slide, preview or notes are still drawn. const emptyJS = `(() => { const shown = sel => { const e = document.querySelector(sel); return !!e && e.checkVisibility() }; const p = document.querySelector('[presenter]'); const empty = p ? document.querySelector('[presenter-empty]') : document.querySelector('[slides-empty]'); return { Empty: !!empty && empty.checkVisibility() ? empty.innerText.trim() : '', Slides: [...document.querySelectorAll('[slides-slide]')].filter(e => e.checkVisibility()).length, Notes: p ? document.querySelector('[presenter-notes]').innerText.trim() : '', Pos: p ? document.querySelector('[presenter-pos]').checkVisibility() : false, Steps: shown('[presenter-stepcount]') } })()` type emptyState struct { Empty string Slides int Notes string Pos bool Steps bool } // TestOpenWindowsShowAnEmptyTalk: with both windows open, every slide // is parked and the phone presses: both windows show the empty state, // with no slide, no preview and no notes left over; a press made in the // presenter window there changes nothing; and when a slide comes back // the next press puts it up again. func TestOpenWindowsShowAnEmptyTalk(t *testing.T) { mt := newMovesTalk(t) presenter := mt.fr.rig.Context() slides, cancel := chromedp.NewContext(presenter) defer cancel() open(t, presenter, mt.fr.rig.Origin, mt.presenter()) open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) mt.fr.ops(t, mt.sqid, `[{"op":"set_slide","id":"steps","talk":"parked"},{"op":"set_slide","id":"plain","talk":"parked"}, {"op":"set_slide","id":"picture","talk":"parked"},{"op":"set_slide","id":"last","talk":"parked"}]`) mt.phone(t, "next") mt.waitRow(t, "", 0) isEmpty := `(` + emptyJS + `).Empty !== '' && (` + emptyJS + `).Slides === 0` check := func(ctx context.Context, which string) { t.Helper() run(t, ctx, waitFor(isEmpty)) var got emptyState run(t, ctx, evalInto(&got, emptyJS)) if got.Empty == "" || got.Slides != 0 || got.Notes != "" || got.Steps { t.Errorf("%s on an empty talk: %+v; want the empty state and nothing left of the slide", which, got) } } check(presenter, "the presenter window") check(slides, "the slide window") run(t, presenter, key(kb.ArrowRight), chromedp.Sleep(300*time.Millisecond)) check(presenter, "the presenter window after a press") mt.fr.ops(t, mt.sqid, `[{"op":"set_slide","id":"plain","talk":null}]`) mt.phone(t, "next") mt.waitRow(t, "plain", 0) for _, ctx := range []context.Context{presenter, slides} { run(t, ctx, waitFor(`[...document.querySelectorAll('[slides-slide]')].some(e => e.checkVisibility() && e.innerText.includes('Plain'))`)) var got emptyState run(t, ctx, evalInto(&got, emptyJS)) if got.Empty != "" { t.Errorf("a slide is back and the empty state still shows: %+v", got) } } } // TestPresenterKeysBeforeTheScriptArrive: a clicker pressed as soon as // the window is on screen is never lost, whether what is late is the // window's script (its keys are kept from before the first paint and // taken when it mounts) or the talk it loads (the driver keeps them). func TestPresenterKeysBeforeTheScriptArrive(t *testing.T) { for _, c := range []struct { name string late func(path string) bool // script says whether the window's script must still be on its // way when the keys are pressed. script bool }{ {"the script late", func(path string) bool { return strings.Contains(path, "/presenter.") && strings.HasSuffix(path, ".js") }, true}, {"the talk late", func(path string) bool { return strings.HasSuffix(path, "/talk") }, false}, } { t.Run(c.name, func(t *testing.T) { mt := newMovesTalk(t) ctx := mt.fr.rig.Context() hold := func(r *http.Request) { if c.late(r.URL.Path) { time.Sleep(2500 * time.Millisecond) } } mt.hold.Store(&hold) run(t, ctx, mt.fr.navigate(mt.presenter())) mt.fr.shown(t) var before string run(t, ctx, evalInto(&before, `window.name`)) if c.script && before == "slides-presenter" { t.Fatal("the window's script was there before the keys; the check needs it not to be") } run(t, ctx, key(kb.PageDown), key(kb.ArrowRight), key(kb.PageDown), key(kb.PageUp)) run(t, ctx, waitFor(rootJS+`.dataset.slide === '1' && `+rootJS+`.dataset.step === '2'`)) mt.waitRow(t, "steps", 2) run(t, ctx, chromedp.Sleep(500*time.Millisecond)) if got := state(t, ctx); got.Step != "2" { t.Errorf("after four keys the window is at step %s, want 2: a key lost or repeated", got.Step) } }) } } // firstPaintLogJS records, from the document's start, every frame's // Next panel: whether its slide is shown (the font gate's hold off and // the slide visible) and whether its picture is decoded. const firstPaintLogJS = `window.__paint = []; (function log () { const next = document.querySelector('[presenter-panel="next"] [slides-slide]'); const img = next && next.querySelector('img'); if (next) window.__paint.push({ t: performance.now(), shown: getComputedStyle(next).visibility === 'visible', img: !!img, ok: !!img && img.complete && img.naturalWidth > 0 }); requestAnimationFrame(log) })()` type paintFrame struct { T float64 Shown, OK bool Img bool } // TestPresenterFirstPaintWaitsForItsPictures: the window opening with // a picture slide coming Next shows its slides once that picture is // decoded, never the preview without it; and a picture that does not // come does not hold the window past its deadline. func TestPresenterFirstPaintWaitsForItsPictures(t *testing.T) { for _, c := range []struct { name string delay time.Duration }{ {"a slow picture", 1500 * time.Millisecond}, {"a picture that does not come", 12 * time.Second}, } { t.Run(c.name, func(t *testing.T) { mt := newMovesTalk(t) mt.gotoSlide(t, 2) // plain: Next is the picture slide ctx := mt.fr.rig.Context() hold := func(r *http.Request) { if strings.Contains(r.URL.Path, "/assets/") { time.Sleep(c.delay) } } mt.hold.Store(&hold) run(t, ctx, cdpStep(page.AddScriptToEvaluateOnNewDocument, page.AddScriptToEvaluateOnNewDocumentParams{Source: firstPaintLogJS}), mt.fr.navigate(mt.presenter())) mt.fr.shown(t) run(t, ctx, chromedp.Sleep(200*time.Millisecond)) var frames []paintFrame run(t, ctx, evalInto(&frames, `window.__paint`)) first := -1 for i, f := range frames { if f.Shown { first = i break } } if first < 0 || !frames[first].Img { t.Fatalf("Next never showed the picture slide in %d frames", len(frames)) } if c.delay < 5*time.Second { for i, f := range frames[first:] { if !f.OK { t.Fatalf("frame %d: Next was shown with its picture still on its way", first+i) } } return } if frames[first].T > 6000 { t.Errorf("a picture that does not come held the window until %.0fms", frames[first].T) } }) } }