2022-03-08 12:24:33 +00:00
|
|
|
//
|
2022-08-11 07:54:24 +00:00
|
|
|
// Copyright 2022 New Vector Ltd
|
2022-03-08 12:24:33 +00:00
|
|
|
//
|
2022-08-11 07:54:24 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2022-03-08 12:24:33 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2022-07-06 13:49:05 +00:00
|
|
|
import Foundation
|
2022-03-08 12:24:33 +00:00
|
|
|
|
|
|
|
class MockRoomTimelineController: RoomTimelineControllerProtocol {
|
2022-12-12 12:27:49 +00:00
|
|
|
/// An array of timeline item arrays that will be inserted in order for each back pagination request.
|
|
|
|
var backPaginationResponses: [[RoomTimelineItemProtocol]] = []
|
2023-01-09 15:17:18 +00:00
|
|
|
/// The time delay added to each back pagination request.
|
|
|
|
var backPaginationDelay: Duration = .milliseconds(500)
|
|
|
|
|
2022-12-12 12:27:49 +00:00
|
|
|
/// An array of timeline items that will be appended in order when ``simulateIncomingItems()`` is called.
|
|
|
|
var incomingItems: [RoomTimelineItemProtocol] = []
|
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
let roomID = "MockRoomIdentifier"
|
2022-09-12 18:34:53 +00:00
|
|
|
|
2022-03-08 12:24:33 +00:00
|
|
|
let callbacks = PassthroughSubject<RoomTimelineControllerCallback, Never>()
|
|
|
|
|
2022-12-12 12:27:49 +00:00
|
|
|
var timelineItems: [RoomTimelineItemProtocol] = RoomTimelineItemFixtures.default
|
|
|
|
|
2023-02-15 14:02:50 +00:00
|
|
|
private var client: UITestsSignalling.Client?
|
2023-01-05 16:35:20 +00:00
|
|
|
|
|
|
|
init(listenForSignals: Bool = false) {
|
2023-02-15 14:02:50 +00:00
|
|
|
guard listenForSignals else { return }
|
|
|
|
|
|
|
|
do {
|
|
|
|
try startListening()
|
|
|
|
} catch {
|
|
|
|
fatalError("Failure setting up signalling: \(error)")
|
2022-12-12 12:27:49 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 12:47:11 +00:00
|
|
|
|
2023-01-09 15:17:18 +00:00
|
|
|
func paginateBackwards(requestSize: UInt, untilNumberOfItems: UInt) async -> Result<Void, RoomTimelineControllerError> {
|
2023-01-11 09:11:36 +00:00
|
|
|
callbacks.send(.canBackPaginate(false))
|
2023-01-09 15:17:18 +00:00
|
|
|
return .success(())
|
2022-03-08 12:24:33 +00:00
|
|
|
}
|
2022-03-17 16:09:29 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func markRoomAsRead() async -> Result<Void, RoomTimelineControllerError> { .success(()) }
|
2022-03-17 16:09:29 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func processItemAppearance(_ itemID: String) async { }
|
2022-04-18 07:54:48 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func processItemDisappearance(_ itemID: String) async { }
|
|
|
|
|
|
|
|
func processItemTap(_ itemID: String) async -> RoomTimelineControllerAction { .none }
|
2022-09-14 08:40:00 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func sendMessage(_ message: String, inReplyTo itemID: String?) async { }
|
2022-12-07 11:19:47 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func sendReaction(_ reaction: String, to itemID: String) async { }
|
2022-11-10 11:41:38 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func editMessage(_ newMessage: String, original itemID: String) async { }
|
2022-09-14 13:21:23 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func redact(_ itemID: String) async { }
|
2022-11-08 11:17:38 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func debugDescription(for itemID: String) -> String {
|
2022-11-08 11:17:38 +00:00
|
|
|
"Mock debug description"
|
|
|
|
}
|
2022-12-22 11:59:38 +00:00
|
|
|
|
2023-01-18 15:14:03 +00:00
|
|
|
func retryDecryption(for sessionID: String) async { }
|
2023-01-05 16:35:20 +00:00
|
|
|
|
|
|
|
// MARK: - UI Test signalling
|
|
|
|
|
2023-02-15 14:02:50 +00:00
|
|
|
/// The cancellable used for UI Tests signalling.
|
|
|
|
private var signalCancellable: AnyCancellable?
|
|
|
|
|
2023-01-05 16:35:20 +00:00
|
|
|
/// Allows the simulation of server responses by listening for signals from UI tests.
|
2023-02-15 14:02:50 +00:00
|
|
|
private func startListening() throws {
|
|
|
|
let client = try UITestsSignalling.Client(mode: .app)
|
2023-01-05 16:35:20 +00:00
|
|
|
|
2023-02-15 14:02:50 +00:00
|
|
|
signalCancellable = client.signals.sink { [weak self] signal in
|
|
|
|
Task { try await self?.handleSignal(signal) }
|
2023-01-05 16:35:20 +00:00
|
|
|
}
|
2023-02-15 14:02:50 +00:00
|
|
|
|
|
|
|
self.client = client
|
2023-01-05 16:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Handles a UI test signal as necessary.
|
|
|
|
private func handleSignal(_ signal: UITestsSignal) async throws {
|
|
|
|
switch signal {
|
|
|
|
case .paginate:
|
|
|
|
try await simulateBackPagination()
|
|
|
|
case .incomingMessage:
|
|
|
|
try await simulateIncomingItem()
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends the next incoming item to the `timelineItems` array.
|
|
|
|
private func simulateIncomingItem() async throws {
|
|
|
|
guard !incomingItems.isEmpty else { return }
|
|
|
|
|
|
|
|
let incomingItem = incomingItems.removeFirst()
|
|
|
|
timelineItems.append(incomingItem)
|
|
|
|
callbacks.send(.updatedTimelineItems)
|
|
|
|
|
2023-02-15 14:02:50 +00:00
|
|
|
try client?.send(.success)
|
2023-01-05 16:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Prepends the next chunk of items to the `timelineItems` array.
|
|
|
|
private func simulateBackPagination() async throws {
|
|
|
|
guard !backPaginationResponses.isEmpty else { return }
|
2023-01-11 09:11:36 +00:00
|
|
|
callbacks.send(.isBackPaginating(true))
|
2023-01-05 16:35:20 +00:00
|
|
|
|
|
|
|
let newItems = backPaginationResponses.removeFirst()
|
|
|
|
timelineItems.insert(contentsOf: newItems, at: 0)
|
|
|
|
callbacks.send(.updatedTimelineItems)
|
2023-01-11 09:11:36 +00:00
|
|
|
callbacks.send(.isBackPaginating(false))
|
2023-01-05 16:35:20 +00:00
|
|
|
|
2023-02-15 14:02:50 +00:00
|
|
|
try client?.send(.success)
|
2023-01-05 16:35:20 +00:00
|
|
|
}
|
2022-03-08 12:24:33 +00:00
|
|
|
}
|