diff --git a/ElementX/Sources/Application/Application.swift b/ElementX/Sources/Application/Application.swift index c3bd641b0..21f211599 100644 --- a/ElementX/Sources/Application/Application.swift +++ b/ElementX/Sources/Application/Application.swift @@ -19,13 +19,13 @@ import SwiftUI @main struct Application: App { @UIApplicationDelegateAdaptor(AppDelegate.self) private var applicationDelegate - private let appCoordinator: AppCoordinatorProtocol! + private let appCoordinator: AppCoordinatorProtocol init() { if Tests.isRunningUITests { appCoordinator = UITestsAppCoordinator() } else if Tests.isRunningUnitTests { - appCoordinator = nil + appCoordinator = UnitTestsAppCoordinator() } else { appCoordinator = AppCoordinator() } @@ -33,15 +33,11 @@ struct Application: App { var body: some Scene { WindowGroup { - if Tests.isRunningUnitTests { - EmptyView() - } else { - appCoordinator.toPresentable() - .task { - appCoordinator.start() - } - .statusBarHidden(shouldHideStatusBar) - } + appCoordinator.toPresentable() + .task { + appCoordinator.start() + } + .statusBarHidden(shouldHideStatusBar) } } diff --git a/UnitTests/Sources/Extensions/XCTest.swift b/ElementX/Sources/UnitTests/UnitTestsAppCoordinator.swift similarity index 53% rename from UnitTests/Sources/Extensions/XCTest.swift rename to ElementX/Sources/UnitTests/UnitTestsAppCoordinator.swift index 1d03e22ca..9a6be438e 100644 --- a/UnitTests/Sources/Extensions/XCTest.swift +++ b/ElementX/Sources/UnitTests/UnitTestsAppCoordinator.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 New Vector Ltd +// Copyright 2022 New Vector Ltd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,14 +14,24 @@ // limitations under the License. // -import XCTest +import SwiftUI -@testable import ElementX - -extension XCTestCase { - func setupAppSettings() { - AppSettings.configureWithSuiteName("io.element.elementx.unitests") +class UnitTestsAppCoordinator: AppCoordinatorProtocol { + let notificationManager: NotificationManagerProtocol = NotificationManagerMock() + + init() { + ServiceLocator.shared.register(userIndicatorController: MockUserIndicatorController()) + + AppSettings.configureWithSuiteName("io.element.elementx.unittests") AppSettings.reset() ServiceLocator.shared.register(appSettings: AppSettings()) + ServiceLocator.shared.register(bugReportService: BugReportServiceMock()) + ServiceLocator.shared.register(analytics: Analytics(client: AnalyticsClientMock())) + } + + func start() { } + + func toPresentable() -> AnyView { + AnyView(ProgressView("Running Unit Tests")) } } diff --git a/UnitTests/Sources/AnalyticsSettingsScreenViewModelTests.swift b/UnitTests/Sources/AnalyticsSettingsScreenViewModelTests.swift index 43a4ec308..8f0e6deee 100644 --- a/UnitTests/Sources/AnalyticsSettingsScreenViewModelTests.swift +++ b/UnitTests/Sources/AnalyticsSettingsScreenViewModelTests.swift @@ -20,15 +20,12 @@ import XCTest @MainActor class AnalyticsSettingsScreenViewModelTests: XCTestCase { - private var applicationSettings: AppSettings! + private var appSettings: AppSettings { ServiceLocator.shared.settings } private var viewModel: AnalyticsSettingsScreenViewModelProtocol! private var context: AnalyticsSettingsScreenViewModelType.Context! @MainActor override func setUpWithError() throws { - AppSettings.configureWithSuiteName("io.element.elementx.unitests") AppSettings.reset() - applicationSettings = AppSettings() - ServiceLocator.shared.register(appSettings: applicationSettings) let analyticsClient = AnalyticsClientMock() analyticsClient.isRunning = false ServiceLocator.shared.register(analytics: Analytics(client: analyticsClient)) @@ -42,13 +39,13 @@ class AnalyticsSettingsScreenViewModelTests: XCTestCase { } func testOptIn() { - applicationSettings.analyticsConsentState = .optedOut + appSettings.analyticsConsentState = .optedOut context.send(viewAction: .toggleAnalytics) XCTAssertTrue(context.enableAnalytics) } func testOptOut() { - applicationSettings.analyticsConsentState = .optedIn + appSettings.analyticsConsentState = .optedIn context.send(viewAction: .toggleAnalytics) XCTAssertFalse(context.enableAnalytics) } diff --git a/UnitTests/Sources/AnalyticsTests.swift b/UnitTests/Sources/AnalyticsTests.swift index aed582058..12f7c7322 100644 --- a/UnitTests/Sources/AnalyticsTests.swift +++ b/UnitTests/Sources/AnalyticsTests.swift @@ -19,15 +19,13 @@ import AnalyticsEvents import XCTest class AnalyticsTests: XCTestCase { - private var applicationSettings: AppSettings! + private var appSettings: AppSettings { ServiceLocator.shared.settings } private var analyticsClient: AnalyticsClientMock! private var bugReportService: BugReportServiceMock! override func setUp() { - AppSettings.configureWithSuiteName("io.element.elementx.unitests") AppSettings.reset() - applicationSettings = AppSettings() - ServiceLocator.shared.register(appSettings: applicationSettings) + bugReportService = BugReportServiceMock() bugReportService.isRunning = false ServiceLocator.shared.register(bugReportService: bugReportService) @@ -47,7 +45,7 @@ class AnalyticsTests: XCTestCase { func testAnalyticsPromptUserDeclinedPostHog() { // Given an existing install of the app where the user previously declined PostHog - applicationSettings.analyticsConsentState = .optedOut + appSettings.analyticsConsentState = .optedOut // When the user is prompted for analytics let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt @@ -58,7 +56,7 @@ class AnalyticsTests: XCTestCase { func testAnalyticsPromptUserAcceptedPostHog() { // Given an existing install of the app where the user previously accepted PostHog - applicationSettings.analyticsConsentState = .optedIn + appSettings.analyticsConsentState = .optedIn // When the user is prompted for analytics let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt @@ -81,7 +79,7 @@ class AnalyticsTests: XCTestCase { // When analytics is opt-out ServiceLocator.shared.analytics.optOut() // Then analytics should be disabled - XCTAssertEqual(applicationSettings.analyticsConsentState, .optedOut) + XCTAssertEqual(appSettings.analyticsConsentState, .optedOut) XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled) XCTAssertFalse(ServiceLocator.shared.analytics.isRunning) XCTAssertFalse(analyticsClient.isRunning) @@ -96,7 +94,7 @@ class AnalyticsTests: XCTestCase { // When analytics is opt-in ServiceLocator.shared.analytics.optIn() // The analytics should be enabled - XCTAssertEqual(applicationSettings.analyticsConsentState, .optedIn) + XCTAssertEqual(appSettings.analyticsConsentState, .optedIn) XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled) // Analytics client and the bug report service should have been started XCTAssertTrue(analyticsClient.startCalled) @@ -105,7 +103,7 @@ class AnalyticsTests: XCTestCase { func testAnalyticsStartIfNotEnabled() { // Given an existing install of the app where the user previously declined the tracking - applicationSettings.analyticsConsentState = .optedOut + appSettings.analyticsConsentState = .optedOut // Analytics should not start XCTAssertFalse(ServiceLocator.shared.analytics.isEnabled) ServiceLocator.shared.analytics.startIfEnabled() @@ -115,7 +113,7 @@ class AnalyticsTests: XCTestCase { func testAnalyticsStartIfEnabled() { // Given an existing install of the app where the user previously accpeted the tracking - applicationSettings.analyticsConsentState = .optedIn + appSettings.analyticsConsentState = .optedIn // Analytics should start XCTAssertTrue(ServiceLocator.shared.analytics.isEnabled) ServiceLocator.shared.analytics.startIfEnabled() @@ -188,14 +186,14 @@ class AnalyticsTests: XCTestCase { func testResetConsentState() { // Given an existing install of the app where the user previously accpeted the tracking - applicationSettings.analyticsConsentState = .optedIn + appSettings.analyticsConsentState = .optedIn XCTAssertFalse(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt) // When forgetting analytics consents ServiceLocator.shared.analytics.resetConsentState() // Then the analytics prompt should be presented again - XCTAssertEqual(applicationSettings.analyticsConsentState, .unknown) + XCTAssertEqual(appSettings.analyticsConsentState, .unknown) XCTAssertTrue(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt) } } diff --git a/UnitTests/Sources/BugReportViewModelTests.swift b/UnitTests/Sources/BugReportViewModelTests.swift index cba8f1b24..d398afbbb 100644 --- a/UnitTests/Sources/BugReportViewModelTests.swift +++ b/UnitTests/Sources/BugReportViewModelTests.swift @@ -62,7 +62,10 @@ class BugReportViewModelTests: XCTestCase { func testSendReportWithSuccess() async throws { let mockService = BugReportServiceMock() - mockService.submitBugReportProgressListenerReturnValue = SubmitBugReportResponse(reportUrl: "https://test.test") + mockService.submitBugReportProgressListenerClosure = { _, _ in + await Task.yield() + return SubmitBugReportResponse(reportUrl: "https://test.test") + } let viewModel = BugReportScreenViewModel(bugReportService: mockService, userID: "@mock.client.com", deviceID: nil, diff --git a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift index dd74f9771..ffa1d31f5 100644 --- a/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift +++ b/UnitTests/Sources/NotificationManager/NotificationManagerTests.swift @@ -27,13 +27,11 @@ final class NotificationManagerTests: XCTestCase { private var shouldDisplayInAppNotificationReturnValue = false private var handleInlineReplyDelegateCalled = false private var notificationTappedDelegateCalled = false - private var settings: AppSettings! + + private var appSettings: AppSettings { ServiceLocator.shared.settings } override func setUp() { - AppSettings.configureWithSuiteName("io.element.elementx.unitests") AppSettings.reset() - settings = AppSettings() - ServiceLocator.shared.register(appSettings: settings) notificationManager = NotificationManager(notificationCenter: notificationCenter) notificationManager.start() @@ -64,7 +62,7 @@ final class NotificationManagerTests: XCTestCase { let pushkeyData = Data("1234".utf8) _ = await notificationManager.register(with: pushkeyData) XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.pushkey, pushkeyData.base64EncodedString()) - XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.appId, settings?.pusherAppId) + XCTAssertEqual(clientProxy.setPusherArgument?.identifiers.appId, appSettings.pusherAppId) XCTAssertEqual(clientProxy.setPusherArgument?.appDisplayName, "\(InfoPlistReader.main.bundleDisplayName) (iOS)") XCTAssertEqual(clientProxy.setPusherArgument?.deviceDisplayName, UIDevice.current.name) XCTAssertNotNil(clientProxy.setPusherArgument?.profileTag) @@ -73,7 +71,7 @@ final class NotificationManagerTests: XCTestCase { XCTFail("Http kind expected") return } - XCTAssertEqual(data.url, settings?.pushGatewayBaseURL.absoluteString) + XCTAssertEqual(data.url, appSettings.pushGatewayBaseURL.absoluteString) XCTAssertEqual(data.format, .eventIdOnly) let defaultPayload = APNSPayload(aps: APSInfo(mutableContent: 1, alert: APSAlert(locKey: "Notification", @@ -83,15 +81,15 @@ final class NotificationManagerTests: XCTestCase { } func test_whenRegisteredAndPusherTagNotSetInSettings_tagGeneratedAndSavedInSettings() async throws { - settings?.pusherProfileTag = nil + appSettings.pusherProfileTag = nil _ = await notificationManager.register(with: Data()) - XCTAssertNotNil(settings?.pusherProfileTag) + XCTAssertNotNil(appSettings.pusherProfileTag) } func test_whenRegisteredAndPusherTagIsSetInSettings_tagNotGenerated() async throws { - settings?.pusherProfileTag = "12345" + appSettings.pusherProfileTag = "12345" _ = await notificationManager.register(with: Data()) - XCTAssertEqual(settings?.pusherProfileTag, "12345") + XCTAssertEqual(appSettings.pusherProfileTag, "12345") } func test_whenShowLocalNotification_notificationRequestGetsAdded() async throws { diff --git a/UnitTests/Sources/StartChatViewModelTests.swift b/UnitTests/Sources/StartChatViewModelTests.swift index 32b8ea02b..9f9e7683a 100644 --- a/UnitTests/Sources/StartChatViewModelTests.swift +++ b/UnitTests/Sources/StartChatViewModelTests.swift @@ -36,7 +36,7 @@ class StartChatScreenViewModelTests: XCTestCase { let userSession = MockUserSession(clientProxy: clientProxy, mediaProvider: MockMediaProvider()) viewModel = StartChatScreenViewModel(userSession: userSession, userIndicatorController: nil, userDiscoveryService: userDiscoveryService) - setupAppSettings() + AppSettings.reset() ServiceLocator.shared.settings.startChatUserSuggestionsEnabled = true }