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 slide window's control bar is out of sight while presenting: the
4// slide has nothing over it. A pointer moving over the window, or focus
5// entering the bar, shows it; two seconds of stillness, or a key that
6// moves the talk, hides it again. With scripts off it stays, because it
7// is then the only way to drive the window. Both scripts do this: the
8// presenter bundle (a session with a position) and static/present.js
9// (the follow link).
10package slidestest
11
12import (
13 "net/url"
14 "testing"
15 "time"
16
17 "amadan.net/rastrillo/rastrillo/harness"
18 "github.com/chromedp/cdproto/emulation"
19 "github.com/chromedp/cdproto/input"
20 "github.com/chromedp/chromedp"
21 "github.com/chromedp/chromedp/kb"
22)
23
24// barShownJS: the bar can be seen. Opacity is what hides it, so the
25// bar stays in the accessibility tree and in the tab order.
26const barShownJS = `(() => { const b = document.querySelector('[slides-bar]'); return !!b && getComputedStyle(b).opacity === '1' })()`
27const barHiddenJS = `(() => { const b = document.querySelector('[slides-bar]'); return !!b && getComputedStyle(b).opacity === '0' })()`
28
29func mouseTo(x, y float64) chromedp.Action[chromedp.Void] {
30 return cdpStep(input.DispatchMouseEvent, input.DispatchMouseEventParams{Type: chromedp.MouseMoved, X: x, Y: y})
31}
32
33// checkQuietBar drives one window, loaded at page, through the whole
34// approved behaviour.
35func checkQuietBar(t *testing.T, rig *harness.Rig, page, ready string, keys bool) {
36 t.Helper()
37 rig.Run(
38 cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1}),
39 chromedp.Navigate(page),
40 waitFor(ready),
41 waitFor(barHiddenJS),
42 )
43 var cursor string
44 var inTree bool
45 rig.Run(
46 evalInto(&cursor, `getComputedStyle(document.querySelector('[slides-surface]')).cursor`),
47 evalInto(&inTree, `(() => { const b = document.querySelector('[slides-bar]'); const s = getComputedStyle(b); return s.display !== 'none' && s.visibility !== 'hidden' && !b.hidden && b.querySelector('a, button') !== null })()`),
48 )
49 if cursor != "none" {
50 t.Errorf("the pointer over the quiet window is %q, want none", cursor)
51 }
52 if !inTree {
53 t.Errorf("the hidden bar must stay displayed (in the accessibility tree) and hold its controls")
54 }
55
56 // Moving the pointer shows it, and the pointer comes back with it.
57 rig.Run(mouseTo(400, 200), waitFor(barShownJS))
58 rig.Run(evalInto(&cursor, `getComputedStyle(document.querySelector('[slides-surface]')).cursor`))
59 if cursor == "none" {
60 t.Errorf("the pointer is hidden while the bar shows")
61 }
62 rig.Run(chromedp.Sleep(2500*time.Millisecond), waitFor(barHiddenJS))
63
64 // A key that moves the talk hides it at once.
65 var quiet bool
66 rig.Run(
67 mouseTo(420, 210), waitFor(barShownJS),
68 chromedp.KeyEvent(kb.ArrowRight),
69 chromedp.Sleep(800*time.Millisecond),
70 evalInto(&quiet, barHiddenJS),
71 )
72 // The follow link drives nothing, so a key there moves nothing.
73 if keys && !quiet {
74 t.Errorf("a key that moves the talk left the bar showing")
75 }
76
77 // Over the bar it stays up past the two seconds.
78 var y float64
79 rig.Run(
80 mouseTo(400, 200), waitFor(barShownJS),
81 evalInto(&y, `(() => { const r = document.querySelector('[slides-bar]').getBoundingClientRect(); return r.top + r.height / 2 })()`),
82 )
83 var still bool
84 rig.Run(mouseTo(640, y), chromedp.Sleep(2500*time.Millisecond), evalInto(&still, barShownJS))
85 if !still {
86 t.Errorf("the bar hid while the pointer was over it")
87 }
88 rig.Run(mouseTo(400, 100), chromedp.Sleep(2500*time.Millisecond), waitFor(barHiddenJS))
89
90 // A pointer that rests on the bar and then leaves the window is idle
91 // from then on: the bar goes two seconds after it leaves.
92 rig.Run(mouseTo(400, 200), waitFor(barShownJS), mouseTo(640, y), chromedp.Sleep(2500*time.Millisecond),
93 mouseTo(640, 719), mouseTo(640, 760))
94 rig.Run(chromedp.Sleep(2500*time.Millisecond), waitFor(barHiddenJS))
95
96 // Tab reaches the hidden bar, and focus shows it and holds it.
97 var inside, held bool
98 rig.Run(
99 chromedp.KeyEvent(kb.Tab), waitFor(barShownJS),
100 evalInto(&inside, `!!document.activeElement?.closest('[slides-bar]')`),
101 chromedp.Sleep(2500*time.Millisecond),
102 evalInto(&held, barShownJS),
103 )
104 if !inside {
105 t.Errorf("Tab did not land in the bar")
106 }
107 if !held {
108 t.Errorf("the bar hid while focus was inside it")
109 }
110}
111
112// checkQuietBarFollowsTheTalk: the bar goes when the talk moves (a press
113// from anywhere, a blackout, a number and Enter), but never while focus
114// is inside it; then it goes on the ordinary idle rule.
115func checkQuietBarFollowsTheTalk(t *testing.T, rig *harness.Rig, page, ready string, press func(op string), keys bool) {
116 t.Helper()
117 rig.Run(
118 cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1}),
119 chromedp.Navigate(page), waitFor(ready), waitFor(barHiddenJS),
120 )
121 settle := chromedp.Sleep(800 * time.Millisecond)
122 var shown, hidden bool
123 expect := func(what string, wantShown bool) {
124 t.Helper()
125 rig.Run(settle, evalInto(&shown, barShownJS), evalInto(&hidden, barHiddenJS))
126 if wantShown && !shown || !wantShown && !hidden {
127 t.Errorf("%s: bar shown=%v hidden=%v, want shown=%v", what, shown, hidden, wantShown)
128 }
129 }
130 for _, op := range []string{"next", "blackout", "blackout"} {
131 rig.Run(mouseTo(400, 200), waitFor(barShownJS))
132 press(op)
133 expect("after "+op+" from elsewhere", false)
134 }
135 if keys {
136 rig.Run(mouseTo(400, 200), waitFor(barShownJS), chromedp.KeyEvent("3"), chromedp.KeyEvent(kb.Enter))
137 expect("after a number and Enter", false)
138 }
139
140 // Focus inside the bar holds it, whatever the talk does.
141 rig.Run(chromedp.KeyEvent(kb.Tab), waitFor(barShownJS))
142 for _, op := range []string{"next", "blackout", "blackout"} {
143 press(op)
144 expect("focus in the bar, after "+op, true)
145 }
146 if keys {
147 rig.Run(chromedp.KeyEvent(kb.ArrowLeft))
148 expect("focus in the bar, after a key that moves the talk", true)
149 }
150 // Focus leaves: the idle rule applies again.
151 rig.Run(evalInto(&shown, `(document.activeElement.blur(), true)`), chromedp.Sleep(2500*time.Millisecond), waitFor(barHiddenJS))
152}
153
154func TestTheSlideWindowBarIsOutOfSightWhilePresenting(t *testing.T) {
155 rig, app := newRigApp(t, nil)
156 cookies := signedIn(t, app, allowedAddress)
157 deck := stepsDeck(t, app, cookies)
158 session, token := startPresenting(t, app, deck, cookies)
159
160 t.Run("positional session", func(t *testing.T) {
161 checkQuietBar(t, rig, rig.Origin+"/p/"+session+"?t="+url.QueryEscape(token), `document.querySelector('[slides-surface][data-talk]')`, true)
162 })
163 t.Run("positional session, the talk moves", func(t *testing.T) {
164 s2, t2 := startPresenting(t, app, deck, cookies)
165 checkQuietBarFollowsTheTalk(t, rig, rig.Origin+"/p/"+s2+"?t="+url.QueryEscape(t2), `document.querySelector('[slides-surface][data-talk]')`,
166 func(name string) { press(t, app, s2, t2, op(t, name), nil) }, true)
167 })
168 t.Run("follow link, the talk moves", func(t *testing.T) {
169 s3, t3 := startPresenting(t, app, deck, cookies)
170 share := mintShareLink(t, app, deck, cookies)
171 checkQuietBarFollowsTheTalk(t, rig, rig.Origin+"/s/"+share+"?follow=1", `document.querySelector('[slides-surface]:not([data-talk])')`,
172 func(name string) { press(t, app, s3, t3, op(t, name), nil) }, false)
173 })
174 t.Run("follow link", func(t *testing.T) {
175 share := mintShareLink(t, app, deck, cookies)
176 checkQuietBar(t, rig, rig.Origin+"/s/"+share+"?follow=1", `document.querySelector('[slides-surface]:not([data-talk])')`, false)
177 })
178}
179
180func TestTheSlideWindowBarStaysWithScriptsOff(t *testing.T) {
181 rig, app := newRigApp(t, nil)
182 cookies := signedIn(t, app, allowedAddress)
183 deck := stepsDeck(t, app, cookies)
184 session, token := startPresenting(t, app, deck, cookies)
185
186 rig.Run(
187 cdpStep(emulation.SetScriptExecutionDisabled, emulation.SetScriptExecutionDisabledParams{Value: true}),
188 chromedp.Navigate(rig.Origin+"/p/"+session+"?t="+url.QueryEscape(token)),
189 chromedp.WaitVisible(chromedp.CSS(`[slides-bar]`)),
190 chromedp.Sleep(2500*time.Millisecond),
191 )
192 var shown bool
193 rig.Run(evalInto(&shown, barShownJS))
194 if !shown {
195 t.Errorf("with scripts off the bar must stay visible")
196 }
197}
198