mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
268d6c8ead
* Store the notifications switch per account Adds AccountSettings.enableNotifications and moves the app-wide value into the signed in accounts before they're restored: the first account takes it out of the app settings and the others get the same switch, so someone who turned notifications off keeps them off. * Add a notification service to every user session Each account's NotificationService sets its pusher when its notifications are on and deletes it when they're off (appending with multiple accounts), keeps the account's unread count and decides which of its delivered notifications to clear. It asks the NotificationManager, the only owner of the notification center, through actions: remove the notifications matching a rule, request authorization and the unread count. The UserSessionManager connects every live session's service to the manager, and flows and screens use the session's service instead of the manager, which leaves the flow parameters. * Route notifications by account Taps only open rooms of the active account for now, a tap made while restoring only opens once its account is the active one, replies are sent from the account they were made for, and signing out only clears that account's notifications unless it was the last one. The notification extension hides pushes for an account that signed out while other accounts are still signed in. * Turn notifications on and off for each account The notification settings switch is now saved per account, and each account's NotificationService sets or deletes its pusher when it changes, asking for permission when it's turned on. * Apply review suggestions to multi-account notifications Migration - Move the notifications switch with a single per-account migration, like 26.10.1 and 26.10.2, and drop its dedicated test. Notification manager and service - Follow each account's badge count publisher directly, removing the badge count action and the stored counts. - Only reset the badge and unregister from APNs when the last account signs out, as each account already clears its own notifications. - Set up the service's subscriptions in a single setupSubscriptions(). - Rename switchChanged(to:) to enableNotificationsChanged(to:) and pusherConfiguration(identifiers:) to pusherConfiguration(for:). - Build the service without hopping to the main actor, it already runs there. Notification service extension - Check the account's switch in NotificationHandler using the session's settings, and log an error when a push arrives for an account that's turned off. - Always show pushes that match no signed-in account and log an error, so a broken setup stays visible. Other - Store a pending inline reply in a PendingInlineReplyDetails struct. - Assign the chats tab badge with weakAssign. - Make PusherConfiguration Equatable so the pusher test checks it in one expectation. - Clarify the comment about notifications without a receiver. * Print instead of logging before the app hooks run in the NSE A push that matches no signed-in account goes back to printing, as MXLog shouldn't be used before the app hooks have run. The push is still shown so a broken setup stays visible.
309 lines
16 KiB
Swift
309 lines
16 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
import Combine
|
|
@testable import ElementX
|
|
import Foundation
|
|
import Testing
|
|
|
|
@MainActor
|
|
struct UserSessionFlowCoordinatorTests {
|
|
private var userSessionFlowCoordinator: UserSessionFlowCoordinator!
|
|
private var rootCoordinator: NavigationRootCoordinator!
|
|
private let userIndicatorController: UserIndicatorControllerMock
|
|
private let stateMachineFactory = PublishedStateMachineFactory()
|
|
|
|
private let networkReachabilitySubject: CurrentValueSubject<NetworkMonitorReachability, Never> = .init(.reachable)
|
|
private let homeserverReachabilitySubject: CurrentValueSubject<HomeserverReachability, Never> = .init(.reachable)
|
|
private var tabCoordinator: NavigationTabCoordinator<UserSessionFlowCoordinator.HomeTab>? {
|
|
rootCoordinator?.rootCoordinator as? NavigationTabCoordinator
|
|
}
|
|
|
|
private var chatsSplitCoordinator: NavigationSplitCoordinator? {
|
|
tabCoordinator?.tabCoordinators.first as? NavigationSplitCoordinator
|
|
}
|
|
|
|
private var detailCoordinator: CoordinatorProtocol? {
|
|
chatsSplitCoordinator?.detailCoordinator
|
|
}
|
|
|
|
private var detailNavigationStack: NavigationStackCoordinator? {
|
|
detailCoordinator as? NavigationStackCoordinator
|
|
}
|
|
|
|
init() async throws {
|
|
rootCoordinator = NavigationRootCoordinator()
|
|
|
|
let clientProxy = ClientProxyMock(.init(userID: "hi@bob", roomSummaryProvider: RoomSummaryProviderMock(.init(state: .loaded(.mockRooms)))))
|
|
clientProxy.homeserverReachabilityPublisher = homeserverReachabilitySubject.asCurrentValuePublisher()
|
|
|
|
let networkMonitor = NetworkMonitorMock(.init())
|
|
networkMonitor.reachabilityPublisher = networkReachabilitySubject.asCurrentValuePublisher()
|
|
let appMediator = AppMediatorMock(.init())
|
|
appMediator.networkMonitor = networkMonitor
|
|
|
|
userIndicatorController = UserIndicatorControllerMock()
|
|
let userSession = UserSessionMock(.init(clientProxy: clientProxy))
|
|
|
|
let flowParameters = CommonFlowParameters(userSession: userSession,
|
|
availableSessionsPublisher: .init([.init(userSession: userSession)]),
|
|
bugReportService: BugReportServiceMock(.init()),
|
|
elementCallService: ElementCallServiceMock(.init()),
|
|
timelineControllerFactory: TimelineControllerFactoryMock(.init()),
|
|
emojiProvider: EmojiProvider(userSettings: userSession.userSettings),
|
|
linkMetadataProvider: LinkMetadataProvider(),
|
|
appMediator: appMediator,
|
|
appHooks: AppHooks(),
|
|
analytics: AnalyticsServiceMock(.init()),
|
|
userIndicatorController: userIndicatorController,
|
|
stateMachineFactory: stateMachineFactory)
|
|
|
|
userSessionFlowCoordinator = UserSessionFlowCoordinator(isNewLogin: false,
|
|
navigationRootCoordinator: rootCoordinator,
|
|
appLockService: AppLockServiceMock(),
|
|
flowParameters: flowParameters)
|
|
|
|
userSessionFlowCoordinator.start()
|
|
}
|
|
|
|
// MARK: Navigation
|
|
|
|
@Test
|
|
func initialState() {
|
|
#expect(chatsSplitCoordinator != nil)
|
|
#expect(detailCoordinator == nil)
|
|
}
|
|
|
|
@Test
|
|
mutating func settingsPresentation() async throws {
|
|
try await process(route: .settings, expectedUserSessionState: .settingsScreen)
|
|
#expect((tabCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is SettingsScreenCoordinator)
|
|
}
|
|
|
|
@Test
|
|
mutating func roomPresentation() async throws {
|
|
try await process(route: .room(roomID: "1", via: []), expectedChatsState: .roomList(detailState: .room(roomID: "1")))
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(detailCoordinator != nil)
|
|
}
|
|
|
|
@Test
|
|
mutating func roomPresentationClearsSettings() async throws {
|
|
try await process(route: .settings, expectedUserSessionState: .settingsScreen)
|
|
#expect((tabCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is SettingsScreenCoordinator)
|
|
#expect(detailCoordinator == nil)
|
|
|
|
try await process(route: .room(roomID: "1", via: []),
|
|
expectedUserSessionState: .tabBar,
|
|
expectedChatsState: .roomList(detailState: .room(roomID: "1")))
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(detailCoordinator != nil)
|
|
}
|
|
|
|
@Test
|
|
mutating func childRoomPresentation() async throws {
|
|
try await process(route: .room(roomID: "1", via: []), expectedChatsState: .roomList(detailState: .room(roomID: "1")))
|
|
let detailNavigationStack = try #require(detailNavigationStack, "There must be a navigation stack.")
|
|
#expect(detailNavigationStack.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(detailCoordinator != nil)
|
|
|
|
let deferred = deferFulfillment(detailNavigationStack.observe(\.stackCoordinators.count)) { $0 == 1 }
|
|
try await process(route: .childRoom(roomID: "2", via: []))
|
|
try await deferred.fulfill()
|
|
#expect(detailNavigationStack.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(detailCoordinator != nil)
|
|
#expect(detailNavigationStack.stackCoordinators.count == 1)
|
|
#expect(detailNavigationStack.stackCoordinators.first is RoomScreenCoordinator)
|
|
}
|
|
|
|
@Test
|
|
mutating func shareMediaRouteWithoutRoom() async throws {
|
|
try await process(route: .settings, expectedUserSessionState: .settingsScreen)
|
|
#expect((tabCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is SettingsScreenCoordinator)
|
|
#expect(chatsSplitCoordinator?.sheetCoordinator == nil)
|
|
|
|
let sharePayload: ShareExtensionPayload = .mediaFiles(roomID: nil, mediaFiles: [.init(url: .picturesDirectory, suggestedName: nil)])
|
|
try await process(route: .share(sharePayload),
|
|
expectedUserSessionState: .tabBar,
|
|
expectedChatsState: .shareExtensionRoomList(sharePayload: sharePayload))
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect((chatsSplitCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is RoomSelectionScreenCoordinator)
|
|
}
|
|
|
|
@Test
|
|
mutating func shareMediaRouteWithRoom() async throws {
|
|
try await process(route: .event(eventID: "1", roomID: "1", via: []), expectedChatsState: .roomList(detailState: .room(roomID: "1")))
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect(chatsSplitCoordinator?.sheetCoordinator == nil)
|
|
|
|
let sharePayload: ShareExtensionPayload = .mediaFiles(roomID: "2", mediaFiles: [.init(url: .picturesDirectory, suggestedName: nil)])
|
|
try await process(route: .share(sharePayload),
|
|
expectedChatsState: .roomList(detailState: .room(roomID: "2")))
|
|
|
|
let splitCoordinator = try #require(chatsSplitCoordinator)
|
|
let deferredSheet = deferFulfillment(splitCoordinator.observe(\.sheetCoordinatorID)) { $0 != nil }
|
|
try await deferredSheet.fulfill()
|
|
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect((splitCoordinator.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is MediaUploadPreviewScreenCoordinator)
|
|
}
|
|
|
|
@Test
|
|
mutating func shareTextRouteWithoutRoom() async throws {
|
|
try await process(route: .settings, expectedUserSessionState: .settingsScreen)
|
|
#expect((tabCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is SettingsScreenCoordinator)
|
|
#expect(chatsSplitCoordinator?.sheetCoordinator == nil)
|
|
|
|
let sharePayload: ShareExtensionPayload = .text(roomID: nil, text: "Important Text")
|
|
try await process(route: .share(sharePayload),
|
|
expectedUserSessionState: .tabBar,
|
|
expectedChatsState: .shareExtensionRoomList(sharePayload: sharePayload))
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect((chatsSplitCoordinator?.sheetCoordinator as? NavigationStackCoordinator)?.rootCoordinator is RoomSelectionScreenCoordinator)
|
|
}
|
|
|
|
@Test
|
|
mutating func shareTextRouteWithRoom() async throws {
|
|
try await process(route: .event(eventID: "1", roomID: "1", via: []), expectedChatsState: .roomList(detailState: .room(roomID: "1")))
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect(chatsSplitCoordinator?.sheetCoordinator == nil)
|
|
|
|
let sharePayload: ShareExtensionPayload = .text(roomID: "2", text: "Important text")
|
|
try await process(route: .share(sharePayload),
|
|
expectedChatsState: .roomList(detailState: .room(roomID: "2")))
|
|
|
|
#expect(detailNavigationStack?.rootCoordinator is RoomScreenCoordinator)
|
|
#expect(tabCoordinator?.sheetCoordinator == nil)
|
|
#expect(chatsSplitCoordinator?.sheetCoordinator == nil, "The media upload sheet shouldn't be shown when sharing text.")
|
|
}
|
|
|
|
// MARK: Indicators
|
|
|
|
@Test
|
|
func reachabilityIndicators() async throws {
|
|
// Given a flow in its initial state.
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then no reachability indicators should be shown.
|
|
#expect(!userIndicatorController.submitIndicatorDelayCalled)
|
|
#expect(retractReachabilityIndicatorCallsCount == 1) // The initial state removes the indicator.
|
|
|
|
// When the homeserver becomes unreachable.
|
|
homeserverReachabilitySubject.send(.unreachable)
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then a server unreachable indicator should be shown.
|
|
#expect(userIndicatorController.submitIndicatorDelayCallsCount == 1)
|
|
#expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.commonServerUnreachable)
|
|
#expect(retractReachabilityIndicatorCallsCount == 1)
|
|
|
|
// When the network also becomes unreachable.
|
|
networkReachabilitySubject.send(.unreachable)
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then the server unreachable indicator should be replaced with an offline indicator.
|
|
#expect(userIndicatorController.submitIndicatorDelayCallsCount == 2)
|
|
#expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.commonOffline)
|
|
#expect(retractReachabilityIndicatorCallsCount == 1)
|
|
|
|
// When the homeserver becomes reachable again.
|
|
homeserverReachabilitySubject.send(.reachable)
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then there should still be an offline indicator (as we don't yet support air-gapped servers on iOS).
|
|
#expect(userIndicatorController.submitIndicatorDelayCallsCount == 3)
|
|
#expect(userIndicatorController.submitIndicatorDelayReceivedArguments?.indicator.title == L10n.commonOffline)
|
|
#expect(retractReachabilityIndicatorCallsCount == 1)
|
|
|
|
// When the network becomes reachable again.
|
|
networkReachabilitySubject.send(.reachable)
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then the indicator should be hidden now as everything is back to normal
|
|
#expect(userIndicatorController.submitIndicatorDelayCallsCount == 3)
|
|
#expect(retractReachabilityIndicatorCallsCount == 2)
|
|
|
|
// When the client is suspended.
|
|
homeserverReachabilitySubject.send(.suspended)
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
// Then no unreachable indicator should be shown as the pause is intentional.
|
|
#expect(userIndicatorController.submitIndicatorDelayCallsCount == 3)
|
|
#expect(retractReachabilityIndicatorCallsCount == 3)
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private func process(route: AppRoute,
|
|
expectedUserSessionState: UserSessionFlowCoordinator.State? = nil,
|
|
expectedChatsState: ChatsTabFlowCoordinatorStateMachine.State? = nil) async throws {
|
|
// Keep the previous root coordinator alive while waiting, otherwise a newly presented
|
|
// coordinator could be allocated at the same address and be mistaken for it below.
|
|
let previousDetailRootCoordinator = chatsSplitCoordinator?.detailRootCoordinator
|
|
let previousDetailRootCoordinatorID = previousDetailRootCoordinator.map { ObjectIdentifier($0) }
|
|
|
|
let deferredUserSession: DeferredFulfillment<UserSessionFlowCoordinator.State>? = if let expectedUserSessionState {
|
|
deferFulfillment(stateMachineFactory.userSessionFlowStatePublisher) { $0 == expectedUserSessionState }
|
|
} else {
|
|
nil
|
|
}
|
|
|
|
let deferredChatsState: DeferredFulfillment<ChatsTabFlowCoordinatorStateMachine.State>? = if let expectedChatsState {
|
|
deferFulfillment(stateMachineFactory.chatsTabFlowStatePublisher) { $0 == expectedChatsState }
|
|
} else {
|
|
nil
|
|
}
|
|
|
|
userSessionFlowCoordinator.handleAppRoute(route, animated: true)
|
|
try await deferredUserSession?.fulfill()
|
|
try await deferredChatsState?.fulfill()
|
|
|
|
// The state machines' states change before the coordinators have updated their stacks,
|
|
// so also wait for the navigation side effects implied by the expected states.
|
|
switch expectedUserSessionState {
|
|
case .settingsScreen:
|
|
let tabCoordinator = try #require(tabCoordinator)
|
|
let deferredSheet = deferFulfillment(tabCoordinator.observe(\.sheetCoordinatorID)) { $0 != nil }
|
|
try await deferredSheet.fulfill()
|
|
case .tabBar:
|
|
let tabCoordinator = try #require(tabCoordinator)
|
|
let deferredSheet = deferFulfillment(tabCoordinator.observe(\.sheetCoordinatorID)) { $0 == nil }
|
|
try await deferredSheet.fulfill()
|
|
default:
|
|
break
|
|
}
|
|
|
|
switch expectedChatsState {
|
|
case .roomList(detailState: .some(.room)):
|
|
let splitCoordinator = try #require(chatsSplitCoordinator)
|
|
let deferredDetail = deferFulfillment(splitCoordinator.observe(\.detailRootCoordinatorID)) { $0 != nil && $0 != previousDetailRootCoordinatorID }
|
|
try await deferredDetail.fulfill()
|
|
case .shareExtensionRoomList:
|
|
let splitCoordinator = try #require(chatsSplitCoordinator)
|
|
let deferredSheet = deferFulfillment(splitCoordinator.observe(\.sheetCoordinatorID)) { $0 != nil }
|
|
try await deferredSheet.fulfill()
|
|
default:
|
|
break
|
|
}
|
|
|
|
withExtendedLifetime(previousDetailRootCoordinator) { }
|
|
}
|
|
|
|
/// Other services retract indicators, so this filters based on the reachability ID.
|
|
private var retractReachabilityIndicatorCallsCount: Int {
|
|
userIndicatorController
|
|
.retractIndicatorWithIdReceivedInvocations
|
|
.filter { $0 == "io.element.elementx.reachability.notification" }
|
|
.count
|
|
}
|
|
}
|