import XCTest import ApplicationServices import CoreGraphics @testable import EPODWindowBar // WindowFilterTests checks the window filter of WindowEnumerator.buildWindows // (spec section 7.3). The test uses the Teams case from the dump: a real window // shows, and a ghost placeholder with the application name as the title hides. // A window on another Space with a distinct title also shows. final class WindowFilterTests: XCTestCase { private let enumerator = WindowEnumerator() private let pid: pid_t = 1234 private let appName = "Microsoft Teams" private func axWindow(title: String) -> WindowEnumerator.AXRawWindow { // The element is a placeholder. buildWindows does not read the element. let element = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier) return WindowEnumerator.AXRawWindow(element: element, title: title, isMinimized: false, subrole: kAXStandardWindowSubrole as String, isFullScreen: false) } private func cgWindow(id: CGWindowID, title: String?, bounds: CGRect) -> WindowEnumerator.CGRawWindow { return WindowEnumerator.CGRawWindow(windowID: id, title: title, layer: 0, bounds: bounds, isOnscreen: true, alpha: 1.0) } private func shownIDs(ax: [WindowEnumerator.AXRawWindow], axIDs: [CGWindowID?], cg: [WindowEnumerator.CGRawWindow], screenBoundsCG: CGRect = CGRect(x: 0, y: 0, width: 2560, height: 1440)) -> Set { let result = enumerator.buildWindows(pid: pid, bundleID: "com.microsoft.teams2", appName: appName, ax: ax, axWindowIDs: axIDs, cg: cg, spaceIDsByWindow: [:], currentSpaceIDs: [], ownPID: 9999, screenBoundsCG: screenBoundsCG, now: Date()) return Set(result.windows.map { $0.id }) } func testShowsRealAXWindow() { let real = axWindow(title: "Chat | Team") let cg = [cgWindow(id: 7476, title: "Chat | Team", bounds: CGRect(x: 215, y: 249, width: 1307, height: 865))] let shown = shownIDs(ax: [real], axIDs: [7476], cg: cg) XCTAssertTrue(shown.contains(7476)) } func testHidesGhostWithAppNameTitle() { // Ghost: no AX element, title equals the application name. let cg = [cgWindow(id: 8274, title: "Microsoft Teams", bounds: CGRect(x: 0, y: 0, width: 800, height: 600))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(8274)) } func testHidesEmptyTitleGhost() { // No AX element, empty CG title. The title falls back to the app name. let cg = [cgWindow(id: 7475, title: nil, bounds: CGRect(x: 0, y: 0, width: 2560, height: 300))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(7475)) } func testShowsOtherSpaceWindowWithDistinctTitle() { // No AX element, distinct title: a real window on another Space. let cg = [cgWindow(id: 999, title: "Terminal — ~/project", bounds: CGRect(x: 0, y: 0, width: 1268, height: 1319))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertTrue(shown.contains(999)) } func testHidesOffScreenLimboWindow() { // The Eclipse limbo shell sits at y=10000, off every screen. let cg = [cgWindow(id: 9231, title: "PartRenderingEngine's limbo", bounds: CGRect(x: 0, y: 10000, width: 1600, height: 900))] let shown = shownIDs(ax: [], axIDs: [], cg: cg) XCTAssertFalse(shown.contains(9231)) } func testKeepsMinimizedOffScreenWindow() { // A minimized window keeps its tab, even with off-screen bounds. let element = AXUIElementCreateApplication(ProcessInfo.processInfo.processIdentifier) let ax = [WindowEnumerator.AXRawWindow(element: element, title: "Minimized Chat", isMinimized: true, subrole: kAXStandardWindowSubrole as String, isFullScreen: false)] let cg = [cgWindow(id: 555, title: "Minimized Chat", bounds: CGRect(x: 0, y: 10000, width: 1200, height: 800))] let shown = shownIDs(ax: ax, axIDs: [555], cg: cg) XCTAssertTrue(shown.contains(555)) } // The stale buffer window case: a no-AX window that claims the current // Space but is not on screen. Some Java GUI toolkits leave these windows. func testHidesStaleCurrentSpaceOffscreenWindow() { let cg = [WindowEnumerator.CGRawWindow(windowID: 500, title: "Report 1 - Overview", layer: 0, bounds: CGRect(x: 0, y: 0, width: 1624, height: 1247), isOnscreen: false, alpha: 1.0)] let result = enumerator.buildWindows(pid: pid, bundleID: "com.example.javaapp", appName: "JavaApp", ax: [], axWindowIDs: [], cg: cg, spaceIDsByWindow: [500: [4]], currentSpaceIDs: [4], ownPID: 9999, screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), now: Date()) XCTAssertFalse(result.windows.contains { $0.id == 500 }) } // A no-AX window on the current Space that IS on screen is a real window. func testKeepsOnscreenCurrentSpaceNoAXWindow() { let cg = [WindowEnumerator.CGRawWindow(windowID: 501, title: "Report 2 - Details", layer: 0, bounds: CGRect(x: 0, y: 0, width: 1624, height: 1247), isOnscreen: true, alpha: 1.0)] let result = enumerator.buildWindows(pid: pid, bundleID: "com.example.javaapp", appName: "JavaApp", ax: [], axWindowIDs: [], cg: cg, spaceIDsByWindow: [501: [4]], currentSpaceIDs: [4], ownPID: 9999, screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), now: Date()) XCTAssertTrue(result.windows.contains { $0.id == 501 }) } // A no-AX window with no Space id is on another Space. The rule keeps it, // even when it is not on screen (decision D4). func testKeepsOtherSpaceOffscreenNoAXWindow() { let cg = [WindowEnumerator.CGRawWindow(windowID: 502, title: "Document 3", layer: 0, bounds: CGRect(x: 0, y: 0, width: 1200, height: 800), isOnscreen: false, alpha: 1.0)] let result = enumerator.buildWindows(pid: pid, bundleID: "com.example.app", appName: "ExampleApp", ax: [], axWindowIDs: [], cg: cg, spaceIDsByWindow: [:], currentSpaceIDs: [4], ownPID: 9999, screenBoundsCG: CGRect(x: 0, y: 0, width: 2560, height: 1440), now: Date()) XCTAssertTrue(result.windows.contains { $0.id == 502 }) } }