import AppKit import SwiftUI import QuartzCore @main struct NativeMetricDrawingChecks { @MainActor static func main() { _ = NSApplication.shared; NSApp.setActivationPolicy(.accessory) Task { @MainActor in let window = NSWindow(contentRect: NSRect(x: 40, y: 40, width: 300, height: 140), styleMask: [.borderless], backing: .buffered, defer: false) window.isReleasedWhenClosed = false window.level = .floating let root = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 140)) window.contentView = root let plot = NativePanelCurve.PlotView(); plot.frame = NSRect(x: 0, y: 0, width: 280, height: 60) root.addSubview(plot) window.orderFrontRegardless() try? await Task.sleep(for: .milliseconds(100)) let dates = (0..<4).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) } func input(_ values: [Double?]) -> NativePanelCurve.Input { NativePanelCurve.Input(frame: PanelCurveFrame(dates: dates, series: [values], intervals: [2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5) } var signed = input([-1, 0, 0.5, 1]) signed.projection = .signed signed.hoveredIndex = 0 plot.configure(signed) let dischargeDot = plot.layer!.sublayers!.last as! CAShapeLayer precondition(dischargeDot.path!.boundingBoxOfPath.midY == 60) precondition(plot.baseline.path!.boundingBoxOfPath.midY == 30) signed.hoveredIndex = 3 plot.configure(signed) precondition(dischargeDot.path!.boundingBoxOfPath.midY == 0) let continuous = input([0.8,0.8,0.8,0.8]) plot.configure(continuous) let image = bitmap(plot) precondition(redPixels(image, from: 0.4, to: 0.6) > 10, "Continuous curve must cross the middle") let fill = image.colorAt(x: image.pixelsWide / 2, y: image.pixelsHigh / 2)! precondition(fill.alphaComponent > 0.10 && fill.alphaComponent < 0.18, "A continuous run must have a translucent area below its real curve") precondition(plot.grid.isHidden, "Background trends must not draw a chart grid") plot.needsDisplay = false; plot.configure(continuous) precondition(!plot.needsDisplay, "Identical inputs must not invalidate the plot") plot.configure(input([0.8,0.8,nil,0.8])) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10, "Missing vertices must connect the surrounding valid points") plot.configure(input([0.8,nil,.nan,0.8])) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10) plot.configure(.init(frame: .init(dates: dates, series: [[0.8,nil,nil,0.8]], intervals: [2,2,2,2], boundaryDates: [dates[2]]), colors: [.red], scales: [1], maximumGap: 5)) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0, "A pause boundary on a missing vertex must still leave a real gap") var missingHover = input([0.8,0.8,nil,0.8]); missingHover.hoveredIndex = 2 plot.configure(missingHover) let dots = plot.layer!.sublayers!.last as! CAShapeLayer precondition(dots.path == nil, "Connecting a missing value must not invent a measured hover dot") plot.configure(input([nil,nil,nil,nil])) precondition(redPixels(bitmap(plot)) == 0) plot.configure(input([nil,0.8,nil,nil])) let single = bitmap(plot) precondition(single.colorAt(x: single.pixelsWide / 2, y: single.pixelsHigh / 2)!.alphaComponent == 0, "A single sample must not create a fabricated area") var hiddenSeries = NativePanelCurve.Input(frame: .init(dates: dates, series: [[nil,nil,nil,nil], [0.8,0.8,0.8,0.8]], intervals: [2,2,2,2], boundaryDates: []), colors: [.red, .red], scales: [1], maximumGap: 5) hiddenSeries.visibleSeriesCount = 1; hiddenSeries.hoveredIndex = 1 plot.configure(hiddenSeries) precondition(redPixels(bitmap(plot)) == 0, "A tooltip-only series must not leak lines, fill or hover dots") var unfilled = continuous; unfilled.filledSeriesCount = 0 plot.configure(unfilled) let noArea = bitmap(plot) precondition(noArea.colorAt(x: noArea.pixelsWide / 2, y: noArea.pixelsHigh / 2)!.alphaComponent == 0) let changedDates = [0.0, 2, 8, 10].map { Date(timeIntervalSinceReferenceDate: $0) } plot.configure(.init(frame: .init(dates: changedDates, series: [[0.8,0.8,0.8,0.8]], intervals: [2,30,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5)) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10, "Reopening before the first background deadline must keep the curve connected") plot.configure(.init(frame: .init(dates: [changedDates[0], changedDates[2]], series: [[0.8,0.8]], intervals: [2,30], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5)) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0, "A later slow cadence must not reconnect an earlier missed sample") var hovered = continuous; hovered.hoveredIndex = 1 plot.configure(hovered) precondition(redPixels(bitmap(plot)) > 0) plot.appearance = NSAppearance(named: .darkAqua) precondition(redPixels(bitmap(plot)) > 0) // Sampling updates are final immediately, and hover keeps cached curve geometry. let nextDates = dates + [Date(timeIntervalSinceReferenceDate: 8)] let next = NativePanelCurve.Input(frame: PanelCurveFrame(dates: nextDates, series: [[0.8,0.8,0.8,0.8,0.5]], intervals: [2,2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5) plot.configure(continuous) let previousBuilds = plot.pathBuildCount plot.configure(next) precondition(plot.pathBuildCount == previousBuilds + 1 && plot.input?.frame == next.frame) let sampled = bitmap(plot) try? await Task.sleep(for: .milliseconds(180)) precondition(plot.pathBuildCount == previousBuilds + 1) precondition(pixels(sampled) == pixels(bitmap(plot)), "A sampled curve must not move between updates") var hover = next; hover.hoveredIndex = 2 plot.configure(hover) precondition(plot.pathBuildCount == previousBuilds + 1, "Hover must not rebuild curve geometry") let fullDates = (0..<60).map { Date(timeIntervalSinceReferenceDate: Double($0 * 2)) } let fullFrame = PanelCurveFrame(dates: fullDates, series: [Array(repeating: 0.8, count: 60)], intervals: Array(repeating: 2, count: 60), boundaryDates: []) plot.configure(.init(frame: fullFrame, colors: [.red], scales: [1], maximumGap: 5)) let appended = PanelCurveFrame(dates: Array(fullDates.dropFirst()) + [Date(timeIntervalSinceReferenceDate: 120)], series: [Array(repeating: 0.8, count: 59) + [0.5]], intervals: fullFrame.intervals, boundaryDates: []) plot.configure(.init(frame: appended, colors: [.red], scales: [1], maximumGap: 5)) precondition(plot.input?.frame.dates.count == 60, "Static history must not retain an outgoing animation point") var moving = NativePanelCurve.Input(frame: appended, colors: [.red], scales: [1], maximumGap: 5, animates: true) plot.configure(.init(frame: fullFrame, colors: [.red], scales: [1], maximumGap: 5)) plot.configure(moving) let slide = plot.curves.animation(forKey: "trend.slide") as? CABasicAnimation let start = (slide?.fromValue as? NSValue)?.caTransform3DValue precondition(abs((start?.m41 ?? 0) - 280.0/59) < 0.001 && slide?.duration == 0.35) let movingBuilds = plot.pathBuildCount moving.hoveredIndex = 3 plot.configure(moving) precondition(plot.curves.animationKeys()?.isEmpty != false && plot.pathBuildCount == movingBuilds) plot.configure(.init(frame: .init(dates: Array(appended.dates.dropFirst()) + [Date(timeIntervalSinceReferenceDate: 122)], series: appended.series, intervals: appended.intervals, boundaryDates: [Date(timeIntervalSinceReferenceDate: 122)]), colors: [.red], scales: [1], maximumGap: 5, animates: true)) precondition(plot.curves.animationKeys()?.isEmpty != false, "Do not slide through a pause") // Adding a series with an unchanged palette must color the new layer too. plot.configure(.init(frame: PanelCurveFrame(dates: dates, series: [[nil,nil,nil,nil], [0.5,0.5,0.5,0.5]], intervals: [2,2,2,2], boundaryDates: []), colors: [.red], scales: [1], maximumGap: 5)) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) > 10) // Distinct cumulative bands must meet at 30%, with 70% total at the full plot scale. var stack = NativePanelCurve.Input(frame: .init(dates: dates, series: [Array(repeating: 0.3, count: 4), Array(repeating: 0.4, count: 4)], intervals: [2,2,2,2], boundaryDates: []), colors: [.red,.blue], scales: [1], maximumGap: 5, filledSeriesCount: 2, stacked: true) plot.configure(stack) let upperArea = (plot.curves.sublayers![1].sublayers![0] as! CAShapeLayer).path!.boundingBoxOfPath precondition(abs(upperArea.minY - 18) < 1e-9 && abs(upperArea.maxY - 42) < 1e-9, "The upper component must fill only between cumulative boundaries") let stackedBitmap = bitmap(plot) var redBand = 0, blueBand = 0 for y in 0.. 0.1 && c.alphaComponent < 0.18 else { continue } if c.redComponent > c.blueComponent + 0.2 { redBand += 1 } if c.blueComponent > c.redComponent + 0.2 { blueBand += 1 } } precondition(redBand > 20 && blueBand > 30, "Both components must produce separate translucent area pixels") let stackedBuilds = plot.pathBuildCount stack.hoveredIndex = 1; plot.configure(stack) let cumulativeDot = plot.layer!.sublayers!.last as! CAShapeLayer precondition(abs(cumulativeDot.path!.boundingBoxOfPath.midY - 18) < 1e-9) precondition(plot.pathBuildCount == stackedBuilds) stack = .init(frame: .init(dates: dates, series: [Array(repeating: 0.3, count: 4), Array(repeating: 0.4, count: 4)], intervals: [2,2,2,2], boundaryDates: [dates[2]]), colors: [.red,.blue], scales: [1], maximumGap: 5, filledSeriesCount: 2, stacked: true) plot.configure(stack) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0, "Every stack band must respect the same real sampling break") // Rate panels use a common zero axis, not a percent scale at the card top. var rates = NativePanelCurve.Input(frame: .init(dates: dates, series: [Array(repeating: 0.8, count: 4), Array(repeating: 0.4, count: 4)], intervals: [2,2,2,2], boundaryDates: []), colors: [.red,.blue], scales: [1], maximumGap: 5, filledSeriesCount: 2, projection: .split(upwardSeries: 0)) plot.configure(rates) @MainActor func areaBox(_ index: Int) -> CGRect { (plot.curves.sublayers![index].sublayers![0] as! CAShapeLayer).path!.boundingBoxOfPath } precondition(abs(areaBox(0).minY - 21.6) < 1e-9 && abs(areaBox(0).maxY - 36) < 1e-9) precondition(abs(areaBox(1).minY - 36) < 1e-9 && abs(areaBox(1).maxY - 43.2) < 1e-9) precondition(!plot.baseline.isHidden && abs(plot.baseline.path!.boundingBoxOfPath.midY - 36) < 1e-9) precondition(redPixels(bitmap(plot)) > 10) let rateBuilds = plot.pathBuildCount rates.hoveredIndex = 1; plot.configure(rates) precondition(abs((plot.layer!.sublayers!.last as! CAShapeLayer).path!.boundingBoxOfPath.midY - 43.2) < 1e-9) precondition(plot.pathBuildCount == rateBuilds) rates.projection = .split(upwardSeries: 1); plot.configure(rates) precondition(abs(areaBox(0).maxY - 50.4) < 1e-9 && abs(areaBox(1).minY - 28.8) < 1e-9, "Network download must go down and upload up without negating raw rates") precondition(plot.pathBuildCount == rateBuilds + 1) rates = .init(frame: .init(dates: dates, series: [Array(repeating: 0.8, count: 4), Array(repeating: 0.4, count: 4)], intervals: [2,2,2,2], boundaryDates: [dates[2]]), colors: [.red,.blue], scales: [1], maximumGap: 5, filledSeriesCount: 2, projection: .split(upwardSeries: 0)) plot.configure(rates) precondition(redPixels(bitmap(plot), from: 0.4, to: 0.6) == 0) for series in 0...1 { precondition(PanelCurveProjection.split(upwardSeries: 0).y(value: 0, scale: 10, series: series, height: 176) == 105.6) } precondition(abs(PanelCurveProjection.split(upwardSeries: 0).y(value: 10, scale: 10, series: 0, height: 176) - 52.8) < 1e-9) assertStatic(plot.layer!) plot.removeFromSuperview() window.orderOut(nil); window.close() print("PASS: bidirectional rate areas/zero axis/direction/hover/gaps, stacked component pixels and cumulative hover, static background lines/area pixels, no single-point fill, hidden-series isolation, reduced-motion stable updates, rolling transform and hover cancellation, real gaps, hover cache, palette growth, appearance, full history and unmount") fflush(stdout); NSApp.terminate(nil) } NSApp.run() } @MainActor private static func assertStatic(_ layer: CALayer) { precondition(layer.animationKeys()?.isEmpty != false) precondition(!(layer is CAGradientLayer) && layer.mask == nil && layer.filters?.isEmpty != false) for child in layer.sublayers ?? [] { assertStatic(child) } } private static func pixels(_ bitmap: NSBitmapImageRep) -> Data { var data = Data() for row in 0.. NSBitmapImageRep { view.layoutSubtreeIfNeeded(); view.displayIfNeeded() let result = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(view.bounds.width * 2), pixelsHigh: Int(view.bounds.height * 2), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0)! let context = NSGraphicsContext(bitmapImageRep: result)!.cgContext context.scaleBy(x: 2, y: 2) view.layer!.render(in: context) return result } private static func redPixels(_ image: NSBitmapImageRep, from: Double = 0, to: Double = 1) -> Int { var count = 0 for x in Int(Double(image.pixelsWide) * from).. color.greenComponent + 0.15 && color.redComponent > color.blueComponent + 0.15 && color.alphaComponent > 0.01 { count += 1 } } } return count } }