|
| 1 | +import XCTest |
| 2 | + |
| 3 | +extension RunnerTests { |
| 4 | + static let navigationBackKeywords = ["back", "close", "cancel"] |
| 5 | + static let navigationFallbackVerificationDelay: TimeInterval = 0.25 |
| 6 | + |
| 7 | + func tapInAppBackControl(app: XCUIApplication) -> Bool { |
| 8 | +#if os(macOS) |
| 9 | + if let back = macOSNavigationBackElement(app: app) { |
| 10 | + tapElementCenter(app: app, element: back) |
| 11 | + return true |
| 12 | + } |
| 13 | + return false |
| 14 | +#elseif os(tvOS) |
| 15 | + _ = pressTvRemote(.menu) |
| 16 | + return true |
| 17 | +#else |
| 18 | + let buttons = app.navigationBars.buttons.allElementsBoundByIndex |
| 19 | + if let back = buttons.first(where: { $0.isHittable }) { |
| 20 | + back.tap() |
| 21 | + return true |
| 22 | + } |
| 23 | + if isSnapshotXCTestChannelPenalized(bundleId: currentBundleId) { |
| 24 | + NSLog("AGENT_DEVICE_RUNNER_IN_APP_BACK_SKIPPED_XCTEST_ENUMERATION bundle=%@", currentBundleId ?? "") |
| 25 | + } else if let back = topNavigationBackElement(app: app) { |
| 26 | + tapElementCenter(app: app, element: back) |
| 27 | + return true |
| 28 | + } |
| 29 | + return tapTopLeadingNavigationFallback(app: app) |
| 30 | +#endif |
| 31 | + } |
| 32 | + |
| 33 | + private func tapElementCenter(app: XCUIApplication, element: XCUIElement) { |
| 34 | + let frame = element.frame |
| 35 | + if !frame.isEmpty { |
| 36 | + _ = tapAt(app: app, x: frame.midX, y: frame.midY) |
| 37 | + return |
| 38 | + } |
| 39 | +#if !os(tvOS) |
| 40 | + element.tap() |
| 41 | +#endif |
| 42 | + } |
| 43 | + |
| 44 | + private func topNavigationBackElement(app: XCUIApplication) -> XCUIElement? { |
| 45 | +#if os(iOS) |
| 46 | + let frame = onScreenWindowFrame(app: app) |
| 47 | + let candidates = app.buttons.matching(Self.navigationBackPredicate()).allElementsBoundByIndex.compactMap { |
| 48 | + element -> (XCUIElement, Int)? in |
| 49 | + guard element.exists, element.isHittable else { return nil } |
| 50 | + guard Self.isTopNavigationControlFrame(element.frame, in: frame) else { return nil } |
| 51 | + guard let rank = Self.navigationBackControlRank( |
| 52 | + label: element.label, |
| 53 | + identifier: element.identifier |
| 54 | + ) else { |
| 55 | + return nil |
| 56 | + } |
| 57 | + return (element, rank) |
| 58 | + } |
| 59 | + return candidates.sorted { lhs, rhs in |
| 60 | + if lhs.1 != rhs.1 { return lhs.1 < rhs.1 } |
| 61 | + let leftFrame = lhs.0.frame |
| 62 | + let rightFrame = rhs.0.frame |
| 63 | + if leftFrame.minY != rightFrame.minY { return leftFrame.minY < rightFrame.minY } |
| 64 | + return leftFrame.minX < rightFrame.minX |
| 65 | + }.first?.0 |
| 66 | +#else |
| 67 | + return nil |
| 68 | +#endif |
| 69 | + } |
| 70 | + |
| 71 | + static func navigationBackPredicate() -> NSPredicate { |
| 72 | + let clauses = navigationBackKeywords.flatMap { _ in |
| 73 | + ["label CONTAINS[c] %@", "identifier CONTAINS[c] %@"] |
| 74 | + }.joined(separator: " OR ") |
| 75 | + let arguments = navigationBackKeywords.flatMap { [$0, $0] } |
| 76 | + return NSPredicate(format: clauses, argumentArray: arguments) |
| 77 | + } |
| 78 | + |
| 79 | + static func navigationBackControlRank(label: String, identifier: String) -> Int? { |
| 80 | + let text = "\(label) \(identifier)".lowercased() |
| 81 | + return navigationBackKeywords.firstIndex { text.contains($0) } |
| 82 | + } |
| 83 | + |
| 84 | + static func isTopNavigationControlFrame(_ candidate: CGRect, in window: CGRect) -> Bool { |
| 85 | + guard |
| 86 | + candidate.width.isFinite, |
| 87 | + candidate.height.isFinite, |
| 88 | + window.width.isFinite, |
| 89 | + window.height.isFinite, |
| 90 | + candidate.width > 0, |
| 91 | + candidate.height > 0, |
| 92 | + window.width > 0, |
| 93 | + window.height > 0 |
| 94 | + else { |
| 95 | + return false |
| 96 | + } |
| 97 | + // Accept the compact navigation/search header band without matching deep content controls. |
| 98 | + let maxY = window.minY + min(max(window.height * 0.22, 96), 180) |
| 99 | + return candidate.midY >= window.minY && candidate.midY <= maxY |
| 100 | + } |
| 101 | + |
| 102 | + static func topLeadingNavigationFallbackPoint(in frame: CGRect) -> CGPoint? { |
| 103 | + guard frame.width.isFinite, frame.height.isFinite, frame.width > 0, frame.height > 0 else { |
| 104 | + return nil |
| 105 | + } |
| 106 | + // Aim at the standard leading navigation slot, bounded for compact and tablet widths. |
| 107 | + let xOffset = min(max(frame.width * 0.08, 28), 44) |
| 108 | + // Sit below the status/dynamic-island region and inside common custom RN search headers. |
| 109 | + let yOffset = min(max(frame.height * 0.155, 56), 132) |
| 110 | + return CGPoint(x: frame.minX + xOffset, y: frame.minY + yOffset) |
| 111 | + } |
| 112 | + |
| 113 | + private func tapTopLeadingNavigationFallback(app: XCUIApplication) -> Bool { |
| 114 | +#if os(iOS) |
| 115 | + let frame = onScreenWindowFrame(app: app) |
| 116 | + guard let point = Self.topLeadingNavigationFallbackPoint(in: frame) else { |
| 117 | + return false |
| 118 | + } |
| 119 | + let before = captureNavigationFallbackVisualState() |
| 120 | + let context = synthesizedCoordinateContext( |
| 121 | + policy: synthesizedGesturePolicy(.coordinateTap) |
| 122 | + )?.withReferenceFrame(frame) |
| 123 | + let synthesized = performGesture(app, idleTimeout: false) { |
| 124 | + synthesizedTapAt(app: app, x: point.x, y: point.y, context: context) |
| 125 | + } |
| 126 | + if case .performed = synthesized.outcome { |
| 127 | + return didNavigationFallbackChangeVisualState(before: before) |
| 128 | + } |
| 129 | + let fallback = performGesture(app) { |
| 130 | + tapAt(app: app, x: point.x, y: point.y) |
| 131 | + } |
| 132 | + if case .performed = fallback.outcome { |
| 133 | + return didNavigationFallbackChangeVisualState(before: before) |
| 134 | + } |
| 135 | +#endif |
| 136 | + return false |
| 137 | + } |
| 138 | + |
| 139 | + private func captureNavigationFallbackVisualState() -> Data? { |
| 140 | +#if os(iOS) |
| 141 | + runnerPngData(for: XCUIScreen.main.screenshot().image) |
| 142 | +#else |
| 143 | + return nil |
| 144 | +#endif |
| 145 | + } |
| 146 | + |
| 147 | + private func didNavigationFallbackChangeVisualState(before: Data?) -> Bool { |
| 148 | + sleepFor(Self.navigationFallbackVerificationDelay) |
| 149 | + let after = captureNavigationFallbackVisualState() |
| 150 | + let changed = Self.didNavigationFallbackChangeVisualState(before: before, after: after) |
| 151 | + if !changed { |
| 152 | + NSLog("AGENT_DEVICE_RUNNER_IN_APP_BACK_FALLBACK_NO_STATE_CHANGE") |
| 153 | + } |
| 154 | + return changed |
| 155 | + } |
| 156 | + |
| 157 | + static func didNavigationFallbackChangeVisualState(before: Data?, after: Data?) -> Bool { |
| 158 | + guard let before, let after else { return false } |
| 159 | + return before != after |
| 160 | + } |
| 161 | + |
| 162 | + private func macOSNavigationBackElement(app: XCUIApplication) -> XCUIElement? { |
| 163 | + let predicate = NSPredicate( |
| 164 | + format: "identifier == %@ OR label == %@", |
| 165 | + "go back", |
| 166 | + "Back" |
| 167 | + ) |
| 168 | + let element = app.descendants(matching: .any).matching(predicate).firstMatch |
| 169 | + return element.exists ? element : nil |
| 170 | + } |
| 171 | + |
| 172 | +#if AGENT_DEVICE_RUNNER_UNIT_TESTS |
| 173 | + func testTopLeadingNavigationFallbackPointTargetsHeaderControlBand() throws { |
| 174 | + let point = try XCTUnwrap( |
| 175 | + Self.topLeadingNavigationFallbackPoint( |
| 176 | + in: CGRect(x: 0, y: 0, width: 430, height: 932) |
| 177 | + ) |
| 178 | + ) |
| 179 | + |
| 180 | + XCTAssertEqual(point.x, 34.4, accuracy: 0.01) |
| 181 | + XCTAssertEqual(point.y, 132, accuracy: 0.01) |
| 182 | + } |
| 183 | + |
| 184 | + func testTopLeadingNavigationFallbackPointRejectsInvalidFrame() { |
| 185 | + XCTAssertNil(Self.topLeadingNavigationFallbackPoint(in: .infinite)) |
| 186 | + XCTAssertNil(Self.topLeadingNavigationFallbackPoint(in: .zero)) |
| 187 | + } |
| 188 | + |
| 189 | + func testNavigationBackControlRankPrefersBackThenCloseThenCancel() { |
| 190 | + XCTAssertEqual(Self.navigationBackControlRank(label: "Back", identifier: ""), 0) |
| 191 | + XCTAssertEqual(Self.navigationBackControlRank(label: "Close", identifier: ""), 1) |
| 192 | + XCTAssertEqual(Self.navigationBackControlRank(label: "Cancel search", identifier: ""), 2) |
| 193 | + XCTAssertNil(Self.navigationBackControlRank(label: "Search for more feeds", identifier: "")) |
| 194 | + } |
| 195 | + |
| 196 | + func testNavigationBackPredicateUsesTheSharedKeywordTable() { |
| 197 | + let predicate = Self.navigationBackPredicate() |
| 198 | + |
| 199 | + XCTAssertTrue(predicate.evaluate(with: ["label": "Back", "identifier": ""])) |
| 200 | + XCTAssertTrue(predicate.evaluate(with: ["label": "", "identifier": "close-button"])) |
| 201 | + XCTAssertFalse(predicate.evaluate(with: ["label": "Search for more feeds", "identifier": ""])) |
| 202 | + } |
| 203 | + |
| 204 | + func testTopNavigationControlFrameAcceptsOnlyHeaderBand() { |
| 205 | + let window = CGRect(x: 0, y: 0, width: 430, height: 932) |
| 206 | + |
| 207 | + XCTAssertTrue( |
| 208 | + Self.isTopNavigationControlFrame( |
| 209 | + CGRect(x: 340, y: 84, width: 72, height: 44), |
| 210 | + in: window |
| 211 | + ) |
| 212 | + ) |
| 213 | + XCTAssertFalse( |
| 214 | + Self.isTopNavigationControlFrame( |
| 215 | + CGRect(x: 20, y: 760, width: 72, height: 44), |
| 216 | + in: window |
| 217 | + ) |
| 218 | + ) |
| 219 | + XCTAssertFalse(Self.isTopNavigationControlFrame(.infinite, in: window)) |
| 220 | + } |
| 221 | + |
| 222 | + func testNavigationFallbackRequiresObservedVisualChange() { |
| 223 | + XCTAssertTrue( |
| 224 | + Self.didNavigationFallbackChangeVisualState( |
| 225 | + before: Data([1, 2, 3]), |
| 226 | + after: Data([1, 2, 4]) |
| 227 | + ) |
| 228 | + ) |
| 229 | + XCTAssertFalse( |
| 230 | + Self.didNavigationFallbackChangeVisualState( |
| 231 | + before: Data([1, 2, 3]), |
| 232 | + after: Data([1, 2, 3]) |
| 233 | + ) |
| 234 | + ) |
| 235 | + XCTAssertFalse(Self.didNavigationFallbackChangeVisualState(before: nil, after: Data([1]))) |
| 236 | + XCTAssertFalse(Self.didNavigationFallbackChangeVisualState(before: Data([1]), after: nil)) |
| 237 | + } |
| 238 | +#endif |
| 239 | +} |
0 commit comments