mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
a520d94760
* Enable Swift 6.2 with default main actor isolation for the app target Turn on SWIFT_VERSION 6.2, SWIFT_APPROACHABLE_CONCURRENCY and SWIFT_DEFAULT_ACTOR_ISOLATION = MainActor for the ElementX target, matching the NSE and Share Extension (which already use 6.2 without the main actor default). # Conflicts: # ElementX.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved * Update the code generation templates for Swift 6 Emit nonisolated/@unchecked Sendable mocks and nonisolated string enums, and mark async mock methods @concurrent so they keep suspending like the real implementations (otherwise intermediate states are unobservable in tests). Includes the regenerated mocks and strings. # Conflicts: # ElementX/Sources/Mocks/Generated/GeneratedMocks.swift * Make the data layer and services nonisolated and Sendable Annotate the value-type models, Rust SDK proxy wrappers, timeline item graph, settings, logging and NSE-shared types as nonisolated, and conform the service protocols to Sendable so their existentials can cross isolation boundaries. These types run off the main actor and no longer inherit the target's default main actor isolation. * Adopt the new isolation across call sites, mocks and signatures Update call sites, closure signatures and mock configuration helpers to match the nonisolated/Sendable data layer, including the main actor isolated mock inits and the @Sendable filter on recentlyVisitedRooms. ^ Conflicts: ^ ElementX/Sources/Services/SecureBackup/SecureBackupController.swift * Remove redundant @MainActor annotations With the app target defaulting to main actor isolation, the explicit @MainActor on view models, coordinators, views and their protocols is no longer required. * Fix concurrency hotspots surfaced by the migration Resolve the room summary main-thread deadlock by moving diff processing onto AsyncStreams, add the SDKListener.onMainActor FIFO bridge for SDK callbacks, hop KVO/timer/UIKit callbacks to the main actor via assumeIsolated, add isolated deinits, and apply the few justified nonisolated(unsafe) escapes for queue-confined Apple APIs. * Fix the unit tests for Swift 6 concurrency Hop the waitForConfirmation body onto the caller's actor (Swift Testing runs confirmation closures off-actor), and adjust the affected suites and async signatures. * offload diffs on concurrent threads instead of main actor * make room info protocols sendable * use a task group for fetching data so that everything runs concurrently in the TextRoomTimelineView when getting the preview. * use the generic onMainActor implementation, which already makes a stream and send the continuation on the main actor. * small pr comments issues being addressed * update enterprise * Update StaticCode to 1.1.0 and specify the required Swift isolation. * fix ElementCallServiceTests * update enterprise * re-generate mocks * update enterprise * enterprise new code conformed to swift 6 --------- Co-authored-by: Doug <douglase@element.io>
184 lines
7.9 KiB
Swift
184 lines
7.9 KiB
Swift
//
|
|
// Copyright 2026 Element Creations Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
@testable import ElementX
|
|
import Foundation
|
|
import Testing
|
|
|
|
final class ClassicAppMediaLoaderTests {
|
|
let account: ClassicAppAccount
|
|
let mediaLoader: ClassicAppMediaLoader
|
|
let urlSession: URLSession
|
|
|
|
@MainActor
|
|
init() throws {
|
|
account = ClassicAppAccount(userID: "@alice:matrix.org",
|
|
displayName: nil,
|
|
avatarURL: nil,
|
|
serverName: "matrix.org",
|
|
homeserverURL: "https://matrix-client.matrix.org",
|
|
cryptoStoreURL: .temporaryDirectory,
|
|
cryptoStorePassphrase: "",
|
|
accessToken: MockURLProtocol.accessToken)
|
|
|
|
let configuration = URLSessionConfiguration.ephemeral
|
|
configuration.protocolClasses = [MockURLProtocol.self]
|
|
urlSession = URLSession(configuration: configuration)
|
|
|
|
mediaLoader = ClassicAppMediaLoader(classicAppAccount: account, urlSession: urlSession)
|
|
}
|
|
|
|
// MARK: - Images
|
|
|
|
@Test
|
|
func loadMediaContent() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
let data = try await mediaLoader.loadMediaContentForSource(source)
|
|
#expect(data == MockURLProtocol.downloadData)
|
|
}
|
|
|
|
@MainActor
|
|
@Test
|
|
func loadMediaContentWithInvalidToken() async throws {
|
|
let accountWithoutToken = ClassicAppAccount(userID: "@bob:matrix.org",
|
|
displayName: nil,
|
|
avatarURL: nil,
|
|
serverName: "matrix.org",
|
|
homeserverURL: "https://matrix-client.matrix.org",
|
|
cryptoStoreURL: .temporaryDirectory,
|
|
cryptoStorePassphrase: "",
|
|
accessToken: "wrongToken")
|
|
let loaderWithoutToken = ClassicAppMediaLoader(classicAppAccount: accountWithoutToken, urlSession: urlSession)
|
|
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
await #expect(throws: MediaLoaderError.unexpectedResponse) {
|
|
try await loaderWithoutToken.loadMediaContentForSource(source)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func loadMediaContentWith404() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.notFoundMXCURL, mimeType: nil)
|
|
await #expect(throws: MediaLoaderError.unexpectedResponse) {
|
|
try await mediaLoader.loadMediaContentForSource(source)
|
|
}
|
|
}
|
|
|
|
// MARK: - Thumbnails
|
|
|
|
@Test
|
|
func loadMediaThumbnail() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
let data = try await mediaLoader.loadMediaThumbnailForSource(source, width: 100, height: 100)
|
|
#expect(data == MockURLProtocol.thumbnailData)
|
|
}
|
|
|
|
@Test
|
|
func loadMediaThumbnailWith404() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.notFoundMXCURL, mimeType: nil)
|
|
await #expect(throws: MediaLoaderError.unexpectedResponse) {
|
|
try await mediaLoader.loadMediaThumbnailForSource(source, width: 100, height: 100)
|
|
}
|
|
}
|
|
|
|
// MARK: - Files (unsupported)
|
|
|
|
@Test
|
|
func loadMediaFileIsNotSupported() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
await #expect(throws: MediaLoaderError.notSupported) {
|
|
try await mediaLoader.loadMediaFileForSource(source, filename: nil)
|
|
}
|
|
}
|
|
|
|
// MARK: - URL construction
|
|
|
|
@Test
|
|
func loadMediaContentBuildsCorrectDownloadURL() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
let capturedRequest = try await mediaLoader.loadMediaContentForSource(source)
|
|
|
|
// The download path must use the download endpoint with no query string.
|
|
#expect(MockURLProtocol.lastRequest?.url?.path() == "/_matrix/client/v1/media/download/matrix.org/testmediaid")
|
|
#expect(MockURLProtocol.lastRequest?.url?.query() == nil)
|
|
#expect(MockURLProtocol.lastRequest?.value(forHTTPHeaderField: "Authorization") == "Bearer \(MockURLProtocol.accessToken)")
|
|
_ = capturedRequest
|
|
}
|
|
|
|
@Test
|
|
func loadMediaThumbnailBuildsCorrectThumbnailURL() async throws {
|
|
let source = try MediaSourceProxy(url: MockURLProtocol.mxcURL, mimeType: nil)
|
|
_ = try await mediaLoader.loadMediaThumbnailForSource(source, width: 320, height: 240)
|
|
|
|
// The thumbnail path must use the thumbnail endpoint with width/height query items.
|
|
let url = try #require(MockURLProtocol.lastRequest?.url)
|
|
#expect(url.path() == "/_matrix/client/v1/media/thumbnail/matrix.org/testmediaid")
|
|
let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems
|
|
#expect(queryItems?.contains(URLQueryItem(name: "width", value: "320")) == true)
|
|
#expect(queryItems?.contains(URLQueryItem(name: "height", value: "240")) == true)
|
|
#expect(MockURLProtocol.lastRequest?.value(forHTTPHeaderField: "Authorization") == "Bearer \(MockURLProtocol.accessToken)")
|
|
}
|
|
}
|
|
|
|
// MARK: - MockURLProtocol
|
|
|
|
private class MockURLProtocol: URLProtocol {
|
|
/// The MXC URL whose media requests will be served successfully.
|
|
static let mxcURL: URL = "mxc://matrix.org/testmediaid"
|
|
/// The MXC URL whose media requests will return a 404.
|
|
static let notFoundMXCURL: URL = "mxc://matrix.org/notfound"
|
|
|
|
/// The access token expected on all authenticated requests.
|
|
static let accessToken = "testAccessToken"
|
|
|
|
/// The data returned for a full-size download of `mxcURL`.
|
|
static let downloadData = Data("download data".utf8)
|
|
/// The data returned for a thumbnail download of `mxcURL`.
|
|
static let thumbnailData = Data("thumbnail data".utf8)
|
|
|
|
/// The last request handled, for URL/header inspection in tests.
|
|
static var lastRequest: URLRequest?
|
|
|
|
/// Maps a URL path to a fixed `(statusCode, Data)` response.
|
|
private static let responses: [String: (Int, Data)] = [
|
|
"/_matrix/client/v1/media/download/matrix.org/testmediaid": (200, downloadData),
|
|
"/_matrix/client/v1/media/thumbnail/matrix.org/testmediaid": (200, thumbnailData)
|
|
]
|
|
|
|
override func startLoading() {
|
|
MockURLProtocol.lastRequest = request
|
|
|
|
guard let url = request.url else {
|
|
client?.urlProtocol(self, didFailWithError: URLError(.badURL))
|
|
return
|
|
}
|
|
|
|
let isAuthenticated = request.value(forHTTPHeaderField: "Authorization") == "Bearer \(MockURLProtocol.accessToken)"
|
|
let path = url.path()
|
|
let (statusCode, data) = isAuthenticated ? MockURLProtocol.responses[path] ?? (404, Data()) : (401, Data())
|
|
|
|
guard let response = HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: nil, headerFields: nil) else {
|
|
client?.urlProtocol(self, didFailWithError: URLError(.badServerResponse))
|
|
return
|
|
}
|
|
|
|
client?.urlProtocol(self, didReceive: response, cacheStoragePolicy: .notAllowed)
|
|
client?.urlProtocol(self, didLoad: data)
|
|
client?.urlProtocolDidFinishLoading(self)
|
|
}
|
|
|
|
override func stopLoading() { }
|
|
|
|
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
|
|
request
|
|
}
|
|
|
|
override class func canInit(with request: URLRequest) -> Bool {
|
|
true
|
|
}
|
|
}
|