//go:build browser package slidestest // Scenes running on every surface, carried on between slides, and // stills wherever code doesn't run (scenes and clips spec §3, §8.7; // Review Focus 1; fixtures/scene-layer.mjs, scene-frame.mjs). import ( "bytes" "context" "encoding/json" "fmt" "image/color" "image/png" "net/url" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/blobs" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // recorderScene draws a box and answers ready after its first init. It // tells the page everything it has been sent, again and again, under a // name of its own (doc), so the test can tell one frame's document from // another's and read what each was told whenever it looks. const recorderScene = `
` // silentScene never answers. const silentScene = ` ` // wanderingScene answers ready, then takes its frame somewhere else. const wanderingScene = ` ` // sceneWatch is installed in a page: every frame's document is learned // from what it says (window.__scenes, doc to log), and each iframe is // marked with its document's name (__doc). const sceneWatch = `(() => { if (window.__sceneWatch) return true; window.__sceneWatch = true; window.__scenes = {}; addEventListener('message', (e) => { if (!e.data || e.data.type !== 'seen') return; window.__scenes[e.data.doc] = e.data.log; for (const f of document.querySelectorAll('iframe')) if (f.contentWindow === e.source) f.__doc = e.data.doc; }); return true; })()` type sceneMsg struct { Type, State, Word string Animate *bool ReducedMotion *bool W float64 } type sceneFrameState struct { Doc string Visible bool Mark bool Log []sceneMsg Left, Top float64 Width, Height float64 BlockLeft float64 BlockTop float64 BlockWidth float64 BlockHeight float64 Live bool StillShown string StillOpacities []string } // framesJS is every scene frame in the page part sel holds (the whole // page by default), with what its document has been told and where it // sits beside the block it is over. func framesJS(sel string) string { return `(() => { const within = ` + sel + `; if (!within) return []; return [...within.querySelectorAll('[slides-scene-layer] iframe')].map((f) => { const r = f.getBoundingClientRect(); const block = within.querySelector('[slides-block][data-id="' + f.getAttribute('data-for') + '"]'); const b = block ? block.getBoundingClientRect() : {left: 0, top: 0, width: 0, height: 0}; const shown = block ? [...block.querySelectorAll('[data-scene-state]')].filter((s) => getComputedStyle(s).visibility === 'visible') : []; return {Doc: f.__doc || '', Visible: getComputedStyle(f).visibility === 'visible' && getComputedStyle(f).opacity === '1', Mark: !!f.__mark, Log: (window.__scenes || {})[f.__doc] || [], Left: r.left, Top: r.top, Width: r.width, Height: r.height, BlockLeft: b.left, BlockTop: b.top, BlockWidth: b.width, BlockHeight: b.height, Live: !!block && block.hasAttribute('data-scene-live'), StillShown: shown.map((s) => s.getAttribute('data-scene-state')).join(' '), StillOpacities: block ? [...block.querySelectorAll('[data-scene-state]')].map((s) => getComputedStyle(s).opacity) : []}; }); })()` } func readFrames(t *testing.T, ctx context.Context, sel string) []sceneFrameState { t.Helper() var out []sceneFrameState run(t, ctx, evalInto(&out, framesJS(sel))) return out } // waitFrames waits until the frames in sel satisfy ok, failing with // what they were at the end. func waitFrames(t *testing.T, ctx context.Context, sel, what string, ok func([]sceneFrameState) bool) []sceneFrameState { t.Helper() deadline := time.Now().Add(10 * time.Second) for { got := readFrames(t, ctx, sel) if ok(got) { return got } if time.Now().After(deadline) { raw, _ := json.MarshalIndent(got, "", " ") t.Fatalf("waiting for %s; the frames are:\n%s", what, raw) } time.Sleep(60 * time.Millisecond) } } func lastMsg(f sceneFrameState) sceneMsg { if len(f.Log) == 0 { return sceneMsg{} } return f.Log[len(f.Log)-1] } func said(f sceneFrameState, typ, state string, animate bool) bool { m := lastMsg(f) return m.Type == typ && m.State == state && (typ == "init" || (m.Animate != nil && *m.Animate == animate)) } const pageJS = `document` // sceneTalk is a session on sceneDeckOver's deck with recorderScene // as Boxes, and other as Silent. type sceneTalk struct { fr *fontRig f sceneFixture session, token string } func newSceneTalk(t *testing.T, other string) *sceneTalk { t.Helper() store, err := blobs.Dir(t.TempDir()) if err != nil { t.Fatalf("blobs.Dir: %v", err) } fr := newFontRigWith(t, store) st := &sceneTalk{fr: fr} st.f = sceneDeckOver(t, fr.app, fr.cookies, fr.token, []byte(recorderScene), []byte(other)) st.session, st.token = startPresenting(t, fr.app, "/d/"+st.f.sqid, fr.cookies) return st } func (st *sceneTalk) slideWindow() string { return "/p/" + st.session + "?t=" + url.QueryEscape(st.token) } func (st *sceneTalk) gotoSlide(t *testing.T, n int) { t.Helper() press(t, st.fr.app, st.session, st.token, url.Values{"op": {"goto"}, "slide": {fmt.Sprint(n)}, "press": {newPressID(t)}}, nil) } // openWatched opens path and waits for the talk to be held (a presenting // window) or a slide to be shown (any other page), with the watch on. func (st *sceneTalk) openWatched(t *testing.T, ctx context.Context, path string, talk bool) { t.Helper() if talk { open(t, ctx, st.fr.rig.Origin, path) } else { run(t, ctx, chromedp.Navigate(st.fr.rig.Origin+path), waitFor(`document.documentElement.getAttribute('data-slides-fonts') !== 'pending' && !!document.querySelector('[slides-slide]')`)) } run(t, ctx, evalValue(nil, sceneWatch)) } func readyOne(t *testing.T, ctx context.Context, sel, state string) sceneFrameState { t.Helper() got := waitFrames(t, ctx, sel, "one frame, ready, told "+state, func(fs []sceneFrameState) bool { return len(fs) == 1 && fs[0].Visible && fs[0].Doc != "" && lastMsg(fs[0]).State == state }) return got[0] } // mark marks the one frame there is, to tell it from any made later. func mark(t *testing.T, ctx context.Context) { t.Helper() run(t, ctx, evalValue(nil, `document.querySelectorAll('[slides-scene-layer] iframe').forEach((f) => { f.__mark = true; })`)) } func nearPx(a, b float64) bool { return a-b < 1.5 && b-a < 1.5 } func TestASceneCarriesOnBetweenSlides(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) first := readyOne(t, ctx, pageJS, "one") if m := first.Log[0]; m.Type != "init" || m.Word != "Second" || m.W < 100 || m.ReducedMotion == nil || *m.ReducedMotion { t.Errorf("the scene was told %+v first, want init with its words, its size and no reduced motion", m) } if !nearPx(first.Left, first.BlockLeft) || !nearPx(first.Top, first.BlockTop) || !nearPx(first.Width, first.BlockWidth) || !nearPx(first.Height, first.BlockHeight) { t.Errorf("the frame is at %v,%v %vx%v; the block at %v,%v %vx%v", first.Left, first.Top, first.Width, first.Height, first.BlockLeft, first.BlockTop, first.BlockWidth, first.BlockHeight) } // While the scene shows, its still is under it, kept for assistive // technology but not drawn. if !first.Live || first.StillShown != "one" || first.StillOpacities[0] != "0" { t.Errorf("under the live frame: live %v, still shown %q, opacities %v", first.Live, first.StillShown, first.StillOpacities) } mark(t, ctx) steps := []struct{ slide, step, state string }{ {"1", "1", "two"}, // a press on the slide {"2", "0", "two"}, // onto the next slide: the same scene in the same box {"2", "1", "three"}, // and on } for _, s := range steps { run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.slide === '`+s.slide+`' && `+rootJS+`.dataset.step === '`+s.step+`'`)) got := waitFrames(t, ctx, pageJS, "the same frame told "+s.state, func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", s.state, true) })[0] if !got.Mark || got.Doc != first.Doc || !got.Visible { t.Fatalf("at slide %s step %s the frame is another (%+v), want the first carried on", s.slide, s.step, got) } if n := strings.Count(fmt.Sprint(got.Log), "init"); n != 1 { t.Errorf("the scene was told init %d times", n) } } // The viewer moving in place carries it on too: the same frame, told // the next slide's state (its finished slide: three). view := "/d/" + st.f.sqid + "?slide=1" st.openWatched(t, ctx, view, false) v := readyOne(t, ctx, pageJS, "two") mark(t, ctx) run(t, ctx, chromedp.Click(chromedp.CSS(`[slides-pager] a[rel="next"]`)), waitFor(`location.search.includes('slide=2')`)) got := waitFrames(t, ctx, pageJS, "the viewer's frame told three", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "three", true) })[0] if !got.Mark || got.Doc != v.Doc { t.Errorf("the viewer made a new frame for the same scene in the same box") } } func TestAJumpLandsOnTheState(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) first := readyOne(t, ctx, pageJS, "one") mark(t, ctx) // From the first slide's first step to the second slide is not one // press: the frame is kept (the same box) and told to jump. st.gotoSlide(t, 2) run(t, ctx, waitFor(rootJS+`.dataset.slide === '2'`)) got := waitFrames(t, ctx, pageJS, "a jump to two", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "two", false) })[0] if !got.Mark || got.Doc != first.Doc { t.Errorf("a jump to the same box made a new frame") } // Back to the first slide from its next one's first step is one // press (the first slide fully built), and moves. run(t, ctx, key(kb.ArrowLeft), waitFor(rootJS+`.dataset.slide === '1' && `+rootJS+`.dataset.step === '1'`)) waitFrames(t, ctx, pageJS, "back to two, animated", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "two", true) }) // A jump to the plain slide and back lands where it is told: a new // frame, started at the state its slide is at. st.gotoSlide(t, 5) run(t, ctx, waitFor(rootJS+`.dataset.slide === '5'`)) waitFrames(t, ctx, pageJS, "no frame on a slide with no scene", func(fs []sceneFrameState) bool { return len(fs) == 0 }) st.gotoSlide(t, 2) run(t, ctx, waitFor(rootJS+`.dataset.slide === '2'`)) again := readyOne(t, ctx, pageJS, "two") if again.Mark || again.Log[0].Type != "init" { t.Errorf("after a slide with no scene, the scene was not started afresh: %+v", again) } } func TestADifferentBoxStartsFresh(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) first := readyOne(t, ctx, pageJS, "one") mark(t, ctx) for _, s := range []string{"1.1", "2.0", "2.1", "3.0"} { slide, step, _ := strings.Cut(s, ".") run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.slide === '`+slide+`' && `+rootJS+`.dataset.step === '`+step+`'`)) } got := readyOne(t, ctx, pageJS, "one") if got.Mark || got.Doc == first.Doc || len(got.Log) == 0 || got.Log[0].Type != "init" { t.Errorf("in another box the scene carried on (%+v), want a new frame told init", got) } if !nearPx(got.Left, got.BlockLeft) || !nearPx(got.Width, got.BlockWidth) { t.Errorf("the new frame is at %v (%v wide), its block at %v (%v)", got.Left, got.Width, got.BlockLeft, got.BlockWidth) } } // TestASilentSceneShowsItsStill (Review Focus 1): a scene that never // says ready, or that navigates its frame away, shows its still; the // talk moves on regardless. func TestASilentSceneShowsItsStill(t *testing.T) { for name, bundle := range map[string]string{"silent": silentScene, "wandering": wanderingScene} { t.Run(name, func(t *testing.T) { st := newSceneTalk(t, bundle) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) readyOne(t, ctx, pageJS, "one") st.gotoSlide(t, 4) run(t, ctx, waitFor(rootJS+`.dataset.slide === '4'`)) start := time.Now() // Its still shows from the start, whatever the frame does. var still string run(t, ctx, evalInto(&still, `getComputedStyle(document.querySelector('[data-id="scened"] [data-scene-state]')).visibility`)) if still != "visible" { t.Errorf("the still is %s while the scene starts", still) } waitFrames(t, ctx, pageJS, "the frame given up", func(fs []sceneFrameState) bool { return len(fs) == 0 }) if name == "silent" && time.Since(start) < 2500*time.Millisecond { t.Errorf("the silent scene was given up after %v, want 3 s", time.Since(start)) } var shown struct{ Visibility, Opacity string } run(t, ctx, evalInto(&shown, `(() => { const s = getComputedStyle(document.querySelector('[data-id="scened"] [data-scene-state]')); return {Visibility: s.visibility, Opacity: s.opacity}; })()`)) if shown.Visibility != "visible" || shown.Opacity != "1" { t.Errorf("after the scene was given up its still is %+v", shown) } // A press still answers at once. run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.slide === '5'`)) }) } } // TestReducedMotionGetsTheStill: a reader who asked for less motion is // shown each state's still, its settled end, and no scene runs at all. func TestReducedMotionGetsTheStill(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() run(t, ctx, cdpStep(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{ {Name: "prefers-reduced-motion", Value: "reduce"}, }})) st.openWatched(t, ctx, st.slideWindow(), true) still := func(want string) { t.Helper() var shown string run(t, ctx, evalInto(&shown, `[...document.querySelectorAll('[data-id="scenea"] [data-scene-state]')] .filter((s) => getComputedStyle(s).visibility === 'visible' && getComputedStyle(s).opacity === '1') .map((s) => s.getAttribute('data-scene-state')).join(' ')`)) if shown != want { t.Errorf("the still shown is %q, want %q", shown, want) } } time.Sleep(time.Second) if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Errorf("with reduced motion %d scenes run", len(fs)) } still("one") run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.step === '1'`)) still("two") time.Sleep(500 * time.Millisecond) if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Errorf("after a press, with reduced motion, %d scenes run", len(fs)) } } // TestSceneStillsInNextAndOnAPhone (plan call R5): the presenter's Now // runs the scene and its Next shows stills; the viewer's phone view // shows stills; the editor runs a scene only while its block is chosen. func TestSceneStillsInNextAndOnAPhone(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() presenter := "/p/" + st.session + "/notes?t=" + url.QueryEscape(st.token) st.openWatched(t, ctx, presenter, true) readyOne(t, ctx, `document.querySelector('[presenter-panel="now"]')`, "one") run(t, ctx, key(kb.ArrowRight)) waitFrames(t, ctx, `document.querySelector('[presenter-panel="now"]')`, "Now told two", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "two", true) }) var next struct{ Frames, Stills int } run(t, ctx, evalInto(&next, `(() => { const n = document.querySelector('[presenter-panel="next"]'); return {Frames: n.querySelectorAll('iframe').length, Stills: n.querySelectorAll('[data-scene-state]').length}; })()`)) if next.Frames != 0 || next.Stills == 0 { t.Errorf("Next has %d frames and %d stills, want stills alone", next.Frames, next.Stills) } // The phone view. st.openWatched(t, ctx, "/d/"+st.f.sqid+"?slide=1&vp=phone", false) time.Sleep(time.Second) if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Errorf("the phone view runs %d scenes", len(fs)) } // The editor: stills until the block is chosen. run(t, ctx, chromedp.Navigate(st.fr.rig.Origin+"/d/"+st.f.sqid+"/edit#carry1"), waitFor(`!!document.querySelector('[slides-stage] [data-id="scenea"]') && document.documentElement.getAttribute('data-slides-fonts') === 'shown'`), evalValue(nil, sceneWatch)) time.Sleep(time.Second) if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Errorf("the editor runs a scene nobody chose: %d frames", len(fs)) } run(t, ctx, chromedp.Click(chromedp.CSS(`[slides-stage] [data-id="scenea"]`))) readyOne(t, ctx, pageJS, "two") } // TestAFollowerSeesTheSceneCarryOn: the follow link runs the scene and // carries it on as the talk moves. func TestAFollowerSeesTheSceneCarryOn(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() share := "/s/" + mintShareLink(t, st.fr.app, "/d/"+st.f.sqid, st.fr.cookies) st.openWatched(t, ctx, share+"?follow=1", false) first := readyOne(t, ctx, pageJS, "one") mark(t, ctx) press(t, st.fr.app, st.session, st.token, op(t, "next"), nil) waitFrames(t, ctx, pageJS, "the follower's frame told two", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "two", true) }) press(t, st.fr.app, st.session, st.token, op(t, "next"), nil) got := waitFrames(t, ctx, pageJS, "the follower's frame carried onto slide 2", func(fs []sceneFrameState) bool { return len(fs) == 1 && fs[0].Mark && said(fs[0], "state", "two", true) && len(fs[0].Log) >= 3 })[0] if got.Doc != first.Doc { t.Errorf("the follower made a new frame for the same scene in the same box") } } // TestACaptionAboveASceneStaysAbove: a box drawn above a scene on a // canvas stays above it while the scene runs; the scene runs inside its // own block there, where the slide's stacking holds. func TestACaptionAboveASceneStaysAbove(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) readyOne(t, ctx, pageJS, "one") st.gotoSlide(t, 6) run(t, ctx, waitFor(rootJS+`.dataset.slide === '6'`)) var got struct{ Live, Inside bool } deadline := time.Now().Add(10 * time.Second) for { run(t, ctx, evalInto(&got, `(() => { const f = document.querySelector('[data-id="scenee"] iframe'); return {Live: !!f && getComputedStyle(f).opacity === '1', Inside: !!f}; })()`)) if got.Live || time.Now().After(deadline) { break } time.Sleep(60 * time.Millisecond) } if !got.Live || !got.Inside { t.Fatalf("the covered scene is not running inside its block: %+v", got) } if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Errorf("the covered scene also has %d frames over the slide", len(fs)) } // What is drawn: the caption's white over the scene's blue, and the // scene's blue beside it. pixel := func(sel string, fx, fy float64) color.RGBA { t.Helper() var shot []byte run(t, ctx, inViewShot(sel, &shot)) img, err := png.Decode(bytes.NewReader(shot)) if err != nil { t.Fatal(err) } b := img.Bounds() r, g, bl, _ := img.At(b.Min.X+int(fx*float64(b.Dx())), b.Min.Y+int(fy*float64(b.Dy()))).RGBA() return color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(bl >> 8), 255} } if c := pixel(`[data-id="caption"]`, 0.1, 0.5); c.R < 240 || c.G < 240 || c.B < 240 { t.Errorf("inside the caption the page shows %v, want the caption's white over the scene", c) } if c := pixel(`[data-id="scenee"]`, 0.05, 0.05); c.B < 140 || c.R > 80 { t.Errorf("beside the caption the page shows %v, want the running scene's blue", c) } } // TestASceneInADelayedContainerWaits: a scene inside a box that arrives // on a later press runs only once that box has arrived. func TestASceneInADelayedContainerWaits(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() st.openWatched(t, ctx, st.slideWindow(), true) readyOne(t, ctx, pageJS, "one") st.gotoSlide(t, 7) run(t, ctx, waitFor(rootJS+`.dataset.slide === '7' && `+rootJS+`.dataset.step === '0'`)) time.Sleep(time.Second) if fs := readFrames(t, ctx, pageJS); len(fs) != 0 { t.Fatalf("a scene in a box still to come runs: %d frames", len(fs)) } run(t, ctx, key(kb.ArrowRight), waitFor(rootJS+`.dataset.step === '1'`)) readyOne(t, ctx, pageJS, "one") } // TestAPhoneWidthGivesTheSceneStillBack: narrowing the window to a phone's // width stops the scene and shows its still; widening starts it again. func TestAPhoneWidthGivesTheSceneStillBack(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() size := func(w int64) chromedp.Action[chromedp.Void] { return cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: w, Height: 900, DeviceScaleFactor: 1}) } st.openWatched(t, ctx, "/d/"+st.f.sqid+"?slide=1", false) readyOne(t, ctx, pageJS, "two") run(t, ctx, size(420)) waitFrames(t, ctx, pageJS, "no scene at a phone's width", func(fs []sceneFrameState) bool { return len(fs) == 0 }) var shown struct{ Opacity, Live string } run(t, ctx, evalInto(&shown, `(() => { const b = document.querySelector('[data-id="scenea"]'); return {Opacity: getComputedStyle(b.querySelector('[data-scene-state="two"]')).opacity, Live: String(b.hasAttribute('data-scene-live'))}; })()`)) if shown.Opacity != "1" || shown.Live != "false" { t.Errorf("at a phone's width the still is %+v, want it drawn", shown) } run(t, ctx, size(1280)) readyOne(t, ctx, pageJS, "two") } // TestAFollowerJumpDoesNotAnimate: on the follow view, the next slide // reached before the last step of this one is a jump. func TestAFollowerJumpDoesNotAnimate(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() share := "/s/" + mintShareLink(t, st.fr.app, "/d/"+st.f.sqid, st.fr.cookies) st.openWatched(t, ctx, share+"?follow=1", false) first := readyOne(t, ctx, pageJS, "one") mark(t, ctx) st.gotoSlide(t, 2) got := waitFrames(t, ctx, pageJS, "the follower's frame told two, not animated", func(fs []sceneFrameState) bool { return len(fs) == 1 && said(fs[0], "state", "two", false) })[0] if !got.Mark || got.Doc != first.Doc { t.Errorf("the follower made a new frame for the same scene in the same box") } } // TestAFollowerKeepsTheSceneOverANewStage: the follow view's stage // drawn again for the same slide (an edit elsewhere on it) puts the // running scene over the new block, and tells it nothing new. func TestAFollowerKeepsTheSceneOverANewStage(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() share := "/s/" + mintShareLink(t, st.fr.app, "/d/"+st.f.sqid, st.fr.cookies) st.openWatched(t, ctx, share+"?follow=1", false) first := readyOne(t, ctx, pageJS, "one") run(t, ctx, evalValue(nil, `(() => { const s = document.querySelector('[slides-stage]'); s.innerHTML = s.innerHTML.replace(/ data-scene-live=""/g, ''); return true; })()`)) got := waitFrames(t, ctx, pageJS, "the frame over the new block", func(fs []sceneFrameState) bool { return len(fs) == 1 && fs[0].Live && fs[0].Visible })[0] if got.Doc != first.Doc || len(got.Log) != len(first.Log) { t.Errorf("drawing the same slide again told the scene %v (was %v)", got.Log, first.Log) } if !nearPx(got.Left, got.BlockLeft) || !nearPx(got.Width, got.BlockWidth) { t.Errorf("the frame is at %v (%v wide), the new block at %v (%v)", got.Left, got.Width, got.BlockLeft, got.BlockWidth) } } // TestASceneFitsAShortRegionOnThePage: in a flow region too short for a // 16:9 box at its width, the still and the scene are the largest 16:9 // box that fits, centred (TestAShortFlowRegionFitsTheScene is the // picture's half). func TestASceneFitsAShortRegionOnThePage(t *testing.T) { st := newSceneTalk(t, silentScene) ctx := st.fr.rig.Context() type fitted struct{ X, Y, W, H, BX, BY, BW, BH, FX, FY, FW, FH, FO float64 } // measure is the still, its block and the scene's frame (over the // slide, or inside the block) once the frame shows. measure := func(block string) fitted { t.Helper() var g fitted deadline := time.Now().Add(10 * time.Second) for { run(t, ctx, evalInto(&g, `(() => { const b = document.querySelector('[data-id="`+block+`"]'); const f = b.querySelector('iframe') || document.querySelector('[slides-scene-layer] iframe[data-for="`+block+`"]'); const s = b.querySelector('[data-scene-state]').getBoundingClientRect(); const r = b.getBoundingClientRect(); const q = f ? f.getBoundingClientRect() : {left: 0, top: 0, width: 0, height: 0}; return {X: s.left, Y: s.top, W: s.width, H: s.height, BX: r.left, BY: r.top, BW: r.width, BH: r.height, FX: q.left, FY: q.top, FW: q.width, FH: q.height, FO: f ? Number(getComputedStyle(f).opacity) : 0}; })()`)) if g.FO == 1 || time.Now().After(deadline) { return g } time.Sleep(60 * time.Millisecond) } } for _, c := range []struct{ slide, block string }{{"8", "sceneg"}, {"9", "sceneh"}} { st.openWatched(t, ctx, "/d/"+st.f.sqid+"?slide="+c.slide, false) g := measure(c.block) if g.FO != 1 { t.Fatalf("slide %s: the scene never showed: %+v", c.slide, g) } if r := g.H / g.W; r < 0.55 || r > 0.575 { t.Errorf("slide %s: the still is %vx%v, want 16:9", c.slide, g.W, g.H) } if g.W > g.BW+1 || g.H > g.BH+1 || g.W >= g.BW-1 { t.Errorf("slide %s: the still is %vx%v in a %vx%v block, want it fitted to the block's height", c.slide, g.W, g.H, g.BW, g.BH) } if mid, bc := g.X+g.W/2, g.BX+g.BW/2; mid < bc-1.5 || mid > bc+1.5 { t.Errorf("slide %s: the still is centred at %v, its block at %v", c.slide, mid, bc) } if !nearPx(g.FX, g.X) || !nearPx(g.FY, g.Y) || !nearPx(g.FW, g.W) || !nearPx(g.FH, g.H) { t.Errorf("slide %s: the scene is at %v,%v %vx%v, its still at %v,%v %vx%v", c.slide, g.FX, g.FY, g.FW, g.FH, g.X, g.Y, g.W, g.H) } } }