Files
conjure/Tests/ConjureKitTests/EmojiIconTests.swift
T

30 lines
1.5 KiB
Swift

import AppKit
import Testing
@testable import Conjure
@Test(arguments: ["emoji", "notes", "translate"])
@MainActor func commandArtworkHasBalancedVisiblePadding(query: String) throws {
_ = NSApplication.shared
let launcher = LauncherWindowController()
func descendants(_ view: NSView) -> [NSView] { [view] + view.subviews.flatMap(descendants) }
let views = descendants(try #require(launcher.window?.contentView))
let search = try #require(views.compactMap { $0 as? NSSearchField }.first)
search.stringValue = query
launcher.controlTextDidChange(Notification(name: NSControl.textDidChangeNotification, object: search))
let table = try #require(views.compactMap { $0 as? NSTableView }.first)
let cell = try #require(launcher.tableView(table, viewFor: nil, row: 0) as? ApplicationCell)
let image = try #require(cell.imageView?.image)
let data = try #require(image.tiffRepresentation)
let bitmap = try #require(NSBitmapImageRep(data: data))
var left = bitmap.pixelsWide, right = -1, top = bitmap.pixelsHigh, bottom = -1
for y in 0..<bitmap.pixelsHigh {
for x in 0..<bitmap.pixelsWide where (bitmap.colorAt(x: x, y: y)?.alphaComponent ?? 0) > 0.5 {
left = min(left, x); right = max(right, x)
top = min(top, y); bottom = max(bottom, y)
}
}
#expect(right > left && bottom > top)
#expect(abs(left - (bitmap.pixelsWide - right - 1)) <= 2)
#expect(abs(top - (bitmap.pixelsHigh - bottom - 1)) <= 2)
}