| 1 | //go:build browser |
| 2 | |
| 3 | // The presenter window and the slide window move at once (Task 10, |
| 4 | // spec §6): a press redraws the window from the talk it holds in |
| 5 | // memory, then goes to the server; a press is never lost or repeated, |
| 6 | // never unanswered, and needs no network. |
| 7 | package slidestest |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | "fmt" |
| 12 | "net/http" |
| 13 | "net/url" |
| 14 | "strconv" |
| 15 | "strings" |
| 16 | "sync" |
| 17 | "sync/atomic" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "amadan.net/rastrillo/rastrillo/blobs" |
| 22 | "amadan.net/rastrillo/rastrillo/harness" |
| 23 | "github.com/chromedp/cdproto/cdp" |
| 24 | "github.com/chromedp/cdproto/input" |
| 25 | "github.com/chromedp/chromedp" |
| 26 | "github.com/chromedp/chromedp/kb" |
| 27 | ) |
| 28 | |
| 29 | // movesTalk is a session on a deck of four slides in the talk: "steps" |
| 30 | // (four presses), "plain", "picture" (an image) and "last". |
| 31 | type movesTalk struct { |
| 32 | fr *fontRig |
| 33 | sqid string |
| 34 | session, token string |
| 35 | |
| 36 | // hold, when set, sees each request first and may hold it. |
| 37 | hold atomic.Pointer[func(r *http.Request)] |
| 38 | |
| 39 | // Chromium's offline emulation stops new requests but leaves a |
| 40 | // stream already open delivering, so a cut network also ends every |
| 41 | // open stream here, and refuses new ones until it is healed. |
| 42 | mu sync.Mutex |
| 43 | off bool |
| 44 | streams map[*http.Request]context.CancelFunc |
| 45 | } |
| 46 | |
| 47 | func newMovesTalk(t *testing.T) *movesTalk { |
| 48 | t.Helper() |
| 49 | store, err := blobs.Dir(t.TempDir()) |
| 50 | if err != nil { |
| 51 | t.Fatalf("blobs.Dir: %v", err) |
| 52 | } |
| 53 | fr := newFontRigWith(t, store) |
| 54 | mt := &movesTalk{fr: fr, streams: map[*http.Request]context.CancelFunc{}} |
| 55 | wrap := func(w http.ResponseWriter, r *http.Request, next http.Handler) { |
| 56 | if hold := mt.hold.Load(); hold != nil { |
| 57 | (*hold)(r) |
| 58 | } |
| 59 | if !strings.HasSuffix(r.URL.Path, "/stream") { |
| 60 | next.ServeHTTP(w, r) |
| 61 | return |
| 62 | } |
| 63 | mt.mu.Lock() |
| 64 | if mt.off { |
| 65 | mt.mu.Unlock() |
| 66 | http.Error(w, "cut", http.StatusServiceUnavailable) |
| 67 | return |
| 68 | } |
| 69 | ctx, cancel := context.WithCancel(r.Context()) |
| 70 | mt.streams[r] = cancel |
| 71 | mt.mu.Unlock() |
| 72 | defer func() { |
| 73 | mt.mu.Lock() |
| 74 | delete(mt.streams, r) |
| 75 | mt.mu.Unlock() |
| 76 | cancel() |
| 77 | }() |
| 78 | next.ServeHTTP(w, r.WithContext(ctx)) |
| 79 | } |
| 80 | fr.wrap.Store(&wrap) |
| 81 | mt.sqid = fr.deck(t, "Moves", `[]`) |
| 82 | record := upload(t, fr.app, "/d/"+mt.sqid, fr.cookies, "chart.png", "image/png", onePixelPNG, "A chart") |
| 83 | registerInDeck(t, fr.app, fr.token, mt.sqid, record) |
| 84 | ops := []string{ |
| 85 | `{"op":"add_slide","id":"steps","layout":"flow","template":"title-body"}`, |
| 86 | `{"op":"add_block","slide":"steps","id":"stepshead","type":"heading","region":"heading","props":{"level":1},"markdown":"Steps"}`, |
| 87 | } |
| 88 | for i, word := range []string{"one", "two", "three", "four"} { |
| 89 | id := quote("b" + strconv.Itoa(i+1)) |
| 90 | ops = append(ops, |
| 91 | `{"op":"add_block","slide":"steps","id":`+id+`,"type":"text","region":"body","markdown":`+quote(word)+`}`, |
| 92 | `{"op":"set_block","slide":"steps","id":`+id+`,"step":`+strconv.Itoa(i+1)+`}`) |
| 93 | } |
| 94 | ops = append(ops, |
| 95 | `{"op":"add_slide","id":"plain","layout":"flow","template":"title-body"}`, |
| 96 | `{"op":"add_block","slide":"plain","id":"plainhead","type":"heading","region":"heading","props":{"level":1},"markdown":"Plain"}`, |
| 97 | `{"op":"add_slide","id":"picture","layout":"flow","template":"image-full"}`, |
| 98 | fmt.Sprintf(`{"op":"add_block","slide":"picture","id":"pictureimg","type":"image","region":"media","props":{"asset":%q}}`, record["id"]), |
| 99 | `{"op":"add_slide","id":"last","layout":"flow","template":"title-body"}`, |
| 100 | `{"op":"add_block","slide":"last","id":"lasthead","type":"heading","region":"heading","props":{"level":1},"markdown":"Last"}`) |
| 101 | fr.ops(t, mt.sqid, "["+strings.Join(ops, ",")+"]") |
| 102 | mt.session, mt.token = startPresenting(t, fr.app, "/d/"+mt.sqid, fr.cookies) |
| 103 | return mt |
| 104 | } |
| 105 | |
| 106 | func (mt *movesTalk) presenter() string { |
| 107 | return "/p/" + mt.session + "/notes?t=" + url.QueryEscape(mt.token) |
| 108 | } |
| 109 | |
| 110 | func (mt *movesTalk) slideWindow() string { |
| 111 | return "/p/" + mt.session + "?t=" + url.QueryEscape(mt.token) |
| 112 | } |
| 113 | |
| 114 | // phone presses as the paired phone does. |
| 115 | func (mt *movesTalk) phone(t *testing.T, name string) pressAnswer { |
| 116 | t.Helper() |
| 117 | return press(t, mt.fr.app, mt.session, mt.token, op(t, name), nil) |
| 118 | } |
| 119 | |
| 120 | func (mt *movesTalk) gotoSlide(t *testing.T, n int) { |
| 121 | t.Helper() |
| 122 | press(t, mt.fr.app, mt.session, mt.token, url.Values{"op": {"goto"}, "slide": {strconv.Itoa(n)}, "press": {newPressID(t)}}, nil) |
| 123 | } |
| 124 | |
| 125 | // row is where the server says the talk is. |
| 126 | func (mt *movesTalk) row(t *testing.T) (string, int, int64) { |
| 127 | t.Helper() |
| 128 | return rowPosition(t, mt.fr.app, mt.session) |
| 129 | } |
| 130 | |
| 131 | // waitRow waits until the server's position is slide at step. |
| 132 | func (mt *movesTalk) waitRow(t *testing.T, slide string, step int) { |
| 133 | t.Helper() |
| 134 | deadline := time.Now().Add(15 * time.Second) |
| 135 | for { |
| 136 | id, k, _ := mt.row(t) |
| 137 | if id == slide && k == step { |
| 138 | return |
| 139 | } |
| 140 | if time.Now().After(deadline) { |
| 141 | t.Fatalf("the server is at %s step %d, want %s step %d", id, k, slide, step) |
| 142 | } |
| 143 | time.Sleep(50 * time.Millisecond) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // root is the window's root on either page. |
| 148 | const rootJS = `(document.querySelector('[presenter]') || document.querySelector('[slides-surface]'))` |
| 149 | |
| 150 | // open loads path in ctx and waits until the window holds the whole talk. |
| 151 | func open(t *testing.T, ctx context.Context, origin, path string) { |
| 152 | t.Helper() |
| 153 | run(t, ctx, chromedp.Navigate(origin+path), waitFor(rootJS+`?.getAttribute('data-loaded') === 'all'`)) |
| 154 | } |
| 155 | |
| 156 | func run(t *testing.T, ctx context.Context, actions ...chromedp.Action[chromedp.Void]) { |
| 157 | t.Helper() |
| 158 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 159 | t.Fatalf("drive: %v", err) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // windowState is what a window's root says it shows. |
| 164 | type windowState struct { |
| 165 | Slide, Step, Mode, Black string |
| 166 | Steps, Pos, Flash, Pulse string |
| 167 | Now string |
| 168 | } |
| 169 | |
| 170 | const windowStateJS = `(() => { const r = ` + rootJS + `; |
| 171 | const sc = document.querySelector('[presenter-stepcount]'); |
| 172 | const pos = document.querySelector('[presenter-pos]'); |
| 173 | const now = document.querySelector('[presenter-panel="now"] [slides-slide]') || document.querySelector('[slides-stage] [slides-slide]'); |
| 174 | return { Slide: r.dataset.slide, Step: r.dataset.step, Mode: r.dataset.mode, Black: r.dataset.blackout, |
| 175 | Steps: sc && sc.checkVisibility() ? sc.innerText : '', Pos: pos ? pos.innerText : '', |
| 176 | Flash: sc ? sc.getAttribute('data-flash') || '' : '', Pulse: pos ? pos.getAttribute('data-pulse') || '' : '', |
| 177 | Now: now ? now.innerText : '' }; })()` |
| 178 | |
| 179 | func state(t *testing.T, ctx context.Context) windowState { |
| 180 | t.Helper() |
| 181 | var s windowState |
| 182 | run(t, ctx, evalInto(&s, windowStateJS)) |
| 183 | return s |
| 184 | } |
| 185 | |
| 186 | func key(k string) chromedp.Action[chromedp.Void] { return chromedp.KeyEvent(k) } |
| 187 | |
| 188 | // mounted waits for the presenter window's script, which names its |
| 189 | // window: a key pressed before any script ran is the browser's. |
| 190 | var mounted = waitFor(`window.name === 'slides-presenter'`) |
| 191 | |
| 192 | // TestPresenterStepCountChangesOnEveryPress: through a slide with steps |
| 193 | // the step count's words change on every press, at once; on slides |
| 194 | // without steps it is not drawn and the position changes instead. |
| 195 | func TestPresenterStepCountChangesOnEveryPress(t *testing.T) { |
| 196 | mt := newMovesTalk(t) |
| 197 | ctx := mt.fr.rig.Context() |
| 198 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 199 | before := state(t, ctx) |
| 200 | if before.Steps == "" { |
| 201 | t.Fatalf("the step count is not drawn on a slide with steps: %+v", before) |
| 202 | } |
| 203 | for k := 1; k <= 4; k++ { |
| 204 | run(t, ctx, key(kb.ArrowRight)) |
| 205 | got := state(t, ctx) |
| 206 | if got.Step != strconv.Itoa(k) || got.Steps == before.Steps || got.Steps == "" { |
| 207 | t.Fatalf("press %d: step %q, step count %q (was %q); want step %d and new words, at once", k, got.Step, got.Steps, before.Steps, k) |
| 208 | } |
| 209 | if got.Flash == before.Flash { |
| 210 | t.Errorf("press %d: the step count's flash did not restart", k) |
| 211 | } |
| 212 | before = got |
| 213 | } |
| 214 | for _, want := range []string{"2", "3", "4"} { |
| 215 | run(t, ctx, key(kb.ArrowRight)) |
| 216 | got := state(t, ctx) |
| 217 | if got.Slide != want || got.Pos == before.Pos || got.Steps != "" { |
| 218 | t.Fatalf("onto slide %s: data-slide %q, position %q (was %q), step count %q; want the position to change and no step count", |
| 219 | want, got.Slide, got.Pos, before.Pos, got.Steps) |
| 220 | } |
| 221 | before = got |
| 222 | } |
| 223 | mt.waitRow(t, "last", 0) |
| 224 | } |
| 225 | |
| 226 | // TestTheLastPressOfTheTalkStillAnswers: next on the last step of the |
| 227 | // last slide moves nothing, and still answers: the position pulses |
| 228 | // again, the step count's flash restarts, and the server hears it. |
| 229 | func TestTheLastPressOfTheTalkStillAnswers(t *testing.T) { |
| 230 | mt := newMovesTalk(t) |
| 231 | ctx := mt.fr.rig.Context() |
| 232 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 233 | run(t, ctx, key(kb.End)) |
| 234 | mt.waitRow(t, "last", 0) |
| 235 | before := state(t, ctx) |
| 236 | _, _, seq := mt.row(t) |
| 237 | for range 2 { |
| 238 | var running bool |
| 239 | run(t, ctx, key(kb.ArrowRight), |
| 240 | evalInto(&running, `document.querySelector('[presenter-pos]').getAnimations().some(a => a.playState === 'running')`)) |
| 241 | got := state(t, ctx) |
| 242 | if got.Slide != before.Slide || got.Step != before.Step { |
| 243 | t.Fatalf("next at the end moved the window: %+v, was %+v", got, before) |
| 244 | } |
| 245 | if got.Pulse == before.Pulse || got.Flash == before.Flash || !running { |
| 246 | t.Errorf("next at the end did not answer: pulse %q (was %q), flash %q (was %q), animating %v", |
| 247 | got.Pulse, before.Pulse, got.Flash, before.Flash, running) |
| 248 | } |
| 249 | before = got |
| 250 | } |
| 251 | deadline := time.Now().Add(10 * time.Second) |
| 252 | for { |
| 253 | _, _, now := mt.row(t) |
| 254 | if now >= seq+2 { |
| 255 | break |
| 256 | } |
| 257 | if time.Now().After(deadline) { |
| 258 | t.Fatalf("the server heard %d of the two presses at the end", now-seq) |
| 259 | } |
| 260 | time.Sleep(50 * time.Millisecond) |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // TestPresenterFastPressesNeverJumpBack: with every press's answer |
| 265 | // slow, three quick presses move the window three steps at once, it |
| 266 | // never shows an earlier step on any frame while the answers come in, |
| 267 | // and the server counts three. |
| 268 | func TestPresenterFastPressesNeverJumpBack(t *testing.T) { |
| 269 | mt := newMovesTalk(t) |
| 270 | ctx := mt.fr.rig.Context() |
| 271 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 272 | slow := func(r *http.Request) { |
| 273 | if r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/command") { |
| 274 | time.Sleep(600 * time.Millisecond) |
| 275 | } |
| 276 | } |
| 277 | mt.hold.Store(&slow) |
| 278 | run(t, ctx, chromedp.Evaluate[chromedp.Void](`(() => { window.__steps = []; const r = `+rootJS+`; |
| 279 | const sample = () => { window.__steps.push(Number(r.dataset.step)); requestAnimationFrame(sample) }; sample(); return true })()`)) |
| 280 | run(t, ctx, key(kb.ArrowRight), key(kb.ArrowRight), key(kb.ArrowRight)) |
| 281 | if got := state(t, ctx); got.Step != "3" { |
| 282 | t.Fatalf("three quick presses: the window is at step %s, want 3 at once", got.Step) |
| 283 | } |
| 284 | mt.waitRow(t, "steps", 3) |
| 285 | run(t, ctx, chromedp.Sleep(1500*time.Millisecond)) |
| 286 | var steps []int |
| 287 | run(t, ctx, evalInto(&steps, `window.__steps`)) |
| 288 | for i := 1; i < len(steps); i++ { |
| 289 | if steps[i] < steps[i-1] { |
| 290 | t.Fatalf("the step went back from %d to %d on frame %d of %d: %v", steps[i-1], steps[i], i, len(steps), steps) |
| 291 | } |
| 292 | } |
| 293 | if len(steps) == 0 || steps[len(steps)-1] != 3 { |
| 294 | t.Errorf("the window ended at %v, want step 3", steps) |
| 295 | } |
| 296 | if _, step, _ := mt.row(t); step != 3 { |
| 297 | t.Errorf("the server counted %d presses, want 3", step) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // TestPresenterIgnoresKeyRepeat: a key held down is one press. |
| 302 | func TestPresenterIgnoresKeyRepeat(t *testing.T) { |
| 303 | mt := newMovesTalk(t) |
| 304 | ctx := mt.fr.rig.Context() |
| 305 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 306 | down := func(repeat bool) chromedp.Action[chromedp.Void] { |
| 307 | return cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyDown, Key: "ArrowRight", Code: "ArrowRight", |
| 308 | WindowsVirtualKeyCode: 39, NativeVirtualKeyCode: 39, AutoRepeat: new(repeat)}) |
| 309 | } |
| 310 | run(t, ctx, down(false), down(true), down(true), down(true), |
| 311 | cdpStep(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: "ArrowRight", Code: "ArrowRight", WindowsVirtualKeyCode: 39, NativeVirtualKeyCode: 39})) |
| 312 | if got := state(t, ctx); got.Step != "1" { |
| 313 | t.Fatalf("a held key moved the window to step %s, want 1", got.Step) |
| 314 | } |
| 315 | mt.waitRow(t, "steps", 1) |
| 316 | run(t, ctx, chromedp.Sleep(time.Second)) |
| 317 | if _, step, _ := mt.row(t); step != 1 { |
| 318 | t.Errorf("a held key moved the talk to step %d, want 1", step) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | // TestPresenterClickerWorksFromAField: with a field focused, the |
| 323 | // clicker's PageDown and PageUp still move the talk, and every other |
| 324 | // key is typed into the field. |
| 325 | func TestPresenterClickerWorksFromAField(t *testing.T) { |
| 326 | mt := newMovesTalk(t) |
| 327 | ctx := mt.fr.rig.Context() |
| 328 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 329 | run(t, ctx, |
| 330 | chromedp.Evaluate[chromedp.Void](`(() => { const f = document.createElement('input'); f.id = 'field'; document.querySelector('[presenter-head]').append(f); return true })()`), |
| 331 | chromedp.Focus(chromedp.CSS(`#field`)), |
| 332 | key(kb.PageDown)) |
| 333 | if got := state(t, ctx); got.Step != "1" { |
| 334 | t.Fatalf("PageDown from a field: step %s, want 1", got.Step) |
| 335 | } |
| 336 | var value string |
| 337 | run(t, ctx, key("b"), key(kb.ArrowRight), harness.Into(&value, chromedp.Value(chromedp.CSS(`#field`)))) |
| 338 | if got := state(t, ctx); got.Step != "1" || got.Black != "false" || value != "b" { |
| 339 | t.Errorf("typing in a field moved the talk: step %s, black %s, the field holds %q", got.Step, got.Black, value) |
| 340 | } |
| 341 | run(t, ctx, key(kb.PageUp)) |
| 342 | if got := state(t, ctx); got.Step != "0" { |
| 343 | t.Errorf("PageUp from a field: step %s, want 0", got.Step) |
| 344 | } |
| 345 | mt.waitRow(t, "steps", 0) |
| 346 | } |
| 347 | |
| 348 | // TestTwoWindowsAndAPhoneStayTogether: a press in either window or on |
| 349 | // the phone moves all three; the laptop and the phone pressing at once |
| 350 | // both count. |
| 351 | func TestTwoWindowsAndAPhoneStayTogether(t *testing.T) { |
| 352 | mt := newMovesTalk(t) |
| 353 | presenter := mt.fr.rig.Context() |
| 354 | slides, cancel := chromedp.NewContext(presenter) |
| 355 | defer cancel() |
| 356 | open(t, presenter, mt.fr.rig.Origin, mt.presenter()) |
| 357 | open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) |
| 358 | both := func(slide, step string) { |
| 359 | t.Helper() |
| 360 | want := rootJS + `.dataset.slide === '` + slide + `' && ` + rootJS + `.dataset.step === '` + step + `'` |
| 361 | run(t, presenter, waitFor(want)) |
| 362 | run(t, slides, waitFor(want)) |
| 363 | } |
| 364 | |
| 365 | run(t, presenter, key(kb.ArrowRight)) |
| 366 | both("1", "1") |
| 367 | run(t, slides, key(kb.ArrowRight)) |
| 368 | both("1", "2") |
| 369 | mt.phone(t, "next") |
| 370 | both("1", "3") |
| 371 | |
| 372 | var wg sync.WaitGroup |
| 373 | wg.Add(1) |
| 374 | go func() { |
| 375 | defer wg.Done() |
| 376 | rec := pressRequest(t, mt.fr.app, mt.session, mt.token, op(t, "next"), nil) |
| 377 | if rec.Code != http.StatusOK { |
| 378 | t.Errorf("the phone's press: %d", rec.Code) |
| 379 | } |
| 380 | }() |
| 381 | run(t, presenter, key(kb.ArrowRight)) |
| 382 | wg.Wait() |
| 383 | mt.waitRow(t, "plain", 0) |
| 384 | both("2", "0") |
| 385 | } |
| 386 | |
| 387 | // emulateNetworkConditions is Network.emulateNetworkConditions as |
| 388 | // chromedp v0.14's cdproto sent it. cdproto v0.158 dropped it for |
| 389 | // overrideNetworkState, but under that one |
| 390 | // TestADroppedNetworkKeepsThePressingWindowMoving's server never reached |
| 391 | // the window's offline position after the network came back, so the cut |
| 392 | // keeps the command it was written against. |
| 393 | var emulateNetworkConditions = cdp.Command[emulateNetworkConditionsParams, cdp.Empty]{Method: "Network.emulateNetworkConditions"} |
| 394 | |
| 395 | type emulateNetworkConditionsParams struct { |
| 396 | Offline bool `json:"offline"` |
| 397 | Latency float64 `json:"latency"` |
| 398 | DownloadThroughput float64 `json:"downloadThroughput"` |
| 399 | UploadThroughput float64 `json:"uploadThroughput"` |
| 400 | } |
| 401 | |
| 402 | // cut takes the pages in ctxs off the network, or puts them back. |
| 403 | func (mt *movesTalk) cut(t *testing.T, off bool, ctxs ...context.Context) { |
| 404 | t.Helper() |
| 405 | for _, ctx := range ctxs { |
| 406 | run(t, ctx, cdpStep(emulateNetworkConditions, emulateNetworkConditionsParams{Offline: off, Latency: 0, DownloadThroughput: -1, UploadThroughput: -1})) |
| 407 | } |
| 408 | mt.mu.Lock() |
| 409 | mt.off = off |
| 410 | if off { |
| 411 | for _, cancel := range mt.streams { |
| 412 | cancel() |
| 413 | } |
| 414 | } |
| 415 | mt.mu.Unlock() |
| 416 | } |
| 417 | |
| 418 | // TestADroppedNetworkKeepsThePressingWindowMoving: with the network cut |
| 419 | // the presenter window still moves across slides, a picture included, |
| 420 | // and says so; the slide window holds still and says it is waiting; when |
| 421 | // the network returns the server takes the presenter window's position |
| 422 | // over a phone press made meanwhile, and the slide window follows. |
| 423 | func TestADroppedNetworkKeepsThePressingWindowMoving(t *testing.T) { |
| 424 | mt := newMovesTalk(t) |
| 425 | mt.gotoSlide(t, 2) |
| 426 | presenter := mt.fr.rig.Context() |
| 427 | slides, cancel := chromedp.NewContext(presenter) |
| 428 | defer cancel() |
| 429 | open(t, presenter, mt.fr.rig.Origin, mt.presenter()) |
| 430 | open(t, slides, mt.fr.rig.Origin, mt.slideWindow()) |
| 431 | run(t, presenter, waitFor(rootJS+`.dataset.slide === '2'`)) |
| 432 | run(t, slides, waitFor(rootJS+`.dataset.slide === '2'`)) |
| 433 | |
| 434 | mt.cut(t, true, presenter, slides) |
| 435 | // The cut is real: a phone press made now reaches neither window. |
| 436 | mt.phone(t, "prev") |
| 437 | run(t, presenter, chromedp.Sleep(2*time.Second)) |
| 438 | for name, ctx := range map[string]context.Context{"presenter window": presenter, "slide window": slides} { |
| 439 | if got := state(t, ctx); got.Slide != "2" { |
| 440 | t.Fatalf("the %s heard the phone through a cut network: on slide %s step %s", name, got.Slide, got.Step) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | picture := `(() => { const img = document.querySelector('[presenter-panel="now"] img'); return !!img && img.complete && img.naturalWidth > 0 })()` |
| 445 | var drawn bool |
| 446 | run(t, presenter, key(kb.ArrowRight), evalInto(&drawn, picture)) |
| 447 | if got := state(t, presenter); got.Slide != "3" || !drawn { |
| 448 | t.Fatalf("offline, next onto the picture: slide %s, picture drawn %v", got.Slide, drawn) |
| 449 | } |
| 450 | run(t, presenter, key(kb.ArrowRight)) |
| 451 | if got := state(t, presenter); got.Slide != "4" || !strings.Contains(got.Now, "Last") { |
| 452 | t.Fatalf("offline, next onto the last slide: slide %s, Now %q", got.Slide, got.Now) |
| 453 | } |
| 454 | run(t, presenter, waitFor(`document.querySelector('[presenter-status]').innerText === 'Offline. This window still moves.'`)) |
| 455 | run(t, slides, waitFor(`document.querySelector('[slides-waiting]')?.checkVisibility() && document.querySelector('[slides-waiting]').innerText === 'Waiting for the network'`)) |
| 456 | if got := state(t, slides); got.Slide != "2" { |
| 457 | t.Errorf("the slide window moved while cut off: slide %s", got.Slide) |
| 458 | } |
| 459 | |
| 460 | mt.cut(t, false, presenter, slides) |
| 461 | mt.waitRow(t, "last", 0) |
| 462 | run(t, slides, waitFor(rootJS+`.dataset.slide === '4'`), waitFor(`!document.querySelector('[slides-waiting]').checkVisibility()`)) |
| 463 | run(t, presenter, waitFor(`document.querySelector('[presenter-status]').innerText === ''`)) |
| 464 | if got := state(t, presenter); got.Slide != "4" { |
| 465 | t.Errorf("the presenter window left its own position on reconnecting: slide %s", got.Slide) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // TestAWindowThatPressedNothingTakesTheServersPosition: a window cut off |
| 470 | // that pressed nothing takes where the phone took the talk meanwhile, |
| 471 | // and tells the server nothing. |
| 472 | func TestAWindowThatPressedNothingTakesTheServersPosition(t *testing.T) { |
| 473 | mt := newMovesTalk(t) |
| 474 | ctx := mt.fr.rig.Context() |
| 475 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 476 | mt.cut(t, true, ctx) |
| 477 | mt.phone(t, "next") |
| 478 | mt.phone(t, "next") |
| 479 | run(t, ctx, chromedp.Sleep(2*time.Second)) |
| 480 | if got := state(t, ctx); got.Step != "0" { |
| 481 | t.Fatalf("the window heard the phone through a cut network: step %s", got.Step) |
| 482 | } |
| 483 | run(t, ctx, waitFor(`document.querySelector('[presenter-status]').innerText === 'Offline. This window still moves.'`)) |
| 484 | _, _, seq := mt.row(t) |
| 485 | mt.cut(t, false, ctx) |
| 486 | run(t, ctx, waitFor(rootJS+`.dataset.step === '2'`), waitFor(`document.querySelector('[presenter-status]').innerText === ''`)) |
| 487 | run(t, ctx, chromedp.Sleep(time.Second)) |
| 488 | if id, step, now := mt.row(t); id != "steps" || step != 2 || now != seq { |
| 489 | t.Errorf("the server is at %s step %d (seq %d, was %d); the window that pressed nothing changed it", id, step, now, seq) |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // TestPresenterTakesAnEditAtTheNextSlide: a deck changed during the |
| 494 | // talk is loaded quietly and taken at the window's next change of |
| 495 | // slide, landing where the server lands: the current slide deleted, |
| 496 | // parked, or left with fewer steps. |
| 497 | func TestPresenterTakesAnEditAtTheNextSlide(t *testing.T) { |
| 498 | for _, c := range []struct { |
| 499 | name string |
| 500 | start func(mt *movesTalk, t *testing.T) |
| 501 | edit string |
| 502 | key string |
| 503 | slide string // where the window and the server land |
| 504 | step int |
| 505 | shows string |
| 506 | }{ |
| 507 | {name: "the current slide deleted", start: func(mt *movesTalk, t *testing.T) { mt.gotoSlide(t, 2) }, |
| 508 | edit: `[{"op":"remove_slide","id":"plain"}]`, key: kb.ArrowLeft, slide: "steps", step: 4, shows: "four"}, |
| 509 | {name: "the current slide parked", start: func(mt *movesTalk, t *testing.T) { mt.gotoSlide(t, 2) }, |
| 510 | edit: `[{"op":"set_slide","id":"plain","talk":"parked"}]`, key: kb.ArrowLeft, slide: "steps", step: 4, shows: "four"}, |
| 511 | {name: "the current slide losing steps", start: func(mt *movesTalk, t *testing.T) { |
| 512 | mt.phone(t, "next") |
| 513 | mt.phone(t, "next") |
| 514 | mt.phone(t, "next") |
| 515 | }, edit: `[{"op":"set_block","slide":"steps","id":"b4","step":null}]`, key: kb.ArrowRight, slide: "plain", step: 0, shows: "Plain"}, |
| 516 | } { |
| 517 | t.Run(c.name, func(t *testing.T) { |
| 518 | mt := newMovesTalk(t) |
| 519 | ctx := mt.fr.rig.Context() |
| 520 | c.start(mt, t) |
| 521 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 522 | before := state(t, ctx) |
| 523 | mt.fr.ops(t, mt.sqid, c.edit) |
| 524 | // The speaker comes back from the editor. |
| 525 | run(t, ctx, chromedp.Evaluate[chromedp.Void](`window.dispatchEvent(new Event('focus'))`), |
| 526 | waitFor(rootJS+`.hasAttribute('data-deck-pending')`)) |
| 527 | if got := state(t, ctx); got.Now != before.Now || got.Slide != before.Slide { |
| 528 | t.Fatalf("the window took the edit before the slide changed: Now %q (was %q)", got.Now, before.Now) |
| 529 | } |
| 530 | run(t, ctx, key(c.key)) |
| 531 | got := state(t, ctx) |
| 532 | mt.waitRow(t, c.slide, c.step) |
| 533 | if got.Step != strconv.Itoa(c.step) || !strings.Contains(got.Now, c.shows) { |
| 534 | t.Errorf("after the press the window is at step %s showing %q; the server is at %s step %d (%s)", |
| 535 | got.Step, got.Now, c.slide, c.step, c.shows) |
| 536 | } |
| 537 | var pending bool |
| 538 | run(t, ctx, evalInto(&pending, rootJS+`.hasAttribute('data-deck-pending')`)) |
| 539 | if pending { |
| 540 | t.Error("the newer version is still waiting after a change of slide") |
| 541 | } |
| 542 | }) |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | // frameLogJS records, on every frame, the slide on the presenter's Now: |
| 547 | // its heading, and whether its picture is drawn. |
| 548 | const frameLogJS = `(() => { window.__frames = []; |
| 549 | const log = () => { const now = document.querySelectorAll('[presenter-panel="now"] [slides-slide]'); |
| 550 | const img = now[0] && now[0].querySelector('img'); |
| 551 | window.__frames.push({ n: now.length, text: now[0] ? now[0].innerText : '', img: !!img, ok: !!img && img.complete && img.naturalWidth > 0 }); |
| 552 | requestAnimationFrame(log) }; |
| 553 | log(); return true })()` |
| 554 | |
| 555 | // TestPressBeforeTheTalkLoadsKeepsTheSlideOnScreen: a press made while |
| 556 | // the talk and the next slide's picture are still on their way leaves |
| 557 | // the slide on screen, never a blank, until the next is complete, which |
| 558 | // arrives whole. |
| 559 | func TestPressBeforeTheTalkLoadsKeepsTheSlideOnScreen(t *testing.T) { |
| 560 | mt := newMovesTalk(t) |
| 561 | mt.gotoSlide(t, 2) |
| 562 | ctx := mt.fr.rig.Context() |
| 563 | // The talk later than the window's first paint, which waits for |
| 564 | // Next's picture up to 3.5 s (the font gate), so the press is made |
| 565 | // while the talk is still on its way. |
| 566 | hold := func(r *http.Request) { |
| 567 | switch { |
| 568 | case strings.HasSuffix(r.URL.Path, "/talk"): |
| 569 | time.Sleep(5 * time.Second) |
| 570 | case strings.Contains(r.URL.Path, "/assets/"): |
| 571 | time.Sleep(3 * time.Second) |
| 572 | } |
| 573 | } |
| 574 | mt.hold.Store(&hold) |
| 575 | run(t, ctx, mt.fr.navigate(mt.presenter())) |
| 576 | mt.fr.shown(t) |
| 577 | run(t, ctx, mounted, chromedp.Evaluate[chromedp.Void](frameLogJS), key(kb.ArrowRight)) |
| 578 | var loaded string |
| 579 | run(t, ctx, evalInto(&loaded, rootJS+`.getAttribute('data-loaded') || ''`)) |
| 580 | if loaded == "all" { |
| 581 | t.Fatal("the talk was loaded before the press; the check needs it not to be") |
| 582 | } |
| 583 | run(t, ctx, waitFor(rootJS+`.dataset.slide === '3'`), chromedp.Sleep(300*time.Millisecond)) |
| 584 | var frames []struct { |
| 585 | N int |
| 586 | Text string |
| 587 | Img, OK bool |
| 588 | } |
| 589 | run(t, ctx, evalInto(&frames, `window.__frames`)) |
| 590 | seenPicture := false |
| 591 | for i, f := range frames { |
| 592 | if f.N != 1 { |
| 593 | t.Fatalf("frame %d of %d had %d slides on Now: never a blank", i, len(frames), f.N) |
| 594 | } |
| 595 | if f.Img && !f.OK { |
| 596 | t.Fatalf("frame %d: the picture slide was up before its picture", i) |
| 597 | } |
| 598 | if f.Img { |
| 599 | seenPicture = true |
| 600 | } else if seenPicture { |
| 601 | t.Fatalf("frame %d: back off the picture slide to %q", i, f.Text) |
| 602 | } else if !strings.Contains(f.Text, "Plain") { |
| 603 | t.Fatalf("frame %d: before the picture Now showed %q, want the slide it was on", i, f.Text) |
| 604 | } |
| 605 | } |
| 606 | if !seenPicture { |
| 607 | t.Errorf("the picture slide never came up in %d frames", len(frames)) |
| 608 | } |
| 609 | mt.waitRow(t, "picture", 0) |
| 610 | } |
| 611 | |
| 612 | // TestPressBeforeTheTalkLoadsStillJumps: a slide number typed and |
| 613 | // Entered while the talk is still on its way is not lost: the window |
| 614 | // goes there once it holds the talk. |
| 615 | func TestPressBeforeTheTalkLoadsStillJumps(t *testing.T) { |
| 616 | mt := newMovesTalk(t) |
| 617 | ctx := mt.fr.rig.Context() |
| 618 | hold := func(r *http.Request) { |
| 619 | if strings.HasSuffix(r.URL.Path, "/talk") { |
| 620 | time.Sleep(1500 * time.Millisecond) |
| 621 | } |
| 622 | } |
| 623 | mt.hold.Store(&hold) |
| 624 | run(t, ctx, mt.fr.navigate(mt.presenter())) |
| 625 | mt.fr.shown(t) |
| 626 | run(t, ctx, mounted, key("4"), key(kb.Enter)) |
| 627 | var loaded string |
| 628 | run(t, ctx, evalInto(&loaded, rootJS+`.getAttribute('data-loaded') || ''`)) |
| 629 | if loaded != "" { |
| 630 | t.Fatal("the talk had arrived before the jump; the check needs it not to have") |
| 631 | } |
| 632 | run(t, ctx, waitFor(rootJS+`.dataset.slide === '4'`)) |
| 633 | mt.waitRow(t, "last", 0) |
| 634 | } |
| 635 | |
| 636 | // nextLogJS records, on every frame, what Now and Next show: each |
| 637 | // one's heading or text, and whether a picture on Next is drawn. |
| 638 | const nextLogJS = `(() => { window.__next = []; |
| 639 | const log = () => { const now = document.querySelector('[presenter-panel="now"] [slides-slide]'); |
| 640 | const next = document.querySelector('[presenter-panel="next"] [slides-slide]'); |
| 641 | const img = next && next.querySelector('img'); |
| 642 | window.__next.push({ now: now ? now.innerText : '', next: next ? next.innerText : '', img: !!img, ok: !!img && img.complete && img.naturalWidth > 0 }); |
| 643 | requestAnimationFrame(log) }; |
| 644 | log(); return true })()` |
| 645 | |
| 646 | // TestPresenterNextWaitsForItsSlide: Next never shows a slide before it |
| 647 | // is ready. With the picture slow, Next keeps the preview it had until |
| 648 | // the picture is in, and then shows the picture slide whole. |
| 649 | func TestPresenterNextWaitsForItsSlide(t *testing.T) { |
| 650 | mt := newMovesTalk(t) |
| 651 | ctx := mt.fr.rig.Context() |
| 652 | for range 3 { |
| 653 | mt.phone(t, "next") |
| 654 | } |
| 655 | hold := func(r *http.Request) { |
| 656 | if strings.Contains(r.URL.Path, "/assets/") { |
| 657 | time.Sleep(4 * time.Second) |
| 658 | } |
| 659 | } |
| 660 | mt.hold.Store(&hold) |
| 661 | run(t, ctx, mt.fr.navigate(mt.presenter())) |
| 662 | mt.fr.shown(t) |
| 663 | run(t, ctx, waitFor(rootJS+`.hasAttribute('data-loaded')`), chromedp.Sleep(300*time.Millisecond), |
| 664 | chromedp.Evaluate[chromedp.Void](nextLogJS), |
| 665 | key(kb.ArrowRight), // step 4: Next is the plain slide |
| 666 | chromedp.Sleep(300*time.Millisecond), |
| 667 | key(kb.ArrowRight), // the plain slide: Next is the picture, still on its way |
| 668 | waitFor(`window.__next.some(f => f.img && f.ok)`), chromedp.Sleep(200*time.Millisecond)) |
| 669 | var frames []struct { |
| 670 | Now, Next string |
| 671 | Img, OK bool |
| 672 | } |
| 673 | run(t, ctx, evalInto(&frames, `window.__next`)) |
| 674 | waited := false |
| 675 | for i, f := range frames { |
| 676 | if f.Img && !f.OK { |
| 677 | t.Fatalf("frame %d of %d: Next showed the picture slide before its picture", i, len(frames)) |
| 678 | } |
| 679 | if strings.Contains(f.Now, "Plain") && strings.Contains(f.Next, "Plain") { |
| 680 | waited = true |
| 681 | } |
| 682 | } |
| 683 | if !waited { |
| 684 | t.Error("Next never had to wait for the picture; the check needs it to") |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // TestADroppedNetworkKeepsBlackAsPressed: the black screen is part of |
| 689 | // where the pressing window went while cut off: blacked offline, the |
| 690 | // server blacks; lifted offline by a press, the server lifts it. |
| 691 | func TestADroppedNetworkKeepsBlackAsPressed(t *testing.T) { |
| 692 | mt := newMovesTalk(t) |
| 693 | ctx := mt.fr.rig.Context() |
| 694 | open(t, ctx, mt.fr.rig.Origin, mt.presenter()) |
| 695 | black := func() bool { |
| 696 | var b bool |
| 697 | if err := mt.fr.app.db.G.Raw("SELECT blackout FROM presentations WHERE sqid = ?", mt.session).Row().Scan(&b); err != nil { |
| 698 | t.Fatalf("read the blackout: %v", err) |
| 699 | } |
| 700 | return b |
| 701 | } |
| 702 | until := func(want bool, step int) { |
| 703 | t.Helper() |
| 704 | deadline := time.Now().Add(15 * time.Second) |
| 705 | for { |
| 706 | _, k, _ := mt.row(t) |
| 707 | if black() == want && k == step { |
| 708 | return |
| 709 | } |
| 710 | if time.Now().After(deadline) { |
| 711 | t.Fatalf("the server is at step %d, black %v; want step %d, black %v", k, black(), step, want) |
| 712 | } |
| 713 | time.Sleep(50 * time.Millisecond) |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | mt.cut(t, true, ctx) |
| 718 | run(t, ctx, key("b")) |
| 719 | if got := state(t, ctx); got.Black != "true" { |
| 720 | t.Fatalf("B offline: the window is black %s", got.Black) |
| 721 | } |
| 722 | run(t, ctx, waitFor(`document.querySelector('[presenter-status]').innerText !== ''`)) |
| 723 | mt.cut(t, false, ctx) |
| 724 | until(true, 0) |
| 725 | |
| 726 | mt.cut(t, true, ctx) |
| 727 | run(t, ctx, key(kb.ArrowRight)) |
| 728 | if got := state(t, ctx); got.Black != "false" || got.Step != "1" { |
| 729 | t.Fatalf("next offline: black %s step %s, want the black lifted at step 1", got.Black, got.Step) |
| 730 | } |
| 731 | run(t, ctx, waitFor(`document.querySelector('[presenter-status]').innerText !== ''`)) |
| 732 | mt.cut(t, false, ctx) |
| 733 | until(false, 1) |
| 734 | } |
| 735 | |