| 1 | //go:build browser |
| 2 | |
| 3 | // What a presenting window takes from the server beside the position |
| 4 | // (spec §6): the talk's timer, which every window shows whoever |
| 5 | // pressed, and the empty talk, which an open window draws as the empty |
| 6 | // state, Now, Next and the notes cleared. |
| 7 | package slidestest |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | "net/http" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/chromedp/cdproto/page" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | "github.com/chromedp/chromedp/kb" |
| 19 | ) |
| 20 | |
| 21 | // timerJS is the presenter window's timer: its start, and its words. |
| 22 | const timerJS = `(() => { const e = document.querySelector('[presenter-elapsed]'); |
| 23 | return { Since: e.getAttribute('data-timer-since') || '', Text: e.textContent.trim() } })()` |
| 24 | |
| 25 | type timerState struct{ Since, Text string } |
| 26 | |
| 27 | // TestPresenterTimerFollowsOtherWindows: the presenter window opened |
| 28 | // before the talk starts its timer when the phone or the slide window |
| 29 | // makes the first press, and goes back to 0:00 when another window |
| 30 | // resets it. |
| 31 | func TestPresenterTimerFollowsOtherWindows(t *testing.T) { |
| 32 | mt := newMovesTalk(t) |
| 33 | presenter := mt.fr.rig.Context() |
| 34 | slides, cancel := chromedp.NewContext(presenter) |
| 35 | defer cancel() |
| 36 | open(t, presenter, mt.fr.rig.Origin, mt.presenter()) |
| 37 | open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) |
| 38 | var got timerState |
| 39 | run(t, presenter, evalInto(&got, timerJS)) |
| 40 | if got.Since != "" || got.Text != "0:00" { |
| 41 | t.Fatalf("before any press the timer is %+v, want 0:00 and not running", got) |
| 42 | } |
| 43 | |
| 44 | // The slide window presses first. |
| 45 | run(t, slides, key(kb.ArrowRight)) |
| 46 | run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) |
| 47 | run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').textContent.trim() !== '0:00'`)) |
| 48 | |
| 49 | // The phone resets it. |
| 50 | mt.phone(t, "timer-reset") |
| 51 | run(t, presenter, waitFor(`!document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) |
| 52 | run(t, presenter, chromedp.Sleep(1200*time.Millisecond), evalInto(&got, timerJS)) |
| 53 | if got.Text != "0:00" { |
| 54 | t.Errorf("after the phone reset it the timer says %q, want 0:00", got.Text) |
| 55 | } |
| 56 | |
| 57 | // And the phone's next starts it again, though the presenter |
| 58 | // window is where it was told to be. |
| 59 | mt.phone(t, "next") |
| 60 | run(t, presenter, waitFor(`document.querySelector('[presenter-elapsed]').hasAttribute('data-timer-since')`)) |
| 61 | } |
| 62 | |
| 63 | // emptyJS is what each window shows of the talk: the empty state, and |
| 64 | // whatever slide, preview or notes are still drawn. |
| 65 | const emptyJS = `(() => { |
| 66 | const shown = sel => { const e = document.querySelector(sel); return !!e && e.checkVisibility() }; |
| 67 | const p = document.querySelector('[presenter]'); |
| 68 | const empty = p ? document.querySelector('[presenter-empty]') : document.querySelector('[slides-empty]'); |
| 69 | return { |
| 70 | Empty: !!empty && empty.checkVisibility() ? empty.innerText.trim() : '', |
| 71 | Slides: [...document.querySelectorAll('[slides-slide]')].filter(e => e.checkVisibility()).length, |
| 72 | Notes: p ? document.querySelector('[presenter-notes]').innerText.trim() : '', |
| 73 | Pos: p ? document.querySelector('[presenter-pos]').checkVisibility() : false, |
| 74 | Steps: shown('[presenter-stepcount]') |
| 75 | } })()` |
| 76 | |
| 77 | type emptyState struct { |
| 78 | Empty string |
| 79 | Slides int |
| 80 | Notes string |
| 81 | Pos bool |
| 82 | Steps bool |
| 83 | } |
| 84 | |
| 85 | // TestOpenWindowsShowAnEmptyTalk: with both windows open, every slide |
| 86 | // is parked and the phone presses: both windows show the empty state, |
| 87 | // with no slide, no preview and no notes left over; a press made in the |
| 88 | // presenter window there changes nothing; and when a slide comes back |
| 89 | // the next press puts it up again. |
| 90 | func TestOpenWindowsShowAnEmptyTalk(t *testing.T) { |
| 91 | mt := newMovesTalk(t) |
| 92 | presenter := mt.fr.rig.Context() |
| 93 | slides, cancel := chromedp.NewContext(presenter) |
| 94 | defer cancel() |
| 95 | open(t, presenter, mt.fr.rig.Origin, mt.presenter()) |
| 96 | open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) |
| 97 | |
| 98 | mt.fr.ops(t, mt.sqid, `[{"op":"set_slide","id":"steps","talk":"parked"},{"op":"set_slide","id":"plain","talk":"parked"}, |
| 99 | {"op":"set_slide","id":"picture","talk":"parked"},{"op":"set_slide","id":"last","talk":"parked"}]`) |
| 100 | mt.phone(t, "next") |
| 101 | mt.waitRow(t, "", 0) |
| 102 | isEmpty := `(` + emptyJS + `).Empty !== '' && (` + emptyJS + `).Slides === 0` |
| 103 | check := func(ctx context.Context, which string) { |
| 104 | t.Helper() |
| 105 | run(t, ctx, waitFor(isEmpty)) |
| 106 | var got emptyState |
| 107 | run(t, ctx, evalInto(&got, emptyJS)) |
| 108 | if got.Empty == "" || got.Slides != 0 || got.Notes != "" || got.Steps { |
| 109 | t.Errorf("%s on an empty talk: %+v; want the empty state and nothing left of the slide", which, got) |
| 110 | } |
| 111 | } |
| 112 | check(presenter, "the presenter window") |
| 113 | check(slides, "the slide window") |
| 114 | |
| 115 | run(t, presenter, key(kb.ArrowRight), chromedp.Sleep(300*time.Millisecond)) |
| 116 | check(presenter, "the presenter window after a press") |
| 117 | |
| 118 | mt.fr.ops(t, mt.sqid, `[{"op":"set_slide","id":"plain","talk":null}]`) |
| 119 | mt.phone(t, "next") |
| 120 | mt.waitRow(t, "plain", 0) |
| 121 | for _, ctx := range []context.Context{presenter, slides} { |
| 122 | run(t, ctx, waitFor(`[...document.querySelectorAll('[slides-slide]')].some(e => e.checkVisibility() && e.innerText.includes('Plain'))`)) |
| 123 | var got emptyState |
| 124 | run(t, ctx, evalInto(&got, emptyJS)) |
| 125 | if got.Empty != "" { |
| 126 | t.Errorf("a slide is back and the empty state still shows: %+v", got) |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // TestPresenterKeysBeforeTheScriptArrive: a clicker pressed as soon as |
| 132 | // the window is on screen is never lost, whether what is late is the |
| 133 | // window's script (its keys are kept from before the first paint and |
| 134 | // taken when it mounts) or the talk it loads (the driver keeps them). |
| 135 | func TestPresenterKeysBeforeTheScriptArrive(t *testing.T) { |
| 136 | for _, c := range []struct { |
| 137 | name string |
| 138 | late func(path string) bool |
| 139 | // script says whether the window's script must still be on its |
| 140 | // way when the keys are pressed. |
| 141 | script bool |
| 142 | }{ |
| 143 | {"the script late", func(path string) bool { |
| 144 | return strings.Contains(path, "/presenter.") && strings.HasSuffix(path, ".js") |
| 145 | }, true}, |
| 146 | {"the talk late", func(path string) bool { return strings.HasSuffix(path, "/talk") }, false}, |
| 147 | } { |
| 148 | t.Run(c.name, func(t *testing.T) { |
| 149 | mt := newMovesTalk(t) |
| 150 | ctx := mt.fr.rig.Context() |
| 151 | hold := func(r *http.Request) { |
| 152 | if c.late(r.URL.Path) { |
| 153 | time.Sleep(2500 * time.Millisecond) |
| 154 | } |
| 155 | } |
| 156 | mt.hold.Store(&hold) |
| 157 | run(t, ctx, mt.fr.navigate(mt.presenter())) |
| 158 | mt.fr.shown(t) |
| 159 | var before string |
| 160 | run(t, ctx, evalInto(&before, `window.name`)) |
| 161 | if c.script && before == "slides-presenter" { |
| 162 | t.Fatal("the window's script was there before the keys; the check needs it not to be") |
| 163 | } |
| 164 | run(t, ctx, key(kb.PageDown), key(kb.ArrowRight), key(kb.PageDown), key(kb.PageUp)) |
| 165 | run(t, ctx, waitFor(rootJS+`.dataset.slide === '1' && `+rootJS+`.dataset.step === '2'`)) |
| 166 | mt.waitRow(t, "steps", 2) |
| 167 | run(t, ctx, chromedp.Sleep(500*time.Millisecond)) |
| 168 | if got := state(t, ctx); got.Step != "2" { |
| 169 | t.Errorf("after four keys the window is at step %s, want 2: a key lost or repeated", got.Step) |
| 170 | } |
| 171 | }) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // firstPaintLogJS records, from the document's start, every frame's |
| 176 | // Next panel: whether its slide is shown (the font gate's hold off and |
| 177 | // the slide visible) and whether its picture is decoded. |
| 178 | const firstPaintLogJS = `window.__paint = []; (function log () { |
| 179 | const next = document.querySelector('[presenter-panel="next"] [slides-slide]'); |
| 180 | const img = next && next.querySelector('img'); |
| 181 | if (next) window.__paint.push({ t: performance.now(), shown: getComputedStyle(next).visibility === 'visible', |
| 182 | img: !!img, ok: !!img && img.complete && img.naturalWidth > 0 }); |
| 183 | requestAnimationFrame(log) })()` |
| 184 | |
| 185 | type paintFrame struct { |
| 186 | T float64 |
| 187 | Shown, OK bool |
| 188 | Img bool |
| 189 | } |
| 190 | |
| 191 | // TestPresenterFirstPaintWaitsForItsPictures: the window opening with |
| 192 | // a picture slide coming Next shows its slides once that picture is |
| 193 | // decoded, never the preview without it; and a picture that does not |
| 194 | // come does not hold the window past its deadline. |
| 195 | func TestPresenterFirstPaintWaitsForItsPictures(t *testing.T) { |
| 196 | for _, c := range []struct { |
| 197 | name string |
| 198 | delay time.Duration |
| 199 | }{ |
| 200 | {"a slow picture", 1500 * time.Millisecond}, |
| 201 | {"a picture that does not come", 12 * time.Second}, |
| 202 | } { |
| 203 | t.Run(c.name, func(t *testing.T) { |
| 204 | mt := newMovesTalk(t) |
| 205 | mt.gotoSlide(t, 2) // plain: Next is the picture slide |
| 206 | ctx := mt.fr.rig.Context() |
| 207 | hold := func(r *http.Request) { |
| 208 | if strings.Contains(r.URL.Path, "/assets/") { |
| 209 | time.Sleep(c.delay) |
| 210 | } |
| 211 | } |
| 212 | mt.hold.Store(&hold) |
| 213 | run(t, ctx, cdpStep(page.AddScriptToEvaluateOnNewDocument, page.AddScriptToEvaluateOnNewDocumentParams{Source: firstPaintLogJS}), mt.fr.navigate(mt.presenter())) |
| 214 | mt.fr.shown(t) |
| 215 | run(t, ctx, chromedp.Sleep(200*time.Millisecond)) |
| 216 | var frames []paintFrame |
| 217 | run(t, ctx, evalInto(&frames, `window.__paint`)) |
| 218 | first := -1 |
| 219 | for i, f := range frames { |
| 220 | if f.Shown { |
| 221 | first = i |
| 222 | break |
| 223 | } |
| 224 | } |
| 225 | if first < 0 || !frames[first].Img { |
| 226 | t.Fatalf("Next never showed the picture slide in %d frames", len(frames)) |
| 227 | } |
| 228 | if c.delay < 5*time.Second { |
| 229 | for i, f := range frames[first:] { |
| 230 | if !f.OK { |
| 231 | t.Fatalf("frame %d: Next was shown with its picture still on its way", first+i) |
| 232 | } |
| 233 | } |
| 234 | return |
| 235 | } |
| 236 | if frames[first].T > 6000 { |
| 237 | t.Errorf("a picture that does not come held the window until %.0fms", frames[first].T) |
| 238 | } |
| 239 | }) |
| 240 | } |
| 241 | } |
| 242 | |