Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Queries return `SemanticsNode` values: `label`, `value`, `identifier`, `traits`,
|---|---|---|
| Create | `SemanticsTester { view }`, `SemanticsTester(hosting:)` | `SemanticsTester.make { view }` |
| Find now | `nodes`, `node(_:)`, `node(id:)`, `node(value:)`, `node(containing:)` | |
| Wait | `expect(_:)`, `expect(id:)`, `expect(containing:)`, `expectGone(_:)`, `wait(where:)` | `expectAsync`, `expectGoneAsync`, `waitAsync` |
| Wait | `expect(_:)`, `expect(id:)`, `expect(containing:)`, `expectGone(_:)`, `wait(where:)` | `expectAsync`, `expectEnabledAsync`, `expectGoneAsync`, `waitAsync` |
| Act | `tap(_:)`, `type(_:into:)`, `forceActivate(_:)`, `node.activate()`, `node.performCustomAction(named:)`, `node.increment()` / `decrement()` | `tapAsync(_:)` |
| Time | `pump(_:)` | `settle(_:)` |
| Debug | `dump()`, `SemanticsWalker.dump(_:)`, `snapshot(_:in:)` | |
Expand All @@ -133,6 +133,11 @@ the fold materialize the way they would for a user.
prefers buttons, and refuses disabled controls. `forceActivate` skips the enabled check, for
negative tests only.

Since iOS 27 SwiftUI refreshes toolbar items as async main-actor work, which the synchronous API
never drains: after an edit, a bar button stays disabled until the test suspends. Use
`expectEnabledAsync` to assert it became enabled. `tapAsync` retries a disabled control until its
timeout, so it reaches the button once the refresh lands.

`snapshot("name")` writes the hosted window, plus menu and popover windows, as a PNG into
`SNAPSHOT_DIR` (`TEST_RUNNER_SNAPSHOT_DIR` via xcodebuild) or a temp folder. It is for looking
at a view, e.g. in a PR review. It compares nothing and does not include the system status bar.
Expand Down
15 changes: 14 additions & 1 deletion Sources/SemanticsTesting/SemanticsTester+Interactions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,21 @@ extension SemanticsTester {
/// `Button { Task { await … } }` is the common shape in this app: the synchronous action
/// fires immediately, but its Task makes no progress while `pump()` blocks the MainActor.
/// This awaits afterwards so the effect has actually landed when the call returns.
///
/// A control that cannot be activated yet (still disabled) is retried until `timeout`, letting
/// async work run in between. Since iOS 27 SwiftUI refreshes toolbar items that way, so a bar
/// button enabled by an edit stays disabled until the test suspends.
public func tapAsync(_ label: String, timeout: TimeInterval = 3) async throws {
try tap(label, timeout: timeout)
let deadline = Date().addingTimeInterval(timeout)
while true {
do {
try tap(label, timeout: max(deadline.timeIntervalSinceNow, 0.05))
break
} catch {
guard Date() < deadline else { throw error }
await settle(0.02)
}
}
await settle()
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/SemanticsTesting/SemanticsTester.swift
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ public final class SemanticsTester {
}
}

/// Waits for an enabled element, letting queued async work run between checks.
///
/// Use it to assert a control became enabled after an edit: the synchronous `expect(_:)` cannot
/// see state that arrives as async main-actor work, such as iOS 27 toolbar refreshes.
@discardableResult
public func expectEnabledAsync(_ label: String, timeout: TimeInterval = 3) async throws -> SemanticsNode {
try await waitAsync(timeout: timeout, description: "enabled node with label '\(label)'") {
$0.label == label && $0.isEnabled
}
}

/// Waits until no element with this label remains, letting queued async work run.
public func expectGoneAsync(_ label: String, timeout: TimeInterval = 3) async throws {
let deadline = Date().addingTimeInterval(timeout)
Expand Down
46 changes: 46 additions & 0 deletions Tests/SemanticsTestingTests/SemanticsTesterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ private struct AsyncCounterView: View {
}
}

/// "Save" is enabled by main-actor work queued from "Edit", the way iOS 27 delivers toolbar
/// refreshes: nothing a synchronous `pump()` drains.
private struct DeferredEnableView: View {
@State private var enabled = false
@State private var saved = 0

var body: some View {
VStack {
Text("Saved: \(saved)")
Button("Edit") { Task { enabled = true } }
Button("Save") { saved += 1 }
.disabled(!enabled)
}
}
}

// MARK: - Tests

/// Host-less: the package tests run without a `UIApplication`, so presentation (sheets, menus,
Expand Down Expand Up @@ -125,6 +141,36 @@ struct SemanticsTesterTests {
try await tester.expectGoneAsync("Async count: 0")
}

@Test func tapAsyncWaitsForAControlEnabledByAsyncWork() async throws {
let tester = await SemanticsTester.make { DeferredEnableView() }
defer { tester.tearDown() }

try tester.tap("Edit")
try await tester.tapAsync("Save")

try await tester.expectAsync("Saved: 1")
}

@Test func expectEnabledAsyncWaitsForAsyncEnablement() async throws {
let tester = await SemanticsTester.make { DeferredEnableView() }
defer { tester.tearDown() }

#expect(try tester.expect("Save").isEnabled == false)
try tester.tap("Edit")

try await tester.expectEnabledAsync("Save")
}

@Test func tapAsyncStillRefusesAControlThatStaysDisabled() async throws {
let tester = await SemanticsTester.make { CounterView(enabled: false) }
defer { tester.tearDown() }

await #expect(throws: SemanticsError.self) {
try await tester.tapAsync("Increment", timeout: 0.3)
}
try await tester.expectAsync("Count: 0")
}

@Test func waitsScrollListsToRevealRowsBelowTheFold() throws {
let tester = SemanticsTester {
List(0 ..< 80, id: \.self) { index in
Expand Down
Loading