mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Added unit tests for NotificationManager (#376)
* NotificationManager tests * More unit tests * More tests, moved classes to separate files * Removed warnings * UserNotificationCenterProtocol as AnyObject * Reverted back Package.resolved
This commit is contained in:
committed by
GitHub
parent
71f1f044c7
commit
a9f2e1bdf8
@@ -302,7 +302,7 @@ class AppCoordinator: AppCoordinatorProtocol {
|
||||
.sink { [weak self] callback in
|
||||
switch callback {
|
||||
case .registeredNotifications(let deviceToken):
|
||||
self?.notificationManager?.register(with: deviceToken)
|
||||
self?.notificationManager?.register(with: deviceToken, completion: nil)
|
||||
case .failedToRegisteredNotifications(let error):
|
||||
self?.notificationManager?.registrationFailed(with: error)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ class MockNotificationManager: NotificationManagerProtocol {
|
||||
delegate?.authorizationStatusUpdated(self, granted: false)
|
||||
}
|
||||
|
||||
func register(with deviceToken: Data) { }
|
||||
func register(with deviceToken: Data, completion: ((Bool) -> Void)? = nil) { }
|
||||
|
||||
func registrationFailed(with error: Error) { }
|
||||
|
||||
|
||||
@@ -19,11 +19,12 @@ import UIKit
|
||||
import UserNotifications
|
||||
|
||||
class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
private let notificationCenter = UNUserNotificationCenter.current()
|
||||
private let notificationCenter: UserNotificationCenterProtocol
|
||||
private let clientProxy: ClientProxyProtocol
|
||||
|
||||
init(clientProxy: ClientProxyProtocol) {
|
||||
init(clientProxy: ClientProxyProtocol, notificationCenter: UserNotificationCenterProtocol = UNUserNotificationCenter.current()) {
|
||||
self.clientProxy = clientProxy
|
||||
self.notificationCenter = notificationCenter
|
||||
super.init()
|
||||
}
|
||||
|
||||
@@ -45,22 +46,25 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
options: [])
|
||||
notificationCenter.setNotificationCategories([replyCategory])
|
||||
notificationCenter.delegate = self
|
||||
Task {
|
||||
do {
|
||||
let granted = try await notificationCenter.requestAuthorization(options: [.alert, .sound, .badge])
|
||||
MXLog.debug("[NotificationManager] permission granted: \(granted)")
|
||||
|
||||
await MainActor.run {
|
||||
delegate?.authorizationStatusUpdated(self, granted: granted)
|
||||
}
|
||||
} catch {
|
||||
notificationCenter.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
|
||||
guard let self else {
|
||||
return
|
||||
}
|
||||
if let error {
|
||||
MXLog.debug("[NotificationManager] request authorization failed: \(error)")
|
||||
} else {
|
||||
MXLog.debug("[NotificationManager] permission granted: \(granted)")
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.authorizationStatusUpdated(self, granted: granted)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func register(with deviceToken: Data) {
|
||||
setPusher(with: deviceToken, clientProxy: clientProxy)
|
||||
func register(with deviceToken: Data, completion: ((Bool) -> Void)? = nil) {
|
||||
setPusher(with: deviceToken,
|
||||
clientProxy: clientProxy,
|
||||
completion: completion)
|
||||
}
|
||||
|
||||
func registrationFailed(with error: Error) { }
|
||||
@@ -74,17 +78,18 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
let request = UNNotificationRequest(identifier: ProcessInfo.processInfo.globallyUniqueString,
|
||||
content: content,
|
||||
trigger: nil)
|
||||
Task {
|
||||
do {
|
||||
try await notificationCenter.add(request)
|
||||
MXLog.debug("[NotificationManager] show local notification succeeded")
|
||||
} catch {
|
||||
notificationCenter.add(request) { error in
|
||||
if let error {
|
||||
MXLog.debug("[NotificationManager] show local notification failed: \(error)")
|
||||
} else {
|
||||
MXLog.debug("[NotificationManager] show local notification succeeded")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func setPusher(with deviceToken: Data, clientProxy: ClientProxyProtocol) {
|
||||
private func setPusher(with deviceToken: Data,
|
||||
clientProxy: ClientProxyProtocol,
|
||||
completion: ((Bool) -> Void)?) {
|
||||
Task {
|
||||
do {
|
||||
try await clientProxy.setPusher(pushkey: deviceToken.base64EncodedString(),
|
||||
@@ -106,8 +111,10 @@ class NotificationManager: NSObject, NotificationManagerProtocol {
|
||||
]
|
||||
])
|
||||
MXLog.debug("[NotificationManager] set pusher succeeded")
|
||||
completion?(true)
|
||||
} catch {
|
||||
MXLog.debug("[NotificationManager] set pusher failed: \(error)")
|
||||
completion?(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,3 +171,5 @@ extension NotificationManager: UNUserNotificationCenterDelegate {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension UNUserNotificationCenter: UserNotificationCenterProtocol { }
|
||||
|
||||
@@ -36,7 +36,7 @@ protocol NotificationManagerProtocol {
|
||||
var delegate: NotificationManagerDelegate? { get set }
|
||||
|
||||
func start()
|
||||
func register(with deviceToken: Data)
|
||||
func register(with deviceToken: Data, completion: ((Bool) -> Void)?)
|
||||
func registrationFailed(with error: Error)
|
||||
func showLocalNotification(with title: String, subtitle: String?)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UserNotifications
|
||||
|
||||
protocol UserNotificationCenterProtocol: AnyObject {
|
||||
var delegate: UNUserNotificationCenterDelegate? { get set }
|
||||
func add(_ request: UNNotificationRequest, withCompletionHandler completionHandler: ((Error?) -> Void)?)
|
||||
func requestAuthorization(options: UNAuthorizationOptions, completionHandler: @escaping (Bool, Error?) -> Void)
|
||||
func setNotificationCategories(_ categories: Set<UNNotificationCategory>)
|
||||
}
|
||||
Reference in New Issue
Block a user