Skip to content

macOS: nsgo test screenshots (cacheDisplayInRect:) leave out shadows, per-corner radii, antialiasing, animations, backdrops and the caret #311

Closed

Description

@houko

Context

Checked at aadbe7e; main at 6160fe6 has the same internal/platform/macos/testsurface.go and internal/platform/apple/decor.go.

On the macOS target, testSurface.Screenshot draws the surface's view with bitmapImageRepForCachingDisplayInRect: and cacheDisplayInRect:toBitmapImageRep: (internal/platform/macos/testsurface.go:434-456), and docs/architecture/testing.md:47 lists cacheDisplayInRect: as the macOS screenshot. That AppKit path redraws the view hierarchy into a bitmap; it does not capture what Core Animation composites for the window. Much of what NSGo's Apple backend draws lives only in the layer tree, so the image differs from the window in ways a test cannot tell from a real regression. A probe test on macOS showed:

  • Shadows are missing entirely. layoutShadows gives each shadow a CALayer with shadowColor, shadowOpacity, shadowRadius and shadowOffset (internal/platform/apple/decor.go:489, :531-536). None of them show, including a spread shadow with zero blur, so halos and focus rings drawn as shadows vanish too.
  • Per-corner radii ignore maskedCorners. setCorners sets one cornerRadius and limits it with maskedCorners (internal/platform/apple/decor.go:330-340). In the image every corner is rounded, so a list whose first row rounds only its top corners and its last row only its bottom ones looks like separate cards.
  • Corners are not antialiased. 6-8 pt circles come out as rounded squares and 36 pt circles have jagged edges.
  • Core Animation keyframes are not captured. A pulse or a looping animation shows its model (resting) value, not the frame on screen.
  • Layer filters draw differently. A glow made with CALayer.filters from layoutFilters (internal/platform/apple/decor.go:801-835) came out hard-edged.
  • A backdrop blur draws as flat grey, not the blurred content behind it (the NSVisualEffectView from setBackdrop, internal/platform/macos/material.go:153).
  • No caret in a focused field.

#184 is about target tests checking the model instead of the backend; #119 gave tests the image (App.CapturePNG / App.Capture on main, nsgotest/target.go:440-461). This issue is about the macOS image not being what the window shows, which matters more now that tests can compare its pixels.

Why it matters

Screenshot galleries are how an app checks its look across themes on a real backend: shadows, rounded groups, status dots, focus halos, frosted bars and pulsing indicators are exactly the parts a model check cannot see. With the current capture they are absent or wrong in every macOS screenshot, so a gallery cannot verify them, and an app that compares against design mockups chases differences that are not on screen (or rewrites a shadow as a bitmap just to make it show up in the test). The other targets do not have the same gap to the same degree: the web uses Page.captureScreenshot of the composited page.

Proposal

Capture the composited window rather than redraw the view:

  • Prefer a window capture of the test app's own window: CGWindowListCreateImage with the window's number and kCGWindowImageBoundsIgnoreFraming | kCGWindowImageBestResolution (deprecated since macOS 14 but still callable), or ScreenCaptureKit's SCScreenshotManager on macOS 14 and later. Either one sees shadows, masks, filters, backdrops, the presentation layer of running animations and the caret. Crop to the surface's view frame so the image keeps today's bounds.
  • If a capture method needs the Screen Recording permission on the test machine, fall back to cacheDisplayInRect: without it and say so once in the test output, so a run never silently compares the wrong pixels.
  • Until then, list in docs/architecture/testing.md what the macOS screenshot leaves out.

Acceptance

  • On nsgo test -target macos, a view with a BoxShadow (blurred, and spread with zero blur), a view rounded on two corners only, a 6 pt round dot, and a focused input show the shadow, the two square corners, an antialiased dot and the caret in Capture.
  • A view under a looping keyframe animation is captured at its on-screen frame.
  • A backdrop-blurred view shows the content behind it.
  • testing.md says how the macOS screenshot is taken and what, if anything, it still leaves out.

Activity

  1. houko commented on Nov 15, 2026

    @houko
    CollaboratorAuthor

    One more difference for the list: the PNG carries the window's colour space, not sRGB. Screenshot takes bitmapImageRepForCachingDisplayInRect: and writes it with representationUsingType:properties: (internal/platform/macos/testsurface.go:439-450 at aadbe7e), so on a Mac with a P3 display the file is tagged Display P3 (sips -g profile prints profile: Display P3) and its pixel values are P3 values. A colour the app sets as sRGB #2B7766 reads back as about #417567.

    That makes screenshots differ by the machine that took them, and any comparison that reads raw pixel values (golden images, colour checks, diffing against a reference rendered elsewhere) sees colours that look desaturated even though the app draws them correctly. Converting the bitmap to sRGB before encoding (bitmapImageRepByConvertingToColorSpace:renderingIntent: with NSColorSpace.sRGBColorSpace), or rendering into an sRGB bitmap in the first place, would make screenshots reproducible across displays.

  2. houko commented on Nov 16, 2026

    @houko
    CollaboratorAuthor

    Another difference in these screenshots: a fill drawn with the layer's cornerRadius (internal/platform/apple/decor.go:406, setCorners) comes out with a visibly smaller radius than set, while a stroke with the same radius drawn as a CAShapeLayer path (decor.go:733, strokeShape) comes out right.

    A 28pt-high filled button with corner radius 7 measures about 4.5pt of rounding in the screenshot, without antialiasing; an outlined control next to it with the same radius shows the full 7pt, antialiased. Where a control has both a fill and a stroke, the fill's corners poke out past the stroke. In a real window Core Animation draws both at 7pt, so this is the capture path again, not the app.

  3. houko commented on Nov 16, 2026

    @houko
    CollaboratorAuthor

    Another case this blocks: Kando (github.com/yldm-tech/kando) has a library test that should check the hero card's shadow spreads past its sides by reading the captured pixels on -target macos. With App.Capture() the pixels around any view with Style.Shadows are the plain page colour; a minimal check (a white page, a dark 200x80 view with Shadows: {Offset Y 12, Blur 32, RGBA(0, 0.5)}) shows no shadow at all. The test stays a layout check until captures include layer shadows.

  4. self-assigned this
    on Nov 20, 2026
  5. houko commented on Nov 20, 2026

    @houko
    CollaboratorAuthor

    Implemented in PR #1005.

    The macOS screenshot path now converts both WindowServer composites and the AppKit fallback bitmap to sRGB before PNG encoding, keeping pixel comparisons stable on Display P3 machines. Existing composited capture still preserves Core Animation effects when WindowServer provides the image, and the fallback remains logged once when the window is unavailable.

    Validation:

    • go tool nsgo check -target macos -jobs 1 passed for macOS arm64 and x86_64.
    • go tool nsgo test -target macos -run '^TestMountIgnoresCommandsForMissingViews$' ./internal/platform/macos passed.
    • The composited capture fixture was attempted. This environment returned no WindowServer image and used the documented fallback, so its shadow/filter assertions could not run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions