Skip to content

Instantly share code, notes, and snippets.

@Jxrgxn
Created February 10, 2023 12:28
Show Gist options
  • Save Jxrgxn/5824d0352799a416b877b722ef7c122a to your computer and use it in GitHub Desktop.
Save Jxrgxn/5824d0352799a416b877b722ef7c122a to your computer and use it in GitHub Desktop.
import XCTest
import CoreLocation
class RBLocationManagerTests: XCTestCase {
var locationManager: RBLocationManager!
override func setUp() {
super.setUp()
locationManager = RBLocationManager.shared
}
func testRequestLocationsPermissions() {
let authorizationExpectation = expectation(description: "Location authorization")
locationManager.requestLocationsPermissions()
locationManager.locationManager.requestWhenInUseAuthorization()
locationManager.locationManager.requestAlwaysAuthorization()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
XCTAssertEqual(self.locationManager.locationManager.authorizationStatus, CLAuthorizationStatus.authorizedWhenInUse)
authorizationExpectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}
func testRequestAlwaysAuthorization() {
let authorizationExpectation = expectation(description: "Location authorization")
locationManager.requestAlwaysAuthorization()
locationManager.locationManager.requestAlwaysAuthorization()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
XCTAssertEqual(self.locationManager.locationManager.authorizationStatus, CLAuthorizationStatus.authorizedAlways)
authorizationExpectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}
func testValidLocation() {
let locationExpectation = expectation(description: "Location validation")
locationManager.startLocationServices()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let validLocation = self.locationManager.validLocation()
XCTAssertNotNil(validLocation)
locationExpectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}
func testNeedsLocationPermissions() {
XCTAssertFalse(locationManager.needsLocationPermissions())
}
func testUserLocationPermissions() {
XCTAssertEqual(locationManager.userLocationPermissions(), "when_in_use")
}
func testStartLocationServices() {
let authorizationExpectation = expectation(description: "Location authorization")
locationManager.startLocationServices()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
XCTAssertEqual(self.locationManager.locationManager.authorizationStatus, CLAuthorizationStatus.authorizedAlways)
authorizationExpectation.fulfill()
}
waitForExpectations(timeout: 2.0, handler: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment