Signal-iOS/SignalServiceKit/TestUtils/MockSSKEnvironment.swift

82 lines
3.2 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import Foundation
import GRDB
#if TESTABLE_BUILD
public class MockSSKEnvironment {
/// Set up a mock SSK environment as well as ``DependenciesBridge``.
@MainActor
public static func activate() async {
let testAppContext = TestAppContext()
SetCurrentAppContext(testAppContext)
let appReadiness = AppReadinessImpl()
_ = await AppSetup().start(
appContext: testAppContext,
appReadiness: appReadiness,
databaseStorage: try! SDSDatabaseStorage(
appReadiness: appReadiness,
databaseFileUrl: SDSDatabaseStorage.grdbDatabaseFileUrl,
keychainStorage: MockKeychainStorage()
),
paymentsEvents: PaymentsEventsNoop(),
mobileCoinHelper: MobileCoinHelperMock(),
callMessageHandler: NoopCallMessageHandler(),
currentCallProvider: CurrentCallNoOpProvider(),
notificationPresenter: NoopNotificationPresenterImpl(),
incrementalMessageTSAttachmentMigratorFactory: IncrementalMessageTSAttachmentMigratorFactoryMock(),
messageBackupErrorPresenterFactory: NoOpMessageBackupErrorPresenterFactory(),
testDependencies: AppSetup.TestDependencies(
contactManager: FakeContactsManager(),
groupV2Updates: MockGroupV2Updates(),
groupsV2: MockGroupsV2(),
messageSender: FakeMessageSender(),
modelReadCaches: ModelReadCaches(
factory: TestableModelReadCacheFactory(appReadiness: appReadiness)
),
networkManager: OWSFakeNetworkManager(appReadiness: appReadiness, libsignalNet: nil),
paymentsCurrencies: MockPaymentsCurrencies(),
paymentsHelper: MockPaymentsHelper(),
pendingReceiptRecorder: NoopPendingReceiptRecorder(),
profileManager: OWSFakeProfileManager(),
reachabilityManager: MockSSKReachabilityManager(),
remoteConfigManager: StubbableRemoteConfigManager(),
signalService: OWSSignalServiceMock(),
storageServiceManager: FakeStorageServiceManager(),
syncManager: OWSMockSyncManager(),
systemStoryManager: SystemStoryManagerMock(),
versionedProfiles: MockVersionedProfiles(),
webSocketFactory: WebSocketFactoryMock()
)
).prepareDatabase()
}
public static func flushAndWait() {
AssertIsOnMainThread()
waitForMainQueue()
// Wait for all pending readers/writers to finish.
SSKEnvironment.shared.databaseStorageRef.grdbStorage.pool.barrierWriteWithoutTransaction { _ in }
// Wait for the main queue *again* in case more work was scheduled.
waitForMainQueue()
}
private static func waitForMainQueue() {
// Spin the main run loop to flush any remaining async work.
var done = false
DispatchQueue.main.async { done = true }
while !done {
CFRunLoopRunInMode(.defaultMode, 0.0, true)
}
}
}
#endif