mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 21:33:26 +00:00
Set up Analytics to track data per session (#780)
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
@MainActor
|
||||
class AnalyticsSettingsScreenViewModelTests: XCTestCase {
|
||||
private var applicationSettings: AppSettings!
|
||||
private var viewModel: AnalyticsSettingsScreenViewModelProtocol!
|
||||
private var context: AnalyticsSettingsScreenViewModelType.Context!
|
||||
|
||||
@MainActor override func setUpWithError() throws {
|
||||
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
|
||||
AppSettings.reset()
|
||||
applicationSettings = AppSettings()
|
||||
ServiceLocator.shared.register(appSettings: applicationSettings)
|
||||
let analyticsClient = AnalyticsClientMock()
|
||||
analyticsClient.isRunning = false
|
||||
ServiceLocator.shared.register(analytics: Analytics(client: analyticsClient))
|
||||
|
||||
viewModel = AnalyticsSettingsScreenViewModel()
|
||||
context = viewModel.context
|
||||
}
|
||||
|
||||
func testInitialState() {
|
||||
XCTAssertFalse(context.enableAnalytics)
|
||||
}
|
||||
|
||||
func testOptIn() {
|
||||
context.send(viewAction: .toggleAnalytics)
|
||||
XCTAssertTrue(context.enableAnalytics)
|
||||
}
|
||||
|
||||
func testOptOut() {
|
||||
applicationSettings.enableAnalytics = true
|
||||
context.send(viewAction: .toggleAnalytics)
|
||||
XCTAssertFalse(context.enableAnalytics)
|
||||
}
|
||||
}
|
||||
@@ -20,17 +20,26 @@ import XCTest
|
||||
|
||||
class AnalyticsTests: XCTestCase {
|
||||
private var applicationSettings: AppSettings!
|
||||
private var analyticsClient: AnalyticsClientMock!
|
||||
private var bugReportService: BugReportServiceMock!
|
||||
|
||||
override func setUp() {
|
||||
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
|
||||
AppSettings.reset()
|
||||
applicationSettings = AppSettings()
|
||||
ServiceLocator.shared.register(appSettings: applicationSettings)
|
||||
bugReportService = BugReportServiceMock()
|
||||
bugReportService.isRunning = false
|
||||
ServiceLocator.shared.register(bugReportService: bugReportService)
|
||||
analyticsClient = AnalyticsClientMock()
|
||||
analyticsClient.isRunning = false
|
||||
ServiceLocator.shared.register(analytics: Analytics(client: analyticsClient))
|
||||
}
|
||||
|
||||
func testAnalyticsPromptNewUser() {
|
||||
// Given a fresh install of the app (without PostHog analytics having been set).
|
||||
// When the user is prompted for analytics.
|
||||
let showPrompt = Analytics.shared.shouldShowAnalyticsPrompt
|
||||
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
|
||||
|
||||
// Then the prompt should be shown.
|
||||
XCTAssertTrue(showPrompt, "A prompt should be shown for a new user.")
|
||||
@@ -41,7 +50,7 @@ class AnalyticsTests: XCTestCase {
|
||||
applicationSettings.enableAnalytics = false
|
||||
|
||||
// When the user is prompted for analytics
|
||||
let showPrompt = Analytics.shared.shouldShowAnalyticsPrompt
|
||||
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
|
||||
|
||||
// Then no prompt should be shown.
|
||||
XCTAssertFalse(showPrompt, "A prompt should not be shown any more.")
|
||||
@@ -52,12 +61,65 @@ class AnalyticsTests: XCTestCase {
|
||||
applicationSettings.enableAnalytics = true
|
||||
|
||||
// When the user is prompted for analytics
|
||||
let showPrompt = Analytics.shared.shouldShowAnalyticsPrompt
|
||||
let showPrompt = ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt
|
||||
|
||||
// Then no prompt should be shown.
|
||||
XCTAssertFalse(showPrompt, "A prompt should not be shown any more.")
|
||||
}
|
||||
|
||||
func testAnalyticsPromptNotDisplayed() {
|
||||
// Given a fresh install of the app both Analytics and BugReportService should be disabled
|
||||
XCTAssertFalse(ServiceLocator.shared.settings.enableAnalytics)
|
||||
XCTAssertFalse(ServiceLocator.shared.analytics.isRunning)
|
||||
XCTAssertFalse(analyticsClient.startCalled)
|
||||
XCTAssertFalse(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
func testAnalyticsOptOut() {
|
||||
// Given a fresh install of the app (without PostHog analytics having been set).
|
||||
// When analytics is opt-out
|
||||
ServiceLocator.shared.analytics.optOut()
|
||||
// Then analytics should be disabled
|
||||
XCTAssertFalse(applicationSettings.enableAnalytics)
|
||||
XCTAssertFalse(ServiceLocator.shared.analytics.isRunning)
|
||||
XCTAssertFalse(analyticsClient.isRunning)
|
||||
XCTAssertFalse(bugReportService.isRunning)
|
||||
// Analytics client and the bug report service should have been stopped
|
||||
XCTAssertTrue(analyticsClient.stopCalled)
|
||||
XCTAssertTrue(bugReportService.stopCalled)
|
||||
}
|
||||
|
||||
func testAnalyticsOptIn() {
|
||||
// Given a fresh install of the app (without PostHog analytics having been set).
|
||||
// When analytics is opt-in
|
||||
ServiceLocator.shared.analytics.optIn()
|
||||
// The analytics should be enabled
|
||||
XCTAssertTrue(applicationSettings.enableAnalytics)
|
||||
// Analytics client and the bug report service should have been started
|
||||
XCTAssertTrue(analyticsClient.startCalled)
|
||||
XCTAssertTrue(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
func testAnalyticsStartIfNotEnabled() {
|
||||
// Given an existing install of the app where the user previously declined the tracking
|
||||
applicationSettings.enableAnalytics = false
|
||||
// Analytics should not start
|
||||
ServiceLocator.shared.analytics.startIfEnabled()
|
||||
XCTAssertFalse(ServiceLocator.shared.settings.enableAnalytics)
|
||||
XCTAssertFalse(analyticsClient.startCalled)
|
||||
XCTAssertFalse(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
func testAnalyticsStartIfEnabled() {
|
||||
// Given an existing install of the app where the user previously accpeted the tracking
|
||||
applicationSettings.enableAnalytics = true
|
||||
// Analytics should start
|
||||
ServiceLocator.shared.analytics.startIfEnabled()
|
||||
XCTAssertTrue(ServiceLocator.shared.settings.enableAnalytics)
|
||||
XCTAssertTrue(analyticsClient.startCalled)
|
||||
XCTAssertTrue(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
func testAddingUserProperties() {
|
||||
// Given a client with no user properties set
|
||||
let client = PostHogAnalyticsClient()
|
||||
@@ -120,23 +182,4 @@ class AnalyticsTests: XCTestCase {
|
||||
// Then the properties should be cleared
|
||||
XCTAssertNil(client.pendingUserProperties, "The user properties should be cleared.")
|
||||
}
|
||||
|
||||
func testSendingUserPropertiesWithIdentify() {
|
||||
// Given a client with user properties set
|
||||
let client = PostHogAnalyticsClient()
|
||||
client.updateUserProperties(AnalyticsEvent.UserProperties(ftueUseCaseSelection: .PersonalMessaging,
|
||||
numFavouriteRooms: nil,
|
||||
numSpaces: nil,
|
||||
allChatsActiveFilter: nil))
|
||||
client.start()
|
||||
|
||||
XCTAssertNotNil(client.pendingUserProperties, "The user properties should be cached.")
|
||||
XCTAssertEqual(client.pendingUserProperties?.ftueUseCaseSelection, .PersonalMessaging, "The use case selection should match.")
|
||||
|
||||
// When calling identify (tests run under Debug configuration so this is sent to the development instance)
|
||||
client.identify(id: UUID().uuidString)
|
||||
|
||||
// Then the properties should be cleared
|
||||
XCTAssertNil(client.pendingUserProperties, "The user properties should be cleared.")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,9 +27,14 @@ final class NotificationManagerTests: XCTestCase {
|
||||
private var shouldDisplayInAppNotificationReturnValue = false
|
||||
private var handleInlineReplyDelegateCalled = false
|
||||
private var notificationTappedDelegateCalled = false
|
||||
private let settings = ServiceLocator.shared.settings
|
||||
private var settings: AppSettings!
|
||||
|
||||
override func setUp() {
|
||||
AppSettings.configureWithSuiteName("io.element.elementx.unitests")
|
||||
AppSettings.reset()
|
||||
settings = AppSettings()
|
||||
ServiceLocator.shared.register(appSettings: settings)
|
||||
|
||||
notificationManager = NotificationManager(notificationCenter: notificationCenter)
|
||||
notificationManager.start()
|
||||
notificationManager.setClientProxy(clientProxy)
|
||||
@@ -114,7 +119,7 @@ final class NotificationManagerTests: XCTestCase {
|
||||
|
||||
func test_whenStart_requestAuthorizationCalledWithCorrectParams() async throws {
|
||||
notificationManager.requestAuthorization()
|
||||
await Task.yield()
|
||||
try await Task.sleep(for: .milliseconds(100))
|
||||
XCTAssertEqual(notificationCenter.requestAuthorizationOptions, [.alert, .sound, .badge])
|
||||
}
|
||||
|
||||
|
||||
@@ -56,4 +56,15 @@ class SettingsScreenViewModelTests: XCTestCase {
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
}
|
||||
|
||||
func testAnalytics() async throws {
|
||||
var correctResult = false
|
||||
viewModel.callback = { result in
|
||||
correctResult = result == .analytics
|
||||
}
|
||||
|
||||
context.send(viewAction: .analytics)
|
||||
await Task.yield()
|
||||
XCTAssert(correctResult)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user