import AppKit import Foundation import ParcelAnimation import ParcelConfig import Testing @testable import ParcelUI struct MiniMapTests { @Test(arguments: ConfigLayout.allCases, [0, 1, 3, 7]) func accessibilityNamesLayoutAndCount(layout: ConfigLayout, count: Int) { let snapshot = Self.snapshot(count: count, layout: layout) let ending = count == 1 ? "1 window" : "\(count) windows" #expect(snapshot.accessibilityDescription == "Tiling \(ending) ยท \(layout.uiTitle)") } @Test(arguments: [0, 1, 6, 7, 99, 100]) func iconFallback(count: Int) { let snapshot = Self.snapshot(count: count) let icon = MenuIcon(snapshot: snapshot, style: .minimap) switch icon { case .logo(let badge): #expect(count == 0 || count > 6) #expect(badge == (count > 6 ? count : nil)) case .tiles(let tiles): #expect((1...6).contains(count)) #expect(tiles.count == count) } #expect(MenuIcon(snapshot: snapshot, style: .logo) == .logo(count: count > 6 ? count : nil)) } @Test func geometryPreservesAspectOriginAndFocus() throws { let snapshot = LayoutSnapshot( layout: .columns, bounds: CGRect(x: -100, y: 50, width: 200, height: 100), tiles: [ LayoutTile(id: 4, frame: CGRect(x: -100, y: 50, width: 100, height: 100), isFocused: true), LayoutTile(id: 5, frame: CGRect(x: 0, y: 50, width: 100, height: 100)), ]) guard case .tiles(let tiles) = MenuIcon(snapshot: snapshot, style: .minimap) else { Issue.record("Expected tiles") return } #expect(tiles.map(\.id) == [4, 5]) #expect(abs(tiles[0].frame.minX - 2.5) < 1e-10) #expect(abs(tiles[1].frame.minX - 9.5) < 1e-10) for tile in tiles { #expect(abs(tile.frame.minY - 6) < 1e-10) #expect(abs(tile.frame.width - 6) < 1e-10) #expect(abs(tile.frame.height - 6) < 1e-10) } #expect(tiles[0].isFocused) #expect(!tiles[1].isFocused) } @Test func clipsOffscreenGeometry() { let snapshot = LayoutSnapshot( layout: .grid, bounds: CGRect(x: 0, y: 0, width: 100, height: 100), tiles: [ LayoutTile(id: 1, frame: CGRect(x: -50, y: -20, width: 100, height: 100)), LayoutTile(id: 2, frame: CGRect(x: 200, y: 200, width: 10, height: 10)), ]) guard case .tiles(let tiles) = MenuIcon(snapshot: snapshot, style: .minimap) else { Issue.record("Expected tiles") return } #expect(tiles.count == 1) #expect(tiles[0].frame.minX >= 2) #expect(tiles[0].frame.minY >= 2) #expect(tiles[0].frame.maxX <= 16) #expect(tiles[0].frame.maxY <= 16) } @Test(arguments: ConfigMotionLevel.allCases, [0.1, 1.0, 4.0]) func morphHasBoundedFrames(level: ConfigMotionLevel, speed: Double) { let initial = MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap) let destination = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) let changed = morph.retarget( destination, configuration: MotionConfiguration(level: level, speed: speed), reduceMotion: false, visible: true) #expect(changed) #expect(morph.remainingFrames == 10) var frames = 0 while morph.remainingFrames > 0 { morph.advance() frames += 1 } #expect(frames <= 10) #expect(morph.current == destination) #expect(morph.advance() == destination) let unchanged = morph.retarget( destination, configuration: MotionConfiguration(), reduceMotion: false, visible: true) #expect(!unchanged) } @Test(arguments: [30.0, 60.0, 120.0], [0.1, 1.0, 4.0]) func elapsedTimeSamplingDoesNotStretchMotionAcrossRefreshRates(refreshRate: Double, speed: Double) { let initial = MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap) let target = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) morph.retarget(target, configuration: MotionConfiguration(speed: speed), reduceMotion: false, visible: true) let duration = morph.frameInterval * Double(morph.remainingFrames) let ticks = Int(ceil(duration * refreshRate)) var changes = 0 for tick in 0...ticks { let elapsed = Double(tick) / refreshRate let previous = morph.current morph.sample(at: elapsed) if morph.current != previous { changes += 1 } let sampled = morph.current #expect(morph.sample(at: elapsed) == sampled) #expect(morph.sample(at: max(0, elapsed - 1 / refreshRate)) == sampled) } #expect(morph.current == target) #expect(morph.remainingFrames == 0) #expect(changes <= 10) } @Test func delayedRefreshCatchesUpAndRetargetingResetsTheSampleOrigin() { let initial = MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap) let target = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) morph.retarget(target, configuration: MotionConfiguration(), reduceMotion: false, visible: true) var expected = morph for _ in 0..<4 { expected.advance() } #expect(morph.sample(at: morph.frameInterval * 4.5) == expected.current) let presentation = morph.current morph.retarget(initial, configuration: MotionConfiguration(), reduceMotion: false, visible: true) #expect(morph.sample(at: 0) == presentation) #expect(morph.sample(at: 100) == initial) #expect(morph.remainingFrames == 0) } @Test(arguments: 0..<8) func policySkipsMotion(flags: Int) { let enabled = flags & 1 != 0 let reduceMotion = flags & 2 != 0 let visible = flags & 4 != 0 let initial = MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap) let destination = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) morph.retarget( destination, configuration: MotionConfiguration(enabled: enabled), reduceMotion: reduceMotion, visible: visible) #expect((morph.remainingFrames > 0) == (enabled && !reduceMotion && visible)) if morph.remainingFrames == 0 { #expect(morph.current == destination) } } @Test func retargetStartsFromPresentationAndSettlesExactly() { let initial = MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap) let destination = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) morph.retarget(destination, configuration: MotionConfiguration(), reduceMotion: false, visible: true) morph.advance() let presentation = morph.current #expect(presentation != initial) #expect(presentation != destination) morph.retarget(initial, configuration: MotionConfiguration(), reduceMotion: false, visible: true) #expect(morph.current == presentation) for _ in 0..<10 { morph.advance() } #expect(morph.current == initial) } @Test func disablingMotionFinishesCurrentMorph() { var morph = MiniMapMorph(icon: MenuIcon(snapshot: Self.snapshot(count: 3), style: .minimap)) let destination = MenuIcon(snapshot: .preview, style: .minimap) morph.retarget(destination, configuration: MotionConfiguration(), reduceMotion: false, visible: true) morph.advance() morph.retarget( destination, configuration: MotionConfiguration(enabled: false), reduceMotion: false, visible: true) #expect(morph.remainingFrames == 0) #expect(morph.current == destination) } @Test func logoTransitionsAreInstant() { var morph = MiniMapMorph(icon: MenuIcon(snapshot: .preview, style: .minimap)) morph.retarget(.logo(count: 7), configuration: MotionConfiguration(), reduceMotion: false, visible: true) #expect(morph.current == .logo(count: 7)) #expect(morph.remainingFrames == 0) } @Test func topologyChangesKeepIdentity() { let initial = MenuIcon(snapshot: .preview, style: .minimap) var morph = MiniMapMorph(icon: initial) let destination = MenuIcon(snapshot: Self.snapshot(count: 2), style: .minimap) morph.retarget(destination, configuration: MotionConfiguration(), reduceMotion: false, visible: true) for _ in 0..<10 { morph.advance() } #expect(morph.current == destination) } static func snapshot(count: Int, layout: ConfigLayout = .columns) -> LayoutSnapshot { let width = 1000.0 / Double(max(1, count)) let tiles = (0.. 0.01 { opaquePixels += 1 #expect(color.redComponent < 0.01) #expect(color.greenComponent < 0.01) #expect(color.blueComponent < 0.01) } else { clearPixels += 1 } } } #expect(opaquePixels > 20) #expect(clearPixels > 20) } @Test(arguments: ConfigMenuBarIcon.allCases, [false, true]) func marksRemainDistinctAcrossStylesAndAppearances(style: ConfigMenuBarIcon, contrast: Bool) throws { for appearanceName in [NSAppearance.Name.aqua, .darkAqua] { try checkMarks(style: style, contrast: contrast, appearanceName: appearanceName) } } private func checkMarks(style: ConfigMenuBarIcon, contrast: Bool, appearanceName: NSAppearance.Name) throws { let renderer = try MenuIconRenderer() let appearance = try #require(NSAppearance(named: appearanceName)) let icon = MenuIcon(snapshot: .preview, style: style) let plain = renderer.image(for: icon, increaseContrast: contrast, appearance: appearance, scale: 2) let clean = renderer.image( for: icon, increaseContrast: contrast, appearance: appearance, scale: 2, cleanSpace: true) let update = renderer.image( for: icon, increaseContrast: contrast, appearance: appearance, scale: 2, updateReady: true) let both = renderer.image( for: icon, increaseContrast: contrast, appearance: appearance, scale: 2, cleanSpace: true, updateReady: true) let images = [plain, clean, update, both] let samples = try images.map { image in try alphaSamples( NSBitmapImageRep(cgImage: #require(image.cgImage(forProposedRect: nil, context: nil, hints: nil)))) } #expect(Set(samples).count == 4) let allTemplates = images.allSatisfy { $0.isTemplate } #expect(allTemplates) #expect( renderer.image( for: icon, increaseContrast: contrast, appearance: appearance, scale: 2, cleanSpace: true) === clean ) } @Test func rasterCacheTracksGeometryAndFocusRatherThanWindowIdentity() throws { let renderer = try MenuIconRenderer() let frame = CGRect(x: 2, y: 2, width: 6, height: 12) let icon = MenuIcon.tiles([MiniMapTile(id: 1, frame: frame, isFocused: false)]) let image = renderer.image(for: icon) let pixels = try renderer.png(for: icon) let renamed = MenuIcon.tiles([MiniMapTile(id: 2, frame: frame, isFocused: false)]) #expect(renderer.image(for: renamed) === image) let moved = MenuIcon.tiles([MiniMapTile(id: 1, frame: frame.offsetBy(dx: 1, dy: 0), isFocused: false)]) let focused = MenuIcon.tiles([MiniMapTile(id: 1, frame: frame, isFocused: true)]) for changed in [moved, focused] { #expect(renderer.image(for: changed) !== image) #expect(try renderer.png(for: changed) != pixels) } #expect(renderer.image(for: icon) === image) } @Test(arguments: [1, 2, 4]) func cachedRasterPreservesItsBackingResolution(scale: Int) throws { let renderer = try MenuIconRenderer() let icon = MenuIcon(snapshot: .preview, style: .minimap) let image = renderer.image(for: icon, scale: CGFloat(scale)) let representation = try #require(image.representations.first) #expect(representation.pixelsWide == 18 * scale) #expect(representation.pixelsHigh == 18 * scale) #expect(image.size == CGSize(width: 18, height: 18)) #expect(renderer.image(for: icon, scale: CGFloat(scale)) === image) let otherScale: CGFloat = scale == 1 ? 2 : 1 #expect(renderer.image(for: icon, scale: otherScale) !== image) } @Test func boundedCacheEvictsOldRastersWithoutChangingTheirPixels() throws { let renderer = try MenuIconRenderer(cacheCapacity: 2) let first = MenuIcon.logo(count: 7) let original = renderer.image(for: first) let pixels = try renderer.png(for: first, scale: 1) _ = renderer.image(for: .logo(count: 8)) let recent = renderer.image(for: .logo(count: 9)) #expect(renderer.image(for: first) !== original) #expect(try renderer.png(for: first, scale: 1) == pixels) #expect(renderer.image(for: .logo(count: 9)) === recent) } @Test func badgeCacheUsesLocalizedTextAndSharesSaturatedCounts() throws { let renderer = try MenuIconRenderer() let english = Locale(identifier: "en_US") let arabic = Locale(identifier: "ar_EG") let icon = MenuIcon.logo(count: 12) let englishImage = renderer.image(for: icon, locale: english) let arabicImage = renderer.image(for: icon, locale: arabic) #expect(12.formatted(.number.locale(english)) != 12.formatted(.number.locale(arabic))) #expect(englishImage !== arabicImage) #expect(renderer.image(for: icon, locale: english) === englishImage) #expect(renderer.image(for: .logo(count: 100)) === renderer.image(for: .logo(count: 1000))) } @Test func brandArtworkLoads() throws { #expect(try BrandAssets.keystone().size == NSSize(width: 1024, height: 1024)) #expect(try BrandAssets.split().isTemplate) } @Test func directBitmapMatchesFlippedTileBadgeAndLogoArtwork() throws { let renderer = try MenuIconRenderer() let appearance = try #require(NSAppearance(named: .aqua)) let icons: [MenuIcon] = [ .tiles([ MiniMapTile(id: 1, frame: CGRect(x: 2, y: 3, width: 5, height: 7), isFocused: true), MiniMapTile(id: 2, frame: CGRect(x: 10, y: 8, width: 4, height: 6), isFocused: false), ]), .logo(count: 12), .logo(count: nil), ] for icon in icons { let image = renderer.image(for: icon, appearance: appearance, scale: 2) let cgImage = try #require(image.cgImage(forProposedRect: nil, context: nil, hints: nil)) let actual = NSBitmapImageRep(cgImage: cgImage) let expected = try flippedReference(for: icon, appearance: appearance) #expect(actual.pixelsWide == expected.pixelsWide) #expect(actual.pixelsHigh == expected.pixelsHigh) #expect(try alphaSamples(actual) == alphaSamples(expected), "Artwork: \(icon)") } } private func flippedReference(for icon: MenuIcon, appearance: NSAppearance) throws -> NSBitmapImageRep { let logo = try BrandAssets.split() let reference = NSImage(size: NSSize(width: 18, height: 18), flipped: true) { rect in NSColor.black.set() switch icon { case .tiles(let tiles): for tile in tiles { let path = NSBezierPath(roundedRect: tile.frame, xRadius: 0.8, yRadius: 0.8) if tile.isFocused { path.fill() } else { path.lineWidth = 0.85 path.stroke() } } case .logo(let count): if let count { logo.draw(in: CGRect(x: 0, y: 0, width: 12, height: 12)) let text = NSAttributedString( string: count.formatted(), attributes: [ .font: NSFont.monospacedDigitSystemFont(ofSize: 7, weight: .bold), .foregroundColor: NSColor.black, ]) text.draw(at: CGPoint(x: max(0, 18 - text.size().width), y: 10)) } else { logo.draw(in: rect) } } return true } let bitmap = try #require( NSBitmapImageRep( bitmapDataPlanes: nil, pixelsWide: 36, pixelsHigh: 36, bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)) let context = try #require(NSGraphicsContext(bitmapImageRep: bitmap)) NSGraphicsContext.saveGraphicsState() defer { NSGraphicsContext.restoreGraphicsState() } NSGraphicsContext.current = context context.cgContext.scaleBy(x: 2, y: 2) appearance.performAsCurrentDrawingAppearance { reference.draw(in: CGRect(x: 0, y: 0, width: 18, height: 18)) } return bitmap } private func alphaSamples(_ bitmap: NSBitmapImageRep) throws -> [UInt8] { try (0..