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)
@@ -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)
@@ -95,7 +97,6 @@ final class NotificationManagerTests: XCTestCase {
} }
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")
@@ -141,7 +142,6 @@ final class NotificationManagerTests: XCTestCase {
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
@@ -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,14 +561,17 @@ 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
state.notificationSettingsState.isLoaded switch state.notificationSettingsState {
case .loaded(settings: let settings):
return settings.mode == .mute
default:
return false
}
} }
notificationSettingsProxyMock.callbacks.send(.settingsDidChange) notificationSettingsProxyMock.callbacks.send(.settingsDidChange)
try await deferred.fulfill() 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)
@@ -591,14 +594,17 @@ 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
state.notificationSettingsState.isLoaded switch state.notificationSettingsState {
case .loaded(settings: let settings):
return settings.mode == .allMessages
default:
return false
}
} }
notificationSettingsProxyMock.callbacks.send(.settingsDidChange) notificationSettingsProxyMock.callbacks.send(.settingsDidChange)
try await deferred.fulfill() 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)