Removing Delivered Notifications (#1305)

* completed

* changelog

* tests

* more tests

* better naming

* fix

* a bit more waiting time

* generated mocks

* xcodegen
This commit is contained in:
Mauro
2023-07-11 10:44:06 +02:00
committed by GitHub
parent 98dabe8500
commit 28fa1140b7
15 changed files with 245 additions and 8 deletions
@@ -22,6 +22,7 @@ class InvitesScreenViewModelTests: XCTestCase {
var viewModel: InvitesScreenViewModelProtocol!
var clientProxy: MockClientProxy!
var userSession: MockUserSession!
var mockNotificationCenter: NotificationCenterMock!
var context: InvitesScreenViewModelType.Context {
viewModel.context
@@ -30,6 +31,7 @@ class InvitesScreenViewModelTests: XCTestCase {
override func setUpWithError() throws {
clientProxy = MockClientProxy(userID: "@a:b.com")
userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider())
mockNotificationCenter = NotificationCenterMock()
}
func testEmptyState() async throws {
@@ -72,6 +74,14 @@ class InvitesScreenViewModelTests: XCTestCase {
context.send(viewAction: .decline(.init(roomDetails: details, isUnread: false)))
XCTAssertNotNil(context.alertInfo)
}
func testHasAppeared() async {
setupViewModel()
context.send(viewAction: .appeared)
await Task.yield()
XCTAssertEqual(mockNotificationCenter.postNameObjectReceivedArguments?.aName, .invitesScreenAppeared)
}
// MARK: - Private
@@ -85,6 +95,7 @@ class InvitesScreenViewModelTests: XCTestCase {
viewModel = InvitesScreenViewModel(userSession: userSession,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: ServiceLocator.shared.userIndicatorController)
userIndicatorController: ServiceLocator.shared.userIndicatorController,
notificationCenterProtocol: mockNotificationCenter)
}
}
@@ -14,9 +14,10 @@
// limitations under the License.
//
import Combine
import NotificationCenter
import XCTest
import Combine
@testable import ElementX
final class NotificationManagerTests: XCTestCase {
@@ -176,6 +177,32 @@ final class NotificationManagerTests: XCTestCase {
await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response)
XCTAssertTrue(notificationTappedDelegateCalled)
}
func test_MessageNotificationsRemoval() async throws {
// No interaction if the object is nil or of the wrong type
NotificationCenter.default.post(name: .roomMarkedAsRead, object: nil)
try await Task.sleep(for: .microseconds(200))
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 0)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsCallsCount, 0)
NotificationCenter.default.post(name: .roomMarkedAsRead, object: 1)
try await Task.sleep(for: .microseconds(200))
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 0)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsCallsCount, 0)
// The center calls the delivered and the removal functions when an id is passed
NotificationCenter.default.post(name: .roomMarkedAsRead, object: "RoomID")
try await Task.sleep(for: .microseconds(200))
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 1)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsCallsCount, 1)
}
func test_InvitesNotificationsRemoval() async throws {
NotificationCenter.default.post(name: .invitesScreenAppeared, object: nil)
try await Task.sleep(for: .microseconds(200))
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 1)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsCallsCount, 1)
}
}
extension NotificationManagerTests: NotificationManagerDelegate {
@@ -17,9 +17,20 @@
import Foundation
import UserNotifications
class UserNotificationCenterSpy: UserNotificationCenterProtocol {
final class UserNotificationCenterSpy: UserNotificationCenterProtocol {
weak var delegate: UNUserNotificationCenterDelegate?
var deliveredNotificationsCallsCount = 0
func deliveredNotifications() async -> [UNNotification] {
deliveredNotificationsCallsCount += 1
return []
}
var removeDeliveredNotificationsCallsCount = 0
func removeDeliveredNotifications(withIdentifiers identifiers: [String]) {
removeDeliveredNotificationsCallsCount += 1
}
var addRequest: UNNotificationRequest?
func add(_ request: UNNotificationRequest) async throws {
addRequest = request
@@ -349,6 +349,27 @@ class RoomScreenViewModelTests: XCTestCase {
await Task.yield()
XCTAssert(roomProxyMock.cancelSendTransactionIDCallsCount == 0)
}
func testMarkAsRead() async {
// Setup
let notificationCenterMock = NotificationCenterMock()
let timelineController = MockRoomTimelineController()
let roomProxyMock = RoomProxyMock(with: .init(displayName: ""))
let viewModel = RoomScreenViewModel(timelineController: timelineController,
mediaProvider: MockMediaProvider(),
roomProxy: roomProxyMock,
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
userIndicatorController: userIndicatorControllerMock,
notificationCenterProtocol: notificationCenterMock)
viewModel.context.send(viewAction: .markRoomAsRead)
await Task.yield()
XCTAssertEqual(notificationCenterMock.postNameObjectReceivedArguments?.aName, .roomMarkedAsRead)
let roomID = notificationCenterMock.postNameObjectReceivedArguments?.anObject as? String
XCTAssertEqual(roomID, roomProxyMock.id)
}
}
private extension TextRoomTimelineItem {