From 9dcf95abdca652c67aa34560df5af620a1a54ea9 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Tue, 3 Oct 2023 10:25:58 +0300 Subject: [PATCH] Unit test fixes (#1846) * Prevent notification manager tests from failing during randomised testing * Fix flakey logging tests * Fix flakey room notification muting tests --- UnitTests/Sources/LoggingTests.swift | 3 ++ .../NotificationManagerTests.swift | 34 +++++++++---------- .../Sources/RoomDetailsViewModelTests.swift | 30 +++++++++------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/UnitTests/Sources/LoggingTests.swift b/UnitTests/Sources/LoggingTests.swift index 3a1eec931..aaae5f4fe 100644 --- a/UnitTests/Sources/LoggingTests.swift +++ b/UnitTests/Sources/LoggingTests.swift @@ -24,6 +24,9 @@ class LoggingTests: XCTestCase { } override func setUpWithError() throws { + // Force MXLogger to flush stderr and clean up after itself + MXLog.configure(logLevel: .info, redirectToFiles: false) + MXLogger.deleteLogFiles() } diff --git a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift index 02dd580de..b570cb2e3 100644 --- a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift +++ b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift @@ -20,6 +20,7 @@ import XCTest @testable import ElementX +@MainActor final class NotificationManagerTests: XCTestCase { var notificationManager: NotificationManager! private let clientProxy = MockClientProxy(userID: "@test:user.net") @@ -36,6 +37,8 @@ final class NotificationManagerTests: XCTestCase { override func setUp() { AppSettings.reset() notificationCenter = UserNotificationCenterMock() + notificationCenter.requestAuthorizationOptionsReturnValue = true + notificationManager = NotificationManager(notificationCenter: notificationCenter, appSettings: appSettings) notificationManager.start() notificationManager.setUserSession(mockUserSession) @@ -55,7 +58,7 @@ final class NotificationManagerTests: XCTestCase { let success = await notificationManager.register(with: Data()) XCTAssertTrue(success) } - + func test_whenRegisteredAndPusherThrowsError_completionFalseIsCalled() async throws { enum TestError: Error { case someError @@ -65,7 +68,6 @@ final class NotificationManagerTests: XCTestCase { XCTAssertFalse(success) } - @MainActor func test_whenRegistered_pusherIsCalledWithCorrectValues() async throws { let pushkeyData = Data("1234".utf8) _ = await notificationManager.register(with: pushkeyData) @@ -87,27 +89,26 @@ final class NotificationManagerTests: XCTestCase { pusherNotificationClientIdentifier: nil) XCTAssertEqual(data.defaultPayload, try defaultPayload.toJsonString()) } - + func test_whenRegisteredAndPusherTagNotSetInSettings_tagGeneratedAndSavedInSettings() async throws { appSettings.pusherProfileTag = nil _ = await notificationManager.register(with: Data()) XCTAssertNotNil(appSettings.pusherProfileTag) } - + func test_whenRegisteredAndPusherTagIsSetInSettings_tagNotGenerated() async throws { - notificationCenter.requestAuthorizationOptionsReturnValue = true appSettings.pusherProfileTag = "12345" _ = await notificationManager.register(with: Data()) XCTAssertEqual(appSettings.pusherProfileTag, "12345") } - + func test_whenShowLocalNotification_notificationRequestGetsAdded() async throws { await notificationManager.showLocalNotification(with: "Title", subtitle: "Subtitle") let request = try XCTUnwrap(notificationCenter.addReceivedRequest) XCTAssertEqual(request.content.title, "Title") XCTAssertEqual(request.content.subtitle, "Subtitle") } - + func test_whenStart_notificationCategoriesAreSet() throws { // let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply, // title: L10n.actionQuickReply, @@ -122,12 +123,12 @@ final class NotificationManagerTests: XCTestCase { options: []) XCTAssertEqual(notificationCenter.setNotificationCategoriesReceivedCategories, [messageCategory, inviteCategory]) } - + func test_whenStart_delegateIsSet() throws { let delegate = try XCTUnwrap(notificationCenter.delegate) XCTAssertTrue(delegate.isEqual(notificationManager)) } - + func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws { let expectation = expectation(description: "requestAuthorization should be called") notificationCenter.requestAuthorizationOptionsClosure = { _ in @@ -138,10 +139,9 @@ final class NotificationManagerTests: XCTestCase { await fulfillment(of: [expectation]) XCTAssertEqual(notificationCenter.requestAuthorizationOptionsReceivedOptions, [.alert, .sound, .badge]) } - + func test_whenStartAndAuthorizationGranted_delegateCalled() async throws { authorizationStatusWasGranted = false - notificationCenter.requestAuthorizationOptionsReturnValue = true notificationManager.delegate = self let expectation: XCTestExpectation = expectation(description: "registerForRemoteNotifications delegate function should be called") expectation.assertForOverFulfill = false @@ -152,14 +152,14 @@ final class NotificationManagerTests: XCTestCase { await fulfillment(of: [expectation]) XCTAssertTrue(authorizationStatusWasGranted) } - + func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws { let archiver = MockCoder(requiringSecureCoding: false) let notification = try XCTUnwrap(UNNotification(coder: archiver)) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) XCTAssertEqual(options, [.badge, .sound, .list, .banner]) } - + func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudNotBeDisplayed_CorrectPresentationOptionsReturned() async throws { shouldDisplayInAppNotificationReturnValue = false notificationManager.delegate = self @@ -168,7 +168,7 @@ final class NotificationManagerTests: XCTestCase { let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) XCTAssertEqual(options, []) } - + func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudBeDisplayed_CorrectPresentationOptionsReturned() async throws { shouldDisplayInAppNotificationReturnValue = true notificationManager.delegate = self @@ -177,7 +177,7 @@ final class NotificationManagerTests: XCTestCase { let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) XCTAssertEqual(options, [.badge, .sound, .list, .banner]) } - + func test_whenNotificationCenterReceivedResponseInLineReply_delegateIsCalled() async throws { handleInlineReplyDelegateCalled = false notificationManager.delegate = self @@ -185,7 +185,7 @@ final class NotificationManagerTests: XCTestCase { await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response) XCTAssertTrue(handleInlineReplyDelegateCalled) } - + func test_whenNotificationCenterReceivedResponseWithActionIdentifier_delegateIsCalled() async throws { notificationTappedDelegateCalled = false notificationManager.delegate = self @@ -210,7 +210,6 @@ final class NotificationManagerTests: XCTestCase { // The center calls the delivered and the removal functions when an id is passed notificationCenter.deliveredNotificationsClosure = nil - notificationCenter.requestAuthorizationOptionsReturnValue = true notificationCenter.deliveredNotificationsReturnValue = [] let expectation = expectation(description: "Notification should be removed") notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in @@ -224,7 +223,6 @@ final class NotificationManagerTests: XCTestCase { func test_InvitesNotificationsRemoval() async { let expectation = expectation(description: "Notification should be removed") - notificationCenter.requestAuthorizationOptionsReturnValue = true notificationCenter.deliveredNotificationsReturnValue = [] notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in expectation.fulfill() diff --git a/UnitTests/Sources/RoomDetailsViewModelTests.swift b/UnitTests/Sources/RoomDetailsViewModelTests.swift index 8acce284d..4e137d375 100644 --- a/UnitTests/Sources/RoomDetailsViewModelTests.swift +++ b/UnitTests/Sources/RoomDetailsViewModelTests.swift @@ -561,15 +561,18 @@ class RoomDetailsScreenViewModelTests: XCTestCase { XCTAssertFalse(context.viewState.isProcessingMuteToggleAction) - do { - let deferred = deferFulfillment(context.$viewState) { state in - state.notificationSettingsState.isLoaded + let deferred = deferFulfillment(context.$viewState) { state in + switch state.notificationSettingsState { + case .loaded(settings: let settings): + return settings.mode == .mute + default: + return false } - - notificationSettingsProxyMock.callbacks.send(.settingsDidChange) - try await deferred.fulfill() } + notificationSettingsProxyMock.callbacks.send(.settingsDidChange) + try await deferred.fulfill() + if case .loaded(let newNotificationSettingsState) = viewModel.state.notificationSettingsState { XCTAssertFalse(newNotificationSettingsState.isDefault) XCTAssertEqual(newNotificationSettingsState.mode, .mute) @@ -591,15 +594,18 @@ class RoomDetailsScreenViewModelTests: XCTestCase { XCTAssertFalse(context.viewState.isProcessingMuteToggleAction) - do { - let deferred = deferFulfillment(context.$viewState) { state in - state.notificationSettingsState.isLoaded + let deferred = deferFulfillment(context.$viewState) { state in + switch state.notificationSettingsState { + case .loaded(settings: let settings): + return settings.mode == .allMessages + default: + return false } - - notificationSettingsProxyMock.callbacks.send(.settingsDidChange) - try await deferred.fulfill() } + notificationSettingsProxyMock.callbacks.send(.settingsDidChange) + try await deferred.fulfill() + if case .loaded(let newNotificationSettingsState) = viewModel.state.notificationSettingsState { XCTAssertFalse(newNotificationSettingsState.isDefault) XCTAssertEqual(newNotificationSettingsState.mode, .allMessages)