import UIKit import XCTest @testable import Codepad @MainActor final class DecorationPaintTests: XCTestCase { private let red: UInt32 = 0xFFC8_322B private let tint: UInt32 = 0xFFFA_E8E6 private func near(_ c: (r: Int, g: Int, b: Int, a: Int), _ argb: UInt32) -> Bool { abs(c.r - Int((argb >> 16) & 0xff)) <= 3 && abs(c.g - Int((argb >> 8) & 0xff)) <= 3 && abs(c.b - Int(argb & 0xff)) <= 3 } private func caretLeft(_ editor: CodeEditorView, _ offset: Int) -> CGFloat { editor.core.setSelection(anchor: offset, head: offset) draw(editor) return editor.latestFrame.caret?.minX ?? .nan } private func theme(activeLine: CGColor) -> ThemeColors { let d = ThemeColors.default return ThemeColors( background: d.background, foreground: d.foreground, gutterBackground: d.gutterBackground, gutterForeground: d.gutterForeground, gutterActiveForeground: d.gutterActiveForeground, activeLine: activeLine, selection: d.selection, cursor: d.cursor, matchingBracket: d.matchingBracket, searchMatch: d.searchMatch, searchMatchActive: d.searchMatchActive, tokens: [:]) } // A checker underlines lines all over a file. Each one below the window cost a layout call per draw, and nine of // them trapped Debug builds. func testUnderlinesBelowTheWindowCostNoLayoutCall() { let editor = launch() editor.setInitialText((0..<200).map { "line \($0)" }.joined(separator: "\n")) layout(editor) draw(editor) let doc = editor.core.document let items = (100..<106).map { line in Decoration.underline( Underline(start: doc.lineStart(line), end: doc.lineStart(line) + 4, color: red, style: .solid, thickness: 2, offset: 4)) } editor.setDecorations(items, revision: -1) draw(editor) XCTAssertEqual(editor.viewport.drawCalls, 0) } func testSolidUnderlineSitsOffsetBelowTheBaseline() { let editor = launch() editor.setLineNumbers(false) editor.setInitialText("if T[j] < T[i]:") layout(editor) // The middle of the T at offset 10: its lowest ink row sits on the baseline. let mid = (caretLeft(editor, 10) + caretLeft(editor, 11)) / 2 editor.core.setSelection(anchor: 0, head: 0) let plain = Pixels(draw(editor)) let x = Int(mid * plain.scale) guard let lowest = (0..