Skip to content

Commit

Permalink
Add MainActor isolation to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Jarvis committed Nov 13, 2024
1 parent 5bef707 commit 75c9bc6
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ jobs:
run: swiftlint .
ui-tests:
strategy:
fail-fast: false
matrix:
include:
- os: macos-12
Expand All @@ -43,4 +44,4 @@ jobs:
- name: Boot simulator
run: xcrun simctl boot '${{ matrix.sim }}'
- name: Run tests
run: xcodebuild -project ./Example/Example.xcodeproj -scheme Example test -destination platform='iOS Simulator',name='${{ matrix.sim }}' -quiet -enableCodeCoverage YES -derivedDataPath "./output"
run: xcodebuild -project ./Example/Example.xcodeproj -scheme Example test -destination platform='iOS Simulator',name='${{ matrix.sim }}' -enableCodeCoverage YES -derivedDataPath "./output"
4 changes: 2 additions & 2 deletions Example/ExampleUITests/ExampleUITestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import XCTest

@MainActor
class ExampleUITestCase: XCUITestCase {
var _sectionNavItem: (() throws -> XCUIElement)? {
var _sectionNavItem: (@MainActor () throws -> XCUIElement)? {
nil
}

func sectionNavItem() throws -> XCUIElement {
try XCTUnwrap(_sectionNavItem)()
}

var _picker: (() throws -> XCUIElement)? {
var _picker: (@MainActor () throws -> XCUIElement)? {
nil
}

Expand Down
10 changes: 6 additions & 4 deletions Example/ExampleUITests/MutliValueUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import XCTest

@MainActor
final class MultiValueUITests: ExampleUITestCase {
override var _sectionNavItem: (() throws -> XCUIElement)? {
@MainActor
override var _sectionNavItem: (@MainActor () throws -> XCUIElement)? {
multiValueNavItem
}

override var _picker: (() throws -> XCUIElement)? {
@MainActor
override var _picker: (@MainActor () throws -> XCUIElement)? {
multiValuePicker
}

Expand Down Expand Up @@ -57,14 +59,14 @@ final class MultiValueUITests: ExampleUITestCase {
XCTAssert(try !buttonOne().isSelected, "'1' is still not selected after being deselected and '0' is deselected")
}

func testGridStyleDeselectAndSelectNew() throws {
func testGridStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
try setGridStyle()

try sharedTestSteps()
}

func testListStyleDeselectAndSelectNew() throws {
func testListStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
try setListStyle()

Expand Down
10 changes: 6 additions & 4 deletions Example/ExampleUITests/SingleOptionalValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import XCTest

@MainActor
final class SingleOptionalValueUITests: ExampleUITestCase {
override var _sectionNavItem: (() throws -> XCUIElement)? {
@MainActor
override var _sectionNavItem: (@MainActor () throws -> XCUIElement)? {
singleOptionalValueNavItem
}

override var _picker: (() throws -> XCUIElement)? {
@MainActor
override var _picker: (@MainActor () throws -> XCUIElement)? {
singleOptionalValuePicker
}

Expand Down Expand Up @@ -55,14 +57,14 @@ final class SingleOptionalValueUITests: ExampleUITestCase {
XCTAssert(try buttonOne().isSelected, "'1' should be selected after it is tapped.")
}

func testGridStyleDeselectAndSelectNew() throws {
func testGridStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
try setGridStyle()

try sharedTestSteps()
}

func testListStyleDeselectAndSelectNew() throws {
func testListStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
try setListStyle()

Expand Down
10 changes: 6 additions & 4 deletions Example/ExampleUITests/SingleValueUITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import XCTest

@MainActor
final class SingleValueUITests: ExampleUITestCase {
override var _sectionNavItem: (() throws -> XCUIElement)? {
@MainActor
override var _sectionNavItem: (@MainActor () throws -> XCUIElement)? {
singleValueNavItem
}

override var _picker: (() throws -> XCUIElement)? {
@MainActor
override var _picker: (@MainActor () throws -> XCUIElement)? {
singleValuePicker
}

Expand Down Expand Up @@ -68,7 +70,7 @@ final class SingleValueUITests: ExampleUITestCase {
XCTAssert(try !buttonZero().isSelected, "'0' should no longer be selected after tapping a new item.")
}

func testGridStyleDeselectAndSelectNew() throws {
func testGridStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
#if os(tvOS)
guard try findVertically(gridStyleToggle(), method: .downOnly) else {
Expand All @@ -81,7 +83,7 @@ final class SingleValueUITests: ExampleUITestCase {
try sharedTestSteps()
}

func testListStyleDeselectAndSelectNew() throws {
func testListStyleDeselectAndSelectNew() async throws {
_ = try gridStyleToggle().waitForExistence(timeout: 1)
#if os(tvOS)
guard try findVertically(gridStyleToggle(), method: .downOnly) else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import XCTest

extension XCUIElementQuery {
@MainActor
public func exactlyOneMatch() throws -> XCUIElement {
XCTAssertEqual(count, 1, "Requiring only one element match the query resulting in `self`")
return firstMatch
Expand Down
15 changes: 11 additions & 4 deletions Example/ExampleUITests/XCUITestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class XCUITestCase: XCTestCase {
try app().descendants(matching: .any)
}

@MainActor
func elementPredicate(labeled label: String) -> NSPredicate {
NSComparisonPredicate(
leftExpression: NSExpression(forKeyPath: \XCUIElement.label),
Expand All @@ -31,13 +32,18 @@ class XCUITestCase: XCTestCase {
}

func elementCompoundOrPredicate(labeled labels: Set<String>) -> NSPredicate {
NSCompoundPredicate(orPredicateWithSubpredicates: labels.map(elementPredicate(labeled:)))
let subpredicates = labels.map { label in
elementPredicate(labeled: label)
}
return NSCompoundPredicate(orPredicateWithSubpredicates: subpredicates)
}

override func setUp() async throws {
_app = XCUIApplication()
continueAfterFailure = false
try? app().launch()
Task { @MainActor in
self._app = XCUIApplication()
self.continueAfterFailure = false
try? self.app().launch()
}
}
}

Expand All @@ -53,6 +59,7 @@ class XCUITestCase: XCTestCase {
)
}

@MainActor
func currentFocus() throws -> XCUIElement {
try allElements().descendants(matching: .any).element(matching: focusPredicate)
}
Expand Down

0 comments on commit 75c9bc6

Please sign in to comment.