Repository navigation
Make shortcut routing focus tests deterministic #6419
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c1d1885
Make shortcut routing focus tests deterministic
azooz2003-bit a452990
Merge remote-tracking branch 'origin/main' into task-shortcut-routing…
azooz2003-bit 6104b31
Scope shortcut routing focus test hook
azooz2003-bit 2163bbd
Restore shortcut repair key delivery coverage
azooz2003-bit fd3169c
Fix shortcut repair probe key comparison
azooz2003-bit 46cd45b
Fix omnibar responder attachment in shortcut tests
azooz2003-bit de4a93a
Preserve deterministic shortcut repair coverage
azooz2003-bit b2010f3
Simplify shortcut routing test swizzle seam
azooz2003-bit 57e75bf
Fix remaining omnibar test responder attachment
azooz2003-bit 4696252
Fix shortcut routing test responder attachments
azooz2003-bit 1159deb
Merge remote-tracking branch 'origin/main' into task-shortcut-routing…
azooz2003-bit 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #if DEBUG | ||
| import AppKit | ||
| import ObjectiveC.runtime | ||
|
|
||
| final class DebugShortcutRoutingFocusedWindowOverrideForTesting { | ||
| weak var window: NSWindow? | ||
| weak var keyRepairFirstResponder: NSResponder? | ||
| var focusedWindowCaptureDepth = 0 | ||
|
|
||
| var shouldCaptureFocusedWindow: Bool { | ||
| focusedWindowCaptureDepth > 0 | ||
| } | ||
| } | ||
|
|
||
| let debugShortcutRoutingFocusedWindowOverrideForTesting = DebugShortcutRoutingFocusedWindowOverrideForTesting() | ||
|
|
||
| private let didInstallShortcutRoutingWindowMakeKeyAndOrderFrontSwizzleForTesting: Void = { | ||
| let targetClass: AnyClass = NSWindow.self | ||
| let originalSelector = #selector(NSWindow.makeKeyAndOrderFront(_:)) | ||
| let swizzledSelector = #selector(NSWindow.cmux_makeKeyAndOrderFront(_:)) | ||
| guard let originalMethod = class_getInstanceMethod(targetClass, originalSelector), | ||
| let swizzledMethod = class_getInstanceMethod(targetClass, swizzledSelector) else { | ||
| return | ||
| } | ||
| method_exchangeImplementations(originalMethod, swizzledMethod) | ||
| }() | ||
|
|
||
| extension AppDelegate { | ||
| func debugResetShortcutRoutingStateForTesting(clearFocusedWindowOverride: Bool = true) { | ||
| clearConfiguredShortcutChordState() | ||
| shortcutEventFocusContextCache = nil | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.keyRepairFirstResponder = nil | ||
| debugFocusedTerminalKeyRepairObserverForTesting = nil | ||
| if clearFocusedWindowOverride { | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.window = nil | ||
| } | ||
| } | ||
|
|
||
| func debugSetShortcutRoutingFocusedWindowForTesting(_ window: NSWindow?) { | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.window = window | ||
| shortcutEventFocusContextCache = nil | ||
| } | ||
|
|
||
| func debugSetShortcutRoutingKeyRepairFirstResponderForTesting(_ responder: NSResponder?) { | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.keyRepairFirstResponder = responder | ||
| } | ||
|
|
||
| func debugBeginShortcutRoutingFocusedWindowCaptureForTesting() { | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.focusedWindowCaptureDepth += 1 | ||
| debugResetShortcutRoutingStateForTesting() | ||
| } | ||
|
|
||
| func debugEndShortcutRoutingFocusedWindowCaptureForTesting() { | ||
| let override = debugShortcutRoutingFocusedWindowOverrideForTesting | ||
| override.focusedWindowCaptureDepth = max(override.focusedWindowCaptureDepth - 1, 0) | ||
| debugResetShortcutRoutingStateForTesting() | ||
| } | ||
|
|
||
| static func installShortcutRoutingFocusedWindowSwizzleForTesting() { | ||
| _ = didInstallShortcutRoutingWindowMakeKeyAndOrderFrontSwizzleForTesting | ||
| } | ||
| } | ||
|
|
||
| extension NSWindow { | ||
| @objc func cmux_makeKeyAndOrderFront(_ sender: Any?) { | ||
| cmux_makeKeyAndOrderFront(sender) | ||
| guard debugShortcutRoutingFocusedWindowOverrideForTesting.shouldCaptureFocusedWindow else { | ||
| return | ||
| } | ||
| AppDelegate.shared?.debugSetShortcutRoutingFocusedWindowForTesting(self) | ||
| } | ||
| } | ||
| #endif |
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,80 @@ | ||
| import AppKit | ||
|
|
||
| extension AppDelegate { | ||
| var shortcutRoutingKeyWindow: NSWindow? { | ||
| #if DEBUG | ||
| if let window = debugShortcutRoutingFocusedWindowOverrideForTesting.window { | ||
| if debugShortcutRoutingFocusedWindowOverrideForTesting.shouldCaptureFocusedWindow { | ||
| return window | ||
| } | ||
| if contextForMainWindow(window) != nil | ||
| || isMainTerminalWindow(window) | ||
| || cmuxWindowShouldOwnCloseShortcut(window) { | ||
| return window | ||
| } | ||
| debugShortcutRoutingFocusedWindowOverrideForTesting.window = nil | ||
| } | ||
| #endif | ||
| return NSApp.keyWindow | ||
| } | ||
|
|
||
| var shortcutRoutingActiveWindow: NSWindow? { | ||
| shortcutRoutingKeyWindow ?? NSApp.mainWindow | ||
| } | ||
|
|
||
| func shortcutRoutingFirstResponder(preferredWindow: NSWindow? = nil) -> NSResponder? { | ||
| preferredWindow?.firstResponder | ||
| ?? shortcutRoutingKeyWindow?.firstResponder | ||
| ?? NSApp.mainWindow?.firstResponder | ||
| } | ||
|
|
||
| func contextForMainWindow(_ window: NSWindow?) -> MainWindowContext? { | ||
| guard let window else { return nil } | ||
| return contextForMainTerminalWindow(window) | ||
| } | ||
|
|
||
| func activeTabManagerForCommands(preferredWindow: NSWindow? = nil) -> TabManager? { | ||
| if let context = contextForMainWindow(preferredWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let context = contextForMainWindow(shortcutRoutingKeyWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let context = contextForMainWindow(NSApp.mainWindow) { | ||
| return context.tabManager | ||
| } | ||
| if let activeManager = tabManager, | ||
| let activeContext = liveMainWindowContext(for: activeManager) { | ||
| return activeContext.tabManager | ||
| } | ||
| return mainWindowContexts.values.first { context in | ||
| resolvedWindow(for: context) != nil | ||
| }?.tabManager | ||
| } | ||
|
|
||
| func repairFocusedTerminalKeyboardRoutingIfNeeded( | ||
| window: NSWindow, | ||
| event: NSEvent | ||
| ) { | ||
| let firstResponderOverride: NSResponder? | ||
| #if DEBUG | ||
| firstResponderOverride = debugShortcutRoutingFocusedWindowOverrideForTesting.keyRepairFirstResponder | ||
| #else | ||
| firstResponderOverride = nil | ||
| #endif | ||
| repairFocusedTerminalKeyboardRoutingIfNeeded( | ||
| window: window, | ||
| event: event, | ||
| firstResponderOverride: firstResponderOverride | ||
| ) | ||
| } | ||
|
|
||
| private func liveMainWindowContext(for tabManager: TabManager) -> MainWindowContext? { | ||
| for context in Array(mainWindowContexts.values) where context.tabManager === tabManager { | ||
| if resolvedWindow(for: context) != nil { | ||
| return context | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Snapshot
mainWindowContexts.valuesbefore iterating withresolvedWindow(for:).Line 70 correctly uses
Array(mainWindowContexts.values)before callingresolvedWindow(for:), but the fallback at lines 47-49 iterates the dictionary directly. SinceresolvedWindow(for:)may reindex/mutatemainWindowContexts, this can cause mutation-during-enumeration issues.🔧 Suggested fix
Based on learnings: "avoid iterating
mainWindowContexts.valuesdirectly while callingresolvedWindow(for:). SinceresolvedWindow(for:)may reindex/mutatemainWindowContexts, this can cause mutation-during-enumeration issues. Instead, snapshot first withArray(mainWindowContexts.values)".🤖 Prompt for AI Agents
Source: Learnings