301 lines
11 KiB
Swift
301 lines
11 KiB
Swift
//
|
|
// CoreLocationProviderTests.swift
|
|
// suixinkanTests
|
|
//
|
|
|
|
import CoreLocation
|
|
import XCTest
|
|
@testable import suixinkan
|
|
|
|
/// Core Location fallback 的授权、定位与逆地理编码测试。
|
|
@MainActor
|
|
final class CoreLocationProviderTests: XCTestCase {
|
|
|
|
func testPrivacyAgreementIsRequiredBeforeLocation() async {
|
|
let context = makeContext(privacyAccepted: false)
|
|
|
|
await assertLocationError(.privacyNotAccepted) {
|
|
_ = try await context.provider.requestCoordinate()
|
|
}
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 0)
|
|
}
|
|
|
|
func testDeniedAuthorizationReturnsPermissionError() async {
|
|
let context = makeContext(authorizationStatus: .denied)
|
|
|
|
await assertLocationError(.permissionDenied) {
|
|
_ = try await context.provider.requestCoordinate()
|
|
}
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 0)
|
|
}
|
|
|
|
func testNotDeterminedAuthorizationContinuesAfterGrant() async throws {
|
|
let coordinate = CLLocationCoordinate2D(latitude: 30.2741, longitude: 120.1551)
|
|
let context = makeContext(
|
|
authorizationStatus: .notDetermined,
|
|
requestedAuthorizationStatus: .authorizedWhenInUse,
|
|
locationResult: .success(CLLocation(
|
|
latitude: coordinate.latitude,
|
|
longitude: coordinate.longitude
|
|
))
|
|
)
|
|
|
|
let result = try await context.provider.requestCoordinate(
|
|
desiredAccuracy: kCLLocationAccuracyHundredMeters
|
|
)
|
|
|
|
XCTAssertEqual(context.manager.requestAuthorizationCallCount, 1)
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 1)
|
|
XCTAssertEqual(context.manager.desiredAccuracy, kCLLocationAccuracyHundredMeters)
|
|
XCTAssertEqual(result.latitude, coordinate.latitude, accuracy: 0.000_001)
|
|
XCTAssertEqual(result.longitude, coordinate.longitude, accuracy: 0.000_001)
|
|
}
|
|
|
|
func testLocationFailureUsesExistingBusinessError() async {
|
|
let context = makeContext(locationResult: .failure)
|
|
|
|
await assertLocationError(.locationFailed) {
|
|
_ = try await context.provider.requestCoordinate()
|
|
}
|
|
}
|
|
|
|
func testSnapshotContainsReverseGeocodedAddress() async throws {
|
|
let context = makeContext(
|
|
locationResult: .success(CLLocation(latitude: 31.2304, longitude: 121.4737)),
|
|
geocodedAddress: "上海市黄浦区"
|
|
)
|
|
|
|
let snapshot = try await context.provider.requestSnapshot()
|
|
|
|
XCTAssertEqual(snapshot.latitude, 31.2304, accuracy: 0.000_001)
|
|
XCTAssertEqual(snapshot.longitude, 121.4737, accuracy: 0.000_001)
|
|
XCTAssertEqual(snapshot.address, "上海市黄浦区")
|
|
XCTAssertEqual(context.geocoder.requestedCoordinates.count, 1)
|
|
}
|
|
|
|
func testSnapshotFallsBackToEmptyAddressWhenGeocoderHasNoResult() async throws {
|
|
let context = makeContext(
|
|
locationResult: .success(CLLocation(latitude: 39.9042, longitude: 116.4074)),
|
|
geocodedAddress: ""
|
|
)
|
|
|
|
let snapshot = try await context.provider.requestSnapshot()
|
|
|
|
XCTAssertEqual(snapshot.address, "")
|
|
}
|
|
|
|
/// 系统始终不回调时结束本次定位,清理后可再次正常获取坐标。
|
|
func testLocationTimeoutReleasesRequestAndAllowsRetry() async throws {
|
|
let context = makeContext(locationResult: .noCallback, locationTimeoutNanoseconds: 30_000_000)
|
|
|
|
await assertLocationError(.locationFailed) {
|
|
_ = try await context.provider.requestCoordinate()
|
|
}
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 1)
|
|
XCTAssertEqual(context.manager.stopUpdatingLocationCallCount, 1)
|
|
|
|
context.manager.locationResult = .success(CLLocation(latitude: 31.2, longitude: 121.4))
|
|
let coordinate = try await context.provider.requestCoordinate()
|
|
XCTAssertEqual(coordinate.latitude, 31.2, accuracy: 0.000_001)
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 2)
|
|
XCTAssertEqual(context.manager.stopUpdatingLocationCallCount, 2)
|
|
}
|
|
|
|
/// 离页取消会立即恢复等待并停止定位,不占用下一次请求。
|
|
func testCancellationStopsPendingLocationAndAllowsRetry() async throws {
|
|
let context = makeContext(locationResult: .noCallback)
|
|
let task = Task { try await context.provider.requestCoordinate() }
|
|
try await waitUntil { context.manager.requestLocationCallCount == 1 }
|
|
|
|
task.cancel()
|
|
do {
|
|
_ = try await task.value
|
|
XCTFail("取消定位后不应成功返回坐标")
|
|
} catch {
|
|
XCTAssertTrue(error is CancellationError)
|
|
}
|
|
XCTAssertEqual(context.manager.stopUpdatingLocationCallCount, 1)
|
|
|
|
context.manager.locationResult = .success(CLLocation(latitude: 30.3, longitude: 120.2))
|
|
let coordinate = try await context.provider.requestCoordinate()
|
|
XCTAssertEqual(coordinate.longitude, 120.2, accuracy: 0.000_001)
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 2)
|
|
}
|
|
|
|
/// 授权弹窗尚未响应时取消,不残留等待者或在迟到授权后发起定位。
|
|
func testCancellationWhileWaitingForPermissionAllowsNewRequest() async throws {
|
|
let context = makeContext(authorizationStatus: .notDetermined, requestedAuthorizationStatus: .notDetermined)
|
|
let task = Task { try await context.provider.requestCoordinate() }
|
|
try await waitUntil { context.manager.requestAuthorizationCallCount == 1 }
|
|
|
|
task.cancel()
|
|
do {
|
|
_ = try await task.value
|
|
XCTFail("取消授权等待后不应继续定位")
|
|
} catch {
|
|
XCTAssertTrue(error is CancellationError)
|
|
}
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 0)
|
|
|
|
context.manager.requestedAuthorizationStatus = .authorizedWhenInUse
|
|
_ = try await context.provider.requestCoordinate()
|
|
XCTAssertEqual(context.manager.requestAuthorizationCallCount, 2)
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 1)
|
|
}
|
|
|
|
/// 已取消任务不能在注册取消处理之前启动授权或定位。
|
|
func testAlreadyCancelledTaskDoesNotStartLocation() async {
|
|
let context = makeContext(authorizationStatus: .notDetermined)
|
|
let task = Task { try await context.provider.requestCoordinate() }
|
|
task.cancel()
|
|
|
|
do {
|
|
_ = try await task.value
|
|
XCTFail("已取消任务不应返回坐标")
|
|
} catch {
|
|
XCTAssertTrue(error is CancellationError)
|
|
}
|
|
XCTAssertEqual(context.manager.requestAuthorizationCallCount, 0)
|
|
XCTAssertEqual(context.manager.requestLocationCallCount, 0)
|
|
}
|
|
|
|
private func makeContext(
|
|
privacyAccepted: Bool = true,
|
|
authorizationStatus: CLAuthorizationStatus = .authorizedWhenInUse,
|
|
requestedAuthorizationStatus: CLAuthorizationStatus = .authorizedWhenInUse,
|
|
locationResult: FakeCoreLocationManager.LocationResult = .success(
|
|
CLLocation(latitude: 39.9042, longitude: 116.4074)
|
|
),
|
|
geocodedAddress: String = "北京市东城区",
|
|
locationTimeoutNanoseconds: UInt64 = 10_000_000_000
|
|
) -> TestContext {
|
|
let suiteName = "CoreLocationProviderTests.\(UUID().uuidString)"
|
|
let defaults = UserDefaults(suiteName: suiteName)!
|
|
defaults.removePersistentDomain(forName: suiteName)
|
|
let appStore = AppStore(defaults: defaults)
|
|
appStore.session.privacyAgreementAccepted = privacyAccepted
|
|
|
|
let manager = FakeCoreLocationManager(
|
|
authorizationStatus: authorizationStatus,
|
|
requestedAuthorizationStatus: requestedAuthorizationStatus,
|
|
locationResult: locationResult
|
|
)
|
|
let geocoder = FakeLocationAddressGeocoder(address: geocodedAddress)
|
|
let provider = CoreLocationProvider(
|
|
appStore: appStore,
|
|
manager: manager,
|
|
geocoder: geocoder,
|
|
locationTimeoutNanoseconds: locationTimeoutNanoseconds
|
|
)
|
|
return TestContext(provider: provider, manager: manager, geocoder: geocoder)
|
|
}
|
|
|
|
private func assertLocationError(
|
|
_ expected: LocationProviderError,
|
|
operation: () async throws -> Void
|
|
) async {
|
|
do {
|
|
try await operation()
|
|
XCTFail("Expected \(expected)")
|
|
} catch {
|
|
XCTAssertEqual(error as? LocationProviderError, expected)
|
|
}
|
|
}
|
|
|
|
/// 等待替身请求开始再取消,避免用固定睡眠决定请求是否已挂起。
|
|
private func waitUntil(_ condition: () -> Bool) async throws {
|
|
for _ in 0..<100 {
|
|
if condition() { return }
|
|
try await Task.sleep(nanoseconds: 1_000_000)
|
|
}
|
|
XCTFail("定位替身未收到预期请求")
|
|
throw NSError(domain: "CoreLocationProviderTests", code: 1)
|
|
}
|
|
}
|
|
|
|
/// Core Location fallback 测试依赖集合。
|
|
@MainActor
|
|
private struct TestContext {
|
|
let provider: CoreLocationProvider
|
|
let manager: FakeCoreLocationManager
|
|
let geocoder: FakeLocationAddressGeocoder
|
|
}
|
|
|
|
/// 可控的 `CLLocationManager` 替身。
|
|
@MainActor
|
|
private final class FakeCoreLocationManager: CoreLocationManaging {
|
|
|
|
/// 单次定位请求的模拟结果。
|
|
enum LocationResult {
|
|
case success(CLLocation)
|
|
case failure
|
|
case noCallback
|
|
}
|
|
|
|
var authorizationStatus: CLAuthorizationStatus
|
|
var desiredAccuracy: CLLocationAccuracy = kCLLocationAccuracyBest
|
|
weak var delegate: CLLocationManagerDelegate?
|
|
private(set) var requestAuthorizationCallCount = 0
|
|
private(set) var requestLocationCallCount = 0
|
|
private(set) var stopUpdatingLocationCallCount = 0
|
|
|
|
var requestedAuthorizationStatus: CLAuthorizationStatus
|
|
var locationResult: LocationResult
|
|
|
|
init(
|
|
authorizationStatus: CLAuthorizationStatus,
|
|
requestedAuthorizationStatus: CLAuthorizationStatus,
|
|
locationResult: LocationResult
|
|
) {
|
|
self.authorizationStatus = authorizationStatus
|
|
self.requestedAuthorizationStatus = requestedAuthorizationStatus
|
|
self.locationResult = locationResult
|
|
}
|
|
|
|
func requestWhenInUseAuthorization() {
|
|
requestAuthorizationCallCount += 1
|
|
authorizationStatus = requestedAuthorizationStatus
|
|
delegate?.locationManagerDidChangeAuthorization?(CLLocationManager())
|
|
}
|
|
|
|
func requestLocation() {
|
|
requestLocationCallCount += 1
|
|
let callbackManager = CLLocationManager()
|
|
switch locationResult {
|
|
case let .success(location):
|
|
delegate?.locationManager?(callbackManager, didUpdateLocations: [location])
|
|
case .failure:
|
|
delegate?.locationManager?(
|
|
callbackManager,
|
|
didFailWithError: CLError(.locationUnknown)
|
|
)
|
|
case .noCallback:
|
|
break
|
|
}
|
|
}
|
|
|
|
/// 记录被测服务是否释放超时或取消的单次请求。
|
|
func stopUpdatingLocation() {
|
|
stopUpdatingLocationCallCount += 1
|
|
}
|
|
}
|
|
|
|
/// 可控的逆地理编码替身。
|
|
@MainActor
|
|
private final class FakeLocationAddressGeocoder: LocationAddressGeocoding {
|
|
private(set) var requestedCoordinates: [CLLocationCoordinate2D] = []
|
|
private let address: String
|
|
|
|
init(address: String) {
|
|
self.address = address
|
|
}
|
|
|
|
func reverseGeocode(latitude: Double, longitude: Double) async -> String {
|
|
requestedCoordinates.append(CLLocationCoordinate2D(
|
|
latitude: latitude,
|
|
longitude: longitude
|
|
))
|
|
return address
|
|
}
|
|
}
|