oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3// The presenter window, measured (Task 9). It never scrolls: the
4// document is exactly the viewport at every size from 2560×1440 to
5// 320×568, at every step of a slide with four presses and 400 words of
6// notes, and every panel lies inside it. Only the notes scroll, inside
7// their own panel.
8package slidestest
9
10import (
11 "fmt"
12 "net/http"
13 "net/url"
14 "strconv"
15 "strings"
16 "testing"
17
18 "github.com/chromedp/cdproto/emulation"
19 "github.com/chromedp/chromedp"
20)
21
22// presenterSizes are the ten windows the presenter is measured in.
23var presenterSizes = []struct{ w, h int64 }{
24 {2560, 1440}, {1920, 1080}, {1680, 1050}, {1440, 900}, {1280, 720},
25 {1024, 768}, {800, 600}, {768, 1024}, {390, 844}, {320, 568},
26}
27
28// longNotes is 400 words in five parts, one for each step of "steps".
29func longNotes() string {
30 var parts []string
31 for p := range 5 {
32 var words []string
33 for w := range 80 {
34 words = append(words, "word"+strconv.Itoa(p*80+w))
35 }
36 parts = append(parts, strings.Join(words, " ")+".")
37 }
38 return strings.Join(parts, "\n\n(Press.)\n\n")
39}
40
41type presenterBox struct {
42 Name string `json:"name"`
43 L float64 `json:"l"`
44 T float64 `json:"t"`
45 R float64 `json:"r"`
46 B float64 `json:"b"`
47}
48
49type presenterGeometry struct {
50 VW, VH float64
51 SH, SW float64
52 BSH, BSW float64
53 ScrollY float64
54 Boxes []presenterBox
55}
56
57const presenterGeometryJS = `(() => {
58 window.scrollTo(0, 1);
59 const de = document.documentElement, b = document.body;
60 const boxes = [...document.querySelectorAll('[presenter-head], [presenter-panel], [presenter-frame]')].map(e => {
61 const r = e.getBoundingClientRect();
62 return { name: e.getAttribute('presenter-panel') || (e.hasAttribute('presenter-head') ? 'head' : 'frame'),
63 l: r.left, t: r.top, r: r.right, b: r.bottom };
64 });
65 return { VW: innerWidth, VH: innerHeight, SH: de.scrollHeight, SW: de.scrollWidth,
66 BSH: b.scrollHeight, BSW: b.scrollWidth, ScrollY: window.scrollY, Boxes: boxes };
67})()`
68
69func TestPresenterNeverScrolls(t *testing.T) {
70 rig, app := newRigApp(t, nil)
71 cookies := signedIn(t, app, allowedAddress)
72 deck := stepsDeck(t, app, cookies)
73 setNotes(t, app, cookies, deck, map[string]string{"steps": longNotes()})
74 session, token := startPresenting(t, app, deck, cookies)
75 window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token)
76 // Once, bounded, so a window that is not there fails here and not
77 // fifty times over.
78 rig.Run(chromedp.Navigate(window), waitFor(`document.querySelector('[presenter-panel="notes"]')`))
79
80 for _, size := range presenterSizes {
81 t.Run(fmt.Sprintf("%dx%d", size.w, size.h), func(t *testing.T) {
82 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}))
83 // Each step as the session reaches it: with scripts on, a
84 // page naming a state the talk is not in goes to where it is.
85 for step := range 5 {
86 // Off the window before the talk moves, so its stream's
87 // reload cannot race this navigation.
88 rig.Run(chromedp.Navigate("about:blank"))
89 if step == 0 {
90 press(t, app, session, token, url.Values{"op": {"goto"}, "slide": {"1"}, "press": {newPressID(t)}}, nil)
91 } else {
92 press(t, app, session, token, op(t, "next"), nil)
93 }
94 var g presenterGeometry
95 rig.Run(
96 chromedp.Navigate(window),
97 waitFor(`document.querySelector('[presenter]')?.getAttribute('data-step') === '`+strconv.Itoa(step)+`'`),
98 evalInto(&g, presenterGeometryJS),
99 )
100 if g.SH != g.VH || g.SW != g.VW || g.BSH > g.VH || g.BSW > g.VW || g.ScrollY != 0 {
101 t.Errorf("step %d: the document is %.0f×%.0f (body %.0f×%.0f, scrolled %.0f) in a %.0f×%.0f window",
102 step, g.SW, g.SH, g.BSW, g.BSH, g.ScrollY, g.VW, g.VH)
103 }
104 if len(g.Boxes) < 6 {
105 t.Fatalf("step %d: %d boxes measured, want the header, three panels and two frames", step, len(g.Boxes))
106 }
107 for _, b := range g.Boxes {
108 if b.L < 0 || b.T < 0 || b.R > g.VW || b.B > g.VH || b.R <= b.L || b.B <= b.T {
109 t.Errorf("step %d: %s at (%.0f,%.0f)-(%.0f,%.0f) is not inside the %.0f×%.0f window",
110 step, b.Name, b.L, b.T, b.R, b.B, g.VW, g.VH)
111 }
112 }
113 }
114 // The notes are all there, inside their own panel: the last
115 // word is in it, and the panel scrolls to reach it.
116 var reach struct {
117 Last bool
118 Scrolls bool
119 Overflow string
120 }
121 rig.Run(waitFor(`document.querySelector('[presenter-notes]')`), evalInto(&reach, `(() => { const n = document.querySelector('[presenter-notes]');
122 return { Last: n.textContent.includes('word399'), Scrolls: n.scrollHeight > n.clientHeight,
123 Overflow: getComputedStyle(n).overflowY }; })()`))
124 if !reach.Last || (reach.Scrolls && reach.Overflow != "auto") {
125 t.Errorf("the notes lose text: last word there %v, overflowing %v, overflow-y %s", reach.Last, reach.Scrolls, reach.Overflow)
126 }
127 })
128 }
129}
130
131// columnsDeck is two two-column slides, each with a box hidden on a
132// phone: what slides.css's phone layer would change if a small frame
133// were taken for a phone.
134func columnsDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string {
135 t.Helper()
136 token := mintAgent(t, a, cookies, "columns-deck")
137 sqid := apiDeck(t, a, token, "Columns")
138 view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK)
139 var ops []string
140 for _, s := range view["slides"].([]any) {
141 ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`)
142 }
143 for _, id := range []string{"colsone", "colstwo"} {
144 ops = append(ops,
145 `{"op":"add_slide","id":"`+id+`","layout":"flow","template":"two-column"}`,
146 `{"op":"add_block","slide":"`+id+`","id":"`+id+`h","type":"heading","region":"heading","props":{"level":1},"markdown":"Two columns"}`,
147 `{"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."}`,
148 `{"op":"add_block","slide":"`+id+`","id":"`+id+`r","type":"text","region":"aside","markdown":"The right column."}`,
149 `{"op":"add_block","slide":"`+id+`","id":"`+id+`x","type":"text","region":"aside","markdown":"Hidden on a phone."}`,
150 `{"op":"set_phone","slide":"`+id+`","id":"`+id+`x","phone":{"hidden":true}}`)
151 }
152 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK)
153 return "/d/" + sqid
154}
155
156// blockLayoutJS is each box of the first slide under root, as fractions
157// of the slide: where it is, how big, and whether it is drawn at all.
158const blockLayoutJS = `((root) => {
159 const s = document.querySelector(root + ' [slides-slide]');
160 const r = s.getBoundingClientRect();
161 return [...s.querySelectorAll('[slides-block]')].map(b => {
162 const q = b.getBoundingClientRect();
163 return { id: b.getAttribute('data-id'), shown: getComputedStyle(b).display !== 'none',
164 x: (q.left - r.left) / r.width, y: (q.top - r.top) / r.height,
165 w: q.width / r.width, h: q.height / r.height };
166 }).concat([{ id: 'clipped', shown: s.scrollHeight > s.clientHeight + 1 }]);
167})`
168
169type blockBox struct {
170 ID string
171 Shown bool
172 X, Y, W, H float64
173}
174
175func sameBoxes(want, got []blockBox) string {
176 if len(want) != len(got) {
177 return fmt.Sprintf("%d boxes, want %d", len(got), len(want))
178 }
179 for i := range want {
180 w, g := want[i], got[i]
181 off := func(a, b float64) bool { return a-b > 0.005 || b-a > 0.005 }
182 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) {
183 return fmt.Sprintf("%s: %+v, the projector has %+v", w.ID, g, w)
184 }
185 }
186 return ""
187}
188
189// TestPresenterDrawsWhatTheProjectorShows: Now and Next are the
190// projector's own layout scaled down, at any window size: two columns
191// stay side by side, a box hidden on a phone is still there, and
192// nothing is cut off. A small frame is not a phone.
193func TestPresenterDrawsWhatTheProjectorShows(t *testing.T) {
194 rig, app := newRigApp(t, nil)
195 cookies := signedIn(t, app, allowedAddress)
196 session, token := startPresenting(t, app, columnsDeck(t, app, cookies), cookies)
197
198 // The projector at 1920×1080, on the session's own slide: a page
199 // named for another slide is put back on the session's by its stream.
200 projector := func(n string) []blockBox {
201 var out []blockBox
202 rig.Run(chromedp.Navigate("about:blank"))
203 press(t, app, session, token, url.Values{"op": {"goto"}, "slide": {n}, "press": {newPressID(t)}}, nil)
204 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1920, Height: 1080, DeviceScaleFactor: 1, Mobile: false}),
205 chromedp.Navigate(rig.Origin+"/p/"+session),
206 waitFor(`document.querySelector('[slides-surface]')?.getAttribute('data-slide') === '`+n+`' && document.querySelector('[slides-stage] [slides-slide]')`),
207 evalInto(&out, blockLayoutJS+`('[slides-stage]')`))
208 return out
209 }
210 two, one := projector("2"), projector("1")
211 if len(one) < 4 || !one[3].Shown || one[2].X < 0.4 || one[1].X > 0.1 {
212 t.Fatalf("the projector itself does not draw two columns with the phone-hidden box: %+v", one)
213 }
214
215 window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token)
216 for _, size := range presenterSizes {
217 var now, next []blockBox
218 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}),
219 chromedp.Navigate(window),
220 waitFor(`document.querySelector('[presenter-panel="next"] [slides-slide]')`),
221 evalInto(&now, blockLayoutJS+`('[presenter-panel="now"]')`),
222 evalInto(&next, blockLayoutJS+`('[presenter-panel="next"]')`))
223 if diff := sameBoxes(one, now); diff != "" {
224 t.Errorf("%dx%d: Now is not the projector's slide 1: %s", size.w, size.h, diff)
225 }
226 if diff := sameBoxes(two, next); diff != "" {
227 t.Errorf("%dx%d: Next is not the projector's slide 2: %s", size.w, size.h, diff)
228 }
229 }
230}
231
232// TestPresenterNextIsHalfOfNow: in the default window Next is drawn at
233// half the width of Now, tall windows and short, stacked or arranged.
234func TestPresenterNextIsHalfOfNow(t *testing.T) {
235 rig, app := newRigApp(t, nil)
236 cookies := signedIn(t, app, allowedAddress)
237 session, token := startPresenting(t, app, stepsDeck(t, app, cookies), cookies)
238 window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token)
239 for _, size := range presenterSizes {
240 var w struct{ Now, Next float64 }
241 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}),
242 chromedp.Navigate(window),
243 waitFor(`document.querySelector('[presenter-panel="next"] [presenter-frame]')`),
244 evalInto(&w, `({ Now: document.querySelector('[presenter-panel="now"] [presenter-frame]').getBoundingClientRect().width,
245 Next: document.querySelector('[presenter-panel="next"] [presenter-frame]').getBoundingClientRect().width })`))
246 if d := w.Next - w.Now/2; d > 1 || d < -1 {
247 t.Errorf("%dx%d: Now is %.1fpx wide and Next %.1fpx, want half", size.w, size.h, w.Now, w.Next)
248 }
249 }
250}
251
252// TestPresenterMenuFitsTheWindow: the ⋯ menu, open, lies inside the
253// window at every size, and every row of it can be reached.
254func TestPresenterMenuFitsTheWindow(t *testing.T) {
255 rig, app := newRigApp(t, nil)
256 cookies := signedIn(t, app, allowedAddress)
257 session, token := startPresenting(t, app, stepsDeck(t, app, cookies), cookies)
258 window := rig.Origin + "/p/" + session + "/notes?t=" + url.QueryEscape(token)
259 for _, size := range presenterSizes {
260 var m struct {
261 Top, Bottom, Left, Right, VW, VH float64
262 Rows int
263 Reach bool
264 SH float64
265 }
266 rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: size.w, Height: size.h, DeviceScaleFactor: 1, Mobile: false}),
267 chromedp.Navigate(window),
268 waitFor(`document.querySelector('[presenter-more]')`),
269 chromedp.Click(chromedp.CSS(`[presenter-more]`)),
270 waitFor(`document.querySelector('[presenter-more-menu]').open`),
271 evalInto(&m, `(() => { const menu = document.querySelector('[presenter-menu]'); const r = menu.getBoundingClientRect();
272 const rows = menu.querySelectorAll('button, a');
273 const style = getComputedStyle(menu);
274 return { Top: r.top, Bottom: r.bottom, Left: r.left, Right: r.right, VW: innerWidth, VH: innerHeight,
275 Rows: rows.length, SH: document.documentElement.scrollHeight,
276 Reach: menu.scrollHeight <= menu.clientHeight + 1 || style.overflowY === 'auto' }; })()`))
277 if m.Rows != 6 {
278 t.Errorf("%dx%d: the menu has %d rows, want 6", size.w, size.h, m.Rows)
279 }
280 if m.Top < 0 || m.Left < 0 || m.Bottom > m.VH || m.Right > m.VW || !m.Reach || m.SH != m.VH {
281 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)",
282 size.w, size.h, m.Left, m.Top, m.Right, m.Bottom, m.VW, m.VH, m.SH)
283 }
284 }
285}
286