Unit test fixes (#1846)

* Prevent notification manager tests from failing during randomised testing

* Fix flakey logging tests

* Fix flakey room notification muting tests
This commit is contained in:
Stefan Ceriu
2023-10-03 10:25:58 +03:00
committed by GitHub
parent e84e9b2938
commit 325ac63d66
3 changed files with 37 additions and 30 deletions
+3
View File
@@ -24,6 +24,9 @@ class LoggingTests: XCTestCase {
} }
override func setUpWithError() throws { override func setUpWithError() throws {
// Force MXLogger to flush stderr and clean up after itself
MXLog.configure(logLevel: .info, redirectToFiles: false)
MXLogger.deleteLogFiles() MXLogger.deleteLogFiles()
} }
@@ -20,6 +20,7 @@ import XCTest
@testable import ElementX @testable import ElementX
@MainActor
final class NotificationManagerTests: XCTestCase { final class NotificationManagerTests: XCTestCase {
var notificationManager: NotificationManager! var notificationManager: NotificationManager!
private let clientProxy = MockClientProxy(userID: "@test:user.net") private let clientProxy = MockClientProxy(userID: "@test:user.net")
@@ -36,6 +37,8 @@ final class NotificationManagerTests: XCTestCase {
override func setUp() { override func setUp() {
AppSettings.reset() AppSettings.reset()
notificationCenter = UserNotificationCenterMock() notificationCenter = UserNotificationCenterMock()
notificationCenter.requestAuthorizationOptionsReturnValue = true
notificationManager = NotificationManager(notificationCenter: notificationCenter, appSettings: appSettings) notificationManager = NotificationManager(notificationCenter: notificationCenter, appSettings: appSettings)
notificationManager.start() notificationManager.start()
notificationManager.setUserSession(mockUserSession) notificationManager.setUserSession(mockUserSession)
@@ -55,7 +58,7 @@ final class NotificationManagerTests: XCTestCase {
let success = await notificationManager.register(with: Data()) let success = await notificationManager.register(with: Data())
XCTAssertTrue(success) XCTAssertTrue(success)
} }
func test_whenRegisteredAndPusherThrowsError_completionFalseIsCalled() async throws { func test_whenRegisteredAndPusherThrowsError_completionFalseIsCalled() async throws {
enum TestError: Error { enum TestError: Error {
case someError case someError
@@ -65,7 +68,6 @@ final class NotificationManagerTests: XCTestCase {
XCTAssertFalse(success) XCTAssertFalse(success)
} }
@MainActor
func test_whenRegistered_pusherIsCalledWithCorrectValues() async throws { func test_whenRegistered_pusherIsCalledWithCorrectValues() async throws {
let pushkeyData = Data("1234".utf8) let pushkeyData = Data("1234".utf8)
_ = await notificationManager.register(with: pushkeyData) _ = await notificationManager.register(with: pushkeyData)
@@ -87,27 +89,26 @@ final class NotificationManagerTests: XCTestCase {
pusherNotificationClientIdentifier: nil) pusherNotificationClientIdentifier: nil)
XCTAssertEqual(data.defaultPayload, try defaultPayload.toJsonString()) XCTAssertEqual(data.defaultPayload, try defaultPayload.toJsonString())
} }
func test_whenRegisteredAndPusherTagNotSetInSettings_tagGeneratedAndSavedInSettings() async throws { func test_whenRegisteredAndPusherTagNotSetInSettings_tagGeneratedAndSavedInSettings() async throws {
appSettings.pusherProfileTag = nil appSettings.pusherProfileTag = nil
_ = await notificationManager.register(with: Data()) _ = await notificationManager.register(with: Data())
XCTAssertNotNil(appSettings.pusherProfileTag) XCTAssertNotNil(appSettings.pusherProfileTag)
} }
func test_whenRegisteredAndPusherTagIsSetInSettings_tagNotGenerated() async throws { func test_whenRegisteredAndPusherTagIsSetInSettings_tagNotGenerated() async throws {
notificationCenter.requestAuthorizationOptionsReturnValue = true
appSettings.pusherProfileTag = "12345" appSettings.pusherProfileTag = "12345"
_ = await notificationManager.register(with: Data()) _ = await notificationManager.register(with: Data())
XCTAssertEqual(appSettings.pusherProfileTag, "12345") XCTAssertEqual(appSettings.pusherProfileTag, "12345")
} }
func test_whenShowLocalNotification_notificationRequestGetsAdded() async throws { func test_whenShowLocalNotification_notificationRequestGetsAdded() async throws {
await notificationManager.showLocalNotification(with: "Title", subtitle: "Subtitle") await notificationManager.showLocalNotification(with: "Title", subtitle: "Subtitle")
let request = try XCTUnwrap(notificationCenter.addReceivedRequest) let request = try XCTUnwrap(notificationCenter.addReceivedRequest)
XCTAssertEqual(request.content.title, "Title") XCTAssertEqual(request.content.title, "Title")
XCTAssertEqual(request.content.subtitle, "Subtitle") XCTAssertEqual(request.content.subtitle, "Subtitle")
} }
func test_whenStart_notificationCategoriesAreSet() throws { func test_whenStart_notificationCategoriesAreSet() throws {
// let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply, // let replyAction = UNTextInputNotificationAction(identifier: NotificationConstants.Action.inlineReply,
// title: L10n.actionQuickReply, // title: L10n.actionQuickReply,
@@ -122,12 +123,12 @@ final class NotificationManagerTests: XCTestCase {
options: []) options: [])
XCTAssertEqual(notificationCenter.setNotificationCategoriesReceivedCategories, [messageCategory, inviteCategory]) XCTAssertEqual(notificationCenter.setNotificationCategoriesReceivedCategories, [messageCategory, inviteCategory])
} }
func test_whenStart_delegateIsSet() throws { func test_whenStart_delegateIsSet() throws {
let delegate = try XCTUnwrap(notificationCenter.delegate) let delegate = try XCTUnwrap(notificationCenter.delegate)
XCTAssertTrue(delegate.isEqual(notificationManager)) XCTAssertTrue(delegate.isEqual(notificationManager))
} }
func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws { func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws {
let expectation = expectation(description: "requestAuthorization should be called") let expectation = expectation(description: "requestAuthorization should be called")
notificationCenter.requestAuthorizationOptionsClosure = { _ in notificationCenter.requestAuthorizationOptionsClosure = { _ in
@@ -138,10 +139,9 @@ final class NotificationManagerTests: XCTestCase {
await fulfillment(of: [expectation]) await fulfillment(of: [expectation])
XCTAssertEqual(notificationCenter.requestAuthorizationOptionsReceivedOptions, [.alert, .sound, .badge]) XCTAssertEqual(notificationCenter.requestAuthorizationOptionsReceivedOptions, [.alert, .sound, .badge])
} }
func test_whenStartAndAuthorizationGranted_delegateCalled() async throws { func test_whenStartAndAuthorizationGranted_delegateCalled() async throws {
authorizationStatusWasGranted = false authorizationStatusWasGranted = false
notificationCenter.requestAuthorizationOptionsReturnValue = true
notificationManager.delegate = self notificationManager.delegate = self
let expectation: XCTestExpectation = expectation(description: "registerForRemoteNotifications delegate function should be called") let expectation: XCTestExpectation = expectation(description: "registerForRemoteNotifications delegate function should be called")
expectation.assertForOverFulfill = false expectation.assertForOverFulfill = false
@@ -152,14 +152,14 @@ final class NotificationManagerTests: XCTestCase {
await fulfillment(of: [expectation]) await fulfillment(of: [expectation])
XCTAssertTrue(authorizationStatusWasGranted) XCTAssertTrue(authorizationStatusWasGranted)
} }
func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateNotSet_CorrectPresentationOptionsReturned() async throws {
let archiver = MockCoder(requiringSecureCoding: false) let archiver = MockCoder(requiringSecureCoding: false)
let notification = try XCTUnwrap(UNNotification(coder: archiver)) let notification = try XCTUnwrap(UNNotification(coder: archiver))
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
XCTAssertEqual(options, [.badge, .sound, .list, .banner]) XCTAssertEqual(options, [.badge, .sound, .list, .banner])
} }
func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudNotBeDisplayed_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudNotBeDisplayed_CorrectPresentationOptionsReturned() async throws {
shouldDisplayInAppNotificationReturnValue = false shouldDisplayInAppNotificationReturnValue = false
notificationManager.delegate = self notificationManager.delegate = self
@@ -168,7 +168,7 @@ final class NotificationManagerTests: XCTestCase {
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
XCTAssertEqual(options, []) XCTAssertEqual(options, [])
} }
func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudBeDisplayed_CorrectPresentationOptionsReturned() async throws { func test_whenWillPresentNotificationsDelegateSetAndNotificationsShoudBeDisplayed_CorrectPresentationOptionsReturned() async throws {
shouldDisplayInAppNotificationReturnValue = true shouldDisplayInAppNotificationReturnValue = true
notificationManager.delegate = self notificationManager.delegate = self
@@ -177,7 +177,7 @@ final class NotificationManagerTests: XCTestCase {
let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification) let options = await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), willPresent: notification)
XCTAssertEqual(options, [.badge, .sound, .list, .banner]) XCTAssertEqual(options, [.badge, .sound, .list, .banner])
} }
func test_whenNotificationCenterReceivedResponseInLineReply_delegateIsCalled() async throws { func test_whenNotificationCenterReceivedResponseInLineReply_delegateIsCalled() async throws {
handleInlineReplyDelegateCalled = false handleInlineReplyDelegateCalled = false
notificationManager.delegate = self notificationManager.delegate = self
@@ -185,7 +185,7 @@ final class NotificationManagerTests: XCTestCase {
await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response) await notificationManager.userNotificationCenter(UNUserNotificationCenter.current(), didReceive: response)
XCTAssertTrue(handleInlineReplyDelegateCalled) XCTAssertTrue(handleInlineReplyDelegateCalled)
} }
func test_whenNotificationCenterReceivedResponseWithActionIdentifier_delegateIsCalled() async throws { func test_whenNotificationCenterReceivedResponseWithActionIdentifier_delegateIsCalled() async throws {
notificationTappedDelegateCalled = false notificationTappedDelegateCalled = false
notificationManager.delegate = self 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 // The center calls the delivered and the removal functions when an id is passed
notificationCenter.deliveredNotificationsClosure = nil notificationCenter.deliveredNotificationsClosure = nil
notificationCenter.requestAuthorizationOptionsReturnValue = true
notificationCenter.deliveredNotificationsReturnValue = [] notificationCenter.deliveredNotificationsReturnValue = []
let expectation = expectation(description: "Notification should be removed") let expectation = expectation(description: "Notification should be removed")
notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in
@@ -224,7 +223,6 @@ final class NotificationManagerTests: XCTestCase {
func test_InvitesNotificationsRemoval() async { func test_InvitesNotificationsRemoval() async {
let expectation = expectation(description: "Notification should be removed") let expectation = expectation(description: "Notification should be removed")
notificationCenter.requestAuthorizationOptionsReturnValue = true
notificationCenter.deliveredNotificationsReturnValue = [] notificationCenter.deliveredNotificationsReturnValue = []
notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in notificationCenter.removeDeliveredNotificationsWithIdentifiersClosure = { _ in
expectation.fulfill() expectation.fulfill()
@@ -561,15 +561,18 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
XCTAssertFalse(context.viewState.isProcessingMuteToggleAction) XCTAssertFalse(context.viewState.isProcessingMuteToggleAction)
do { let deferred = deferFulfillment(context.$viewState) { state in
let deferred = deferFulfillment(context.$viewState) { state in switch state.notificationSettingsState {
state.notificationSettingsState.isLoaded 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 { if case .loaded(let newNotificationSettingsState) = viewModel.state.notificationSettingsState {
XCTAssertFalse(newNotificationSettingsState.isDefault) XCTAssertFalse(newNotificationSettingsState.isDefault)
XCTAssertEqual(newNotificationSettingsState.mode, .mute) XCTAssertEqual(newNotificationSettingsState.mode, .mute)
@@ -591,15 +594,18 @@ class RoomDetailsScreenViewModelTests: XCTestCase {
XCTAssertFalse(context.viewState.isProcessingMuteToggleAction) XCTAssertFalse(context.viewState.isProcessingMuteToggleAction)
do { let deferred = deferFulfillment(context.$viewState) { state in
let deferred = deferFulfillment(context.$viewState) { state in switch state.notificationSettingsState {
state.notificationSettingsState.isLoaded 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 { if case .loaded(let newNotificationSettingsState) = viewModel.state.notificationSettingsState {
XCTAssertFalse(newNotificationSettingsState.isDefault) XCTAssertFalse(newNotificationSettingsState.isDefault)
XCTAssertEqual(newNotificationSettingsState.mode, .allMessages) XCTAssertEqual(newNotificationSettingsState.mode, .allMessages)