-
Notifications
You must be signed in to change notification settings - Fork 2
fix(selection): hit-test line selection against resolved line rects #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Tests/MarkdownViewTests/LineSelectionOverlayViewTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| import XCTest | ||
| @testable import MarkdownView | ||
| #if canImport(AppKit) | ||
| import AppKit | ||
| #elseif canImport(UIKit) | ||
| import UIKit | ||
| #endif | ||
|
|
||
| @MainActor | ||
| final class LineSelectionOverlayViewTests: XCTestCase { | ||
| private let lineHeight: CGFloat = 20 | ||
| private let gap: CGFloat = 4 | ||
| private let firstLineY: CGFloat = 10 | ||
|
|
||
| /// Synthetic resolved line rects: three 20pt lines separated by a 4pt gap, | ||
| /// starting at y=10 — line 1 spans 10..<30, line 2 spans 34..<54, line 3 | ||
| /// spans 58..<78. | ||
| private func makeOverlay(lineCount: Int = 3) -> LineSelectionOverlayView { | ||
| let overlay = LineSelectionOverlayView(frame: .zero) | ||
| let rects = (0 ..< lineCount).map { index in | ||
| CGRect( | ||
| x: 0, | ||
| y: firstLineY + CGFloat(index) * (lineHeight + gap), | ||
| width: 100, | ||
| height: lineHeight | ||
| ) | ||
| } | ||
| overlay.updateLineRects(rects) | ||
| return overlay | ||
| } | ||
|
|
||
| func testHasLineRects() { | ||
| let empty = LineSelectionOverlayView(frame: .zero) | ||
| XCTAssertFalse(empty.hasLineRects) | ||
| XCTAssertTrue(makeOverlay().hasLineRects) | ||
| } | ||
|
|
||
| func testEmptyRectsReturnsNil() { | ||
| let overlay = LineSelectionOverlayView(frame: .zero) | ||
| XCTAssertNil(overlay.lineIndex(atY: 10, trailingGap: gap)) | ||
| } | ||
|
|
||
| func testPointInsideLine() { | ||
| let overlay = makeOverlay() | ||
| XCTAssertEqual(overlay.lineIndex(atY: 10, trailingGap: gap), 1) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 29, trailingGap: gap), 1) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 44, trailingGap: gap), 2) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 70, trailingGap: gap), 3) | ||
| } | ||
|
|
||
| func testPointInGapBelongsToLineAbove() { | ||
| let overlay = makeOverlay() | ||
| // Gap between line 1 (ends 30) and line 2 (starts 34). | ||
| XCTAssertEqual(overlay.lineIndex(atY: 30, trailingGap: gap), 1) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 33.5, trailingGap: gap), 1) | ||
| // Gap between line 2 (ends 54) and line 3 (starts 58). | ||
| XCTAssertEqual(overlay.lineIndex(atY: 56, trailingGap: gap), 2) | ||
| } | ||
|
|
||
| func testPointAboveFirstLineReturnsNil() { | ||
| let overlay = makeOverlay() | ||
| XCTAssertNil(overlay.lineIndex(atY: 9.5, trailingGap: gap)) | ||
| XCTAssertNil(overlay.lineIndex(atY: -5, trailingGap: gap)) | ||
| } | ||
|
|
||
| func testTrailingGapBelongsToLastLine() { | ||
| let overlay = makeOverlay() | ||
| // Line 3 ends at 78; it owns one trailing gap (< 82). | ||
| XCTAssertEqual(overlay.lineIndex(atY: 78, trailingGap: gap), 3) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 81.5, trailingGap: gap), 3) | ||
| } | ||
|
|
||
| /// CoreText emits no line for a trailing blank row (text ending in a | ||
| /// newline), so logical rows can outnumber resolved rects. Points past | ||
| /// the last rect extrapolate at the last line's advance; callers cap to | ||
| /// their logical line count. | ||
| func testExtrapolatesBeyondLastRect() { | ||
| let overlay = makeOverlay() | ||
| // Line 3's owned region ends at 82; each extrapolated row advances 24. | ||
| XCTAssertEqual(overlay.lineIndex(atY: 82, trailingGap: gap), 4) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 105.5, trailingGap: gap), 4) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 106, trailingGap: gap), 5) | ||
| } | ||
|
|
||
| func testSingleLine() { | ||
| let overlay = makeOverlay(lineCount: 1) | ||
| XCTAssertNil(overlay.lineIndex(atY: 9, trailingGap: gap)) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 10, trailingGap: gap), 1) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 33, trailingGap: gap), 1) | ||
| // Beyond the owned region: extrapolated to the (possibly virtual) | ||
| // next row, for the caller's count guard to accept or reject. | ||
| XCTAssertEqual(overlay.lineIndex(atY: 34, trailingGap: gap), 2) | ||
| } | ||
|
|
||
| /// The motivating case for rect-based hit-testing: lines whose real | ||
| /// heights vary (fallback-font glyphs, accumulated layout drift) must | ||
| /// still resolve to the rect that contains the point. | ||
| func testUnevenLineHeights() { | ||
| let overlay = LineSelectionOverlayView(frame: .zero) | ||
| overlay.updateLineRects([ | ||
| CGRect(x: 0, y: 0, width: 100, height: 20), | ||
| CGRect(x: 0, y: 24, width: 100, height: 32), // taller (emoji/CJK) | ||
| CGRect(x: 0, y: 60, width: 100, height: 20), | ||
| ]) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 12, trailingGap: gap), 1) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 40, trailingGap: gap), 2) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 59, trailingGap: gap), 2) | ||
| XCTAssertEqual(overlay.lineIndex(atY: 61, trailingGap: gap), 3) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in b60efc9:
LineSelectionOverlayViewTestscovers inside-line hits, gap ownership, above-first bounds, trailing gap, single line, and uneven line heights (the motivating case). Extended in 409c033 for the beyond-last-rect extrapolation.