-
Notifications
You must be signed in to change notification settings - Fork 1.6k
fix(desktop): stop screen recording drops when frontmost is helper app (#6640) #6644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+362
−8
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
desktop/Desktop/Tests/ScreenCaptureResolverCacheTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import CoreGraphics | ||
| import XCTest | ||
|
|
||
| @testable import Omi_Computer | ||
|
|
||
| /// Tests for the "last known good captureable window" fallback policy in | ||
| /// `ScreenCaptureService.getActiveWindowInfoAsync()`. Issue #6640: a helper app | ||
| /// without a captureable window (LogiPluginService, Dock, UserNotificationCenter) | ||
| /// used to poison the cache with a nil-windowID snapshot, silently breaking | ||
| /// screen capture for up to `activeWindowCacheTTL` seconds per transition. | ||
| final class ScreenCaptureResolverCacheTests: XCTestCase { | ||
|
|
||
| override func setUp() { | ||
| super.setUp() | ||
| ScreenCaptureService._resetActiveWindowCacheForTests() | ||
| ScreenCaptureService._resolverOverrideForTests = nil | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| ScreenCaptureService._resetActiveWindowCacheForTests() | ||
| ScreenCaptureService._resolverOverrideForTests = nil | ||
| super.tearDown() | ||
| } | ||
|
|
||
| // MARK: - Happy path | ||
|
|
||
| func testSuccessfulResolutionOverwritesCache() async { | ||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "Safari", windowTitle: "GitHub", windowID: CGWindowID(42)) | ||
| } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| XCTAssertEqual(result.appName, "Safari") | ||
| XCTAssertEqual(result.windowTitle, "GitHub") | ||
| XCTAssertEqual(result.windowID, CGWindowID(42)) | ||
|
|
||
| let cached = ScreenCaptureService._peekActiveWindowCacheForTests() | ||
| XCTAssertEqual(cached?.appName, "Safari") | ||
| XCTAssertEqual(cached?.windowID, CGWindowID(42)) | ||
| } | ||
|
|
||
| // MARK: - The bug: nil-window poisoning | ||
|
|
||
| func testNilWindowIDResolutionDoesNotPoisonFreshCache() async { | ||
| // Seed a fresh good snapshot (as if capture was working moments ago). | ||
| ScreenCaptureService._seedActiveWindowCacheForTests( | ||
| appName: "Safari", | ||
| windowTitle: "GitHub", | ||
| windowID: CGWindowID(42), | ||
| resolvedAt: Date() | ||
| ) | ||
| let seededSnapshot = ScreenCaptureService._peekActiveWindowCacheForTests() | ||
|
|
||
| // Simulate frontmost switching to a helper app with no captureable window. | ||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "LogiPluginService", windowTitle: nil, windowID: nil) | ||
| } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| // Caller sees the last known good window, not nil. | ||
| XCTAssertEqual(result.windowID, CGWindowID(42)) | ||
| XCTAssertEqual(result.appName, "Safari") | ||
| XCTAssertEqual(result.windowTitle, "GitHub") | ||
|
|
||
| // Cache is unchanged: still the Safari snapshot, same timestamp. | ||
| let cached = ScreenCaptureService._peekActiveWindowCacheForTests() | ||
| XCTAssertEqual(cached, seededSnapshot) | ||
| } | ||
|
|
||
| func testNilWindowIDResolutionWithNoCacheReturnsResolvedAsIs() async { | ||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "LogiPluginService", windowTitle: nil, windowID: nil) | ||
| } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| XCTAssertEqual(result.appName, "LogiPluginService") | ||
| XCTAssertNil(result.windowID) | ||
| XCTAssertNil(result.windowTitle) | ||
| XCTAssertNil( | ||
| ScreenCaptureService._peekActiveWindowCacheForTests(), | ||
| "nil-windowID result must not be written to the cache" | ||
| ) | ||
| } | ||
|
|
||
| func testExpiredCacheIsNotReusedForNilWindowResolution() async { | ||
| // Seed an expired snapshot (>activeWindowCacheTTL in the past). The service | ||
| // uses 2s TTL, so 10s ago is safely expired. | ||
| ScreenCaptureService._seedActiveWindowCacheForTests( | ||
| appName: "Safari", | ||
| windowTitle: "GitHub", | ||
| windowID: CGWindowID(42), | ||
| resolvedAt: Date().addingTimeInterval(-10) | ||
| ) | ||
|
|
||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "LogiPluginService", windowTitle: nil, windowID: nil) | ||
| } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| // Expired cache must not be used; resolver's raw nil-window result is returned. | ||
| XCTAssertEqual(result.appName, "LogiPluginService") | ||
| XCTAssertNil(result.windowID) | ||
| } | ||
|
|
||
| // MARK: - Timeout path | ||
|
|
||
| func testTimeoutPathWithFreshCacheStillFallsBack() async { | ||
| ScreenCaptureService._seedActiveWindowCacheForTests( | ||
| appName: "Safari", | ||
| windowTitle: "GitHub", | ||
| windowID: CGWindowID(42), | ||
| resolvedAt: Date() | ||
| ) | ||
|
|
||
| // Simulate the resolver hitting the timeout race (returning nil). | ||
| ScreenCaptureService._resolverOverrideForTests = { nil } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| XCTAssertEqual(result.windowID, CGWindowID(42)) | ||
| XCTAssertEqual(result.appName, "Safari") | ||
| } | ||
|
|
||
| func testTimeoutPathWithNoCacheReturnsAllNil() async { | ||
| ScreenCaptureService._resolverOverrideForTests = { nil } | ||
|
|
||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| XCTAssertNil(result.appName) | ||
| XCTAssertNil(result.windowTitle) | ||
| XCTAssertNil(result.windowID) | ||
| } | ||
|
|
||
| // MARK: - Recovery | ||
|
|
||
| func testSuccessfulResolutionAfterFallbackOverwritesCache() async { | ||
| ScreenCaptureService._seedActiveWindowCacheForTests( | ||
| appName: "Safari", | ||
| windowTitle: "GitHub", | ||
| windowID: CGWindowID(42), | ||
| resolvedAt: Date() | ||
| ) | ||
|
|
||
| // First: helper app transition -> fallback to Safari. | ||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "LogiPluginService", windowTitle: nil, windowID: nil) | ||
| } | ||
| _ = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| // Then: user refocuses a real window. | ||
| ScreenCaptureService._resolverOverrideForTests = { | ||
| (appName: "Xcode", windowTitle: "Project.swift", windowID: CGWindowID(99)) | ||
| } | ||
| let result = await ScreenCaptureService.getActiveWindowInfoAsync() | ||
|
|
||
| XCTAssertEqual(result.windowID, CGWindowID(99)) | ||
| XCTAssertEqual(result.appName, "Xcode") | ||
|
|
||
| let cached = ScreenCaptureService._peekActiveWindowCacheForTests() | ||
| XCTAssertEqual(cached?.windowID, CGWindowID(99)) | ||
| XCTAssertEqual(cached?.appName, "Xcode") | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isInNilWindowFallbackStreaknot reset on the expired-cache / no-cache pathWhen the resolver returns a nil-windowID result and the cache is either expired or absent, execution falls through to
return resolved/return (nil,nil,nil)without ever resettingisInNilWindowFallbackStreak. If a prior call left the flagtrue(e.g. a successful fallback streak while the cache was still fresh), and the cache then expires while the helper app is still frontmost, the very first log for the next fresh-cache fallback streak will be suppressed — the rate-limiter already thinks it's mid-streak.This is a cosmetic logging issue only (not a correctness bug), but it's worth resetting the flag when no fallback is actually taken so the streak tracks only active fallback windows: