AppSettings are Mockable (#5589)

* create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

fix whitespace modifications

updates from PR feedback

a pass over converting AppSettings and AnalyticsService test instance properties to per test instantiation

fix a missed PR feedback

Sync up after rebase

create DependenciesProtocol and replace ServiceLocator registration with it

replace mocking calls for ServiceLocator to Dependencies

replace ServiceLocator with instance level calls

replace ServiceLocator configs with TestDependencies config in Tests

replace servicelocator usage in analytics service

replace remaining ServiceLocator calls in unit tests

that depended on the UnitTestAppCoordinator configuration with per suite dependencies instances

fix swiftlint warning

remove remaining references to ServiceLocator

make dependencies properties consistent in tests

delete Dependencies and DependenciesProtocol

* AppSettings is instance level mockable now

* replace mocked and preview usages of reset + AppSettings global init with

AppSettings(store: UserDefaultsMock())

* AppSettings.UserDefaultsKeys is now a RawRep struct, making it extensible

* update tests with new AppSettings so it builds

* update remaining AppSettings test/mock inits

* AppSettings.UserDefaultsKey conformed to ExpressibleByStringLiteral

* addressed issues found in first pass ai review

* update AppSettings instantiations after rebase

* fix accessibility tests coordinator after rebase

* Rename to VolatileUserDefaults

* AppSettings init uses mandatory parameter

* Revert UserDefaultsKey to enum

* Add TrackedUserDefaults to be able to reset the defaults object

* Added volatile static func to AppSettings

* Use .volatile() static func instead of full init

* review fixups

* Resolve build issues after rebase

* updated enterprise module

* remove the static reset settings call from the preview tests

---------

Co-authored-by: Mauro Romito <mauro.romito@element.io>
This commit is contained in:
Michael
2026-05-22 08:14:08 -05:00
committed by GitHub
parent 97d4a35af7
commit 22d14ff8e4
122 changed files with 572 additions and 747 deletions
@@ -18,8 +18,7 @@ final class AnalyticsSettingsScreenViewModelTests {
private var context: AnalyticsSettingsScreenViewModelType.Context!
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
let client = AnalyticsClientMock()
client.isRunning = false
analytics = AnalyticsService(client: client, appSettings: appSettings)
@@ -29,10 +28,6 @@ final class AnalyticsSettingsScreenViewModelTests {
context = viewModel.context
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func initialState() {
#expect(!context.enableAnalytics)
+1 -6
View File
@@ -19,8 +19,7 @@ final class AnalyticsTests {
@MainActor
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
analyticsClient = AnalyticsClientMock()
analyticsClient.isRunning = false
@@ -30,10 +29,6 @@ final class AnalyticsTests {
posthogMock.configureMockBehavior()
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func analyticsPromptNewUser() {
// Given a fresh install of the app (without PostHog analytics having been set).
@@ -21,17 +21,12 @@ final class AppLockScreenViewModelTests {
}
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
keychainController = KeychainControllerMock()
appLockService = AppLockService(keychainController: keychainController, appSettings: appSettings)
viewModel = AppLockScreenViewModel(appLockService: appLockService)
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func unlock() async throws {
// Given a valid PIN code.
@@ -17,8 +17,7 @@ final class AppLockServiceTests {
private var service: AppLockService
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
keychainController = KeychainController(service: .tests, accessGroup: InfoPlistReader.main.keychainAccessGroupIdentifier)
keychainController.resetSecrets()
@@ -27,10 +26,6 @@ final class AppLockServiceTests {
service.disable()
}
deinit {
AppSettings.resetAllSettings()
}
// MARK: - PIN Code
@Test
@@ -21,7 +21,7 @@ struct AppLockSetupSettingsScreenViewModelTests {
init() {
keychainController = KeychainControllerMock()
appLockService = AppLockService(keychainController: keychainController, appSettings: AppSettings())
appLockService = AppLockService(keychainController: keychainController, appSettings: .volatile())
viewModel = AppLockSetupSettingsScreenViewModel(appLockService: AppLockServiceMock.mock())
}
@@ -19,8 +19,6 @@ final class AppLockSetupBiometricsScreenViewModelTests {
}
init() {
AppSettings.resetAllSettings()
appLockService = AppLockServiceMock()
appLockService.underlyingIsEnabled = true
appLockService.underlyingBiometryType = .touchID
@@ -28,10 +26,6 @@ final class AppLockSetupBiometricsScreenViewModelTests {
viewModel = AppLockSetupBiometricsScreenViewModel(appLockService: appLockService)
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func allow() async throws {
// When allowing Touch/Face ID.
@@ -19,10 +19,6 @@ final class AppLockSetupPINScreenViewModelTests {
viewModel.context
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func createPIN() async throws {
setup(mode: .create)
@@ -187,9 +183,8 @@ final class AppLockSetupPINScreenViewModelTests {
// MARK: - Helpers
private func setup(mode: AppLockSetupPINScreenMode) {
AppSettings.resetAllSettings()
keychainController = KeychainControllerMock()
appLockService = AppLockService(keychainController: keychainController, appSettings: AppSettings())
appLockService = AppLockService(keychainController: keychainController, appSettings: .volatile())
viewModel = AppLockSetupPINScreenViewModel(initialMode: mode, isMandatory: false, appLockService: appLockService)
}
}
@@ -15,8 +15,7 @@ struct AppRouteURLParserTests {
var appRouteURLParser: AppRouteURLParser
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
appRouteURLParser = AppRouteURLParser(appSettings: appSettings)
}
@@ -153,7 +153,7 @@ struct AuthenticationServiceTests {
encryptionKeyProvider: encryptionKeyProvider,
classicAppManager: classicAppManager,
clientFactory: clientFactory,
appSettings: AppSettings(),
appSettings: .volatile(),
appHooks: AppHooks())
if let classicAppAccount = service.classicAppAccount {
@@ -26,12 +26,7 @@ final class AuthenticationStartScreenViewModelTests {
}
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
}
deinit {
AppSettings.resetAllSettings()
appSettings = AppSettings.volatile()
}
@Test
@@ -16,8 +16,7 @@ final class BugReportServiceTests {
var bugReportService: BugReportServiceProtocol!
init() throws {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
appSettings.bugReportRageshakeURL.reset()
let bugReportServiceMock = BugReportServiceMock()
@@ -31,7 +31,8 @@ struct ChatsTabFlowCoordinatorTests {
}
init() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
clientProxy = ClientProxyMock(.init(userID: "hi@bob", roomSummaryProvider: RoomSummaryProviderMock(.init(state: .loaded(.mockRooms)))))
timelineControllerFactory = TimelineControllerFactoryMock(.init())
@@ -21,15 +21,9 @@ final class ComposerToolbarViewModelTests {
private var draftServiceMock: ComposerDraftServiceMock!
init() {
AppSettings.resetAllSettings()
setUpViewModel()
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func composerFocus() {
viewModel.process(timelineAction: .setMode(mode: .edit(originalEventOrTransactionID: .eventID("mock"), type: .default)))
@@ -89,7 +83,7 @@ final class ComposerToolbarViewModelTests {
.init(suggestionType: .user(.init(id: "@user_mention_2:matrix.org", displayName: "User 2", avatarURL: nil)), range: .init(), rawSuggestionText: "")]
let mockCompletionSuggestionService = CompletionSuggestionServiceMock(configuration: .init(suggestions: suggestions))
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = ComposerToolbarViewModel(roomProxy: JoinedRoomProxyMock(.init()),
wysiwygViewModel: wysiwygViewModel,
@@ -708,7 +702,7 @@ final class ComposerToolbarViewModelTests {
let mockSubject = CurrentValueSubject<[IdentityStatusChange], Never>([])
roomProxyMock.underlyingIdentityStatusChangesPublisher = mockSubject.asCurrentValuePublisher()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = ComposerToolbarViewModel(roomProxy: roomProxyMock,
wysiwygViewModel: wysiwygViewModel,
@@ -755,7 +749,7 @@ final class ComposerToolbarViewModelTests {
roomProxyMock.underlyingIdentityStatusChangesPublisher = mockSubject.asCurrentValuePublisher()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = ComposerToolbarViewModel(roomProxy: roomProxyMock,
wysiwygViewModel: wysiwygViewModel,
@@ -791,7 +785,7 @@ final class ComposerToolbarViewModelTests {
}
roomProxyMock.underlyingIdentityStatusChangesPublisher = CurrentValueSubject([IdentityStatusChange(userId: "@alice:localhost", changedTo: .pinViolation)]).asCurrentValuePublisher()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = ComposerToolbarViewModel(roomProxy: roomProxyMock,
wysiwygViewModel: wysiwygViewModel,
@@ -816,7 +810,7 @@ final class ComposerToolbarViewModelTests {
draftServiceMock.loadDraftClosure = loadDraftClosure
}
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = ComposerToolbarViewModel(initialText: initialText,
roomProxy: JoinedRoomProxyMock(.init()),
@@ -26,11 +26,10 @@ final class CreateRoomScreenViewModelTests {
}
init() {
appSettings = AppSettings()
appSettings = AppSettings.volatile()
}
deinit {
AppSettings.resetAllSettings()
viewModel = nil
clientProxy = nil
spaceService = nil
@@ -45,7 +45,7 @@ struct EmojiPickerScreenViewModelTests {
viewModel = EmojiPickerScreenViewModel(itemID: .randomEvent,
selectedEmojis: selectedEmojis,
emojiProvider: EmojiProvider(appSettings: AppSettings()),
emojiProvider: EmojiProvider(appSettings: .volatile()),
timelineController: MockTimelineController(timelineProxy: timelineProxy))
}
}
+4 -4
View File
@@ -19,7 +19,7 @@ struct EmojiProviderTests {
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = [category]
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: AppSettings())
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: .volatile())
let categories = await emojiProvider.categories()
#expect(emojiLoaderMock.categories == categories)
@@ -33,7 +33,7 @@ struct EmojiProviderTests {
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = [category]
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: AppSettings())
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: .volatile())
let categories = await emojiProvider.categories(searchString: "")
#expect(emojiLoaderMock.categories == categories)
@@ -51,7 +51,7 @@ struct EmojiProviderTests {
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = categoriesForFirstLoad
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: AppSettings())
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: .volatile())
_ = await emojiProvider.categories()
emojiLoaderMock.categories = categoriesForSecondLoad
@@ -82,7 +82,7 @@ struct EmojiProviderTests {
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = categories
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: AppSettings())
let emojiProvider = EmojiProvider(loader: emojiLoaderMock, appSettings: .volatile())
_ = await emojiProvider.categories()
let result = await emojiProvider.categories(searchString: searchString)
@@ -25,12 +25,7 @@ final class HomeScreenViewModelTests {
var cancellables = Set<AnyCancellable>()
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
}
deinit {
AppSettings.resetAllSettings()
appSettings = AppSettings.volatile()
}
@Test
@@ -109,7 +109,7 @@ struct IdentityConfirmationScreenViewModelTests {
userSession.sessionSecurityStatePublisher = securityStateSubject.asCurrentValuePublisher()
viewModel = IdentityConfirmationScreenViewModel(userSession: userSession,
appSettings: AppSettings(),
appSettings: .volatile(),
userIndicatorController: UserIndicatorControllerMock())
}
}
@@ -15,14 +15,6 @@ final class InviteUsersScreenViewModelTests {
var viewModel: InviteUsersScreenViewModelProtocol!
var userDiscoveryService: UserDiscoveryServiceMock!
var clientProxy: ClientProxyMock!
init() {
AppSettings.resetAllSettings()
}
deinit {
AppSettings.resetAllSettings()
}
var context: InviteUsersScreenViewModel.Context {
viewModel.context
@@ -179,7 +171,7 @@ final class InviteUsersScreenViewModelTests {
isSkippable: false,
userDiscoveryService: userDiscoveryService,
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
appSettings: .volatile())
// The locked invitee starts pre-selected and locked.
#expect(context.viewState.selectedUsers.map(\.userID) == [UserProfileProxy.mockAlice.userID])
@@ -244,7 +236,7 @@ final class InviteUsersScreenViewModelTests {
isSkippable: isSkippable,
userDiscoveryService: userDiscoveryService,
userIndicatorController: UserIndicatorControllerMock(),
appSettings: AppSettings())
appSettings: .volatile())
viewModel.state.usersSection = .init(type: .suggestions, users: [.mockAlice, .mockBob, .mockCharlie])
self.viewModel = viewModel
}
@@ -29,14 +29,12 @@ final class JoinRoomScreenViewModelTests {
private let appSettings: AppSettings
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
}
deinit {
viewModel = nil
clientProxy = nil
AppSettings.resetAllSettings()
}
@Test
@@ -11,10 +11,6 @@ import Testing
@MainActor
struct KnockRequestsListScreenViewModelTests {
init() {
AppSettings.resetAllSettings()
}
@Test
func loadingState() async throws {
let roomProxyMock = JoinedRoomProxyMock(.init(knockRequestsState: .loading, joinRule: .knock))
@@ -19,14 +19,6 @@ final class LiveLocationManagerTests {
private var appSettings: AppSettings!
private var beaconInfoSubject: PassthroughSubject<LiveLocationOwnInfoUpdate, Never>!
init() {
AppSettings.resetAllSettings()
}
deinit {
AppSettings.resetAllSettings()
}
// MARK: - startLiveLocation
@Test
@@ -236,7 +228,7 @@ final class LiveLocationManagerTests {
}
private func setUp(accuracyAuthorization: CLAccuracyAuthorization = .fullAccuracy) {
appSettings = AppSettings()
appSettings = AppSettings.volatile()
clientProxy = ClientProxyMock(.init())
beaconInfoSubject = PassthroughSubject<LiveLocationOwnInfoUpdate, Never>()
clientProxy.liveLocationOwnInfoUpdatesPublisher = beaconInfoSubject.eraseToAnyPublisher()
@@ -394,7 +394,7 @@ struct LocationSharingScreenViewModelTests {
let roomProxyMock = JoinedRoomProxyMock(.init(members: .allMembers))
roomProxyMock.makeLiveLocationServiceReturnValue = liveLocationServiceMock
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
viewModel = LocationSharingScreenViewModel(interactionMode: .viewLive(sender: nil, initialLiveLocationShare: nil),
mapURLBuilder: appSettings.mapTilerConfiguration,
@@ -429,7 +429,7 @@ struct LocationSharingScreenViewModelTests {
private mutating func setupViewModel(liveLocationManagerConfiguration: LiveLocationManagerMock.Configuration = .init(),
members: [RoomMemberProxyMock] = .allMembersAsAdmin) {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
timelineProxy = TimelineProxyMock(.init())
viewModel = LocationSharingScreenViewModel(interactionMode: .picker(shouldShowLiveLocationOption: true),
mapURLBuilder: appSettings.mapTilerConfiguration,
@@ -444,7 +444,7 @@ struct LocationSharingScreenViewModelTests {
private mutating func setupViewModel(liveLocationManagerMock: LiveLocationManagerMock,
members: [RoomMemberProxyMock] = .allMembersAsAdmin) {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
timelineProxy = TimelineProxyMock(.init())
viewModel = LocationSharingScreenViewModel(interactionMode: .picker(shouldShowLiveLocationOption: true),
mapURLBuilder: appSettings.mapTilerConfiguration,
@@ -461,7 +461,7 @@ struct LocationSharingScreenViewModelTests {
initialShare: LiveLocationShare,
liveLocationsSubject: CurrentValueSubject<[LiveLocationShare], Never>,
members: [RoomMemberProxyMock] = .allMembers) {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let liveLocationServiceMock = RoomLiveLocationServiceMock()
liveLocationServiceMock.liveLocationsPublisher = liveLocationsSubject.asCurrentValuePublisher()
@@ -227,7 +227,7 @@ struct LoginScreenViewModelTests {
// MARK: - Helpers
private mutating func setupViewModel(homeserverAddress: String = "example.com", loginHint: String? = nil) async {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
clientFactory = AuthenticationClientFactoryMock(configuration: .init())
service = AuthenticationService(userSessionStore: UserSessionStoreMock(configuration: .init()),
@@ -28,17 +28,12 @@ final class MediaUploadPreviewScreenViewModelTests {
}
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
appSettings.optimizeMediaUploads = false
userIndicatorController = UserIndicatorControllerMock.default
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func imageUploadWithoutCaption() async throws {
setUpViewModel(urls: [imageURL], expectedCaption: nil)
@@ -17,16 +17,11 @@ final class MediaUploadingPreprocessorTests {
var mediaUploadingPreprocessor: MediaUploadingPreprocessor!
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
appSettings.optimizeMediaUploads = false
mediaUploadingPreprocessor = MediaUploadingPreprocessor(appSettings: appSettings)
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func audioFileProcessing() async throws {
let url = try #require(Bundle(for: Self.self).url(forResource: "test_audio.mp3", withExtension: nil), "Failed retrieving test asset")
@@ -25,8 +25,7 @@ final class NotificationManagerTests {
private let appSettings: AppSettings
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
notificationCenter = UserNotificationCenterMock()
notificationCenter.requestAuthorizationOptionsReturnValue = true
notificationCenter.authorizationStatusReturnValue = .authorized
@@ -21,11 +21,9 @@ struct NotificationSettingsScreenViewModelTests {
private var toneManager: NotificationToneManagerMock
init() throws {
AppSettings.resetAllSettings()
userNotificationCenter = UserNotificationCenterMock()
userNotificationCenter.authorizationStatusReturnValue = .authorized
appSettings = AppSettings()
appSettings = AppSettings.volatile()
notificationSettingsProxy = NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration())
notificationSettingsProxy.getDefaultRoomNotificationModeIsEncryptedIsOneToOneReturnValue = .allMessages
notificationSettingsProxy.isRoomMentionEnabledReturnValue = true
@@ -18,8 +18,7 @@ struct NotificationToneManagerTests {
private let manager: NotificationToneManager
init() {
AppSettings.resetAllSettings()
manager = NotificationToneManager(appSettings: AppSettings())
manager = NotificationToneManager(appSettings: .volatile())
}
// MARK: - Deletion
+11 -11
View File
@@ -15,7 +15,7 @@ import Testing
struct PillContextTests {
@Test
func user() async {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let id = "@test:matrix.org"
@@ -49,7 +49,7 @@ struct PillContextTests {
@Test
func ownUser() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let id = "@test:matrix.org"
@@ -74,7 +74,7 @@ struct PillContextTests {
@Test
func allUsers() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let avatarURL = URL(string: "https://matrix.jpg")
@@ -102,7 +102,7 @@ struct PillContextTests {
@Test
func roomIDMention() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -130,7 +130,7 @@ struct PillContextTests {
@Test
func roomIDMentionMissingRoom() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -156,7 +156,7 @@ struct PillContextTests {
@Test
func roomAliasMention() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -186,7 +186,7 @@ struct PillContextTests {
@Test
func roomAliasMentionMissingRoom() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -212,7 +212,7 @@ struct PillContextTests {
@Test
func eventOnRoomIDMention() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -240,7 +240,7 @@ struct PillContextTests {
@Test
func eventOnRoomIDMentionMissingRoom() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -266,7 +266,7 @@ struct PillContextTests {
@Test
func eventOnRoomAliasMention() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -296,7 +296,7 @@ struct PillContextTests {
@Test
func eventOnRoomAliasMentionMissingRoom() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userIndicatorController = UserIndicatorControllerMock.default
let proxyMock = JoinedRoomProxyMock(.init())
@@ -167,7 +167,7 @@ struct RoomDetailsEditScreenViewModelTests {
userIndicatorController = UserIndicatorControllerMock.default
viewModel = .init(roomProxy: JoinedRoomProxyMock(roomProxyConfiguration),
userSession: UserSessionMock(.init()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: AppSettings()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: .volatile()),
userIndicatorController: userIndicatorController)
}
}
@@ -25,7 +25,6 @@ struct RoomDetailsScreenViewModelTests {
var cancellables = Set<AnyCancellable>()
init() {
AppSettings.resetAllSettings()
cancellables.removeAll()
roomProxyMock = JoinedRoomProxyMock(.init(name: "Test"))
notificationSettingsProxyMock = NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration())
@@ -35,7 +34,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
}
@Test
@@ -48,7 +47,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState.bindings.leaveRoomAlertItem)) { $0 != nil }
context.send(viewAction: .processTapLeave)
@@ -68,7 +67,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState.bindings.leaveRoomAlertItem)) { $0 != nil }
context.send(viewAction: .processTapLeave)
@@ -89,7 +88,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
context.send(viewAction: .processTapLeave)
#expect(context.leaveRoomAlertItem?.state == .empty)
@@ -147,7 +146,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(viewModel.context.observe(\.viewState.dmRecipientInfo)) { $0 != nil }
@@ -168,7 +167,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferredRecipient = deferFulfillment(viewModel.context.observe(\.viewState.dmRecipientInfo)) { $0 != nil }
@@ -199,7 +198,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferredRecipient = deferFulfillment(viewModel.context.observe(\.viewState.dmRecipientInfo)) { $0 != nil }
@@ -229,7 +228,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferredRecipient = deferFulfillment(viewModel.context.observe(\.viewState.dmRecipientInfo)) { $0 != nil }
@@ -260,7 +259,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferredRecipient = deferFulfillment(viewModel.context.observe(\.viewState.dmRecipientInfo)) { $0 != nil }
@@ -292,7 +291,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -309,7 +308,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -361,7 +360,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -400,7 +399,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -439,7 +438,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -459,7 +458,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -479,7 +478,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: NotificationSettingsProxyMock(with: NotificationSettingsProxyMockConfiguration()),
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
_ = await context.observe(\.viewState).debounce(for: .milliseconds(100)).first()
@@ -497,7 +496,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
var deferred = deferFulfillment(context.observe(\.viewState.notificationSettingsState)) { $0.isError }
@@ -705,7 +704,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState)) { state in
state.knockRequestsCount == 2 && state.canSeeKnockingRequests
@@ -726,7 +725,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState)) { state in
state.knockRequestsCount == 0 && state.canSeeKnockingRequests
@@ -749,7 +748,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState)) { state in
state.knockRequestsCount == 2 &&
@@ -772,7 +771,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferred = deferFulfillment(context.observe(\.viewState)) { state in
state.knockRequestsCount == 2 &&
@@ -799,7 +798,7 @@ struct RoomDetailsScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock.default,
notificationSettingsProxy: notificationSettingsProxyMock,
attributedStringBuilder: AttributedStringBuilder(mentionBuilder: MentionBuilder()),
appSettings: AppSettings())
appSettings: .volatile())
let deferredShared = deferFulfillment(context.observe(\.viewState),
message: "The pill should be shown for rooms with shared history visibility") { state in
@@ -22,13 +22,9 @@ final class RoomFlowCoordinatorTests {
private let appSettings: AppSettings
init() {
appSettings = AppSettings()
appSettings = AppSettings.volatile()
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func roomPresentation() async throws {
setupRoomFlowCoordinator()
@@ -15,15 +15,10 @@ final class RoomListFiltersStateTests {
let allCasesWithoutLowPriority = RoomListFilter.allCases.filter { $0 != .lowPriority }
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
state = RoomListFiltersState(appSettings: appSettings)
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func initialState() {
#expect(!state.isFiltering)
@@ -213,6 +213,6 @@ struct RoomMemberDetailsViewModelTests {
userSession: userSession,
userIndicatorController: UserIndicatorControllerMock.default,
analytics: AnalyticsServiceMock.default,
appSettings: AppSettings())
appSettings: .volatile())
}
}
@@ -51,7 +51,7 @@ struct RoomMembersFlowCoordinatorTests {
let clientProxy = ClientProxyMock(.init())
clientProxy.directRoomForUserIDReturnValue = .success(nil)
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let flowParameters = CommonFlowParameters(userSession: UserSessionMock(.init(clientProxy: clientProxy)),
bugReportService: BugReportServiceMock(.init()),
@@ -20,12 +20,7 @@ final class RoomScreenViewModelTests {
private let appSettings: AppSettings
init() async throws {
AppSettings.resetAllSettings()
appSettings = AppSettings()
}
deinit {
AppSettings.resetAllSettings()
appSettings = AppSettings.volatile()
}
@Test
@@ -22,10 +22,6 @@ final class RoomSummaryProviderTests {
var dynamicEntriesController: RoomListDynamicEntriesControllerSDKMock!
var roomSummaryProvider: RoomSummaryProvider!
deinit {
AppSettings.resetAllSettings()
}
@Test
func defaultRustFilters() async {
// Given a new room provider.
@@ -94,8 +90,7 @@ final class RoomSummaryProviderTests {
// MARK: - Helpers
private func setup(isLowPriorityFilterEnabled: Bool = false) {
AppSettings.resetAllSettings()
appSettings = AppSettings()
appSettings = AppSettings.volatile()
appSettings.lowPriorityFilterEnabled = isLowPriorityFilterEnabled
let stateEventStringBuilder = RoomStateEventStringBuilder(userID: "@me:matrix.org")
@@ -20,15 +20,10 @@ final class SecurityAndPrivacyScreenViewModelTests {
var context: SecurityAndPrivacyScreenViewModelType.Context {
viewModel.context
}
init() {
AppSettings.resetAllSettings()
}
deinit {
viewModel = nil
roomProxy = nil
AppSettings.resetAllSettings()
}
@Test
@@ -444,7 +439,7 @@ final class SecurityAndPrivacyScreenViewModelTests {
private func setupViewModel(joinedParentSpaces: [SpaceServiceRoom],
topLevelSpaces: [SpaceServiceRoom] = [],
joinRule: ElementX.JoinRule) {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
appSettings.knockingEnabled = true
roomProxy = JoinedRoomProxyMock(.init(isEncrypted: false,
canonicalAlias: "#room:matrix.org",
@@ -25,12 +25,7 @@ final class ServerConfirmationScreenViewModelTests {
private let appSettings: AppSettings
init() {
AppSettings.resetAllSettings()
appSettings = AppSettings()
}
deinit {
AppSettings.resetAllSettings()
appSettings = AppSettings.volatile()
}
// MARK: - Confirmation mode
@@ -148,7 +148,7 @@ struct ServerSelectionScreenViewModelTests {
// MARK: - Helpers
private mutating func setup(authenticationFlow: AuthenticationFlow) {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
clientFactory = AuthenticationClientFactoryMock(configuration: .init())
service = AuthenticationService(userSessionStore: UserSessionStoreMock(configuration: .init()),
@@ -21,7 +21,7 @@ struct SessionVerificationViewModelTests {
sessionVerificationController = SessionVerificationControllerProxyMock.configureMock()
viewModel = SessionVerificationScreenViewModel(sessionVerificationControllerProxy: sessionVerificationController,
flow: .deviceInitiator,
appSettings: AppSettings(),
appSettings: .volatile(),
mediaProvider: MediaProviderMock(configuration: .init()))
context = viewModel.context
}
@@ -16,7 +16,7 @@ struct SettingsScreenViewModelTests {
private var context: SettingsScreenViewModelType.Context
init() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let userSession = UserSessionMock(.init(clientProxy: ClientProxyMock(.init(userID: ""))))
viewModel = SettingsScreenViewModel(userSession: userSession,
appSettings: appSettings,
@@ -22,8 +22,7 @@ final class SpacesScreenViewModelTests {
}
init() {
AppSettings.resetAllSettings()
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let clientProxy = ClientProxyMock(.init())
let userSession = UserSessionMock(.init(clientProxy: clientProxy))
@@ -47,10 +46,6 @@ final class SpacesScreenViewModelTests {
userIndicatorController: UserIndicatorControllerMock())
}
deinit {
AppSettings.resetAllSettings()
}
@Test
func initialState() {
#expect(context.viewState.topLevelSpaces.count == 3)
@@ -20,7 +20,7 @@ struct StartChatScreenViewModelTests {
}
init() {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
clientProxy = .init(.init(userID: ""))
userDiscoveryService = UserDiscoveryServiceMock()
@@ -17,7 +17,6 @@ final class TimelineViewModelTests {
var cancellables = Set<AnyCancellable>()
init() async throws {
AppSettings.resetAllSettings()
cancellables.removeAll()
}
@@ -307,7 +306,7 @@ final class TimelineViewModelTests {
timelineController.timelineItems = items
timelineController.roomProxy = roomProxy
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let viewModel = TimelineViewModel(roomProxy: roomProxy,
timelineController: timelineController,
@@ -333,7 +332,7 @@ final class TimelineViewModelTests {
addReadReceipts: receipts)
let id = message.id
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
// When showing them in a timeline.
let timelineController = MockTimelineController()
@@ -360,7 +359,7 @@ final class TimelineViewModelTests {
@Test
func showManageUserAsAdmin() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let viewModel = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "",
members: [RoomMemberProxyMock.mockAdmin,
@@ -399,7 +398,7 @@ final class TimelineViewModelTests {
@Test
func showDetailsForAnAdmin() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let viewModel = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "",
members: [RoomMemberProxyMock.mockAdmin,
@@ -438,7 +437,7 @@ final class TimelineViewModelTests {
@Test
func showDetailsForABannedUser() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let viewModel = TimelineViewModel(roomProxy: JoinedRoomProxyMock(.init(name: "",
members: [RoomMemberProxyMock.mockAdmin,
@@ -480,7 +479,7 @@ final class TimelineViewModelTests {
@Test
func pinnedEvents() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
var configuration = JoinedRoomProxyMockConfiguration(name: "",
pinnedEventIDs: .init(["test1"]))
@@ -511,7 +510,7 @@ final class TimelineViewModelTests {
@Test
func canUserPinEvents() async throws {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let configuration = JoinedRoomProxyMockConfiguration(name: "",
powerLevelsConfiguration: .init(canUserPin: true))
@@ -588,7 +587,7 @@ final class TimelineViewModelTests {
private func makeViewModel(roomProxy: JoinedRoomProxyProtocol? = nil,
focussedEventID: String? = nil,
timelineController: TimelineControllerProtocol) -> TimelineViewModel {
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
return TimelineViewModel(roomProxy: roomProxy ?? JoinedRoomProxyMock(.init(name: "")),
focussedEventID: focussedEventID,
@@ -20,7 +20,7 @@ struct UserDetailsEditScreenViewModelTests {
init() {
viewModel = .init(userSession: UserSessionMock(.init()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: AppSettings()),
mediaUploadingPreprocessor: MediaUploadingPreprocessor(appSettings: .volatile()),
userIndicatorController: UserIndicatorControllerMock.default)
}
+55 -46
View File
@@ -11,128 +11,131 @@ import Foundation
import Testing
struct UserPreferenceTests {
init() {
UserDefaults.testDefaults.removeVolatileDomain(forName: .userDefaultsSuiteName)
UserDefaults.testDefaults.removePersistentDomain(forName: .userDefaultsSuiteName)
}
@Test
func storePlistValue() {
let testDefaults = VolatileUserDefaults()
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.plist = "Hello"
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
#expect(value.plist == "Hello")
#expect(UserDefaults.testDefaults.string(forKey: .key2) != nil)
#expect(UserDefaults.testDefaults.data(forKey: .key2) == nil)
#expect(testDefaults.object(forKey: TestsKey.key2.rawValue) is String)
#expect(testDefaults.data(forKey: TestsKey.key2.rawValue) == nil)
}
@Test
func storeCodableValue() {
let testDefaults = VolatileUserDefaults()
let storedType = CodableTestType(a: "some", b: [1, 2, 3])
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.codable = storedType
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
#expect(value.codable == storedType)
#expect(UserDefaults.testDefaults.data(forKey: .key3) != nil)
#expect(testDefaults.data(forKey: TestsKey.key3.rawValue) != nil)
}
@Test
func storePlistValueOnVolatileStorage() {
let testDefaults = VolatileUserDefaults()
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.volatileVar = "Hello"
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
#expect(value.volatileVar == nil)
}
@Test
func storeCodableValueOnVolatileStorage() {
let testDefaults = VolatileUserDefaults()
let storedType = CodableTestType(a: "some", b: [1, 2, 3])
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.volatileCodable = storedType
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
#expect(value.volatileCodable == nil)
#expect(UserDefaults.testDefaults.data(forKey: .key4) == nil)
#expect(testDefaults.data(forKey: TestsKey.key4.rawValue) == nil)
}
@Test
func storePlistArray() {
let testDefaults = VolatileUserDefaults()
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.plistArray = [1, 2, 3]
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
#expect(value.plistArray == [1, 2, 3])
#expect(UserDefaults.testDefaults.array(forKey: .key5) as? [Int] == [1, 2, 3])
#expect(UserDefaults.testDefaults.data(forKey: .key5) == nil)
#expect(testDefaults.object(forKey: TestsKey.key5.rawValue) as? [Int] == [1, 2, 3])
#expect(testDefaults.data(forKey: TestsKey.key5.rawValue) == nil)
}
@Test
func assignNilToPlistType() {
let testDefaults = VolatileUserDefaults()
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.plist = "Hello"
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.plist = nil
#expect(value.plist == nil)
#expect(UserDefaults.testDefaults.string(forKey: .key2) == nil)
#expect(testDefaults.object(forKey: TestsKey.key2.rawValue) as? String == nil)
}
@Test
func assignNilToCodableType() {
let testDefaults = VolatileUserDefaults()
let storedType = CodableTestType(a: "some", b: [1, 2, 3])
let setPreference = {
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.codable = storedType
}
setPreference()
let value = TestPreferences()
let value = TestPreferences(testDefaults)
value.codable = nil
#expect(value.codable == nil)
#expect(UserDefaults.testDefaults.data(forKey: .key3) == nil)
#expect(testDefaults.data(forKey: TestsKey.key3.rawValue) == nil)
}
@Test
func localOverRemoteValue() {
@UserPreference(key: "testKey", defaultValue: "", storageType: .userDefaults(.testDefaults)) var preference
let storage = VolatileUserDefaults()
@UserPreference(key: TestsKey.testKey, defaultValue: "", storage: storage) var preference
#expect(preference == "")
_preference.remoteValue = "remote"
@@ -144,7 +147,8 @@ struct UserPreferenceTests {
@Test
func remoteOverLocalValue() {
@UserPreference(key: "testKey", defaultValue: "", storageType: .userDefaults(.testDefaults), mode: .remoteOverLocal) var preference
let storage = VolatileUserDefaults()
@UserPreference(key: TestsKey.testKey, defaultValue: "", storage: storage, mode: .remoteOverLocal) var preference
#expect(preference == "")
_preference.remoteValue = "remote"
@@ -157,20 +161,28 @@ struct UserPreferenceTests {
}
private struct TestPreferences {
@UserPreference(key: .key1, storageType: .volatile)
@UserPreference
var volatileVar: String?
@UserPreference(key: .key2, storageType: .userDefaults(.testDefaults))
@UserPreference
var plist: String?
@UserPreference(key: .key3, storageType: .userDefaults(.testDefaults))
@UserPreference
var codable: CodableTestType?
@UserPreference(key: .key4, storageType: .volatile)
@UserPreference
var volatileCodable: CodableTestType?
@UserPreference(key: .key5, storageType: .userDefaults(.testDefaults))
@UserPreference
var plistArray: [Int]?
init(_ storage: UserDefaultsProtocol) {
_volatileVar = UserPreference(key: TestsKey.key1, storage: VolatileUserDefaults())
_plist = UserPreference(key: TestsKey.key2, storage: storage)
_codable = UserPreference(key: TestsKey.key3, storage: storage)
_volatileCodable = UserPreference(key: TestsKey.key4, storage: VolatileUserDefaults())
_plistArray = UserPreference(key: TestsKey.key5, storage: storage)
}
}
private struct CodableTestType: Equatable, Codable {
@@ -178,16 +190,13 @@ private struct CodableTestType: Equatable, Codable {
let b: [Int]
}
private extension String {
static let key1 = "foo.volatile"
static let key2 = "foo.plist"
static let key3 = "foo.codable"
static let key4 = "foo.volatile.codable"
static let key5 = "foo.plist.array"
static let userDefaultsSuiteName = "io.element.elementx.unitests"
}
private extension UserDefaults {
// swiftlint:disable:next force_unwrapping
static let testDefaults = UserDefaults(suiteName: .userDefaultsSuiteName)!
private struct TestsKey: PreferenceKeyable, RawRepresentable {
static let testKey = Self(rawValue: "testKey")
static let key1 = Self(rawValue: "foo.volatile")
static let key2 = Self(rawValue: "foo.plist")
static let key3 = Self(rawValue: "foo.codable")
static let key4 = Self(rawValue: "foo.volatile.codable")
static let key5 = Self(rawValue: "foo.plist.array")
let rawValue: String
}
@@ -24,7 +24,7 @@ struct UserProfileScreenViewModelTests {
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: userIndicatorController,
analytics: AnalyticsServiceMock.default,
appSettings: AppSettings())
appSettings: .volatile())
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -48,7 +48,7 @@ struct UserProfileScreenViewModelTests {
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: userIndicatorController,
analytics: AnalyticsServiceMock.default,
appSettings: AppSettings())
appSettings: .volatile())
let context = viewModel.context
let waitForMemberToLoad = deferFulfillment(context.observe(\.viewState.userProfile)) { $0 != nil }
@@ -74,7 +74,7 @@ struct UserProfileScreenViewModelTests {
userSession: UserSessionMock(.init(clientProxy: clientProxy)),
userIndicatorController: userIndicatorController,
analytics: AnalyticsServiceMock.default,
appSettings: AppSettings())
appSettings: .volatile())
let context = viewModel.context
@@ -50,7 +50,7 @@ struct UserSessionFlowCoordinatorTests {
appMediator.networkMonitor = networkMonitor
userIndicatorController = UserIndicatorControllerMock.default
let appSettings = AppSettings()
let appSettings = AppSettings.volatile()
let flowParameters = CommonFlowParameters(userSession: UserSessionMock(.init(clientProxy: clientProxy)),
bugReportService: BugReportServiceMock(.init()),