mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
22d14ff8e4
* 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>
315 lines
16 KiB
Swift
315 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 AnalyticsEvents
|
|
@testable import ElementX
|
|
import PostHog
|
|
import Testing
|
|
|
|
final class AnalyticsTests {
|
|
private let appSettings: AppSettings
|
|
private let analytics: AnalyticsServiceProtocol
|
|
private let analyticsClient: AnalyticsClientMock
|
|
private var posthogMock: PHGPostHogMock
|
|
|
|
@MainActor
|
|
init() {
|
|
appSettings = AppSettings.volatile()
|
|
|
|
analyticsClient = AnalyticsClientMock()
|
|
analyticsClient.isRunning = false
|
|
analytics = AnalyticsService(client: analyticsClient, appSettings: appSettings)
|
|
|
|
posthogMock = PHGPostHogMock()
|
|
posthogMock.configureMockBehavior()
|
|
}
|
|
|
|
@Test
|
|
func analyticsPromptNewUser() {
|
|
// Given a fresh install of the app (without PostHog analytics having been set).
|
|
// When the user is prompted for analytics.
|
|
let showPrompt = analytics.shouldShowAnalyticsPrompt
|
|
|
|
// Then the prompt should be shown.
|
|
#expect(showPrompt, "A prompt should be shown for a new user.")
|
|
}
|
|
|
|
@Test
|
|
func analyticsPromptUserDeclinedPostHog() {
|
|
// Given an existing install of the app where the user previously declined PostHog
|
|
appSettings.analyticsConsentState = .optedOut
|
|
|
|
// When the user is prompted for analytics
|
|
let showPrompt = analytics.shouldShowAnalyticsPrompt
|
|
|
|
// Then no prompt should be shown.
|
|
#expect(!showPrompt, "A prompt should not be shown any more.")
|
|
}
|
|
|
|
@Test
|
|
func analyticsPromptUserAcceptedPostHog() {
|
|
// Given an existing install of the app where the user previously accepted PostHog
|
|
appSettings.analyticsConsentState = .optedIn
|
|
|
|
// When the user is prompted for analytics
|
|
let showPrompt = analytics.shouldShowAnalyticsPrompt
|
|
|
|
// Then no prompt should be shown.
|
|
#expect(!showPrompt, "A prompt should not be shown any more.")
|
|
}
|
|
|
|
@Test
|
|
func analyticsPromptNotDisplayed() {
|
|
// Given a fresh install of the app Analytics should be disabled
|
|
#expect(appSettings.analyticsConsentState == .unknown)
|
|
#expect(!analytics.isEnabled)
|
|
#expect(!analyticsClient.startAnalyticsConfigurationCalled)
|
|
}
|
|
|
|
@Test
|
|
func analyticsOptOut() {
|
|
// Given a fresh install of the app (without PostHog analytics having been set).
|
|
// When analytics is opt-out
|
|
analytics.optOut()
|
|
// Then analytics should be disabled
|
|
#expect(appSettings.analyticsConsentState == .optedOut)
|
|
#expect(!analytics.isEnabled)
|
|
#expect(!analyticsClient.isRunning)
|
|
// Analytics client should have been stopped
|
|
#expect(analyticsClient.stopCalled)
|
|
}
|
|
|
|
@Test
|
|
func analyticsOptIn() {
|
|
// Given a fresh install of the app (without PostHog analytics having been set).
|
|
// When analytics is opt-in
|
|
analytics.optIn()
|
|
// The analytics should be enabled
|
|
#expect(appSettings.analyticsConsentState == .optedIn)
|
|
#expect(analytics.isEnabled)
|
|
// Analytics client should have been started
|
|
#expect(analyticsClient.startAnalyticsConfigurationCalled)
|
|
}
|
|
|
|
@Test
|
|
func analyticsStartIfNotEnabled() {
|
|
// Given an existing install of the app where the user previously declined the tracking
|
|
appSettings.analyticsConsentState = .optedOut
|
|
// Analytics should not start
|
|
#expect(!analytics.isEnabled)
|
|
analytics.startIfEnabled()
|
|
#expect(!analyticsClient.startAnalyticsConfigurationCalled)
|
|
}
|
|
|
|
@Test
|
|
func analyticsStartIfEnabled() {
|
|
// Given an existing install of the app where the user previously accepted the tracking
|
|
appSettings.analyticsConsentState = .optedIn
|
|
// Analytics should start
|
|
#expect(analytics.isEnabled)
|
|
analytics.startIfEnabled()
|
|
#expect(analyticsClient.startAnalyticsConfigurationCalled)
|
|
}
|
|
|
|
@Test
|
|
func addingUserProperties() {
|
|
// Given a client with no user properties set
|
|
let client = PostHogAnalyticsClient()
|
|
#expect(client.pendingUserProperties == nil, "No user properties should have been set yet.")
|
|
|
|
// When updating the user properties
|
|
client.updateUserProperties(AnalyticsEvent.UserProperties(URLPreviewsEnabled: nil,
|
|
allChatsActiveFilter: nil,
|
|
ftueUseCaseSelection: .PersonalMessaging,
|
|
numFavouriteRooms: 4,
|
|
numSpaces: 5, recoveryState: .Disabled, verificationState: .Verified))
|
|
|
|
// Then the properties should be cached
|
|
#expect(client.pendingUserProperties != nil, "The user properties should be cached.")
|
|
#expect(client.pendingUserProperties?.ftueUseCaseSelection == .PersonalMessaging, "The use case selection should match.")
|
|
#expect(client.pendingUserProperties?.numFavouriteRooms == 4, "The number of favorite rooms should match.")
|
|
#expect(client.pendingUserProperties?.numSpaces == 5, "The number of spaces should match.")
|
|
#expect(client.pendingUserProperties?.verificationState == AnalyticsEvent.UserProperties.VerificationState.Verified, "The verification state should match.")
|
|
#expect(client.pendingUserProperties?.recoveryState == AnalyticsEvent.UserProperties.RecoveryState.Disabled, "The recovery state should match.")
|
|
}
|
|
|
|
@Test
|
|
func mergingUserProperties() {
|
|
// Given a client with a cached use case user properties
|
|
let client = PostHogAnalyticsClient()
|
|
client.updateUserProperties(AnalyticsEvent.UserProperties(URLPreviewsEnabled: nil,
|
|
allChatsActiveFilter: nil,
|
|
ftueUseCaseSelection: .PersonalMessaging,
|
|
numFavouriteRooms: nil,
|
|
numSpaces: nil, recoveryState: nil, verificationState: nil))
|
|
|
|
#expect(client.pendingUserProperties != nil, "The user properties should be cached.")
|
|
#expect(client.pendingUserProperties?.ftueUseCaseSelection == .PersonalMessaging, "The use case selection should match.")
|
|
#expect(client.pendingUserProperties?.numFavouriteRooms == nil, "The number of favorite rooms should not be set.")
|
|
#expect(client.pendingUserProperties?.numSpaces == nil, "The number of spaces should not be set.")
|
|
|
|
// When updating the number of spaced
|
|
client.updateUserProperties(AnalyticsEvent.UserProperties(URLPreviewsEnabled: nil,
|
|
allChatsActiveFilter: nil,
|
|
ftueUseCaseSelection: nil,
|
|
numFavouriteRooms: 4,
|
|
numSpaces: 5, recoveryState: nil, verificationState: nil))
|
|
|
|
// Then the new properties should be updated and the existing properties should remain unchanged
|
|
#expect(client.pendingUserProperties != nil, "The user properties should be cached.")
|
|
#expect(client.pendingUserProperties?.ftueUseCaseSelection == .PersonalMessaging, "The use case selection shouldn't have changed.")
|
|
#expect(client.pendingUserProperties?.numFavouriteRooms == 4, "The number of favorite rooms should have been updated.")
|
|
#expect(client.pendingUserProperties?.numSpaces == 5, "The number of spaces should have been updated.")
|
|
}
|
|
|
|
@Test
|
|
func sendingUserProperties() throws {
|
|
// Given a client with user properties set
|
|
|
|
let client = PostHogAnalyticsClient(posthogFactory: MockPostHogFactory(mock: posthogMock))
|
|
try client.start(analyticsConfiguration: #require(appSettings.analyticsConfiguration))
|
|
|
|
client.updateUserProperties(AnalyticsEvent.UserProperties(URLPreviewsEnabled: nil,
|
|
allChatsActiveFilter: nil,
|
|
ftueUseCaseSelection: .PersonalMessaging,
|
|
numFavouriteRooms: nil,
|
|
numSpaces: nil, recoveryState: nil, verificationState: nil))
|
|
|
|
#expect(client.pendingUserProperties != nil, "The user properties should be cached.")
|
|
#expect(client.pendingUserProperties?.ftueUseCaseSelection == .PersonalMessaging, "The use case selection should match.")
|
|
|
|
// When sending an event (tests run under Debug configuration so this is sent to the development instance)
|
|
let someEvent = AnalyticsEvent.Error(context: nil,
|
|
cryptoModule: .Rust,
|
|
cryptoSDK: .Rust,
|
|
domain: .E2EE,
|
|
eventLocalAgeMillis: nil,
|
|
isFederated: nil,
|
|
isMatrixDotOrg: nil,
|
|
name: .OlmKeysNotSentError,
|
|
timeToDecryptMillis: nil,
|
|
userTrustsOwnIdentity: nil,
|
|
wasVisibleToUser: nil)
|
|
client.capture(someEvent)
|
|
|
|
let capturedEvent = posthogMock.capturePropertiesUserPropertiesReceivedArguments
|
|
|
|
// The user properties should have been added
|
|
#expect(capturedEvent?.userProperties?["ftueUseCaseSelection"] as? String == AnalyticsEvent.UserProperties.FtueUseCaseSelection.PersonalMessaging.rawValue)
|
|
|
|
// Then the properties should be cleared
|
|
#expect(client.pendingUserProperties == nil, "The user properties should be cleared.")
|
|
}
|
|
|
|
@Test
|
|
func resetConsentState() {
|
|
// Given an existing install of the app where the user previously accpeted the tracking
|
|
appSettings.analyticsConsentState = .optedIn
|
|
#expect(!analytics.shouldShowAnalyticsPrompt)
|
|
|
|
// When forgetting analytics consents
|
|
analytics.resetConsentState()
|
|
|
|
// Then the analytics prompt should be presented again
|
|
#expect(appSettings.analyticsConsentState == .unknown)
|
|
#expect(analytics.shouldShowAnalyticsPrompt)
|
|
}
|
|
|
|
@Test
|
|
func sendingAndUpdatingSuperProperties() throws {
|
|
// Given a client with user properties set
|
|
let client = PostHogAnalyticsClient(posthogFactory: MockPostHogFactory(mock: posthogMock))
|
|
try client.start(analyticsConfiguration: #require(appSettings.analyticsConfiguration))
|
|
|
|
client.updateSuperProperties(AnalyticsEvent.SuperProperties(appPlatform: .EXI,
|
|
cryptoSDK: .Rust,
|
|
cryptoSDKVersion: "000"))
|
|
|
|
// When sending an event (tests run under Debug configuration so this is sent to the development instance)
|
|
client.screen(AnalyticsEvent.MobileScreen(durationMs: nil, screenName: .Home))
|
|
|
|
let screenEvent = posthogMock.screenPropertiesReceivedArguments
|
|
|
|
#expect(screenEvent?.screenTitle == AnalyticsEvent.MobileScreen.ScreenName.Home.rawValue)
|
|
|
|
// All the super properties should have been added
|
|
#expect(screenEvent?.properties?["cryptoSDK"] as? String == AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
|
#expect(screenEvent?.properties?["appPlatform"] as? String == "EXI")
|
|
#expect(screenEvent?.properties?["cryptoSDKVersion"] as? String == "000")
|
|
|
|
// It should be the same for any event
|
|
let someEvent = AnalyticsEvent.Error(context: nil,
|
|
cryptoModule: .Rust,
|
|
cryptoSDK: .Rust,
|
|
domain: .E2EE,
|
|
eventLocalAgeMillis: nil,
|
|
isFederated: nil,
|
|
isMatrixDotOrg: nil,
|
|
name: .OlmKeysNotSentError,
|
|
timeToDecryptMillis: nil,
|
|
userTrustsOwnIdentity: nil,
|
|
wasVisibleToUser: nil)
|
|
client.capture(someEvent)
|
|
|
|
let capturedEvent = posthogMock.capturePropertiesUserPropertiesReceivedArguments
|
|
|
|
// All the super properties should have been added
|
|
#expect(capturedEvent?.properties?["cryptoSDK"] as? String == AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
|
#expect(capturedEvent?.properties?["appPlatform"] as? String == "EXI")
|
|
#expect(capturedEvent?.properties?["cryptoSDKVersion"] as? String == "000")
|
|
|
|
// Updating should keep the previously set properties
|
|
client.updateSuperProperties(AnalyticsEvent.SuperProperties(appPlatform: .EXI,
|
|
cryptoSDK: .Rust,
|
|
cryptoSDKVersion: "001"))
|
|
|
|
client.capture(someEvent)
|
|
let capturedEvent2 = posthogMock.capturePropertiesUserPropertiesReceivedArguments
|
|
|
|
// All the super properties should have been added, with the one udpated
|
|
#expect(capturedEvent2?.properties?["cryptoSDK"] as? String == AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
|
#expect(capturedEvent2?.properties?["appPlatform"] as? String == "EXI")
|
|
#expect(capturedEvent2?.properties?["cryptoSDKVersion"] as? String == "001")
|
|
}
|
|
|
|
@Test
|
|
func shouldNotReportIfNotStarted() throws {
|
|
// Given a client with user properties set
|
|
let client = PostHogAnalyticsClient(posthogFactory: MockPostHogFactory(mock: posthogMock))
|
|
|
|
// No call to start
|
|
|
|
client.screen(AnalyticsEvent.MobileScreen(durationMs: nil, screenName: .Home))
|
|
|
|
#expect(posthogMock.screenPropertiesCalled == false)
|
|
|
|
// It should be the same for any event
|
|
let someEvent = AnalyticsEvent.Error(context: nil,
|
|
cryptoModule: .Rust,
|
|
cryptoSDK: .Rust,
|
|
domain: .E2EE,
|
|
eventLocalAgeMillis: nil,
|
|
isFederated: nil,
|
|
isMatrixDotOrg: nil,
|
|
name: .OlmKeysNotSentError,
|
|
timeToDecryptMillis: nil,
|
|
userTrustsOwnIdentity: nil,
|
|
wasVisibleToUser: nil)
|
|
client.capture(someEvent)
|
|
|
|
#expect(posthogMock.capturePropertiesUserPropertiesCalled == false)
|
|
|
|
// start now
|
|
try client.start(analyticsConfiguration: #require(appSettings.analyticsConfiguration))
|
|
#expect(posthogMock.optInCalled == true)
|
|
|
|
client.capture(someEvent)
|
|
#expect(posthogMock.capturePropertiesUserPropertiesCalled == true)
|
|
}
|
|
}
|