Skip to content

Instantly share code, notes, and snippets.

@JonnyBeeGod
Created December 25, 2019 06:41
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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 {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment