Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ jobs:
- name: Install xcpretty
run: gem install xcpretty --no-document

- name: Swift Package Manager - Build
run: swift build

- name: Swift Package Manager - Test
run: swift test

Expand All @@ -48,7 +45,7 @@ jobs:
-scheme Annotate \
-configuration Debug \
-destination "platform=macOS" \
clean build | xcpretty && exit ${PIPESTATUS[0]}
build | xcpretty && exit ${PIPESTATUS[0]}

- name: Xcode - Test
run: |
Expand Down
2 changes: 2 additions & 0 deletions Annotate/CursorHighlightWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class CursorHighlightWindow: NSPanel {
hasShadow = false
ignoresMouseEvents = true
isRestorable = false
// Swift owns the reference, so closing must not also release the window.
isReleasedWhenClosed = false
hidesOnDeactivate = false // Stay visible even when app is hidden
collectionBehavior = [.canJoinAllSpaces, .transient, .ignoresCycle]

Expand Down
4 changes: 4 additions & 0 deletions Annotate/OverlayWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ class OverlayWindow: NSPanel {
}

override func close() {
// The fade timer retains the window, and the observer would keep re-measuring a toolbar
// nobody sees, so a closed overlay has to drop both or it lives for the rest of the process.
stopFadeLoop()
NotificationCenter.default.removeObserver(self, name: .shortcutsDidChange, object: nil)
toolbarPanel?.tearDown()
toolbarPanel = nil
super.close()
Expand Down
2 changes: 2 additions & 0 deletions AnnotateTests/AnnotationTextEditorContrastTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class AnnotationTextEditorContrastTests: XCTestCase, Sendable {
nonisolated override func setUp() {
super.setUp()
MainActor.assumeIsolated {
TestWindows.recordOpenWindows()
AppDelegate.shared = nil
previousAppearance = NSApp.appearance
BoardManager.shared = BoardManager(userDefaults: TestUserDefaults.create())
Expand All @@ -29,6 +30,7 @@ final class AnnotationTextEditorContrastTests: XCTestCase, Sendable {
NSApp.appearance = previousAppearance
BoardManager.shared = BoardManager()
previousAppearance = nil
assertNoLeakedWindows()
}
TestUserDefaults.removeSuite()
super.tearDown()
Expand Down
32 changes: 20 additions & 12 deletions AnnotateTests/AppDelegateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,45 @@ import XCTest
final class AppDelegateTests: XCTestCase, Sendable {
var appDelegate: AppDelegate!
var testDefaults: UserDefaults!
private var launchedDelegates: [AppDelegate] = []

nonisolated override func setUp() {
super.setUp()

MainActor.assumeIsolated {
TestWindows.recordOpenWindows()
testDefaults = TestUserDefaults.create()
BoardManager.shared = BoardManager(userDefaults: testDefaults)
ShortcutManager.shared = ShortcutManager(userDefaults: testDefaults)

appDelegate = AppDelegate(userDefaults: testDefaults)
appDelegate.applicationDidFinishLaunching(
Notification(name: NSApplication.didFinishLaunchingNotification))
appDelegate = launchAppDelegate()
}
}

nonisolated override func tearDown() {
MainActor.assumeIsolated {
SettingsWindowManager.shared.settingsWindow?.close()
appDelegate.overlayKeyWindowOverride = nil
launchedDelegates.forEach { $0.tearDownAfterLaunch() }
launchedDelegates = []
appDelegate = nil
assertNoLeakedWindows()
}
TestUserDefaults.removeSuite()
super.tearDown()
}

/// Every delegate a test launches goes through here so tearDown can dismantle it. Launching
/// opens an overlay and a cursor highlight window per screen, a status item and seven event
/// monitors, none of which go away on their own.
private func launchAppDelegate() -> AppDelegate {
let delegate = AppDelegate(userDefaults: testDefaults)
launchedDelegates.append(delegate)
delegate.applicationDidFinishLaunching(
Notification(name: NSApplication.didFinishLaunchingNotification))
return delegate
}

func testInitialization() {
XCTAssertNotNil(appDelegate.statusItem)
XCTAssertNotNil(appDelegate.statusItem.menu)
Expand Down Expand Up @@ -267,9 +281,7 @@ final class AppDelegateTests: XCTestCase, Sendable {
}

func testToggleFadeModeUpdatesPersistence() {
let appDelegate = AppDelegate(userDefaults: testDefaults)
appDelegate.applicationDidFinishLaunching(
Notification(name: NSApplication.didFinishLaunchingNotification))
let appDelegate = launchAppDelegate()

guard let overlayWindow = appDelegate.overlayWindows.values.first else {
XCTFail("No overlay window found")
Expand Down Expand Up @@ -412,9 +424,7 @@ final class AppDelegateTests: XCTestCase, Sendable {
func testOverlayWindowsRestorePersistedFadeMode() {
testDefaults.set(false, forKey: UserDefaults.fadeModeKey)

let appDelegate = AppDelegate(userDefaults: testDefaults)
appDelegate.applicationDidFinishLaunching(
Notification(name: NSApplication.didFinishLaunchingNotification))
let appDelegate = launchAppDelegate()

// All overlay windows should be initialized with fade mode set to false.
for window in appDelegate.overlayWindows.values {
Expand Down Expand Up @@ -609,9 +619,7 @@ final class AppDelegateTests: XCTestCase, Sendable {
func testLaunchRestoresPersistedLastUsedTool() {
testDefaults.lastUsedTool = .highlighter

let appDelegate = AppDelegate(userDefaults: testDefaults)
appDelegate.applicationDidFinishLaunching(
Notification(name: NSApplication.didFinishLaunchingNotification))
let appDelegate = launchAppDelegate()

for window in appDelegate.overlayWindows.values {
XCTAssertEqual(window.overlayView.currentTool, .highlighter, "Overlay windows should restore the persisted last-used tool on launch")
Expand Down
5 changes: 3 additions & 2 deletions AnnotateTests/BackgroundDimmingShortcutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class BackgroundDimmingShortcutTests: XCTestCase {

override func setUp() {
super.setUp()
TestWindows.recordOpenWindows()
let defaults = TestUserDefaults.create()
originalCursorManager = CursorHighlightManager.shared
originalShortcutManager = ShortcutManager.shared
Expand All @@ -22,12 +23,12 @@ final class BackgroundDimmingShortcutTests: XCTestCase {
}

override func tearDown() {
window.stopFadeLoop()
window.orderOut(nil)
window.close()
window = nil
CursorHighlightManager.shared = originalCursorManager
ShortcutManager.shared = originalShortcutManager
TestUserDefaults.removeSuite()
assertNoLeakedWindows()
super.tearDown()
}

Expand Down
10 changes: 6 additions & 4 deletions AnnotateTests/CursorHighlightManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class CursorHighlightManagerTests: XCTestCase {

override func setUp() {
super.setUp()
TestWindows.recordOpenWindows()
testDefaults = TestUserDefaults.create()
manager = CursorHighlightManager(userDefaults: testDefaults)
AppDelegate.shared = nil
Expand All @@ -18,6 +19,7 @@ final class CursorHighlightManagerTests: XCTestCase {
TestUserDefaults.removeSuite()
manager = nil
AppDelegate.shared = nil
assertNoLeakedWindows()
super.tearDown()
}

Expand Down Expand Up @@ -272,7 +274,7 @@ final class CursorHighlightManagerTests: XCTestCase {
)
appDelegate.overlayWindows[screen] = overlayWindow
AppDelegate.shared = appDelegate
defer { overlayWindow.orderOut(nil) }
defer { overlayWindow.close() }

overlayWindow.orderFront(nil)
try XCTSkipUnless(overlayWindow.isVisible, "Overlay window is not visible in this test environment")
Expand Down Expand Up @@ -740,7 +742,7 @@ final class CursorHighlightManagerTests: XCTestCase {
)
appDelegate.overlayWindows[screen] = overlayWindow
AppDelegate.shared = appDelegate
defer { overlayWindow.orderOut(nil) }
defer { overlayWindow.close() }

overlayWindow.orderFront(nil)
try XCTSkipUnless(overlayWindow.isVisible, "Overlay window is not visible in this test environment")
Expand Down Expand Up @@ -904,7 +906,7 @@ final class CursorHighlightManagerTests: XCTestCase {
)
appDelegate.overlayWindows[screen] = overlayWindow
AppDelegate.shared = appDelegate
defer { overlayWindow.orderOut(nil) }
defer { overlayWindow.close() }

overlayWindow.orderFront(nil)
try XCTSkipUnless(overlayWindow.isVisible, "Overlay window is not visible in this test environment")
Expand Down Expand Up @@ -936,7 +938,7 @@ final class CursorHighlightManagerTests: XCTestCase {
)
appDelegate.overlayWindows[screen] = overlayWindow
AppDelegate.shared = appDelegate
defer { overlayWindow.orderOut(nil) }
defer { overlayWindow.close() }

overlayWindow.orderFront(nil)
try XCTSkipUnless(overlayWindow.isVisible, "Overlay window is not visible in this test environment")
Expand Down
13 changes: 12 additions & 1 deletion AnnotateTests/CursorHighlightWindowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ import XCTest

@MainActor
final class CursorHighlightWindowTests: XCTestCase {
override func setUp() {
super.setUp()
TestWindows.recordOpenWindows()
}

override func tearDown() {
assertNoLeakedWindows()
super.tearDown()
}

func testAnimationLoopOwnsWindowDisplayLinkOnlyWhileRunning() {
let window = CursorHighlightWindow(
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
Expand All @@ -22,6 +32,7 @@ final class CursorHighlightWindowTests: XCTestCase {

window.stopAnimationLoop()
XCTAssertNil(window.animationDisplayLink)
window.close()
}

func testUpdateVisibilityHidesSpotlightAndDimmingImmediatelyWhenClickEffectsKeepWindowAlive() throws {
Expand All @@ -37,7 +48,7 @@ final class CursorHighlightWindowTests: XCTestCase {
)
defer {
window.stopAnimationLoop()
window.orderOut(nil)
window.close()
CursorHighlightManager.shared = originalShared
TestUserDefaults.removeSuite()
}
Expand Down
3 changes: 2 additions & 1 deletion AnnotateTests/KeyboardLayoutShortcutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ final class KeyboardLayoutShortcutTests: XCTestCase {

override func setUp() {
super.setUp()
TestWindows.recordOpenWindows()
let defaults = TestUserDefaults.create()
originalAppDelegate = AppDelegate.shared
originalShortcutManager = ShortcutManager.shared
Expand All @@ -25,13 +26,13 @@ final class KeyboardLayoutShortcutTests: XCTestCase {
}

override func tearDown() {
window.stopFadeLoop()
window.close()
window = nil
AppDelegate.shared = originalAppDelegate
ShortcutManager.shared = originalShortcutManager
appDelegate = nil
TestUserDefaults.removeSuite()
assertNoLeakedWindows()
super.tearDown()
}

Expand Down
3 changes: 3 additions & 0 deletions AnnotateTests/OverlayWindowTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class OverlayWindowTests: XCTestCase, Sendable {
nonisolated override func setUp() {
super.setUp()
MainActor.assumeIsolated {
TestWindows.recordOpenWindows()
// A spy leaked from another suite would redirect pickerUserDefaults.
AppDelegate.shared = nil
originalMouseCoalescingEnabled = NSEvent.isMouseCoalescingEnabled
Expand All @@ -28,8 +29,10 @@ final class OverlayWindowTests: XCTestCase, Sendable {
window.cancelQuickPicker()
window.stopFadeLoop()
window.overlayView.pickerUserDefaultsOverride = nil
window.close()
window = nil
NSEvent.isMouseCoalescingEnabled = originalMouseCoalescingEnabled
assertNoLeakedWindows()
}
TestUserDefaults.removeSuite()
super.tearDown()
Expand Down
2 changes: 2 additions & 0 deletions AnnotateTests/RedactionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ final class RedactionTests: XCTestCase, Sendable {
nonisolated override func setUp() {
super.setUp()
MainActor.assumeIsolated {
TestWindows.recordOpenWindows()
AppDelegate.shared = nil
// Redaction rendering consults the board state; keep it off the user's live defaults.
originalBoardManager = BoardManager.shared
Expand All @@ -77,6 +78,7 @@ final class RedactionTests: XCTestCase, Sendable {
overlayView = nil
sampler = nil
BoardManager.shared = originalBoardManager
assertNoLeakedWindows()
}
TestUserDefaults.removeSuite()
super.tearDown()
Expand Down
3 changes: 3 additions & 0 deletions AnnotateTests/ShiftConstraintTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class ShiftConstraintTests: XCTestCase, Sendable {
nonisolated override func setUp() {
super.setUp()
MainActor.assumeIsolated {
TestWindows.recordOpenWindows()
let frame = NSRect(x: 0, y: 0, width: 800, height: 600)
window = OverlayWindow(
contentRect: frame,
Expand All @@ -21,7 +22,9 @@ final class ShiftConstraintTests: XCTestCase, Sendable {

nonisolated override func tearDown() {
MainActor.assumeIsolated {
window.close()
window = nil
assertNoLeakedWindows()
}
super.tearDown()
}
Expand Down
Loading
Loading