| 1 | //go:build browser |
| 2 | |
| 3 | // The presenter window's notes (Task 11): fitted or scrolling, sized by |
| 4 | // the presenter, following the presses, and remembered in the cookie |
| 5 | // the server draws the first paint from. |
| 6 | package slidestest |
| 7 | |
| 8 | import ( |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | "github.com/chromedp/cdproto/emulation" |
| 17 | "github.com/chromedp/chromedp" |
| 18 | ) |
| 19 | |
| 20 | // wordsNotes is n words in parts parts, "w0 w1 ..." with no press lines. |
| 21 | func wordsNotes(prefix string, n, parts int) string { |
| 22 | var out []string |
| 23 | per := n / parts |
| 24 | for p := range parts { |
| 25 | var ws []string |
| 26 | for w := range per { |
| 27 | ws = append(ws, prefix+strconv.Itoa(p*per+w)) |
| 28 | } |
| 29 | out = append(out, strings.Join(ws, " ")+".") |
| 30 | } |
| 31 | return strings.Join(out, "\n\n") |
| 32 | } |
| 33 | |
| 34 | const presenterNotesJS = `(() => { |
| 35 | const n = document.querySelector('[presenter-notes]'); |
| 36 | const de = document.documentElement; |
| 37 | const sizeNow = parseFloat(getComputedStyle(n).fontSize); |
| 38 | const over = n.scrollHeight > n.clientHeight + 1; |
| 39 | // What the next pixel up would do. |
| 40 | const was = n.style.fontSize; |
| 41 | n.style.fontSize = (sizeNow + 1) + 'px'; |
| 42 | const overUp = n.scrollHeight > n.clientHeight + 1; |
| 43 | n.style.fontSize = was; |
| 44 | return { Size: sizeNow, Over: over, OverUp: overUp, Mode: n.getAttribute('data-notes-mode'), |
| 45 | Fade: n.getAttribute('data-fade') || '', DocScrolls: de.scrollHeight > innerHeight || de.scrollWidth > innerWidth, |
| 46 | Text: n.textContent, Top: n.scrollTop, |
| 47 | Pressed: [...document.querySelectorAll('[presenter-tools] [aria-pressed]')].map(b => b.getAttribute('aria-pressed')).join(',') }; |
| 48 | })()` |
| 49 | |
| 50 | type presenterNotesState struct { |
| 51 | Size float64 |
| 52 | Over bool |
| 53 | OverUp bool |
| 54 | Mode string |
| 55 | Fade string |
| 56 | DocScrolls bool |
| 57 | Text string |
| 58 | Top float64 |
| 59 | Pressed string |
| 60 | } |
| 61 | |
| 62 | type notesRig struct { |
| 63 | rig *harness.Rig |
| 64 | pt *presenterTalk |
| 65 | } |
| 66 | |
| 67 | func (nr notesRig) window() string { |
| 68 | return nr.rig.Origin + "/p/" + nr.pt.session + "/notes?t=" + url.QueryEscape(nr.pt.token) |
| 69 | } |
| 70 | |
| 71 | // goTo puts the session on slide n and draws the window there. |
| 72 | func (nr notesRig) goTo(t *testing.T, n int) { |
| 73 | t.Helper() |
| 74 | nr.rig.Run(chromedp.Navigate("about:blank")) |
| 75 | press(t, nr.pt.app, nr.pt.session, nr.pt.token, url.Values{"op": {"goto"}, "slide": {strconv.Itoa(n)}, "press": {newPressID(t)}}, nil) |
| 76 | nr.rig.Run(chromedp.Navigate(nr.window()), waitFor(`document.querySelector('[presenter]')?.getAttribute('data-slide') === '`+strconv.Itoa(n)+`' && document.querySelector('[presenter-notes][data-notes-scrolls]')`)) |
| 77 | } |
| 78 | |
| 79 | func (nr notesRig) state(t *testing.T) presenterNotesState { |
| 80 | t.Helper() |
| 81 | var s presenterNotesState |
| 82 | nr.rig.Run(evalInto(&s, presenterNotesJS)) |
| 83 | return s |
| 84 | } |
| 85 | |
| 86 | func (nr notesRig) click(t *testing.T, attr string) { |
| 87 | t.Helper() |
| 88 | nr.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[` + attr + `]').click()`)) |
| 89 | } |
| 90 | |
| 91 | func newNotesRig(t *testing.T, notes map[string]string) notesRig { |
| 92 | t.Helper() |
| 93 | rig, app := newRigApp(t, nil) |
| 94 | cookies := signedIn(t, app, allowedAddress) |
| 95 | deck := stepsDeck(t, app, cookies) |
| 96 | setNotes(t, app, cookies, deck, notes) |
| 97 | session, token := startPresenting(t, app, deck, cookies) |
| 98 | rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1, Mobile: false})) |
| 99 | return notesRig{rig: rig, pt: &presenterTalk{app: app, cookies: cookies, deck: deck, session: session, token: token}} |
| 100 | } |
| 101 | |
| 102 | // TestNotesFitShrinksToTheFloor: short notes keep the chosen size, |
| 103 | // longer ones shrink a pixel at a time until they fit (one pixel more |
| 104 | // would not), and ones too long for the floor sit at 16px and scroll. |
| 105 | func TestNotesFitShrinksToTheFloor(t *testing.T) { |
| 106 | nr := newNotesRig(t, map[string]string{ |
| 107 | "plain": "A few short words.", |
| 108 | "steps": wordsNotes("mid", 110, 1), |
| 109 | "last": wordsNotes("long", 1500, 1), |
| 110 | "extra": wordsNotes("long", 1500, 1), |
| 111 | "parked": wordsNotes("long", 1500, 1), |
| 112 | }) |
| 113 | nr.goTo(t, 2) |
| 114 | if s := nr.state(t); s.Size != 24 || s.Over || s.Mode != "fit" { |
| 115 | t.Errorf("short notes: size %v over %v mode %s, want 24px, fitting, fit", s.Size, s.Over, s.Mode) |
| 116 | } |
| 117 | nr.goTo(t, 1) |
| 118 | s := nr.state(t) |
| 119 | t.Logf("medium notes fit at %vpx", s.Size) |
| 120 | if s.Over || s.Size < 16 || s.Size >= 24 || !s.OverUp { |
| 121 | t.Errorf("medium notes: size %v over %v, one more pixel over %v; want a size under 24 that fits and is the largest that does", s.Size, s.Over, s.OverUp) |
| 122 | } |
| 123 | nr.goTo(t, 3) |
| 124 | s = nr.state(t) |
| 125 | if s.Size != 16 || !s.Over || s.DocScrolls { |
| 126 | t.Errorf("notes past the floor: size %v overflowing %v document scrolls %v, want 16px, scrolling inside, document still", s.Size, s.Over, s.DocScrolls) |
| 127 | } |
| 128 | if s.Fade != "bottom" { |
| 129 | t.Errorf("notes past the floor fade at %q, want the bottom (more below)", s.Fade) |
| 130 | } |
| 131 | // Smaller and larger move 2px from the chosen size, and keep Fit. |
| 132 | nr.click(t, "presenter-notes-larger") |
| 133 | nr.goTo(t, 2) |
| 134 | if s := nr.state(t); s.Size != 26 || s.Mode != "fit" || s.Pressed != "true,false" { |
| 135 | t.Errorf("after larger: size %v mode %s pressed %s, want 26px, fit, true,false", s.Size, s.Mode, s.Pressed) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // TestNotesTooLongScrollInsideAndLoseNoText: in Scroll the chosen size |
| 140 | // is used, the panel scrolls, every word is reachable, and the |
| 141 | // document does not scroll. |
| 142 | func TestNotesTooLongScrollInsideAndLoseNoText(t *testing.T) { |
| 143 | nr := newNotesRig(t, map[string]string{"steps": wordsNotes("word", 1200, 5)}) |
| 144 | nr.goTo(t, 1) |
| 145 | nr.click(t, "presenter-notes-scroll") |
| 146 | for range 4 { |
| 147 | nr.click(t, "presenter-notes-larger") |
| 148 | } |
| 149 | s := nr.state(t) |
| 150 | if s.Mode != "scroll" || s.Size != 32 || !s.Over || s.DocScrolls || s.Pressed != "false,true" { |
| 151 | t.Fatalf("scroll: mode %s size %v over %v document scrolls %v pressed %s, want scroll, 32px, overflowing inside, document still", s.Mode, s.Size, s.Over, s.DocScrolls, s.Pressed) |
| 152 | } |
| 153 | // Reach the end by scrolling the panel only: the last word's box |
| 154 | // lies inside the panel's visible rect, and the document stays put. |
| 155 | var reach struct { |
| 156 | Last bool |
| 157 | Doc bool |
| 158 | Inside bool |
| 159 | AtBottom bool |
| 160 | } |
| 161 | nr.rig.Run(evalInto(&reach, `(() => { const n = document.querySelector('[presenter-notes]'); n.scrollTop = n.scrollHeight; |
| 162 | const de = document.documentElement; |
| 163 | const sec = [...n.querySelectorAll('section')].pop(); |
| 164 | const t = sec.lastChild.nodeType === 3 ? sec.lastChild : sec.lastChild.lastChild; |
| 165 | const i = t.textContent.lastIndexOf('word1199'); |
| 166 | const r = document.createRange(); r.setStart(t, i); r.setEnd(t, i + 8); |
| 167 | const w = r.getBoundingClientRect(), p = n.getBoundingClientRect(); |
| 168 | return { Last: n.textContent.includes('word1199'), Doc: de.scrollHeight > innerHeight || window.scrollY > 0, |
| 169 | Inside: w.height > 0 && w.top >= p.top && w.bottom <= p.bottom && w.left >= p.left && w.right <= p.right, |
| 170 | AtBottom: n.scrollTop + n.clientHeight >= n.scrollHeight - 1 }; })()`)) |
| 171 | if !reach.Last || reach.Doc || !reach.AtBottom || !reach.Inside { |
| 172 | t.Errorf("last word there %v, the document scrolled %v, panel reached its end %v, last word inside the panel %v", reach.Last, reach.Doc, reach.AtBottom, reach.Inside) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | // TestNotesFollowThePresses: five parts for four presses show one bright |
| 177 | // part per press (the last staying bright if there are presses left over, |
| 178 | // and the last part if there are more presses than parts) and no "(Press.)". |
| 179 | func TestNotesFollowThePresses(t *testing.T) { |
| 180 | nr := newNotesRig(t, map[string]string{ |
| 181 | "steps": wordsNotes("a", 400, 1) + "\n\n(Press.)\n\n" + wordsNotes("b", 400, 1) + "\n\n(Press.)\n\n" + wordsNotes("c", 400, 1), |
| 182 | "plain": "First part.\n\n(Press.)\n\nSecond part.\n\n(Press.)\n\nThird part.", |
| 183 | }) |
| 184 | brightJS := `(() => { const n = document.querySelector('[presenter-notes]'); |
| 185 | const now = [...n.querySelectorAll('[data-part]')].map(s => s.getAttribute('data-part')); |
| 186 | const b = n.querySelector('[data-part="now"]'); |
| 187 | let inView = false; |
| 188 | if (b) { const nb = n.getBoundingClientRect(), bb = b.getBoundingClientRect(); inView = bb.top < nb.bottom && bb.bottom > nb.top; } |
| 189 | return { Parts: now.join(','), InView: inView, Press: n.textContent.includes('(Press.)'), |
| 190 | Step: document.querySelector('[presenter]').getAttribute('data-step') }; })()` |
| 191 | type bright struct { |
| 192 | Parts string |
| 193 | InView bool |
| 194 | Press bool |
| 195 | Step string |
| 196 | } |
| 197 | check := func(label, want, step string) { |
| 198 | t.Helper() |
| 199 | var b bright |
| 200 | nr.rig.Run(waitFor(`document.querySelector('[presenter]').getAttribute('data-step') === '`+step+`'`), evalInto(&b, brightJS)) |
| 201 | if b.Parts != want || !b.InView || b.Press { |
| 202 | t.Errorf("%s: parts %q in view %v press line %v, want %q in view with no (Press.) line", label, b.Parts, b.InView, b.Press, want) |
| 203 | } |
| 204 | } |
| 205 | // "steps" has four presses; the notes have three parts, so presses |
| 206 | // past the last part keep it bright. |
| 207 | nr.goTo(t, 1) |
| 208 | nr.click(t, "presenter-notes-scroll") |
| 209 | check("step 0", "now,later,later", "0") |
| 210 | nr.click(t, `presenter-press="next"`) |
| 211 | check("step 1", "past,now,later", "1") |
| 212 | nr.click(t, `presenter-press="next"`) |
| 213 | check("step 2", "past,past,now", "2") |
| 214 | nr.click(t, `presenter-press="next"`) |
| 215 | check("step 3 (more presses than parts)", "past,past,now", "3") |
| 216 | nr.click(t, `presenter-press="prev"`) |
| 217 | check("back to step 2", "past,past,now", "2") |
| 218 | nr.click(t, `presenter-press="prev"`) |
| 219 | check("back to step 1", "past,now,later", "1") |
| 220 | // More parts than presses: "plain" has none, so only the first is bright. |
| 221 | nr.goTo(t, 2) |
| 222 | check("no presses", "now,later,later", "0") |
| 223 | } |
| 224 | |
| 225 | // TestNotesChoiceIsRememberedAtFirstPaint: a choice is written to the |
| 226 | // cookie (path /p, percent-encoded, panels kept), and the server draws |
| 227 | // the next window with it. |
| 228 | func TestNotesChoiceIsRememberedAtFirstPaint(t *testing.T) { |
| 229 | nr := newNotesRig(t, map[string]string{"plain": "Some words."}) |
| 230 | nr.goTo(t, 2) |
| 231 | nr.click(t, "presenter-notes-scroll") |
| 232 | nr.click(t, "presenter-notes-larger") |
| 233 | nr.click(t, "presenter-notes-larger") |
| 234 | var raw string |
| 235 | nr.rig.Run(evalInto(&raw, `(document.cookie.split('; ').find(c => c.startsWith('slides-presenter=')) || '').slice('slides-presenter='.length)`)) |
| 236 | if !strings.HasPrefix(raw, "scroll.28.") || strings.Contains(raw, ";") { |
| 237 | t.Fatalf("cookie value %q, want scroll.28.<panels>, with no raw semicolon", raw) |
| 238 | } |
| 239 | if !strings.HasSuffix(raw, "0%2C0%2C7%2C8%3B0%2C8%2C7%2C4%3B7%2C0%2C5%2C12") && !strings.HasSuffix(raw, "0,0,7,8%3B0,8,7,4%3B7,0,5,12") { |
| 240 | t.Errorf("cookie value %q lost the panels", raw) |
| 241 | } |
| 242 | // The server's first paint, with no scripts, has the choice. |
| 243 | page := mustGet(t, nr.pt.app, "/p/"+nr.pt.session+"/notes?t="+url.QueryEscape(nr.pt.token), 200, |
| 244 | []*http.Cookie{{Name: "slides-presenter", Value: raw}}).Body.String() |
| 245 | for _, want := range []string{`data-notes-mode="scroll"`, "font-size: 28px", `presenter-notes-scroll aria-pressed="true"`} { |
| 246 | if !strings.Contains(page, want) { |
| 247 | t.Errorf("first paint lacks %s", want) |
| 248 | } |
| 249 | } |
| 250 | // And the next window starts there, in the browser. |
| 251 | nr.goTo(t, 2) |
| 252 | if s := nr.state(t); s.Mode != "scroll" || s.Size != 28 { |
| 253 | t.Errorf("reopened: mode %s size %v, want scroll, 28px", s.Mode, s.Size) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // TestNotesNewSlideStartsAtItsBeginning: scroll long notes down, move |
| 258 | // the talk to another slide with long single-part notes (the window |
| 259 | // follows without a reload), and its first word is showing. |
| 260 | func TestNotesNewSlideStartsAtItsBeginning(t *testing.T) { |
| 261 | nr := newNotesRig(t, map[string]string{ |
| 262 | "steps": wordsNotes("first", 1500, 1), |
| 263 | "plain": wordsNotes("second", 1500, 1), |
| 264 | }) |
| 265 | nr.goTo(t, 1) |
| 266 | nr.click(t, "presenter-notes-scroll") |
| 267 | var before float64 |
| 268 | nr.rig.Run(evalInto(&before, `(() => { const n = document.querySelector('[presenter-notes]'); n.scrollTop = 900; return n.scrollTop })()`)) |
| 269 | if before < 100 { |
| 270 | t.Fatalf("the first slide's notes scrolled to %v, want well down", before) |
| 271 | } |
| 272 | press(t, nr.pt.app, nr.pt.session, nr.pt.token, url.Values{"op": {"goto"}, "slide": {"2"}, "press": {newPressID(t)}}, nil) |
| 273 | nr.rig.Run(waitFor(`document.querySelector('[presenter-notes]').textContent.includes('second0 ')`)) |
| 274 | var got struct { |
| 275 | Top float64 |
| 276 | Visible bool |
| 277 | } |
| 278 | nr.rig.Run(evalInto(&got, `(() => { const n = document.querySelector('[presenter-notes]'); |
| 279 | const r = document.createRange(); const tn = document.createTreeWalker(n, NodeFilter.SHOW_TEXT).nextNode(); r.setStart(tn, 0); r.setEnd(tn, 7); |
| 280 | const w = r.getBoundingClientRect(), p = n.getBoundingClientRect(); |
| 281 | return { Top: n.scrollTop, Visible: w.top >= p.top && w.bottom <= p.bottom }; })()`)) |
| 282 | if got.Top != 0 || !got.Visible { |
| 283 | t.Errorf("new slide's notes: scrolled to %v, first word visible %v, want the beginning", got.Top, got.Visible) |
| 284 | } |
| 285 | } |
| 286 | |