import Foundation import ParcelCore import Testing struct ClassifierDefaultsAcceptanceTests { @Test func defaultCatalogContainsEveryBuiltInAndNoDuplicates() { let expected: Set = [ "com.apple.ScreenContinuity", "com.apple.iphonesimulator", "com.apple.calculator", "com.apple.Stickies", "com.apple.clock", "com.apple.PhotoBooth", "com.apple.QuickTimePlayerX", "com.apple.PIPAgent", "com.apple.Magnifier", "com.apple.DeskCam", "com.apple.DigitalColorMeter", "com.apple.screenshot.launcher", "com.apple.archiveutility", "com.apple.installer", "com.apple.MigrateAssistant", "com.apple.CaptiveNetworkAssistant", "com.apple.ProblemReporter", ] #expect(Set(DefaultFloatApps.all.map(\.bundleID)) == expected) #expect(DefaultFloatApps.all.count == expected.count) #expect(DefaultFloatApps.all.allSatisfy { !$0.name.isEmpty }) } @Test(arguments: DefaultFloatApps.all) func eachBuiltInFloatsAtItsOwnSize(app: DefaultFloatApp) { let facts = standardFacts(bundleID: app.bundleID, appName: "Unrelated app name", title: "Document") let result = WindowClassifier().classify(facts) #expect(result == WindowClassification(disposition: .float, reason: .defaultFloatApp(bundleID: app.bundleID))) #expect(result.display == nil) #expect(result.position == nil) #expect(result.layout == nil) #expect(result.space == nil) } @Test(arguments: ["System Settings", "Ajustes del Sistema", ""]) func systemSettingsMainWindowTilesWithoutAnOverride(title: String) { let facts = standardFacts(bundleID: "com.apple.systempreferences", title: title) #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .tile, reason: .standardWindow)) } @Test func systemSettingsCanTileWhenOnlyItsWidthIsFixed() { var facts = standardFacts(bundleID: "com.apple.systempreferences") facts.minimumSize = Size(width: 668, height: 400) facts.maximumSize = Size(width: 668, height: 2000) #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .tile, reason: .standardWindow)) #expect(WindowClassifier().appProfile(for: facts).fixedSize == nil) } @Test(arguments: [ ClassificationReason.dialog, .sheet, .panel, .nonResizable, .fixedSize, .fullScreen, .minimized, ]) func systemSettingsAuxiliariesRetainTheirWindowSafety(reason: ClassificationReason) { var facts = standardFacts(bundleID: "com.apple.systempreferences") switch reason { case .dialog: facts.isModal = true case .sheet: facts.hasParent = true case .panel: facts.subrole = "AXFloatingWindow" case .nonResizable: facts.isResizable = false case .fixedSize: facts.minimumSize = Size(width: 668, height: 400) facts.maximumSize = facts.minimumSize case .fullScreen: facts.isFullScreen = true case .minimized: facts.isMinimized = true default: Issue.record("Unexpected Settings safety fixture"); return } let disposition: WindowDisposition = reason == .fullScreen || reason == .minimized ? .ignore : .float #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: disposition, reason: reason)) } @Test func anExplicitSystemSettingsFloatRuleStillWins() throws { let bundle = "com.apple.systempreferences" let classifier = WindowClassifier(rules: [try WindowRule(bundleID: bundle, action: .float)]) #expect( classifier.classify(standardFacts(bundleID: bundle)) == WindowClassification(disposition: .float, reason: .rule(index: 0, bundleID: bundle))) } @Test(arguments: DefaultFloatApps.all) func disablingEachBuiltInLetsAStandardWindowTile(app: DefaultFloatApp) { let result = WindowClassifier(disabledDefaultFloatApps: [app.bundleID]).classify( standardFacts(bundleID: app.bundleID)) #expect(result == WindowClassification(disposition: .tile, reason: .standardWindow)) let other = DefaultFloatApps.all.first { $0.bundleID != app.bundleID } if let other { #expect( WindowClassifier(disabledDefaultFloatApps: [app.bundleID]).classify( standardFacts(bundleID: other.bundleID) ).disposition == .float) } } @Test(arguments: DefaultFloatApps.all) func explicitTileRuleOverridesEachDefault(app: DefaultFloatApp) throws { let rule = try WindowRule(bundleID: app.bundleID, action: .tile) let result = WindowClassifier(rules: [rule]).classify(standardFacts(bundleID: app.bundleID)) #expect(result == WindowClassification(disposition: .tile, reason: .rule(index: 0, bundleID: app.bundleID))) } @Test(arguments: DefaultFloatApps.all) func titlesAndNamesDoNotImpersonateAnExcludedBundle(app: DefaultFloatApp) { let facts = standardFacts(bundleID: "test.document-app", appName: app.name, title: app.name) #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .tile, reason: .standardWindow)) } @Test(arguments: [true, false]) func explicitRulesAndRemovedDefaultsCannotMoveFullscreenOrMinimizedWindows(fullscreen: Bool) throws { let bundle = "com.apple.calculator" var facts = standardFacts(bundleID: bundle) facts.isFullScreen = fullscreen facts.isMinimized = !fullscreen let rule = try WindowRule(bundleID: bundle, action: .tile, layout: .rows, space: 2) let classifier = WindowClassifier(rules: [rule], disabledDefaultFloatApps: [bundle]) #expect( classifier.classify(facts) == WindowClassification(disposition: .ignore, reason: fullscreen ? .fullScreen : .minimized)) } @Test func removingAnAppDefaultDoesNotRemoveWindowSafetyHeuristics() { let bundle = "com.apple.calculator" let classifier = WindowClassifier(disabledDefaultFloatApps: [bundle]) var facts = standardFacts(bundleID: bundle) facts.isResizable = false #expect(classifier.classify(facts).reason == .nonResizable) #expect(classifier.classify(facts).disposition == .float) facts.isResizable = true facts.isModal = true #expect(classifier.classify(facts).reason == .dialog) facts.isModal = false facts.hasParent = true #expect(classifier.classify(facts).reason == .sheet) } @Test( arguments: ["AXStandardWindow", "AXFloatingWindow", "AXSystemFloatingWindow"], ["Picture in Picture", "画中画", ""]) func systemPictureInPictureFloatsByIdentityWithoutDependingOnItsTitle(subrole: String, title: String) { let bundle = "com.apple.PIPAgent" var facts = standardFacts(bundleID: bundle, title: title) facts.subrole = subrole facts.activationPolicy = .accessory #expect( WindowClassifier().classify(facts) == WindowClassification(disposition: .float, reason: .defaultFloatApp(bundleID: bundle))) } @Test(arguments: [true, false]) func systemPlayerStillCannotMoveWhileFullscreenOrMinimized(fullscreen: Bool) { var facts = standardFacts(bundleID: "com.apple.PIPAgent") facts.isFullScreen = fullscreen facts.isMinimized = !fullscreen #expect( WindowClassifier().classify(facts) == WindowClassification(disposition: .ignore, reason: fullscreen ? .fullScreen : .minimized)) } @Test(arguments: ["com.apple.AVKit", "com.apple.AVKit.AVPlayer", "com.apple.WebKit.GPU", "example.video-app"]) func videoTitlesDoNotImplyAnUnverifiedPlayerShape(bundle: String) { var facts = standardFacts(bundleID: bundle, title: "Video Player") #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .tile, reason: .standardWindow)) facts.subrole = "AXFloatingWindow" #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .float, reason: .panel)) } @Test(arguments: ["AXFloatingWindow", "AXSystemFloatingWindow"], ["Picture in Picture", "PICTURE-IN-PICTURE"]) func pictureInPictureStyleFloatingPanelsAreLeftUnmanaged(subrole: String, title: String) { var facts = standardFacts(bundleID: "example.host-app", title: title) facts.subrole = subrole #expect( WindowClassifier().classify(facts) == WindowClassification(disposition: .ignore, reason: .pictureInPicture)) } @Test(arguments: [ "Picture in Picture", "Picture-in-Picture", "A Picture in Picture Tutorial", "Picture in Picture — Project", ]) func pictureInPictureDocumentTitlesKeepNormalTiling(title: String) { #expect( WindowClassifier().classify(standardFacts(bundleID: "example.editor", title: title)).reason == .standardWindow) } @Test func pictureInPictureWordsDoNotIgnoreDialogsOrUnknownRoles() { var facts = standardFacts(bundleID: "example.host-app", title: "Picture in Picture") facts.subrole = "AXDialog" #expect(WindowClassifier().classify(facts).reason == .dialog) facts.subrole = "AXFloatingWindow" facts.role = nil #expect(WindowClassifier().classify(facts).reason == .panel) } @Test func nativeTabsRemainTiledOnlyForOtherwiseStandardWindows() { var facts = standardFacts(bundleID: "test.tabbed-app") facts.hasNativeTabs = true #expect(WindowClassifier().classify(facts) == WindowClassification(disposition: .tile, reason: .nativeTab)) facts.hasNativeTabs = false #expect(WindowClassifier().classify(facts).reason == .standardWindow) facts.hasNativeTabs = true facts.isModal = true #expect(WindowClassifier().classify(facts).reason == .dialog) facts.isModal = false facts.isResizable = false #expect(WindowClassifier().classify(facts).reason == .nonResizable) } @Test func assignmentOnlyRulesPreserveDispositionAndLaterAssignmentsWin() throws { let facts = standardFacts(bundleID: "test.rules") let first = try WindowRule(bundleID: facts.bundleID, layout: .columns, space: 1) let second = try WindowRule(bundleID: facts.bundleID, layout: .rows, space: 16) let unmatched = try WindowRule(bundleID: "test.other", layout: .side, space: 2) let result = WindowClassifier(rules: [first, second, unmatched]).classify(facts) #expect(result.disposition == .tile) #expect(result.reason == .standardWindow) #expect(result.layout == .rows) #expect(result.space == 16) let excluded = WindowClassifier(rules: [try WindowRule(bundleID: "com.apple.calculator", layout: .rows)]) .classify(standardFacts(bundleID: "com.apple.calculator")) #expect(excluded.disposition == .float) #expect(excluded.layout == .rows) } @Test func spaceAssignmentRequiresConsentOnTheMatchingSpaceRule() throws { let facts = standardFacts(bundleID: "test.rules") let defaultRule = try WindowRule(bundleID: facts.bundleID, space: 2) #expect(!defaultRule.moveToSpace) #expect(!WindowClassifier(rules: [defaultRule]).classify(facts).moveToSpace) let consenting = try WindowRule(bundleID: facts.bundleID, space: 3, moveToSpace: true) let result = WindowClassifier(rules: [consenting]).classify(facts) #expect(result.space == 3 && result.moveToSpace) let unmatched = try WindowRule(bundleID: "test.other", space: 4, moveToSpace: true) #expect(!WindowClassifier(rules: [defaultRule, unmatched]).classify(facts).moveToSpace) } @Test(arguments: [true, false]) func lastMatchingSpaceRuleOwnsItsConsent(consentingLast: Bool) throws { let facts = standardFacts(bundleID: "test.rules") let consenting = try WindowRule(bundleID: facts.bundleID, space: 3, moveToSpace: true) let nonconsenting = try WindowRule(bundleID: facts.bundleID, space: 2) let rules = consentingLast ? [nonconsenting, consenting] : [consenting, nonconsenting] let result = WindowClassifier(rules: rules).classify(facts) #expect(result.moveToSpace == consentingLast) #expect(result.space == (consentingLast ? 3 : 2)) } @Test func rulesWithoutSpaceCannotGrantOrRevokeSpaceConsent() throws { let facts = standardFacts(bundleID: "test.rules") let consenting = try WindowRule(bundleID: facts.bundleID, space: 3, moveToSpace: true) let actionOnly = try WindowRule(bundleID: facts.bundleID, action: .float) let grantingWithoutSpace = try WindowRule(bundleID: facts.bundleID, layout: .rows, moveToSpace: true) #expect(WindowClassifier(rules: [consenting, actionOnly]).classify(facts).moveToSpace) #expect(!WindowClassifier(rules: [grantingWithoutSpace]).classify(facts).moveToSpace) } @Test func oldClassificationsDoNotAcquireSpaceConsentDuringDecoding() throws { let old = Data(#"{"disposition":"tile","reason":{"standardWindow":{}},"space":3}"#.utf8) let result = try JSONDecoder().decode(WindowClassification.self, from: old) #expect(result.space == 3 && !result.moveToSpace) } @Test(arguments: [0, -1, 17, Int.max]) func invalidNativeSpaceNumbersAreRejected(space: Int) { #expect(throws: WindowRuleError.invalidSpace) { try WindowRule(bundleID: "test.rules", space: space) } } @Test func layoutAndSpaceOutcomesRoundTripInClassification() throws { let value = WindowClassification( disposition: .tile, reason: .standardWindow, layout: .side, space: 3, moveToSpace: true) #expect(try JSONDecoder().decode(WindowClassification.self, from: JSONEncoder().encode(value)) == value) } private func standardFacts(bundleID: String, appName: String = "Document app", title: String = "Document") -> WindowFacts { WindowFacts( bundleID: bundleID, appName: appName, activationPolicy: .regular, role: "AXWindow", subrole: "AXStandardWindow", title: title, isResizable: true, isModal: false, hasParent: false, isFullScreen: false, isMinimized: false, layer: 0, serverSize: Size(width: 640, height: 480)) } }