//go:build browser // The presenter window, measured (Task 9). It never scrolls: the // document is exactly the viewport at every size from 2560×1440 to // 320×568, at every step of a slide with four presses and 400 words of // notes, and every panel lies inside it. Only the notes scroll, inside // their own panel. package slidestest import ( "fmt" "net/http" "net/url" "strconv" "strings" "testing" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" ) // presenterSizes are the ten windows the presenter is measured in. var presenterSizes = []struct{ w, h int64 }{ {2560, 1440}, {1920, 1080}, {1680, 1050}, {1440, 900}, {1280, 720}, {1024, 768}, {800, 600}, {768, 1024}, {390, 844}, {320, 568}, } // longNotes is 400 words in five parts, one for each step of "steps". func longNotes() string { var parts []string for p := range 5 { var words []string for w := range 80 { words = append(words, "word"+strconv.Itoa(p*80+w)) } parts = append(parts, strings.Join(words, " ")+".") } return strings.Join(parts, "\n\n(Press.)\n\n") } type presenterBox struct { Name string `json:"name"` L float64 `json:"l"` T float64 `json:"t"` R float64 `json:"r"` B float64 `json:"b"` } type presenterGeometry struct { VW, VH float64 SH, SW float64 BSH, BSW float64 ScrollY float64 Boxes []presenterBox } const presenterGeometryJS = `(() => { window.scrollTo(0, 1); const de = document.documentElement, b = document.body; const boxes = [...document.querySelectorAll('[presenter-head], [presenter-panel], [presenter-frame]')].map(e => { const r = e.getBoundingClientRect(); return { name: e.getAttribute('presenter-panel') || (e.hasAttribute('presenter-head') ? 'head' : 'frame'), l: r.left, t: r.top, r: r.right, b: r.bottom }; }); return { VW: innerWidth, VH: innerHeight, SH: de.scrollHeight, SW: de.scrollWidth, BSH: b.scrollHeight, BSW: b.scrollWidth, ScrollY: window.scrollY, Boxes: boxes }; })()` func TestPresenterNeverScrolls(t *testing.T) { rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) deck := stepsDeck(t, app, cookies) setNotes(t, app, cookies, deck, map[string]string{"steps": longNotes()}) session, token := startPresenting(t, app, deck, cookies) window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token) // Once, bounded, so a window that is not there fails here and not // fifty times over. rig.Run(chromedp.Navigate(window), waitFor(`document.querySelector('[presenter-panel="notes"]')`)) for _, size := range presenterSizes { t.Run(fmt.Sprintf("%dx%d", size.w, size.h), func(t *testing.T) { rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false})) // Each step as the session reaches it: with scripts on, a // page naming a state the talk is not in goes to where it is. for step := range 5 { // Off the window before the talk moves, so its stream's // reload cannot race this navigation. rig.Run(chromedp.Navigate("about:blank")) if step == 0 { press(t, app, session, token, url.Values{"op": {"goto"}, "slide": {"1"}, "press": {newPressID(t)}}, nil) } else { press(t, app, session, token, op(t, "next"), nil) } var g presenterGeometry rig.Run( chromedp.Navigate(window), waitFor(`document.querySelector('[presenter]')?.getAttribute('data-step') === '`+strconv.Itoa(step)+`'`), evalInto(&g, presenterGeometryJS), ) if g.SH != g.VH || g.SW != g.VW || g.BSH > g.VH || g.BSW > g.VW || g.ScrollY != 0 { t.Errorf("step %d: the document is %.0f×%.0f (body %.0f×%.0f, scrolled %.0f) in a %.0f×%.0f window", step, g.SW, g.SH, g.BSW, g.BSH, g.ScrollY, g.VW, g.VH) } if len(g.Boxes) < 6 { t.Fatalf("step %d: %d boxes measured, want the header, three panels and two frames", step, len(g.Boxes)) } for _, b := range g.Boxes { if b.L < 0 || b.T < 0 || b.R > g.VW || b.B > g.VH || b.R <= b.L || b.B <= b.T { t.Errorf("step %d: %s at (%.0f,%.0f)-(%.0f,%.0f) is not inside the %.0f×%.0f window", step, b.Name, b.L, b.T, b.R, b.B, g.VW, g.VH) } } } // The notes are all there, inside their own panel: the last // word is in it, and the panel scrolls to reach it. var reach struct { Last bool Scrolls bool Overflow string } rig.Run(waitFor(`document.querySelector('[presenter-notes]')`), evalInto(&reach, `(() => { const n = document.querySelector('[presenter-notes]'); return { Last: n.textContent.includes('word399'), Scrolls: n.scrollHeight > n.clientHeight, Overflow: getComputedStyle(n).overflowY }; })()`)) if !reach.Last || (reach.Scrolls && reach.Overflow != "auto") { t.Errorf("the notes lose text: last word there %v, overflowing %v, overflow-y %s", reach.Last, reach.Scrolls, reach.Overflow) } }) } } // columnsDeck is two two-column slides, each with a box hidden on a // phone: what slides.css's phone layer would change if a small frame // were taken for a phone. func columnsDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { t.Helper() token := mintAgent(t, a, cookies, "columns-deck") sqid := apiDeck(t, a, token, "Columns") view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) var ops []string for _, s := range view["slides"].([]any) { ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) } for _, id := range []string{"colsone", "colstwo"} { ops = append(ops, `{"op":"add_slide","id":"`+id+`","layout":"flow","template":"two-column"}`, `{"op":"add_block","slide":"`+id+`","id":"`+id+`h","type":"heading","region":"heading","props":{"level":1},"markdown":"Two columns"}`, `{"op":"add_block","slide":"`+id+`","id":"`+id+`l","type":"text","region":"body","markdown":"The left column, long enough to wrap onto a second line at the projector's size."}`, `{"op":"add_block","slide":"`+id+`","id":"`+id+`r","type":"text","region":"aside","markdown":"The right column."}`, `{"op":"add_block","slide":"`+id+`","id":"`+id+`x","type":"text","region":"aside","markdown":"Hidden on a phone."}`, `{"op":"set_phone","slide":"`+id+`","id":"`+id+`x","phone":{"hidden":true}}`) } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) return "/d/" + sqid } // blockLayoutJS is each box of the first slide under root, as fractions // of the slide: where it is, how big, and whether it is drawn at all. const blockLayoutJS = `((root) => { const s = document.querySelector(root + ' [slides-slide]'); const r = s.getBoundingClientRect(); return [...s.querySelectorAll('[slides-block]')].map(b => { const q = b.getBoundingClientRect(); return { id: b.getAttribute('data-id'), shown: getComputedStyle(b).display !== 'none', x: (q.left - r.left) / r.width, y: (q.top - r.top) / r.height, w: q.width / r.width, h: q.height / r.height }; }).concat([{ id: 'clipped', shown: s.scrollHeight > s.clientHeight + 1 }]); })` type blockBox struct { ID string Shown bool X, Y, W, H float64 } func sameBoxes(want, got []blockBox) string { if len(want) != len(got) { return fmt.Sprintf("%d boxes, want %d", len(got), len(want)) } for i := range want { w, g := want[i], got[i] off := func(a, b float64) bool { return a-b > 0.005 || b-a > 0.005 } if w.ID != g.ID || w.Shown != g.Shown || off(w.X, g.X) || off(w.Y, g.Y) || off(w.W, g.W) || off(w.H, g.H) { return fmt.Sprintf("%s: %+v, the projector has %+v", w.ID, g, w) } } return "" } // TestPresenterDrawsWhatTheProjectorShows: Now and Next are the // projector's own layout scaled down, at any window size: two columns // stay side by side, a box hidden on a phone is still there, and // nothing is cut off. A small frame is not a phone. func TestPresenterDrawsWhatTheProjectorShows(t *testing.T) { rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) session, token := startPresenting(t, app, columnsDeck(t, app, cookies), cookies) // The projector at 1920×1080, on the session's own slide: a page // named for another slide is put back on the session's by its stream. projector := func(n string) []blockBox { var out []blockBox rig.Run(chromedp.Navigate("about:blank")) press(t, app, session, token, url.Values{"op": {"goto"}, "slide": {n}, "press": {newPressID(t)}}, nil) rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1920, Height: 1080, DeviceScaleFactor: 1, Mobile: false}), chromedp.Navigate(rig.Origin+"/p/"+session), waitFor(`document.querySelector('[slides-surface]')?.getAttribute('data-slide') === '`+n+`' && document.querySelector('[slides-stage] [slides-slide]')`), evalInto(&out, blockLayoutJS+`('[slides-stage]')`)) return out } two, one := projector("2"), projector("1") if len(one) < 4 || !one[3].Shown || one[2].X < 0.4 || one[1].X > 0.1 { t.Fatalf("the projector itself does not draw two columns with the phone-hidden box: %+v", one) } window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token) for _, size := range presenterSizes { var now, next []blockBox rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}), chromedp.Navigate(window), waitFor(`document.querySelector('[presenter-panel="next"] [slides-slide]')`), evalInto(&now, blockLayoutJS+`('[presenter-panel="now"]')`), evalInto(&next, blockLayoutJS+`('[presenter-panel="next"]')`)) if diff := sameBoxes(one, now); diff != "" { t.Errorf("%dx%d: Now is not the projector's slide 1: %s", size.w, size.h, diff) } if diff := sameBoxes(two, next); diff != "" { t.Errorf("%dx%d: Next is not the projector's slide 2: %s", size.w, size.h, diff) } } } // TestPresenterNextIsHalfOfNow: in the default window Next is drawn at // half the width of Now, tall windows and short, stacked or arranged. func TestPresenterNextIsHalfOfNow(t *testing.T) { rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) session, token := startPresenting(t, app, stepsDeck(t, app, cookies), cookies) window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token) for _, size := range presenterSizes { var w struct{ Now, Next float64 } rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}), chromedp.Navigate(window), waitFor(`document.querySelector('[presenter-panel="next"] [presenter-frame]')`), evalInto(&w, `({ Now: document.querySelector('[presenter-panel="now"] [presenter-frame]').getBoundingClientRect().width, Next: document.querySelector('[presenter-panel="next"] [presenter-frame]').getBoundingClientRect().width })`)) if d := w.Next - w.Now/2; d > 1 || d < -1 { t.Errorf("%dx%d: Now is %.1fpx wide and Next %.1fpx, want half", size.w, size.h, w.Now, w.Next) } } } // TestPresenterMenuFitsTheWindow: the ⋯ menu, open, lies inside the // window at every size, and every row of it can be reached. func TestPresenterMenuFitsTheWindow(t *testing.T) { rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) session, token := startPresenting(t, app, stepsDeck(t, app, cookies), cookies) window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token) for _, size := range presenterSizes { var m struct { Top, Bottom, Left, Right, VW, VH float64 Rows int Reach bool SH float64 } rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}), chromedp.Navigate(window), waitFor(`document.querySelector('[presenter-more]')`), chromedp.Click(chromedp.CSS(`[presenter-more]`)), waitFor(`document.querySelector('[presenter-more-menu]').open`), evalInto(&m, `(() => { const menu = document.querySelector('[presenter-menu]'); const r = menu.getBoundingClientRect(); const rows = menu.querySelectorAll('button, a'); const style = getComputedStyle(menu); return { Top: r.top, Bottom: r.bottom, Left: r.left, Right: r.right, VW: innerWidth, VH: innerHeight, Rows: rows.length, SH: document.documentElement.scrollHeight, Reach: menu.scrollHeight <= menu.clientHeight + 1 || style.overflowY === 'auto' }; })()`)) if m.Rows != 6 { t.Errorf("%dx%d: the menu has %d rows, want 6", size.w, size.h, m.Rows) } if m.Top < 0 || m.Left < 0 || m.Bottom > m.VH || m.Right > m.VW || !m.Reach || m.SH != m.VH { t.Errorf("%dx%d: the open menu at (%.0f,%.0f)-(%.0f,%.0f) leaves the %.0f×%.0f window, or cannot reach its rows (document %.0f tall)", size.w, size.h, m.Left, m.Top, m.Right, m.Bottom, m.VW, m.VH, m.SH) } } }