Files
Kymo/kymoTests/NativeMetricDrawingChecks.swift
T

120 lines
7.9 KiB
Swift

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)
let ring = NativeMetricRing.RingView(); ring.frame = NSRect(x: 0, y: 70, width: 28, height: 28)
root.addSubview(ring)
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)
}
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")
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) == 0, "Missing values must leave a real gap")
plot.configure(input([nil,nil,nil,nil]))
precondition(redPixels(bitmap(plot)) == 0)
var hovered = continuous; hovered.hoveredIndex = 1
plot.configure(hovered)
precondition(redPixels(bitmap(plot)) > 0)
plot.appearance = NSAppearance(named: .darkAqua)
precondition(redPixels(bitmap(plot)) > 0)
ring.configure(NativeMetricRing.Input(value: 0, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(redPixels(bitmap(ring), opaque: true) == 0, "Zero ring must not draw a colored arc")
ring.configure(NativeMetricRing.Input(value: 0.25, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
let quarter = redPixels(bitmap(ring), opaque: true)
ring.configure(NativeMetricRing.Input(value: 1, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
let full = redPixels(bitmap(ring), opaque: true)
precondition(quarter > 0 && full > quarter * 2, "Ring fraction must change the rendered arc")
// 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")
ring.configure(.init(value: 0.25, color: .red, lineWidth: 3, trackOpacity: 0.17, inset: 2))
precondition(ring.arc.strokeEnd == 0.25, "The arc must immediately show the new value")
let updated = bitmap(ring)
try? await Task.sleep(for: .milliseconds(180))
precondition(pixels(updated) == pixels(bitmap(ring)), "A sampled ring must not interpolate later")
// 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)
assertStatic(plot.layer!)
assertStatic(ring.layer!)
plot.removeFromSuperview(); ring.removeFromSuperview()
window.orderOut(nil); window.close()
print("PASS: static curve/ring pixels, immediate stable updates, no animation/material/gradient layers, 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..<bitmap.pixelsHigh {
data.append(bitmap.bitmapData!.advanced(by: row * bitmap.bytesPerRow), count: bitmap.pixelsWide * bitmap.bitsPerPixel / 8)
}
return data
}
@MainActor private static func bitmap(_ view: NSView) -> 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, opaque: Bool = false) -> Int {
var count = 0
for x in Int(Double(image.pixelsWide) * from)..<Int(Double(image.pixelsWide) * to) {
for y in 0..<image.pixelsHigh {
guard let color = image.colorAt(x: x, y: y)?.usingColorSpace(.deviceRGB) else { continue }
if color.redComponent > color.greenComponent + 0.15 && color.redComponent > color.blueComponent + 0.15 && color.alphaComponent > (opaque ? 0.8 : 0.01) { count += 1 }
}
}
return count
}
}