//go:build browser // The presenter window's notes (Task 11): fitted or scrolling, sized by // the presenter, following the presses, and remembered in the cookie // the server draws the first paint from. package slidestest import ( "net/http" "net/url" "strconv" "strings" "testing" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/chromedp" ) // wordsNotes is n words in parts parts, "w0 w1 ..." with no press lines. func wordsNotes(prefix string, n, parts int) string { var out []string per := n / parts for p := range parts { var ws []string for w := range per { ws = append(ws, prefix+strconv.Itoa(p*per+w)) } out = append(out, strings.Join(ws, " ")+".") } return strings.Join(out, "\n\n") } const presenterNotesJS = `(() => { const n = document.querySelector('[presenter-notes]'); const de = document.documentElement; const sizeNow = parseFloat(getComputedStyle(n).fontSize); const over = n.scrollHeight > n.clientHeight + 1; // What the next pixel up would do. const was = n.style.fontSize; n.style.fontSize = (sizeNow + 1) + 'px'; const overUp = n.scrollHeight > n.clientHeight + 1; n.style.fontSize = was; return { Size: sizeNow, Over: over, OverUp: overUp, Mode: n.getAttribute('data-notes-mode'), Fade: n.getAttribute('data-fade') || '', DocScrolls: de.scrollHeight > innerHeight || de.scrollWidth > innerWidth, Text: n.textContent, Top: n.scrollTop, Pressed: [...document.querySelectorAll('[presenter-tools] [aria-pressed]')].map(b => b.getAttribute('aria-pressed')).join(',') }; })()` type presenterNotesState struct { Size float64 Over bool OverUp bool Mode string Fade string DocScrolls bool Text string Top float64 Pressed string } type notesRig struct { rig *harness.Rig pt *presenterTalk } func (nr notesRig) window() string { return nr.rig.Origin + "/p/" + nr.pt.session + "/notes?t=" + url.QueryEscape(nr.pt.token) } // goTo puts the session on slide n and draws the window there. func (nr notesRig) goTo(t *testing.T, n int) { t.Helper() nr.rig.Run(chromedp.Navigate("about:blank")) press(t, nr.pt.app, nr.pt.session, nr.pt.token, url.Values{"op": {"goto"}, "slide": {strconv.Itoa(n)}, "press": {newPressID(t)}}, nil) nr.rig.Run(chromedp.Navigate(nr.window()), waitFor(`document.querySelector('[presenter]')?.getAttribute('data-slide') === '`+strconv.Itoa(n)+`' && document.querySelector('[presenter-notes][data-notes-scrolls]')`)) } func (nr notesRig) state(t *testing.T) presenterNotesState { t.Helper() var s presenterNotesState nr.rig.Run(evalInto(&s, presenterNotesJS)) return s } func (nr notesRig) click(t *testing.T, attr string) { t.Helper() nr.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[` + attr + `]').click()`)) } func newNotesRig(t *testing.T, notes map[string]string) notesRig { t.Helper() rig, app := newRigApp(t, nil) cookies := signedIn(t, app, allowedAddress) deck := stepsDeck(t, app, cookies) setNotes(t, app, cookies, deck, notes) session, token := startPresenting(t, app, deck, cookies) rig.Run(cdpStep(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 720, DeviceScaleFactor: 1, Mobile: false})) return notesRig{rig: rig, pt: &presenterTalk{app: app, cookies: cookies, deck: deck, session: session, token: token}} } // TestNotesFitShrinksToTheFloor: short notes keep the chosen size, // longer ones shrink a pixel at a time until they fit (one pixel more // would not), and ones too long for the floor sit at 16px and scroll. func TestNotesFitShrinksToTheFloor(t *testing.T) { nr := newNotesRig(t, map[string]string{ "plain": "A few short words.", "steps": wordsNotes("mid", 110, 1), "last": wordsNotes("long", 1500, 1), "extra": wordsNotes("long", 1500, 1), "parked": wordsNotes("long", 1500, 1), }) nr.goTo(t, 2) if s := nr.state(t); s.Size != 24 || s.Over || s.Mode != "fit" { t.Errorf("short notes: size %v over %v mode %s, want 24px, fitting, fit", s.Size, s.Over, s.Mode) } nr.goTo(t, 1) s := nr.state(t) t.Logf("medium notes fit at %vpx", s.Size) if s.Over || s.Size < 16 || s.Size >= 24 || !s.OverUp { 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) } nr.goTo(t, 3) s = nr.state(t) if s.Size != 16 || !s.Over || s.DocScrolls { 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) } if s.Fade != "bottom" { t.Errorf("notes past the floor fade at %q, want the bottom (more below)", s.Fade) } // Smaller and larger move 2px from the chosen size, and keep Fit. nr.click(t, "presenter-notes-larger") nr.goTo(t, 2) if s := nr.state(t); s.Size != 26 || s.Mode != "fit" || s.Pressed != "true,false" { t.Errorf("after larger: size %v mode %s pressed %s, want 26px, fit, true,false", s.Size, s.Mode, s.Pressed) } } // TestNotesTooLongScrollInsideAndLoseNoText: in Scroll the chosen size // is used, the panel scrolls, every word is reachable, and the // document does not scroll. func TestNotesTooLongScrollInsideAndLoseNoText(t *testing.T) { nr := newNotesRig(t, map[string]string{"steps": wordsNotes("word", 1200, 5)}) nr.goTo(t, 1) nr.click(t, "presenter-notes-scroll") for range 4 { nr.click(t, "presenter-notes-larger") } s := nr.state(t) if s.Mode != "scroll" || s.Size != 32 || !s.Over || s.DocScrolls || s.Pressed != "false,true" { 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) } // Reach the end by scrolling the panel only: the last word's box // lies inside the panel's visible rect, and the document stays put. var reach struct { Last bool Doc bool Inside bool AtBottom bool } nr.rig.Run(evalInto(&reach, `(() => { const n = document.querySelector('[presenter-notes]'); n.scrollTop = n.scrollHeight; const de = document.documentElement; const sec = [...n.querySelectorAll('section')].pop(); const t = sec.lastChild.nodeType === 3 ? sec.lastChild : sec.lastChild.lastChild; const i = t.textContent.lastIndexOf('word1199'); const r = document.createRange(); r.setStart(t, i); r.setEnd(t, i + 8); const w = r.getBoundingClientRect(), p = n.getBoundingClientRect(); return { Last: n.textContent.includes('word1199'), Doc: de.scrollHeight > innerHeight || window.scrollY > 0, Inside: w.height > 0 && w.top >= p.top && w.bottom <= p.bottom && w.left >= p.left && w.right <= p.right, AtBottom: n.scrollTop + n.clientHeight >= n.scrollHeight - 1 }; })()`)) if !reach.Last || reach.Doc || !reach.AtBottom || !reach.Inside { 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) } } // TestNotesFollowThePresses: five parts for four presses show one bright // part per press (the last staying bright if there are presses left over, // and the last part if there are more presses than parts) and no "(Press.)". func TestNotesFollowThePresses(t *testing.T) { nr := newNotesRig(t, map[string]string{ "steps": wordsNotes("a", 400, 1) + "\n\n(Press.)\n\n" + wordsNotes("b", 400, 1) + "\n\n(Press.)\n\n" + wordsNotes("c", 400, 1), "plain": "First part.\n\n(Press.)\n\nSecond part.\n\n(Press.)\n\nThird part.", }) brightJS := `(() => { const n = document.querySelector('[presenter-notes]'); const now = [...n.querySelectorAll('[data-part]')].map(s => s.getAttribute('data-part')); const b = n.querySelector('[data-part="now"]'); let inView = false; if (b) { const nb = n.getBoundingClientRect(), bb = b.getBoundingClientRect(); inView = bb.top < nb.bottom && bb.bottom > nb.top; } return { Parts: now.join(','), InView: inView, Press: n.textContent.includes('(Press.)'), Step: document.querySelector('[presenter]').getAttribute('data-step') }; })()` type bright struct { Parts string InView bool Press bool Step string } check := func(label, want, step string) { t.Helper() var b bright nr.rig.Run(waitFor(`document.querySelector('[presenter]').getAttribute('data-step') === '`+step+`'`), evalInto(&b, brightJS)) if b.Parts != want || !b.InView || b.Press { 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) } } // "steps" has four presses; the notes have three parts, so presses // past the last part keep it bright. nr.goTo(t, 1) nr.click(t, "presenter-notes-scroll") check("step 0", "now,later,later", "0") nr.click(t, `presenter-press="next"`) check("step 1", "past,now,later", "1") nr.click(t, `presenter-press="next"`) check("step 2", "past,past,now", "2") nr.click(t, `presenter-press="next"`) check("step 3 (more presses than parts)", "past,past,now", "3") nr.click(t, `presenter-press="prev"`) check("back to step 2", "past,past,now", "2") nr.click(t, `presenter-press="prev"`) check("back to step 1", "past,now,later", "1") // More parts than presses: "plain" has none, so only the first is bright. nr.goTo(t, 2) check("no presses", "now,later,later", "0") } // TestNotesChoiceIsRememberedAtFirstPaint: a choice is written to the // cookie (path /p, percent-encoded, panels kept), and the server draws // the next window with it. func TestNotesChoiceIsRememberedAtFirstPaint(t *testing.T) { nr := newNotesRig(t, map[string]string{"plain": "Some words."}) nr.goTo(t, 2) nr.click(t, "presenter-notes-scroll") nr.click(t, "presenter-notes-larger") nr.click(t, "presenter-notes-larger") var raw string nr.rig.Run(evalInto(&raw, `(document.cookie.split('; ').find(c => c.startsWith('slides-presenter=')) || '').slice('slides-presenter='.length)`)) if !strings.HasPrefix(raw, "scroll.28.") || strings.Contains(raw, ";") { t.Fatalf("cookie value %q, want scroll.28., with no raw semicolon", raw) } 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") { t.Errorf("cookie value %q lost the panels", raw) } // The server's first paint, with no scripts, has the choice. page := mustGet(t, nr.pt.app, "/p/"+nr.pt.session+"/notes?t="+url.QueryEscape(nr.pt.token), 200, []*http.Cookie{{Name: "slides-presenter", Value: raw}}).Body.String() for _, want := range []string{`data-notes-mode="scroll"`, "font-size: 28px", `presenter-notes-scroll aria-pressed="true"`} { if !strings.Contains(page, want) { t.Errorf("first paint lacks %s", want) } } // And the next window starts there, in the browser. nr.goTo(t, 2) if s := nr.state(t); s.Mode != "scroll" || s.Size != 28 { t.Errorf("reopened: mode %s size %v, want scroll, 28px", s.Mode, s.Size) } } // TestNotesNewSlideStartsAtItsBeginning: scroll long notes down, move // the talk to another slide with long single-part notes (the window // follows without a reload), and its first word is showing. func TestNotesNewSlideStartsAtItsBeginning(t *testing.T) { nr := newNotesRig(t, map[string]string{ "steps": wordsNotes("first", 1500, 1), "plain": wordsNotes("second", 1500, 1), }) nr.goTo(t, 1) nr.click(t, "presenter-notes-scroll") var before float64 nr.rig.Run(evalInto(&before, `(() => { const n = document.querySelector('[presenter-notes]'); n.scrollTop = 900; return n.scrollTop })()`)) if before < 100 { t.Fatalf("the first slide's notes scrolled to %v, want well down", before) } press(t, nr.pt.app, nr.pt.session, nr.pt.token, url.Values{"op": {"goto"}, "slide": {"2"}, "press": {newPressID(t)}}, nil) nr.rig.Run(waitFor(`document.querySelector('[presenter-notes]').textContent.includes('second0 ')`)) var got struct { Top float64 Visible bool } nr.rig.Run(evalInto(&got, `(() => { const n = document.querySelector('[presenter-notes]'); const r = document.createRange(); const tn = document.createTreeWalker(n, NodeFilter.SHOW_TEXT).nextNode(); r.setStart(tn, 0); r.setEnd(tn, 7); const w = r.getBoundingClientRect(), p = n.getBoundingClientRect(); return { Top: n.scrollTop, Visible: w.top >= p.top && w.bottom <= p.bottom }; })()`)) if got.Top != 0 || !got.Visible { t.Errorf("new slide's notes: scrolled to %v, first word visible %v, want the beginning", got.Top, got.Visible) } }