Skip to content

Instantly share code, notes, and snippets.

@JonnyBeeGod
Created December 25, 2019 06:41
Show Gist options
  • Save JonnyBeeGod/acb25823ab9511657e2e9c434898c08a to your computer and use it in GitHub Desktop.
Save JonnyBeeGod/acb25823ab9511657e2e9c434898c08a to your computer and use it in GitHub Desktop.
This code allows for testing UNNotificationCenter and UNNotificationSettings
func testNotifications() {
// map all authorizationStatus with expected Result
let authorizationStatusMap: [UNAuthorizationStatus: Int] = [.authorized: 1, .denied: 0, .notDetermined: 0, .provisional: 1]
UNNotificationSettings.swizzleAuthorizationStatus()
authorizationStatusMap.forEach { (key: UNAuthorizationStatus, value: Int) in
UNNotificationSettings.fakeAuthorizationStatus = key
let mockCenter = UserNotificationCenterMock()
let mockCoder = MockNSCoder()
mockCenter.settingsCoder = mockCoder
let mockDelegate = MockCountdownDelegate()
let defaults = MockUserDefaults()
let timer = Countdown(delegate: mockDelegate, defaults: defaults, userNotificationCenter: mockCenter)
XCTAssertEqual(mockCenter.pendingNotifications.count, 0)
let mockContent = UNMutableNotificationContent()
mockContent.title = "title"
mockContent.body = "body"
timer.startCountdown(with: Date().addingTimeInterval(1), with: mockContent)
XCTAssertEqual(mockCenter.pendingNotifications.count, value)
timer.skipRunningCountdown()
XCTAssertEqual(mockCenter.pendingNotifications.count, 0)
}
}
extension UNNotificationSettings {
static var fakeAuthorizationStatus: UNAuthorizationStatus = .authorized
static func swizzleAuthorizationStatus() {
let originalMethod = class_getInstanceMethod(self, #selector(getter: authorizationStatus))!
let swizzledMethod = class_getInstanceMethod(self, #selector(getter: swizzledAuthorizationStatus))!
method_exchangeImplementations(originalMethod, swizzledMethod)
}
@objc var swizzledAuthorizationStatus: UNAuthorizationStatus {
return Self.fakeAuthorizationStatus
}
}
class UserNotificationCenterMock: UserNotificationCenter {
var pendingNotifications = [UNNotificationRequest]()
var settingsCoder = MockNSCoder()
func getNotificationSettings(completionHandler: @escaping (UNNotificationSettings) -> Void) {
let settings = UNNotificationSettings(coder: settingsCoder)!
completionHandler(settings)
}
func removeAllPendingNotificationRequests() {
pendingNotifications.removeAll()
}
func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)?) {
pendingNotifications.append(request)
completionHandler?(nil)
}
}
class MockNSCoder: NSCoder {
var authorizationStatus = UNAuthorizationStatus.authorized.rawValue
override func decodeInt64(forKey key: String) -> Int64 {
return Int64(authorizationStatus)
}
override func decodeBool(forKey key: String) -> Bool {
return true
}
}
protocol UserNotificationCenter {
func getNotificationSettings(completionHandler: @escaping (UNNotificationSettings) -> Void)
func removeAllPendingNotificationRequests()
func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)?)
}
extension UNUserNotificationCenter: UserNotificationCenter {}
@Pavel-Laukhin
Copy link

Could you, please, explain where we can get a 'Countdown' and 'MockCountdownDelegate'? For what purpose we should use 'MockUserDefaults'?

@JonnyBeeGod
Copy link
Author

Hi @Pavel-Laukhin, this is an example from production code of a unit test mocking UNNotificationCenter. The Countdown is the class under test but ultimately it doesn't matter what it does.
The interesting parts are how to setup the UserNotificationCenterMock and the UNNotificationSettings extensions where the fake authorization state will be returned using method swizzling.
However as this code is already quite old I am not sure whether this even still works.

@Pavel-Laukhin
Copy link

Hi @Pavel-Laukhin, this is an example from production code of a unit test mocking UNNotificationCenter. The Countdown is the class under test but ultimately it doesn't matter what it does. The interesting parts are how to setup the UserNotificationCenterMock and the UNNotificationSettings extensions where the fake authorization state will be returned using method swizzling. However as this code is already quite old I am not sure whether this even still works.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment