2022-07-06 14:49:05 +01:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2022-2024 New Vector Ltd.
|
2022-02-14 18:05:21 +02:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2022-02-14 18:05:21 +02:00
|
|
|
//
|
|
|
|
|
2023-06-20 12:20:26 +03:00
|
|
|
import Combine
|
2022-02-14 18:05:21 +02:00
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
2023-04-05 17:07:12 +02:00
|
|
|
@MainActor
|
2022-02-14 18:05:21 +02:00
|
|
|
class HomeScreenViewModelTests: XCTestCase {
|
2022-06-06 12:38:07 +03:00
|
|
|
var viewModel: HomeScreenViewModelProtocol!
|
2023-09-14 12:53:33 +03:00
|
|
|
var context: HomeScreenViewModelType.Context! { viewModel.context }
|
2025-02-18 12:22:13 +00:00
|
|
|
|
|
|
|
var clientProxy: ClientProxyMock!
|
2024-02-28 18:38:13 +02:00
|
|
|
var roomSummaryProvider: RoomSummaryProviderMock!
|
2025-02-18 12:22:13 +00:00
|
|
|
var appSettings: AppSettings!
|
|
|
|
|
|
|
|
var cancellables = Set<AnyCancellable>()
|
2022-09-21 11:21:58 +03:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
override func setUp() {
|
2023-09-14 12:53:33 +03:00
|
|
|
cancellables.removeAll()
|
2025-02-18 12:22:13 +00:00
|
|
|
|
|
|
|
AppSettings.resetAllSettings()
|
|
|
|
appSettings = AppSettings()
|
|
|
|
ServiceLocator.shared.register(appSettings: appSettings)
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
2022-09-21 11:21:58 +03:00
|
|
|
|
2024-02-08 16:50:44 +01:00
|
|
|
override func tearDown() {
|
2024-03-21 14:01:23 +02:00
|
|
|
AppSettings.resetAllSettings()
|
2024-02-08 16:50:44 +01:00
|
|
|
}
|
|
|
|
|
2023-04-05 17:07:12 +02:00
|
|
|
func testSelectRoom() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
let mockRoomID = "mock_room_id"
|
2022-06-06 12:38:07 +03:00
|
|
|
var correctResult = false
|
2025-02-18 12:22:13 +00:00
|
|
|
var selectedRoomID = ""
|
2023-09-14 12:53:33 +03:00
|
|
|
|
|
|
|
viewModel.actions
|
|
|
|
.sink { action in
|
|
|
|
switch action {
|
2025-02-18 12:22:13 +00:00
|
|
|
case .presentRoom(let roomID):
|
2023-09-14 12:53:33 +03:00
|
|
|
correctResult = true
|
2025-02-18 12:22:13 +00:00
|
|
|
selectedRoomID = roomID
|
2023-09-14 12:53:33 +03:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
2023-09-14 12:53:33 +03:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
context.send(viewAction: .selectRoom(roomIdentifier: mockRoomID))
|
2022-06-06 12:38:07 +03:00
|
|
|
await Task.yield()
|
|
|
|
XCTAssert(correctResult)
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertEqual(mockRoomID, selectedRoomID)
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
2022-06-06 12:38:07 +03:00
|
|
|
|
2023-04-05 17:07:12 +02:00
|
|
|
func testTapUserAvatar() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2022-06-06 12:38:07 +03:00
|
|
|
var correctResult = false
|
2023-09-14 12:53:33 +03:00
|
|
|
|
|
|
|
viewModel.actions
|
|
|
|
.sink { action in
|
|
|
|
switch action {
|
|
|
|
case .presentSettingsScreen:
|
|
|
|
correctResult = true
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2022-06-06 12:38:07 +03:00
|
|
|
}
|
2023-09-14 12:53:33 +03:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2024-02-20 14:36:04 +01:00
|
|
|
context.send(viewAction: .showSettings)
|
2022-06-06 12:38:07 +03:00
|
|
|
await Task.yield()
|
|
|
|
XCTAssert(correctResult)
|
|
|
|
}
|
2023-05-04 12:02:18 +02:00
|
|
|
|
|
|
|
func testLeaveRoomAlert() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
let mockRoomID = "1"
|
2024-02-27 16:22:47 +02:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
clientProxy.roomForIdentifierClosure = { _ in .joined(JoinedRoomProxyMock(.init(id: mockRoomID, name: "Some room"))) }
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(context.$viewState) { value in
|
|
|
|
value.bindings.leaveRoomAlertItem != nil
|
|
|
|
}
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
context.send(viewAction: .leaveRoom(roomIdentifier: mockRoomID))
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
XCTAssertEqual(context.leaveRoomAlertItem?.roomID, mockRoomID)
|
2023-05-04 12:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testLeaveRoomError() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
let mockRoomID = "1"
|
|
|
|
let room = JoinedRoomProxyMock(.init(id: mockRoomID, name: "Some room"))
|
2024-04-04 10:17:55 +03:00
|
|
|
room.leaveRoomClosure = { .failure(.sdkError(ClientProxyMockError.generic)) }
|
2024-02-27 16:22:47 +02:00
|
|
|
|
2024-08-20 16:13:27 +03:00
|
|
|
clientProxy.roomForIdentifierClosure = { _ in .joined(room) }
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
let deferred = deferFulfillment(context.$viewState) { value in
|
|
|
|
value.bindings.alertInfo != nil
|
|
|
|
}
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
context.send(viewAction: .confirmLeaveRoom(roomIdentifier: mockRoomID))
|
2023-09-26 13:28:29 +03:00
|
|
|
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
XCTAssertNotNil(context.alertInfo)
|
2023-05-04 12:02:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func testLeaveRoomSuccess() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
let mockRoomID = "1"
|
2023-05-04 12:02:18 +02:00
|
|
|
var correctResult = false
|
2023-07-11 10:08:27 +02:00
|
|
|
let expectation = expectation(description: #function)
|
2023-09-14 12:53:33 +03:00
|
|
|
viewModel.actions
|
|
|
|
.sink { action in
|
|
|
|
switch action {
|
|
|
|
case .roomLeft(let roomIdentifier):
|
2025-02-18 12:22:13 +00:00
|
|
|
correctResult = roomIdentifier == mockRoomID
|
2023-09-14 12:53:33 +03:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
expectation.fulfill()
|
2023-05-04 12:02:18 +02:00
|
|
|
}
|
2023-09-14 12:53:33 +03:00
|
|
|
.store(in: &cancellables)
|
2025-02-18 12:22:13 +00:00
|
|
|
let room = JoinedRoomProxyMock(.init(id: mockRoomID, name: "Some room"))
|
2023-05-04 12:02:18 +02:00
|
|
|
room.leaveRoomClosure = { .success(()) }
|
2024-02-27 16:22:47 +02:00
|
|
|
|
2024-08-20 16:13:27 +03:00
|
|
|
clientProxy.roomForIdentifierClosure = { _ in .joined(room) }
|
2024-02-27 16:22:47 +02:00
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
context.send(viewAction: .confirmLeaveRoom(roomIdentifier: mockRoomID))
|
2023-07-11 10:08:27 +02:00
|
|
|
await fulfillment(of: [expectation])
|
2023-05-04 12:02:18 +02:00
|
|
|
XCTAssertNil(context.alertInfo)
|
|
|
|
XCTAssertTrue(correctResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testShowRoomDetails() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
let mockRoomID = "1"
|
2023-05-04 12:02:18 +02:00
|
|
|
var correctResult = false
|
2023-09-14 12:53:33 +03:00
|
|
|
viewModel.actions
|
|
|
|
.sink { action in
|
|
|
|
switch action {
|
|
|
|
case .presentRoomDetails(let roomIdentifier):
|
2025-02-18 12:22:13 +00:00
|
|
|
correctResult = roomIdentifier == mockRoomID
|
2023-09-14 12:53:33 +03:00
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2023-05-04 12:02:18 +02:00
|
|
|
}
|
2023-09-14 12:53:33 +03:00
|
|
|
.store(in: &cancellables)
|
2025-02-18 12:22:13 +00:00
|
|
|
context.send(viewAction: .showRoomDetails(roomIdentifier: mockRoomID))
|
2023-05-04 12:02:18 +02:00
|
|
|
await Task.yield()
|
|
|
|
XCTAssertNil(context.alertInfo)
|
|
|
|
XCTAssertTrue(correctResult)
|
|
|
|
}
|
2024-02-08 16:50:44 +01:00
|
|
|
|
|
|
|
func testFilters() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2024-02-13 02:09:55 +01:00
|
|
|
context.filtersState.activateFilter(.people)
|
2024-02-08 16:50:44 +01:00
|
|
|
try await Task.sleep(for: .milliseconds(100))
|
2024-02-28 18:38:13 +02:00
|
|
|
XCTAssertEqual(roomSummaryProvider.roomListPublisher.value.count, 2)
|
|
|
|
XCTAssertEqual(roomSummaryProvider.roomListPublisher.value.first?.name, "Foundation and Earth")
|
2024-03-06 11:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSearch() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2024-02-08 16:50:44 +01:00
|
|
|
context.isSearchFieldFocused = true
|
2024-02-28 18:38:13 +02:00
|
|
|
context.searchQuery = "lude to Found"
|
2024-02-08 16:50:44 +01:00
|
|
|
try await Task.sleep(for: .milliseconds(100))
|
2024-02-28 18:38:13 +02:00
|
|
|
XCTAssertEqual(roomSummaryProvider.roomListPublisher.value.first?.name, "Prelude to Foundation")
|
|
|
|
XCTAssertEqual(roomSummaryProvider.roomListPublisher.value.count, 1)
|
2024-03-06 11:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func testFiltersEmptyState() async throws {
|
2024-11-18 11:38:20 +00:00
|
|
|
setupViewModel()
|
|
|
|
|
2024-03-06 11:02:30 +01:00
|
|
|
context.filtersState.activateFilter(.people)
|
|
|
|
context.filtersState.activateFilter(.favourites)
|
|
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
|
|
XCTAssertTrue(context.viewState.shouldShowEmptyFilterState)
|
|
|
|
context.isSearchFieldFocused = true
|
|
|
|
XCTAssertFalse(context.viewState.shouldShowEmptyFilterState)
|
2024-02-08 16:50:44 +01:00
|
|
|
}
|
2024-11-18 11:38:20 +00:00
|
|
|
|
|
|
|
func testSetUpRecoveryBannerState() async throws {
|
|
|
|
// Given a view model without a visible security banner.
|
|
|
|
let securityStateStateSubject = CurrentValueSubject<SessionSecurityState, Never>(.init(verificationState: .verified, recoveryState: .unknown))
|
|
|
|
setupViewModel(securityStatePublisher: securityStateStateSubject.asCurrentValuePublisher())
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .none)
|
|
|
|
|
|
|
|
// When the recovery state comes through as disabled.
|
|
|
|
var deferred = deferFulfillment(context.$viewState) { $0.requiresExtraAccountSetup == true }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .disabled))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// Then the banner should be shown to set up recovery.
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .show(.setUpRecovery))
|
|
|
|
|
|
|
|
// When the recovery is enabled.
|
|
|
|
deferred = deferFulfillment(context.$viewState) { $0.requiresExtraAccountSetup == false }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .enabled))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// Then the banner should no longer be shown.
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .none)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDismissSetUpRecoveryBannerState() async throws {
|
|
|
|
// Given a view model with the setup recovery banner shown.
|
|
|
|
let securityStateStateSubject = CurrentValueSubject<SessionSecurityState, Never>(.init(verificationState: .verified, recoveryState: .unknown))
|
|
|
|
setupViewModel(securityStatePublisher: securityStateStateSubject.asCurrentValuePublisher())
|
|
|
|
var deferred = deferFulfillment(context.$viewState) { $0.securityBannerMode == .show(.setUpRecovery) }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .disabled))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// When the banner is dismissed.
|
|
|
|
deferred = deferFulfillment(context.$viewState) { $0.securityBannerMode == .dismissed }
|
|
|
|
context.send(viewAction: .skipRecoveryKeyConfirmation)
|
|
|
|
|
|
|
|
// Then the banner should no longer be shown.
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// And when the recovery state comes through a second time the banner should still not be shown.
|
|
|
|
let failure = deferFailure(context.$viewState, timeout: 1) { $0.securityBannerMode != .dismissed }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .disabled))
|
|
|
|
try await failure.fulfill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testOutOfSyncRecoveryBannerState() async throws {
|
|
|
|
// Given a view model without a visible security banner.
|
|
|
|
let securityStateStateSubject = CurrentValueSubject<SessionSecurityState, Never>(.init(verificationState: .verified, recoveryState: .unknown))
|
|
|
|
setupViewModel(securityStatePublisher: securityStateStateSubject.asCurrentValuePublisher())
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .none)
|
|
|
|
|
|
|
|
// When the recovery state comes through as incomplete.
|
|
|
|
var deferred = deferFulfillment(context.$viewState) { $0.requiresExtraAccountSetup == true }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .incomplete))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// Then the banner should be shown for out of sync recovery.
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .show(.recoveryOutOfSync))
|
|
|
|
|
|
|
|
// When the recovery is enabled.
|
|
|
|
deferred = deferFulfillment(context.$viewState) { $0.requiresExtraAccountSetup == false }
|
|
|
|
securityStateStateSubject.send(.init(verificationState: .verified, recoveryState: .enabled))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
// Then the banner should no longer be shown.
|
|
|
|
XCTAssertEqual(context.viewState.securityBannerMode, .none)
|
|
|
|
}
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
func testInviteUnreadBadge() async throws {
|
|
|
|
setupViewModel(withInvites: true)
|
|
|
|
var invites = context.viewState.rooms.invites
|
|
|
|
XCTAssertEqual(invites.count, 2)
|
|
|
|
|
|
|
|
for invite in invites {
|
|
|
|
XCTAssertTrue(invite.badges.isDotShown)
|
|
|
|
}
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(context.$viewState) { state in
|
|
|
|
state.rooms.contains { room in
|
|
|
|
room.roomID == invites[0].roomID && room.badges.isDotShown == false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
appSettings.seenInvites = Set(invites.compactMap(\.roomID))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
invites = context.viewState.rooms.invites
|
|
|
|
|
|
|
|
for invite in invites {
|
|
|
|
XCTAssertFalse(invite.badges.isDotShown)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAcceptInvite() async throws {
|
|
|
|
setupViewModel(withInvites: true)
|
|
|
|
|
|
|
|
let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID)
|
|
|
|
appSettings.seenInvites = Set(invitedRoomIDs)
|
|
|
|
XCTAssertEqual(invitedRoomIDs.count, 2)
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(viewModel.actions) { $0 == .presentRoom(roomIdentifier: invitedRoomIDs[0]) }
|
|
|
|
context.send(viewAction: .acceptInvite(roomIdentifier: invitedRoomIDs[0]))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
XCTAssertEqual(appSettings.seenInvites, [invitedRoomIDs[1]])
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDeclineInvite() async throws {
|
|
|
|
setupViewModel(withInvites: true)
|
|
|
|
|
|
|
|
let invitedRoomIDs = context.viewState.rooms.invites.compactMap(\.roomID)
|
|
|
|
appSettings.seenInvites = Set(invitedRoomIDs)
|
|
|
|
XCTAssertEqual(invitedRoomIDs.count, 2)
|
|
|
|
|
|
|
|
let deferred = deferFulfillment(context.$viewState) { $0.bindings.alertInfo != nil }
|
|
|
|
context.send(viewAction: .declineInvite(roomIdentifier: invitedRoomIDs[0]))
|
|
|
|
try await deferred.fulfill()
|
|
|
|
|
|
|
|
let rejectExpectation = expectation(description: "Expected rejectInvitation to be called.")
|
|
|
|
clientProxy.roomForIdentifierClosure = { _ in
|
|
|
|
let roomProxy = InvitedRoomProxyMock(.init())
|
|
|
|
roomProxy.rejectInvitationClosure = {
|
|
|
|
rejectExpectation.fulfill()
|
|
|
|
return .success(())
|
|
|
|
}
|
|
|
|
|
|
|
|
return .invited(roomProxy)
|
|
|
|
}
|
|
|
|
context.viewState.bindings.alertInfo?.primaryButton.action?()
|
|
|
|
await fulfillment(of: [rejectExpectation], timeout: 1.0)
|
|
|
|
|
|
|
|
XCTAssertEqual(appSettings.seenInvites, [invitedRoomIDs[1]])
|
|
|
|
}
|
|
|
|
|
2024-11-18 11:38:20 +00:00
|
|
|
// MARK: - Helpers
|
|
|
|
|
2025-02-18 12:22:13 +00:00
|
|
|
private func setupViewModel(securityStatePublisher: CurrentValuePublisher<SessionSecurityState, Never>? = nil, withInvites: Bool = false) {
|
|
|
|
var rooms: [RoomSummary] = .mockRooms
|
|
|
|
if withInvites {
|
|
|
|
rooms += .mockInvites
|
|
|
|
}
|
|
|
|
|
|
|
|
roomSummaryProvider = RoomSummaryProviderMock(.init(state: .loaded(rooms)))
|
|
|
|
|
2024-11-18 11:38:20 +00:00
|
|
|
clientProxy = ClientProxyMock(.init(userID: "@mock:client.com",
|
|
|
|
roomSummaryProvider: roomSummaryProvider))
|
2025-02-18 12:22:13 +00:00
|
|
|
if withInvites {
|
|
|
|
clientProxy.joinRoomViaReturnValue = .success(())
|
|
|
|
clientProxy.joinRoomAliasReturnValue = .success(())
|
|
|
|
clientProxy.roomForIdentifierClosure = { _ in .invited(InvitedRoomProxyMock(.init())) }
|
|
|
|
}
|
|
|
|
|
2024-11-18 11:38:20 +00:00
|
|
|
let userSession = UserSessionMock(.init(clientProxy: clientProxy))
|
|
|
|
if let securityStatePublisher {
|
|
|
|
userSession.sessionSecurityStatePublisher = securityStatePublisher
|
|
|
|
}
|
|
|
|
|
|
|
|
viewModel = HomeScreenViewModel(userSession: userSession,
|
|
|
|
analyticsService: ServiceLocator.shared.analytics,
|
2025-02-18 12:22:13 +00:00
|
|
|
appSettings: appSettings,
|
2024-11-18 11:38:20 +00:00
|
|
|
selectedRoomPublisher: CurrentValueSubject<String?, Never>(nil).asCurrentValuePublisher(),
|
|
|
|
userIndicatorController: ServiceLocator.shared.userIndicatorController)
|
|
|
|
}
|
2022-02-14 18:05:21 +02:00
|
|
|
}
|
2025-02-18 12:22:13 +00:00
|
|
|
|
|
|
|
private extension [HomeScreenRoom] {
|
|
|
|
var invites: [HomeScreenRoom] {
|
|
|
|
filter { room in
|
|
|
|
if case .invite = room.type {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|