Stop relying on the NotificationCenter for removing delivered push notifications:

- this will improve the architecture but more importantly will remove some very flakey unit tests
This commit is contained in:
Stefan Ceriu
2024-04-12 17:46:37 +03:00
committed by Stefan Ceriu
parent 891315ef04
commit 4d513f9ed1
15 changed files with 113 additions and 147 deletions
@@ -85,14 +85,6 @@ 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
@@ -204,55 +204,6 @@ final class NotificationManagerTests: XCTestCase {
await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response)
XCTAssertTrue(notificationTappedDelegateCalled)
}
func test_MessageNotificationsRemoval() async throws {
notificationCenter.deliveredNotificationsClosure = {
XCTFail("deliveredNotifications should not be called if the object is nil or of the wrong type")
return []
}
// No interaction if the object is nil or of the wrong type
NotificationCenter.default.post(name: .roomMarkedAsRead, object: nil)
try? await Task.sleep(for: .seconds(0.25)) // Wait for the notification to be delivered
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 0)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsWithIdentifiersCallsCount, 0)
NotificationCenter.default.post(name: .roomMarkedAsRead, object: 1)
try? await Task.sleep(for: .seconds(0.25)) // Wait for the notification to be delivered
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 0)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsWithIdentifiersCallsCount, 0)
// The center calls the delivered and the removal functions when an id is passed
notificationCenter.deliveredNotificationsClosure = nil
notificationCenter.deliveredNotificationsReturnValue = []
let expectation = expectation(description: "Notification should be removed")
notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in
expectation.fulfill()
}
NotificationCenter.default.post(name: .roomMarkedAsRead, object: "RoomID")
try? await Task.sleep(for: .seconds(0.25)) // Wait for the notification to be delivered
await fulfillment(of: [expectation])
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 1)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsWithIdentifiersCallsCount, 1)
}
func test_InvitesNotificationsRemoval() async {
let expectation = expectation(description: "Notification should be removed")
notificationCenter.deliveredNotificationsReturnValue = []
notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in
expectation.fulfill()
}
NotificationCenter.default.post(name: .invitesScreenAppeared, object: nil)
try? await Task.sleep(for: .seconds(0.25)) // Wait for the notification to be delivered
await fulfillment(of: [expectation])
XCTAssertEqual(notificationCenter.deliveredNotificationsCallsCount, 1)
XCTAssertEqual(notificationCenter.removeDeliveredNotificationsWithIdentifiersCallsCount, 1)
}
}
extension NotificationManagerTests: NotificationManagerDelegate {
@@ -336,11 +336,6 @@ class RoomScreenViewModelTests: XCTestCase {
let arguments = timelineProxy.sendReadReceiptForTypeReceivedArguments
XCTAssertEqual(arguments?.eventID, "t3")
XCTAssertEqual(arguments?.type, .read)
// And the notifications should be cleared.
XCTAssertEqual(notificationCenter.postNameObjectReceivedArguments?.aName, .roomMarkedAsRead)
let roomID = notificationCenter.postNameObjectReceivedArguments?.anObject as? String
XCTAssertEqual(roomID, roomProxy.id)
}
func testSendMoreReadReceipts() async throws {
@@ -23,6 +23,8 @@ import Combine
class UserSessionFlowCoordinatorTests: XCTestCase {
var userSessionFlowCoordinator: UserSessionFlowCoordinator!
var navigationRootCoordinator: NavigationRootCoordinator!
var notificationManager: NotificationManagerMock!
var cancellables = Set<AnyCancellable>()
var detailCoordinator: CoordinatorProtocol? {
@@ -45,6 +47,8 @@ class UserSessionFlowCoordinatorTests: XCTestCase {
navigationRootCoordinator = NavigationRootCoordinator()
notificationManager = NotificationManagerMock()
userSessionFlowCoordinator = UserSessionFlowCoordinator(userSession: userSession,
navigationRootCoordinator: navigationRootCoordinator,
windowManager: WindowManagerMock(),
@@ -53,7 +57,7 @@ class UserSessionFlowCoordinatorTests: XCTestCase {
roomTimelineControllerFactory: MockRoomTimelineControllerFactory(),
appSettings: ServiceLocator.shared.settings,
analytics: ServiceLocator.shared.analytics,
notificationManager: NotificationManagerMock(),
notificationManager: notificationManager,
isNewLogin: false)
let deferred = deferFulfillment(userSessionFlowCoordinator.statePublisher) { $0 == .roomList(selectedRoomID: nil) }
@@ -81,6 +85,8 @@ class UserSessionFlowCoordinatorTests: XCTestCase {
try await process(route: .roomList, expectedState: .roomList(selectedRoomID: nil))
XCTAssertNil(detailNavigationStack?.rootCoordinator)
XCTAssertNil(detailCoordinator)
XCTAssertEqual(notificationManager.removeDeliveredMessageNotificationsForReceivedInvocations, ["1", "1", "2"])
}
func testRoomDetailsPresentation() async throws {