Skip to content

Commit 5814ba4

Browse files
pjleonard37github-actions[bot]
authored andcommitted
Add two potential fixes for flaky interactions tests (#8093)
This PR includes 2 potential fixes to our interactions tests, which have been flaky (for example [here](https://github.com/mapbox/mapbox-sdk/actions/runs/19340074549/job/55401597566?pr=7250)). 1. Added layoutIfNeeded() before camera setup Potential root cause: iOS doesn't immediately layout subviews when added to the view hierarchy. Without forcing layout, the MapView's bounds weren't finalized, causing `map.point(for: coordinate)` to return incorrect screen coordinates. This made taps miss their intended features. Fix: `rootView.layoutIfNeeded()` forces a layout pass before setting the camera, ensuring coordinate-to-screen-point conversions are accurate. 2. Moved observer setup before `map.load()` Potential root cause: Since the test style is inline JSON (not a network fetch), it could load synchronously. Attaching the onMapLoaded observer after calling `load()` created a race condition where the event might fire before the observer was ready. Fix: Moved `map.onMapLoaded.observeNext { }` before `map.load()`, guaranteeing we capture the event. cc @mapbox/maps-ios GitOrigin-RevId: ff6ace82571c77e2b1d2e0fdf675f4e248c61e2e
1 parent 8ab7f80 commit 5814ba4

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

Tests/MapboxMapsTests/Foundation/FeaturesetsTests.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ final class FeaturesetsTests: IntegrationTestCase {
1111

1212
let rootView = try XCTUnwrap(rootViewController?.view)
1313
let size = CGSize(width: 200, height: 200)
14-
mapView = MapView(frame: .init(origin: CGPoint(x: 100, y: 100), size: size))
14+
15+
// Pass style and camera in mapInitOptions to avoid loading default style first
16+
let mapInitOptions = MapInitOptions(
17+
mapStyle: .featuresetTestsStyle,
18+
cameraOptions: CameraOptions(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), zoom: 10)
19+
)
20+
mapView = MapView(frame: .init(origin: CGPoint(x: 100, y: 100), size: size), mapInitOptions: mapInitOptions)
1521
rootView.addSubview(mapView)
1622

17-
map.setCamera(to: CameraOptions(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), zoom: 10))
23+
// Force layout pass to ensure map view has correct bounds before rendering
24+
// This ensures coordinate-to-screen-point conversions are accurate
25+
rootView.layoutIfNeeded()
1826

1927
let expectation = expectation(description: "Load the map")
2028

21-
map.load(mapStyle: .featuresetTestsStyle)
22-
29+
// Set up observer to wait for map to be fully loaded
2330
map.onMapLoaded.observeNext { _ in
2431
expectation.fulfill()
2532
}.store(in: &cancelables)

Tests/MapboxMapsTests/Foundation/InteractionsTests.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,22 @@ final class InteractionsTests: IntegrationTestCase {
1111

1212
let rootView = try XCTUnwrap(rootViewController?.view)
1313
let size = CGSize(width: 200, height: 200)
14-
mapView = MapView(frame: .init(origin: CGPoint(x: 100, y: 100), size: size))
14+
15+
// Pass style and camera in mapInitOptions to avoid loading default style first
16+
let mapInitOptions = MapInitOptions(
17+
mapStyle: .featuresetTestsStyle,
18+
cameraOptions: CameraOptions(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), zoom: 10)
19+
)
20+
mapView = MapView(frame: .init(origin: CGPoint(x: 100, y: 100), size: size), mapInitOptions: mapInitOptions)
1521
rootView.addSubview(mapView)
1622

17-
map.setCamera(to: CameraOptions(center: CLLocationCoordinate2D(latitude: 0, longitude: 0), zoom: 10))
23+
// Force layout pass to ensure map view has correct bounds before rendering
24+
// This ensures coordinate-to-screen-point conversions are accurate
25+
rootView.layoutIfNeeded()
1826

1927
let expectation = expectation(description: "Load the map")
2028

21-
map.load(mapStyle: .featuresetTestsStyle)
22-
29+
// Set up observer to wait for map to be fully loaded
2330
map.onMapLoaded.observeNext { _ in
2431
expectation.fulfill()
2532
}.store(in: &cancelables)

0 commit comments

Comments
 (0)