Files
letro-ios/UnitTests/Sources/TimelineMediaPreviewViewModelTests.swift
Copilot 4834f453ef Finish migration of UnitTests target from XCTestCase to Swift Testing (#5129)
* Initial plan

* Migrate 3 test files from XCTest to Swift Testing

- MediaUploadPreviewScreenViewModelTests: @MainActor @Suite struct with init(),
  BundleFinder class for Bundle(for:), mutating test/setup functions,
  [self] capture replacing [weak self] in closures
- NotificationManagerTests: @MainActor @Suite final class with init()/deinit,
  expectation/fulfillment(of:) replaced with confirmation(...), test_ prefix stripped
- NotificationSettingsScreenViewModelTests: @MainActor @Suite struct with
  init() throws, non-optional stored properties, test prefix stripped

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 3 XCTest files to Swift Testing

- NotificationSettingsEditScreenViewModelTests: @MainActor @Suite struct with init() throws, mutating test methods
- TimelineViewModelTests: @MainActor @Suite final class with init() async throws + deinit
- AttributedStringBuilderTests: @Suite struct with init() async throws

All XCT assertions replaced with #expect/#require/Issue.record

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 4 test files from XCTest to Swift Testing

- TimelineMediaPreviewViewModelTests: @Suite struct, mutating @Test funcs,
  testLoadingItem renamed to loadingItem (called internally by other tests)
- ServerConfirmationScreenViewModelTests: @Suite final class with init()/deinit
- CompletionSuggestionServiceTests: @Suite struct with init()
- RoomFlowCoordinatorTests: @Suite final class with deinit

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 4 test files from XCTest to Swift Testing

- VoiceMessageRecorderTests: @Suite struct with init() async throws,
  added BundleFinder class for Bundle lookup, migrated all assertions
- SpaceScreenViewModelTests: @Suite struct, private mutating setupViewModel,
  all test funcs mutating, XCTestExpectation → confirmation
- RoomNotificationSettingsScreenViewModelTests: @Suite struct with
  init() throws, cancellable tests marked mutating
- JoinRoomScreenViewModelTests: @Suite final class with init()/deinit,
  XCTestExpectation → confirmation

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate 6 test files from XCTestCase to Swift Testing

Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>

* Fix trailing blank line in RoomPollsHistoryScreenViewModelTests

Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>

* Migrate 3 test files from XCTest to Swift Testing

- MediaUploadingPreprocessorTests: @Suite final class with init()/deinit,
  removed executionTimeAllowance, XCTAssertEqual(accuracy:) → abs(Double)
- SecurityAndPrivacyScreenViewModelTests: @MainActor @Suite final class,
  5 expectation+fulfillment → await confirmation(...)
- CreateRoomViewModelTests: @MainActor @Suite final class,
  4 expectation+fulfillment → await confirmation(...)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate RoomScreenViewModelTests and RoomDetailsScreenViewModelTests to Swift Testing

- Replace XCTest with Testing framework
- RoomScreenViewModelTests: final class with init() async throws + deinit
- RoomDetailsScreenViewModelTests: struct with init() and mutating funcs
- Convert XCT assertions to #expect / Issue.record
- Convert XCTestExpectation patterns to confirmation { confirm in }
- Strip 'test' prefix from all test function names

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Migrate ComposerToolbarViewModelTests from XCTest to Swift Testing

- Replace import XCTest with import Testing
- Convert XCTestCase class to @MainActor @Suite final class
- Replace setUp()/tearDown() with init()/deinit
- Strip 'test' prefix from all 41 test method names and add @Test
- Replace XCTAssert* with #expect()/#require()
- Replace try XCTUnwrap() with try #require()
- Convert expectation+wait patterns to deferFulfillment with PassthroughSubject
- Convert isInverted expectation to boolean flag checked after await
- Use deferFulfillment on $viewState for state-transition tests

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address comments with Copilot.

* Fix the failing tests.

* Fixed flaky tests (#5137)

resolved flaky tests

* Tweaks and fixes.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: pixlwave <6060466+pixlwave@users.noreply.github.com>
Co-authored-by: Doug <douglase@element.io>
Co-authored-by: Mauro <34335419+Velin92@users.noreply.github.com>
2026-02-24 12:20:01 +00:00

348 lines
16 KiB
Swift

//
// Copyright 2025 Element Creations Ltd.
// Copyright 2022-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 MatrixRustSDK
import QuickLook
import SwiftUI
import Testing
@Suite
@MainActor
struct TimelineMediaPreviewViewModelTests {
var viewModel: TimelineMediaPreviewViewModel!
var context: TimelineMediaPreviewViewModel.Context {
viewModel.context
}
var mediaProvider: MediaProviderMock!
var photoLibraryManager: PhotoLibraryManagerMock!
var timelineController: MockTimelineController!
@Test
mutating func loadingItem() async throws {
// Given a fresh view model.
setupViewModel()
#expect(!mediaProvider.loadFileFromSourceFilenameCalled)
#expect(context.viewState.currentItem == .media(context.viewState.dataSource.previewItems[0]))
#expect(context.viewState.currentItemActions != nil)
// When the preview controller sets the current item.
try await loadInitialItem()
// Then the view model should load the item and update its view state.
#expect(mediaProvider.loadFileFromSourceFilenameCalled)
#expect(context.viewState.currentItem == .media(context.viewState.dataSource.previewItems[0]))
#expect(context.viewState.currentItemActions != nil)
}
@Test
mutating func loadingItemFailure() async throws {
// Given a fresh view model.
setupViewModel()
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item")
return
}
#expect(!mediaProvider.loadFileFromSourceFilenameCalled)
#expect(mediaItem == context.viewState.dataSource.previewItems[0])
#expect(mediaItem.downloadError == nil)
// When the preview controller sets an item that fails to load.
mediaProvider.loadFileFromSourceFilenameClosure = { _, _ in .failure(.failedRetrievingFile) }
let failure = deferFailure(viewModel.state.previewControllerDriver, timeout: .seconds(1)) { $0.isItemLoaded }
context.send(viewAction: .updateCurrentItem(.media(context.viewState.dataSource.previewItems[0])))
try await failure.fulfill()
// Then the view model should load the item and update its view state.
#expect(mediaProvider.loadFileFromSourceFilenameCalled)
#expect(mediaItem == context.viewState.dataSource.previewItems[0])
#expect(mediaItem.downloadError != nil)
}
@Test
mutating func swipingBetweenItems() async throws {
// Given a view model with a loaded item.
try await loadingItem()
// When swiping to another item.
let deferred = deferFulfillment(viewModel.state.previewControllerDriver) { $0.isItemLoaded }
context.send(viewAction: .updateCurrentItem(.media(context.viewState.dataSource.previewItems[1])))
try await deferred.fulfill()
// Then the view model should load the item and update its view state.
#expect(mediaProvider.loadFileFromSourceFilenameCallsCount == 2)
#expect(context.viewState.currentItem == .media(context.viewState.dataSource.previewItems[1]))
// When swiping back to the first item.
let failure = deferFailure(viewModel.state.previewControllerDriver, timeout: .seconds(1)) { $0.isItemLoaded }
context.send(viewAction: .updateCurrentItem(.media(context.viewState.dataSource.previewItems[0])))
try await failure.fulfill()
// Then the view model should not need to load the item, but should still update its view state.
#expect(mediaProvider.loadFileFromSourceFilenameCallsCount == 2)
#expect(context.viewState.currentItem == .media(context.viewState.dataSource.previewItems[0]))
}
@Test
mutating func loadingMoreItems() async throws {
// Given a view model with a loaded item.
try await loadingItem()
#expect(timelineController.paginateBackwardsCallCount == 0)
// When swiping to a "loading more" item and there are more media items to load.
timelineController.paginationState = .init(backward: .idle, forward: .endReached)
timelineController.backPaginationResponses.append(RoomTimelineItemFixtures.mediaChunk)
let failure = deferFailure(viewModel.state.previewControllerDriver, timeout: .seconds(1)) { $0.isItemLoaded }
context.send(viewAction: .updateCurrentItem(.loading(.paginatingBackwards)))
try await failure.fulfill()
// Then there should no longer be a media preview and instead of loading any media, a pagination request should be made.
#expect(mediaProvider.loadFileFromSourceFilenameCallsCount == 1)
#expect(context.viewState.currentItem == .loading(.paginatingBackwards)) // Note: This item only changes when the preview controller handles the new items.
#expect(timelineController.paginateBackwardsCallCount == 1)
}
@Test
mutating func pagination() async throws {
// Given a view model with a loaded item.
try await loadingItem()
#expect(context.viewState.dataSource.previewItems.count == 3)
// When more items are added via a back pagination.
let deferred = deferFulfillment(context.viewState.dataSource.previewItemsPaginationPublisher) { _ in true }
timelineController.backPaginationResponses.append(makeItems())
_ = await timelineController.paginateBackwards(requestSize: 20)
try await deferred.fulfill()
// And the preview controller attempts to update the current item (now at a new index in the array but it hasn't changed in the data source).
mediaProvider.loadFileFromSourceFilenameClosure = { _, _ in .failure(.failedRetrievingFile) }
let failure = deferFailure(viewModel.state.previewControllerDriver, timeout: .seconds(1)) { $0.isItemLoaded }
context.send(viewAction: .updateCurrentItem(.media(context.viewState.dataSource.previewItems[3])))
try await failure.fulfill()
// Then the current item shouldn't need to be reloaded.
#expect(context.viewState.dataSource.previewItems.count == 6)
#expect(mediaProvider.loadFileFromSourceFilenameCallsCount == 1)
}
@Test
mutating func viewInRoomTimeline() async throws {
// Given a view model with a loaded item.
try await loadingItem()
// When choosing to view the current item in the timeline.
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item.")
return
}
let deferred = deferFulfillment(viewModel.actions) { $0 == .viewInRoomTimeline(mediaItem.timelineItem.id) }
context.send(viewAction: .menuAction(.viewInRoomTimeline, item: mediaItem))
// Then the action should be sent upwards to make this happen.
try await deferred.fulfill()
}
@Test
mutating func redactConfirmation() async throws {
// Given a view model with a loaded item.
try await loadingItem()
#expect(context.redactConfirmationItem == nil)
#expect(!timelineController.redactCalled)
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item.")
return
}
// When choosing to show the item details.
let deferredDriver = deferFulfillment(context.viewState.previewControllerDriver) { $0.isShowItemDetails }
context.send(viewAction: .showItemDetails(mediaItem))
// Then the details sheet should be presented.
let action = try await deferredDriver.fulfill()
guard case let .showItemDetails(mediaDetailsItem) = action else {
Issue.record("The action should include the media item.")
return
}
#expect(.media(mediaDetailsItem) == context.viewState.currentItem)
// When choosing to redact the item.
context.send(viewAction: .menuAction(.redact, item: mediaItem))
// Then the confirmation sheet should be presented.
#expect(context.redactConfirmationItem == mediaItem)
#expect(!timelineController.redactCalled)
// When confirming the redaction.
let deferred = deferFulfillment(viewModel.actions) { $0 == .dismiss }
context.send(viewAction: .redactConfirmation(item: mediaItem))
// Then the item should be redacted and the view should be dismissed.
try await deferred.fulfill()
#expect(timelineController.redactCalled)
}
@Test
mutating func saveImage() async throws {
// Given a view model with a loaded image.
try await loadingItem()
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item")
return
}
#expect(mediaItem.contentType == "JPEG image")
// When choosing to save the image.
context.send(viewAction: .menuAction(.save, item: mediaItem))
try await Task.sleep(for: .seconds(0.5))
// Then the image should be saved as a photo to the user's photo library.
#expect(photoLibraryManager.addResourceAtCalled)
#expect(photoLibraryManager.addResourceAtReceivedArguments?.type == .photo)
#expect(photoLibraryManager.addResourceAtReceivedArguments?.url == mediaItem.fileHandle?.url)
}
@Test
mutating func saveImageWithoutAuthorization() async throws {
// Given a view model with a loaded image where the user has denied access to the photo library.
setupViewModel(photoLibraryAuthorizationDenied: true)
try await loadInitialItem()
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item")
return
}
#expect(mediaItem.contentType == "JPEG image")
// When choosing to save the image.
let deferred = deferFulfillment(context.viewState.previewControllerDriver) { $0.isAuthorizationRequired }
context.send(viewAction: .menuAction(.save, item: mediaItem))
// Then the user should be prompted to allow access.
try await deferred.fulfill()
#expect(photoLibraryManager.addResourceAtCalled)
}
@Test
mutating func saveVideo() async throws {
// Given a view model with a loaded video.
setupViewModel(initialItemIndex: 1)
try await loadInitialItem()
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item")
return
}
#expect(mediaItem.contentType == "MPEG-4 movie")
// When choosing to save the video.
context.send(viewAction: .menuAction(.save, item: mediaItem))
try await Task.sleep(for: .seconds(0.5))
// Then the video should be saved as a video in the user's photo library.
#expect(photoLibraryManager.addResourceAtCalled)
#expect(photoLibraryManager.addResourceAtReceivedArguments?.type == .video)
#expect(photoLibraryManager.addResourceAtReceivedArguments?.url == mediaItem.fileHandle?.url)
}
@Test
mutating func saveFile() async throws {
// Given a view model with a loaded file.
setupViewModel(initialItemIndex: 2)
try await loadInitialItem()
guard case let .media(mediaItem) = context.viewState.currentItem else {
Issue.record("There should be a current item")
return
}
#expect(mediaItem.contentType == "PDF document")
// When choosing to save the file.
let deferred = deferFulfillment(context.viewState.previewControllerDriver) { $0.isExportFile }
context.send(viewAction: .menuAction(.save, item: mediaItem))
let exportAction = try await deferred.fulfill()
guard case let .exportFile(file) = exportAction else {
Issue.record("Unexpected action")
return
}
// Then the binding should be set for the user to export the file to their specified location.
#expect(!photoLibraryManager.addResourceAtCalled)
#expect(file.url == mediaItem.fileHandle?.url)
}
// MARK: - Helpers
private func loadInitialItem() async throws {
let deferred = deferFulfillment(viewModel.state.previewControllerDriver) { $0.isItemLoaded }
let initialItem = context.viewState.dataSource.previewController(QLPreviewController(),
previewItemAt: context.viewState.dataSource.initialItemIndex)
guard let initialPreviewItem = initialItem as? TimelineMediaPreviewItem.Media else {
Issue.record("The initial item should be a media preview.")
return
}
context.send(viewAction: .updateCurrentItem(.media(initialPreviewItem)))
try await deferred.fulfill()
}
private mutating func setupViewModel(initialItemIndex: Int = 0, photoLibraryAuthorizationDenied: Bool = false) {
let initialItems = makeItems()
timelineController = MockTimelineController(timelineKind: .media(.mediaFilesScreen))
timelineController.timelineItems = initialItems
mediaProvider = MediaProviderMock(configuration: .init())
photoLibraryManager = PhotoLibraryManagerMock(.init(authorizationDenied: photoLibraryAuthorizationDenied))
viewModel = TimelineMediaPreviewViewModel(initialItem: initialItems[initialItemIndex],
timelineViewModel: TimelineViewModel.mock(timelineKind: .media(.mediaFilesScreen),
timelineController: timelineController),
mediaProvider: mediaProvider,
photoLibraryManager: photoLibraryManager,
userIndicatorController: UserIndicatorControllerMock(),
appMediator: AppMediatorMock())
}
private func makeItems() -> [EventBasedMessageTimelineItemProtocol] {
[
ImageRoomTimelineItem(id: .randomEvent,
timestamp: .mock,
isOutgoing: false,
isEditable: false,
canBeRepliedTo: true,
sender: .init(id: "", displayName: "Sally Sanderson"),
content: .init(filename: "Amazing image.jpeg",
caption: "A caption goes right here.",
imageInfo: .mockImage,
thumbnailInfo: .mockThumbnail,
contentType: .jpeg)),
VideoRoomTimelineItem(id: .randomEvent,
timestamp: .mock,
isOutgoing: false,
isEditable: false,
canBeRepliedTo: true,
sender: .init(id: ""),
content: .init(filename: "Super video.mp4",
videoInfo: .mockVideo,
thumbnailInfo: .mockThumbnail,
contentType: .mpeg4Movie)),
FileRoomTimelineItem(id: .randomEvent,
timestamp: .mock,
isOutgoing: false,
isEditable: false,
canBeRepliedTo: true,
sender: .init(id: ""),
content: .init(filename: "Important file.pdf",
source: try? .init(url: .mockMXCFile, mimeType: "document/pdf"),
fileSize: 2453,
thumbnailSource: nil,
contentType: .pdf))
]
}
}