Skip to content

Commit

Permalink
Use constant test values in locations tests, update fixtures (mapbox#…
Browse files Browse the repository at this point in the history
…2269)

Co-authored-by: Release SDK bot for Maps SDK team <[email protected]>
  • Loading branch information
pjleonard37 and Release SDK bot for Maps SDK team authored Aug 30, 2024
1 parent b724094 commit 7016454
Show file tree
Hide file tree
Showing 36 changed files with 228 additions and 175 deletions.
2 changes: 1 addition & 1 deletion Tests/MapboxMapsTests/Foundation/CameraStateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class CameraStateTests: XCTestCase {

override func setUp() {
super.tearDown()
center = .random()
center = .testConstantValue()
padding = .random()
anchor = CGPoint(
x: .random(in: 0...100),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class FeatureExtensionValueTests: XCTestCase {
func testInitializationWithWithNonNilValues() throws {
let value = Int.random(in: 0..<100)
let features = Array.random(withLength: .random(in: 0..<10)) { () -> Feature in
var feature = Feature(geometry: Point(.random()))
var feature = Feature(geometry: Point(.testConstantValue()))
feature.identifier = .number(Double(Int.random(in: (.min)...(.max))))
return feature
}
Expand All @@ -38,7 +38,7 @@ final class FeatureExtensionValueTests: XCTestCase {

func testNonNilFeatures() {
let features = Array.random(withLength: .random(in: 0..<10)) { () -> Feature in
var feature = Feature(geometry: Point(.random()))
var feature = Feature(geometry: Point(.testConstantValue()))
feature.identifier = .number(Double(Int.random(in: (.min)...(.max))))
return feature
}.map(MapboxCommon.Feature.init(_:))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class OfflineRegionGeometryDefinitionTests: XCTestCase {
override func setUp() {
super.setUp()
styleURL = .randomASCII(withLength: .random(in: 0...50))
coordinate = .random()
coordinate = .testConstantValue()
minZoom = .random(in: 0..<10)
maxZoom = .random(in: 10...20)
// swiftlint:disable:next syntactic_sugar
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

final class FeatureTests: XCTestCase {

let geometry = Geometry.point(Point(.random()))
let geometry = Geometry.point(Point(.testConstantValue()))

func testInitializingTurfFeatureFromCommonFeatureNilIdentifier() throws {
let commonFeature = MapboxCommon.Feature(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CLLocationCoordinate2DCodableTest: XCTestCase {
}

func testCoreLocationCoordinatesConversion() throws {
let coordinates: CLLocationCoordinate2D = .random()
let coordinates: CLLocationCoordinate2D = .testConstantValue()
let codableCoordinates = CLLocationCoordinate2DCodable(coordinates)

XCTAssertEqual(coordinates, codableCoordinates.coordinates)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class CoordinateInterpolatorTests: XCTestCase {
}

func testInterpolate() throws {
let from = CLLocationCoordinate2D.random()
let to = CLLocationCoordinate2D.random()
let from = CLLocationCoordinate2D.testConstantValue()
let to = CLLocationCoordinate2D.testConstantValue()
let fraction = Double.random(in: 0...1)
doubleInterpolator.interpolateStub.defaultReturnValue = .random(in: 0..<100)
longitudeInterpolator.interpolateStub.defaultReturnValue = .random(in: 0..<100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class MockCoordinateInterpolator: CoordinateInterpolatorProtocol {
var to: CLLocationCoordinate2D
var fraction: Double
}
let interpolateStub = Stub<InterpolateParams, CLLocationCoordinate2D>(defaultReturnValue: .random())
let interpolateStub = Stub<InterpolateParams, CLLocationCoordinate2D>(defaultReturnValue: .testConstantValue())
func interpolate(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D, fraction: Double) -> CLLocationCoordinate2D {
interpolateStub.call(with: .init(from: from, to: to, fraction: fraction))
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/MapboxMapsTests/Foundation/MapboxMapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ final class MapboxMapTests: XCTestCase {
tileId: nil,
timestamp: Date())
let cameraChanged = CameraChanged(
cameraState: CameraState(center: .random(), padding: .random(), zoom: 0, bearing: 0, pitch: 0),
cameraState: CameraState(center: .testConstantValue(), padding: .random(), zoom: 0, bearing: 0, pitch: 0),
timestamp: Date())

checkEvent(\.onMapIdle, \.onMapIdle, value: MapIdle(timestamp: Date()))
Expand Down
2 changes: 1 addition & 1 deletion Tests/MapboxMapsTests/Foundation/Mocks/MockMapboxMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class MockMapboxMap: MapboxMapProtocol {
setCameraStub.call(with: cameraOptions)
}

let coordinateForPointStub = Stub<CGPoint, CLLocationCoordinate2D>(defaultReturnValue: .random())
let coordinateForPointStub = Stub<CGPoint, CLLocationCoordinate2D>(defaultReturnValue: .testConstantValue())
func coordinate(for point: CGPoint) -> CLLocationCoordinate2D {
coordinateForPointStub.call(with: point)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension CameraOptions {
}
static func random() -> Self {
return CameraOptions(
center: .random(),
center: .testConstantValue(),
padding: .random(),
anchor: .random(),
zoom: .random(in: 0...20),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension CameraState {

static func random() -> Self {
return CameraState(
center: .random(),
center: .testConstantValue(),
padding: .random(),
zoom: .random(in: 0...20),
bearing: .random(in: 0..<360),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ final class StyleSourceManagerTests: XCTestCase {
// given
let sourceId = String.randomASCII(withLength: 10)
let dataId = String.randomASCII(withLength: 11)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand All @@ -476,7 +476,7 @@ final class StyleSourceManagerTests: XCTestCase {
// given
let sourceId = String.randomASCII(withLength: 10)
let dataId = String.randomASCII(withLength: 11)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand All @@ -499,7 +499,7 @@ final class StyleSourceManagerTests: XCTestCase {
func testPartialUpdateAPIsDontCancelPreviousUpdates() throws {
// given
let sourceId = String.randomASCII(withLength: 10)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand All @@ -520,7 +520,7 @@ final class StyleSourceManagerTests: XCTestCase {
func testFullUpdateAPIsCancelsAllPreviousUpdates() throws {
// given
let sourceId = String.randomASCII(withLength: 10)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand Down
8 changes: 4 additions & 4 deletions Tests/MapboxMapsTests/Foundation/Style/StyleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -422,13 +422,13 @@ final class StyleManagerTests: XCTestCase {
styleManager.invalidateStyleCustomGeometrySourceRegionStub.defaultReturnValue = Expected(value: NSNull())
XCTAssertNoThrow(try style.invalidateCustomGeometrySourceRegion(
forSourceId: "dummy-source-id",
bounds: CoordinateBounds(southwest: .random(), northeast: .random()))
bounds: CoordinateBounds(southwest: .testConstantValue(), northeast: .testConstantValue()))
)

styleManager.invalidateStyleCustomGeometrySourceRegionStub.defaultReturnValue = Expected(error: "Cannot invalidate custom geometry source tile")
XCTAssertThrowsError(try style.invalidateCustomGeometrySourceRegion(
forSourceId: "dummy-source-id",
bounds: CoordinateBounds(southwest: .random(), northeast: .random()))
bounds: CoordinateBounds(southwest: .testConstantValue(), northeast: .testConstantValue()))
)
}

Expand Down Expand Up @@ -623,7 +623,7 @@ final class StyleManagerTests: XCTestCase {
// given
let sourceId = String.randomASCII(withLength: 10)
let dataId = String.randomASCII(withLength: 11)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand All @@ -643,7 +643,7 @@ final class StyleManagerTests: XCTestCase {
// given
let sourceId = String.randomASCII(withLength: 10)
let dataId = String.randomASCII(withLength: 11)
let point = Point(.random())
let point = Point(.testConstantValue())
let featureIdentifier = Double.random(in: 0...1000)
var feature = Feature.init(geometry: point.geometry)
feature.identifier = .number(featureIdentifier)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import CoreLocation

extension CLLocationCoordinate2D {
static func random() -> Self {
static func testConstantValue() -> Self {
return CLLocationCoordinate2D(
latitude: .random(in: -90...90),
longitude: .random(in: -180..<180))
latitude: .testConstantValue(),
longitude: .testConstantValue())
}
}
8 changes: 8 additions & 0 deletions Tests/MapboxMapsTests/Helpers/Random/UIColor+Random.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ extension UIColor {
blue: .random(in: 0...1),
alpha: .random(in: 0...1))
}

static func testConstantValue() -> UIColor {
return UIColor(
red: 0.1,
green: 0.2,
blue: 0,
alpha: 1)
}
}
76 changes: 64 additions & 12 deletions Tests/MapboxMapsTests/Location/AppleLocationProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,24 @@ final class AppleLocationProviderTests: XCTestCase {
}
#endif

func testAddingALocationConsumerDoesNotRequestWhenInUseAuthorizationForOtherStatuses() {
locationManager.compatibleAuthorizationStatus = [.restricted, .denied, .authorizedWhenInUse].randomElement()!
func testAddingALocationConsumerDoesNotRequestWhenInUseAuthorizationForOtherStatusesRestricted() {
locationManager.compatibleAuthorizationStatus = .restricted

_ = locationProvider.onLocationUpdate.observe { _ in }

XCTAssertTrue(locationManager.requestWhenInUseAuthorizationStub.invocations.isEmpty)
}

func testAddingALocationConsumerDoesNotRequestWhenInUseAuthorizationForOtherStatusesDenied() {
locationManager.compatibleAuthorizationStatus = .denied

_ = locationProvider.onLocationUpdate.observe { _ in }

XCTAssertTrue(locationManager.requestWhenInUseAuthorizationStub.invocations.isEmpty)
}

func testAddingALocationConsumerDoesNotRequestWhenInUseAuthorizationForOtherStatusesAuthWhenInUse() {
locationManager.compatibleAuthorizationStatus = .authorizedWhenInUse

_ = locationProvider.onLocationUpdate.observe { _ in }

Expand Down Expand Up @@ -191,8 +207,8 @@ final class AppleLocationProviderTests: XCTestCase {
XCTAssertEqual(observedViaObjectAPI, observed)
XCTAssertEqual(locationProvider.latestHeading, nil)

let heading1 = Heading.random()
let heading2 = Heading.random()
let heading1 = Heading.testConstantValue()
let heading2 = Heading.testConstantValue()

let clHeading1 = MockHeading()
clHeading1.trueHeadingStub.defaultReturnValue = heading1.direction
Expand All @@ -215,7 +231,7 @@ final class AppleLocationProviderTests: XCTestCase {
#endif

func testConsumersAreNotifiedOfNewAccuracyAuthorizationsAfterLatestLocationIsUpdated() {
let coordinate = CLLocationCoordinate2D.random()
let coordinate = CLLocationCoordinate2D.testConstantValue()
let clLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
let accuracyAuthorization = CLAccuracyAuthorization.reducedAccuracy

Expand Down Expand Up @@ -306,7 +322,7 @@ final class AppleLocationProviderTests: XCTestCase {

func testDidChangeAuthorizationNotifiesDelegateIfAccuracyAuthorizationChanged() {
let accuracyAuthorizationValues: [CLAccuracyAuthorization] = [.fullAccuracy, .reducedAccuracy]
let initialIndex = Int.random(in: 0...1)
let initialIndex = 0
let changedIndex = (initialIndex + 1) % 2 // the other one
locationManager.compatibleAccuracyAuthorization = accuracyAuthorizationValues[initialIndex]
locationProvider = AppleLocationProvider(
Expand All @@ -326,8 +342,8 @@ final class AppleLocationProviderTests: XCTestCase {
XCTAssertEqual(delegate.didChangeAccuracyAuthorizationStub.invocations.first?.parameters.accuracyAuthorization, locationManager.compatibleAccuracyAuthorization)
}

func testDidChangeAuthorizationDoesNotNotifyDelegateIfAccuracyAuthorizationDidNotChange() {
locationManager.compatibleAccuracyAuthorization = [.fullAccuracy, .reducedAccuracy].randomElement()!
func testDidChangeAuthorizationDoesNotNotifyDelegateIfAccuracyAuthorizationDidNotChangeFull() {
locationManager.compatibleAccuracyAuthorization = .fullAccuracy
locationProvider = AppleLocationProvider(
locationManager: locationManager,
interfaceOrientation: $interfaceOrientation,
Expand All @@ -342,6 +358,22 @@ final class AppleLocationProviderTests: XCTestCase {
XCTAssertEqual(delegate.didChangeAccuracyAuthorizationStub.invocations.count, 0)
}

func testDidChangeAuthorizationDoesNotNotifyDelegateIfAccuracyAuthorizationDidNotChangeReduced() {
locationManager.compatibleAccuracyAuthorization = .reducedAccuracy
locationProvider = AppleLocationProvider(
locationManager: locationManager,
interfaceOrientation: $interfaceOrientation,
mayRequestWhenInUseAuthorization: true,
locationManagerDelegateProxy: locationManagerDelegateProxy)
delegate = MockLocationProducerDelegate()
locationProvider.delegate = delegate
addNoopLocationObserver()

locationProvider.locationManagerDidChangeAuthorization(CLLocationManager())

XCTAssertEqual(delegate.didChangeAccuracyAuthorizationStub.invocations.count, 0)
}

func testRequestsTemporaryFullAccuracyAuthorizationWhenAccuracyIsReduced() {
addNoopLocationObserver()
locationManager.compatibleAuthorizationStatus = .authorizedWhenInUse
Expand All @@ -358,9 +390,29 @@ final class AppleLocationProviderTests: XCTestCase {
}
}

func testDoesNotRequestTemporaryFullAccuracyAuthorizationIfPermissionsNotGranted() {
func testDoesNotRequestTemporaryFullAccuracyAuthorizationIfPermissionsNotGrantedNotDetermined() {
addNoopLocationObserver()
locationManager.compatibleAuthorizationStatus = .notDetermined
locationManager.compatibleAccuracyAuthorization = .reducedAccuracy

locationProvider.locationManagerDidChangeAuthorization(CLLocationManager())

XCTAssertTrue(locationManager.requestTemporaryFullAccuracyAuthorizationStub.invocations.isEmpty)
}

func testDoesNotRequestTemporaryFullAccuracyAuthorizationIfPermissionsNotGrantedRestricted() {
addNoopLocationObserver()
locationManager.compatibleAuthorizationStatus = .restricted
locationManager.compatibleAccuracyAuthorization = .reducedAccuracy

locationProvider.locationManagerDidChangeAuthorization(CLLocationManager())

XCTAssertTrue(locationManager.requestTemporaryFullAccuracyAuthorizationStub.invocations.isEmpty)
}

func testDoesNotRequestTemporaryFullAccuracyAuthorizationIfPermissionsNotGrantedDenied() {
addNoopLocationObserver()
locationManager.compatibleAuthorizationStatus = [.notDetermined, .restricted, .denied].randomElement()!
locationManager.compatibleAuthorizationStatus = .denied
locationManager.compatibleAccuracyAuthorization = .reducedAccuracy

locationProvider.locationManagerDidChangeAuthorization(CLLocationManager())
Expand Down Expand Up @@ -434,8 +486,8 @@ final class AppleLocationProviderTests: XCTestCase {

func testOptions() {
// given
let options = AppleLocationProvider.Options(distanceFilter: .random(in: 0...10_000),
desiredAccuracy: .random(in: 0...1000),
let options = AppleLocationProvider.Options(distanceFilter: 45890,
desiredAccuracy: 783,
activityType: .other)
// when
locationProvider.options = options
Expand Down
32 changes: 32 additions & 0 deletions Tests/MapboxMapsTests/Location/Fixtures/Location+Fixtures.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import MapboxMaps
import MapboxCommon

extension Location {
static func testConstantValue() -> Location {
Location(
coordinate: .testConstantValue(),
timestamp: Date(),
bearing: .testConstantValue(),
bearingAccuracy: .testConstantValue()
)
}
}

extension Heading {
static func testConstantValue() -> Heading {
Heading(direction: .testConstantValue(),
accuracy: .testSourceValue())
}
}

extension PuckRenderingData {
static func testConstantValue() -> PuckRenderingData {
PuckRenderingData(location: .testConstantValue(), heading: .testConstantValue())
}
}

extension CLAccuracyAuthorization {
static func testConstantValue() -> Self {
return .reducedAccuracy
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class LocationInterpolatorTests: XCTestCase {
var result = me.interpolate(from: [], to: [], fraction: 1)
XCTAssertEqual(result, [])

let l1 = Location.random()
let l1 = Location.testConstantValue()
result = me.interpolate(from: [l1], to: [], fraction: 1)
XCTAssertEqual(result, [])

Expand Down
Loading

0 comments on commit 7016454

Please sign in to comment.