|
| 1 | +package com.composea11yscanner.rules |
| 2 | + |
| 3 | +import com.composea11yscanner.core.model.A11ySeverity |
| 4 | +import com.composea11yscanner.core.model.Rect |
| 5 | +import org.junit.Assert.assertEquals |
| 6 | +import org.junit.Assert.assertTrue |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +class TouchTargetOverlapRuleTest { |
| 10 | + |
| 11 | + private val rule = TouchTargetOverlapRule() |
| 12 | + |
| 13 | + @Test |
| 14 | + fun `overlapping effective targets report each affected node once`() { |
| 15 | + val first = target("first", Rect(0, 0, 48, 48)) |
| 16 | + val second = target("second", Rect(40, 0, 88, 48)) |
| 17 | + |
| 18 | + val issues = rule.evaluateAll(listOf(first, second)) |
| 19 | + |
| 20 | + assertEquals(2, issues.size) |
| 21 | + assertEquals(setOf("first", "second"), issues.map { it.affectedNode.nodeId }.toSet()) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `adjacent targets that only share an edge pass`() { |
| 26 | + val first = target("first", Rect(0, 0, 48, 48)) |
| 27 | + val second = target("second", Rect(48, 0, 96, 48)) |
| 28 | + |
| 29 | + assertTrue(rule.evaluateAll(listOf(first, second)).isEmpty()) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `non-interactive merged and missing bounds nodes are ignored`() { |
| 34 | + val valid = target("valid", Rect(0, 0, 48, 48)) |
| 35 | + val nonInteractive = createNode( |
| 36 | + nodeId = "non-interactive", |
| 37 | + isTouchTarget = false, |
| 38 | + effectiveTouchBounds = Rect(0, 0, 48, 48), |
| 39 | + ) |
| 40 | + val merged = target("merged", Rect(0, 0, 48, 48), isMergedDescendant = true) |
| 41 | + val missingBounds = createNode(nodeId = "missing", isTouchTarget = true) |
| 42 | + |
| 43 | + assertTrue(rule.evaluateAll(listOf(valid, nonInteractive, merged, missingBounds)).isEmpty()) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `one node overlapping multiple targets produces one aggregated issue`() { |
| 48 | + val center = target("center", Rect(20, 0, 68, 48)) |
| 49 | + val left = target("left", Rect(0, 0, 40, 48)) |
| 50 | + val right = target("right", Rect(60, 0, 108, 48)) |
| 51 | + |
| 52 | + val centerIssue = rule.evaluateAll(listOf(center, left, right)) |
| 53 | + .single { it.affectedNode.nodeId == "center" } |
| 54 | + |
| 55 | + assertTrue(centerIssue.message.contains("2 other targets")) |
| 56 | + assertEquals(A11ySeverity.Warning, centerIssue.severity) |
| 57 | + assertEquals(null, centerIssue.wcagReference) |
| 58 | + } |
| 59 | + |
| 60 | + private fun target( |
| 61 | + id: String, |
| 62 | + bounds: Rect, |
| 63 | + isMergedDescendant: Boolean = false, |
| 64 | + ) = createNode( |
| 65 | + nodeId = id, |
| 66 | + isTouchTarget = true, |
| 67 | + effectiveTouchBounds = bounds, |
| 68 | + isMergedDescendant = isMergedDescendant, |
| 69 | + ) |
| 70 | +} |
0 commit comments