Test: a late editor's slide pictures taken without a relayout
TestFontALateEditorMovesNothing took its before and after pictures of the slide with chromedp.Screenshot, which captures with captureBeyondViewport. For that capture Chromium lays the page out at another size for one frame (1x1 CSS px: the narrow layout, the slide 80px wide), and the font probe's frame sampler sometimes caught it as the slide's layout changing twice. It did so on the stylesheet before Q9 too (2 of 12 runs). inViewShot captures the slide, which is on screen, with captureBeyondViewport off and the clip rounded as chromedp rounds it, so the page the probe watches is the page as shown. What the test asserts, its tolerances and the frames it samples are unchanged. No other test in fonts_browser_test.go takes an element screenshot.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+29
−2
internal/slidestest/fonts_browser_test.go+29 −2
diff --git a/internal/slidestest/fonts_browser_test.go b/internal/slidestest/fonts_browser_test.go| index 4b94017..44f22a4 100644 |
| --- a/internal/slidestest/fonts_browser_test.go |
| +++ b/internal/slidestest/fonts_browser_test.go |
| @@ -30,6 +30,7 @@ import ( |
| "image/png" |
| "io" |
| "log/slog" |
| + "math" |
| "net/http" |
| "os" |
| "os/exec" |
| @@ -1497,13 +1498,13 @@ func TestFontALateEditorMovesNothing(t *testing.T) { |
| var before, after snap |
| var a, b []byte |
| fr.rig.Run(chromedp.Sleep(200*time.Millisecond), chromedp.Evaluate(snapJS, &before), |
| - chromedp.Screenshot(`[slides-stage] [slides-slide]`, &a, chromedp.ByQuery)) |
| + inViewShot(`[slides-stage] [slides-slide]`, &a)) |
| if before.Mounted || !before.Shown { |
| t.Fatalf("deck %d: shown %v, mounted %v before the late editor arrived; the delay did not apply", i, before.Shown, before.Mounted) |
| } |
| fr.rig.Run(chromedp.Poll(`!!document.querySelector('[slides-stage] [contenteditable="true"]')`, nil, chromedp.WithPollingTimeout(15*time.Second)), |
| chromedp.Sleep(500*time.Millisecond), chromedp.Evaluate(snapJS, &after), |
| - chromedp.Screenshot(`[slides-stage] [slides-slide]`, &b, chromedp.ByQuery)) |
| + inViewShot(`[slides-stage] [slides-slide]`, &b)) |
| for k := range before.Markup { |
| if k < len(after.Markup) && before.Markup[k] != after.Markup[k] { |
| t.Errorf("deck %d: text %d was\n %s\nand the editor drew\n %s", i, k, before.Markup[k], after.Markup[k]) |
| @@ -1527,6 +1528,32 @@ func TestFontALateEditorMovesNothing(t *testing.T) { |
| } |
| } |
| +// inViewShot captures the first element sel matches, which must be on |
| +// screen, as a PNG. chromedp.Screenshot captures with |
| +// captureBeyondViewport, and for that capture Chromium lays the page |
| +// out at another size for a frame (1×1 CSS px, seen by the font probe's |
| +// frame sampler as the slide jumping to 80px wide and back); a capture |
| +// of what is already in the viewport needs no relayout, so the page the |
| +// probe watches is the page as shown. The clip is rounded as chromedp |
| +// rounds it. |
| +func inViewShot(sel string, buf *[]byte) chromedp.Action { |
| + return chromedp.ActionFunc(func(ctx context.Context) error { |
| + var r struct{ X, Y, Width, Height float64 } |
| + if err := chromedp.Evaluate(`(r => ({ X: r.left, Y: r.top, Width: r.width, Height: r.height }))(document.querySelector(`+"`"+sel+"`"+`).getBoundingClientRect())`, &r).Do(ctx); err != nil { |
| + return err |
| + } |
| + x, y := math.Round(r.X), math.Round(r.Y) |
| + clip := &page.Viewport{X: x, Y: y, Width: math.Round(r.Width + r.X - x), Height: math.Round(r.Height + r.Y - y), Scale: 1} |
| + pic, err := page.CaptureScreenshot().WithFormat(page.CaptureScreenshotFormatPng). |
| + WithCaptureBeyondViewport(false).WithFromSurface(true).WithClip(clip).Do(ctx) |
| + if err != nil { |
| + return err |
| + } |
| + *buf = pic |
| + return nil |
| + }) |
| +} |
| + |
| // pixelDiff is "" when two PNG captures are the same picture, and the |
| // changed box otherwise. |
| func pixelDiff(t *testing.T, a, b []byte) string { |