Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,458 changes: 0 additions & 1,458 deletions UnitTests/MParticleTestsSwift.swift

This file was deleted.

134 changes: 134 additions & 0 deletions UnitTests/SwiftTests/Core/MParticleConfigurationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
//
// MParticleConfigurationTests.swift
// mParticle-Apple-SDK
//
// Created by Nick Dimitrakas on 11/3/25.
//

import XCTest
#if MPARTICLE_LOCATION_DISABLE
import mParticle_Apple_SDK_NoLocation
#else
import mParticle_Apple_SDK
#endif

final class MParticleConfigurationTests: MParticleTestBase {

func testStartWithKeyCallbackFirstRun() {
XCTAssertFalse(mparticle.initialized)

mparticle.start(withKeyCallback: true, options: options, userDefaults: userDefaults)

XCTAssertTrue(mparticle.initialized)
XCTAssertNil(mparticle.settingsProvider.configSettings)

XCTAssertNotNil(userDefaults.setMPObjectValueParam)
XCTAssertEqual(userDefaults.setMPObjectKeyParam, "firstrun")
XCTAssertEqual(userDefaults.setMPObjectUserIdParam, 0)
XCTAssertTrue(userDefaults.synchronizeCalled)
}

func testStartWithKeyCallbackNotFirstRunWithIdentityRequest() {
let user = mparticle.identity.currentUser
options.identifyRequest = MPIdentityApiRequest(user: user!)

mparticle.start(withKeyCallback: false, options: options, userDefaults: userDefaults as MPUserDefaultsProtocol)

XCTAssertTrue(mparticle.initialized)
XCTAssertNil(mparticle.settingsProvider.configSettings)

XCTAssertFalse(userDefaults.setMPObjectCalled)
XCTAssertFalse(userDefaults.synchronizeCalled)
}

func testConfigureDefaultConfigurationExistOptionParametersAreNotSet() {
mparticle.backendController = MPBackendController_PRIVATE()
mparticle.configure(with: options)
XCTAssertEqual(mparticle.backendController.sessionTimeout, 0.0)
XCTAssertEqual(mparticle.backendController.uploadInterval, 60.0)
XCTAssertEqual(mparticle.customUserAgent, nil)
XCTAssertEqual(mparticle.collectUserAgent, true)
XCTAssertEqual(mparticle.trackNotifications, true)
}

func testConfigureWhenDefaultConfigurationExists() {
let settings: NSMutableDictionary = [
"session_timeout": NSNumber(value: 2.0),
"upload_interval": NSNumber(value: 3.0),
"custom_user_agent": "custom_user_agent",
"collect_user_agent": false,
"track_notifications": false,
"enable_location_tracking": true,
"location_tracking_accuracy": 100.0,
"location_tracking_distance_filter": 10.0,
]
settingsProvider.configSettings = settings
mparticle.settingsProvider = settingsProvider
mparticle.backendController = MPBackendController_PRIVATE()
mparticle.configure(with: options)

XCTAssertEqual(mparticle.backendController.sessionTimeout, 2.0)
XCTAssertEqual(mparticle.backendController.uploadInterval, 3.0)
XCTAssertEqual(mparticle.customUserAgent, "custom_user_agent")
XCTAssertEqual(mparticle.collectUserAgent, false)
XCTAssertEqual(mparticle.trackNotifications, false)
}

func testConfigureWithOptionsNoSettings() {
mparticle.configure(with: .init())
XCTAssertEqual(backendController.sessionTimeout, 0.0)
XCTAssertEqual(backendController.uploadInterval, 0.0)
XCTAssertNil(mparticle.customUserAgent)
XCTAssertTrue(mparticle.collectUserAgent)
XCTAssertTrue(mparticle.trackNotifications)
#if os(iOS)
#if !MPARTICLE_LOCATION_DISABLE
XCTAssertNil(listenerController.onAPICalledApiName)
#endif
#endif
}

func testConfigureWithOptionsWithSettingsAndOptionNotSet() {
settingsProvider.configSettings = [
"session_timeout": 100,
"upload_interval": 50,
"custom_user_agent": "agent",
"collect_user_agent": false,
"track_notifications": false,
"enable_location_tracking": true,
]
options.isSessionTimeoutSet = false
options.isUploadIntervalSet = false
options.isCollectUserAgentSet = false
options.isCollectUserAgentSet = false
options.isTrackNotificationsSet = false
mparticle.configure(with: .init())
XCTAssertEqual(backendController.sessionTimeout, 100.0)
XCTAssertEqual(backendController.uploadInterval, 50.0)
XCTAssertEqual(mparticle.customUserAgent, "agent")
XCTAssertFalse(mparticle.collectUserAgent)
XCTAssertFalse(mparticle.trackNotifications)

#if os(iOS)
#if !MPARTICLE_LOCATION_DISABLE
XCTAssertEqual(listenerController.onAPICalledApiName?.description,
"beginLocationTracking:minDistance:authorizationRequest:")
#endif
#endif
}

func testResetForSwitchingWorkspaces() {
let expectation = XCTestExpectation()

mparticle.reset {
expectation.fulfill()
}

wait(for: [expectation], timeout: 1.0)

XCTAssertTrue(kitContainer.flushSerializedKitsCalled)
XCTAssertTrue(kitContainer.removeAllSideloadedKitsCalled)
XCTAssertEqual(persistenceController.resetDatabaseCalled, true)
XCTAssertTrue(backendController.unproxyOriginalAppDelegateCalled)
}
}
52 changes: 52 additions & 0 deletions UnitTests/SwiftTests/Core/MParticleIdentityTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// MParticleIdentityTests.swift
// mParticle-Apple-SDK
//
// Created by Nick Dimitrakas on 11/3/25.
//

import Foundation

import XCTest
#if MPARTICLE_LOCATION_DISABLE
import mParticle_Apple_SDK_NoLocation
#else
import mParticle_Apple_SDK
#endif

final class MParticleIdentityTests: MParticleTestBase {

func testIdentifyNoDispatchCallbackNoErrorDefferedKitAvailable() {
mparticle.deferredKitConfiguration_PRIVATE = [[String: String]]()
let expectedApiResult = MPIdentityApiResult()
let expectation = XCTestExpectation()
options.onIdentifyComplete = { apiResult, error in
XCTAssertTrue(expectedApiResult === apiResult)
XCTAssertNil(error)

expectation.fulfill()
}
mparticle.identifyNoDispatchCallback(expectedApiResult, error: nil, options: options)

wait(for: [expectation], timeout: 1.0)
XCTAssertNil(receivedMessage)
XCTAssertNil(mparticle.deferredKitConfiguration_PRIVATE)
}

func testIdentifyNoDispatchCallbackWithErrorDefferedKitAvailable() {
mparticle.deferredKitConfiguration_PRIVATE = [[String: String]]()
let expectedApiResult = MPIdentityApiResult()
let expectation = XCTestExpectation()
options.onIdentifyComplete = { apiResult, _ in
XCTAssertTrue(expectedApiResult === apiResult)
XCTAssertTrue(self.error == self.error)

expectation.fulfill()
}
mparticle.identifyNoDispatchCallback(expectedApiResult, error: error, options: options)

wait(for: [expectation], timeout: 0.1)
assertReceivedMessage("Identify request failed with error: \(error)")
XCTAssertNil(mparticle.deferredKitConfiguration_PRIVATE)
}
}
85 changes: 85 additions & 0 deletions UnitTests/SwiftTests/Core/MParticleRuntimeValuesTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// MParticleRuntimeValuesTests.swift
// mParticle-Apple-SDK
//
// Created by Nick Dimitrakas on 11/3/25.
//

import XCTest
#if MPARTICLE_LOCATION_DISABLE
import mParticle_Apple_SDK_NoLocation
#else
import mParticle_Apple_SDK
#endif

final class MParticleRuntimeValuesTests: MParticleTestBase {

func testSetSharedInstance() {
MParticle.setSharedInstance(mparticle)
XCTAssertEqual(listenerController.onAPICalledApiName?.description, "setSharedInstance:")
XCTAssertTrue(listenerController.onAPICalledParameter1 === mparticle)
}

func testSetOptOutCompletionSuccess() {
mparticle.setOptOutCompletion(.success, optOut: true)
assertReceivedMessage("Set Opt Out: 1")
}

func testSetOptOutCompletionFailure() {
mparticle.setOptOutCompletion(.fail, optOut: true)
assertReceivedMessage("Set Opt Out Failed: 1")
}

func testIndentityReturnsTheSameObject() {
let identity = mparticle.identity
XCTAssertTrue(identity === mparticle.identity)
}

func testRoktReturnsTheSameObject() {
let rokt = mparticle.rokt
XCTAssertTrue(rokt === mparticle.rokt)
}

func testSessionTimeoutReturnsValueFromBackendController() {
mparticle.backendController.sessionTimeout = 100
XCTAssertEqual(mparticle.sessionTimeout, 100)
}

func testUniqueIdentifierRwturnedFromStateMachine() {
state.consumerInfo.uniqueIdentifier = "test"
XCTAssertEqual(mparticle.uniqueIdentifier, "test")
}

func testSetUploadIntervalChangeValueInBackendControllerWhenIntervalGreaterThenOne() {
mparticle.setUploadInterval(3)
XCTAssertEqual(backendController.uploadInterval, 3)
}

func testSetUploadIntervalNotChangeValueInBackendControllerWhenIntervalLessThenOne() {
mparticle.setUploadInterval(0.1)
XCTAssertEqual(backendController.uploadInterval, 0.0)
}

func testUploadIntervalGetFromBackendController() {
backendController.uploadInterval = 100
XCTAssertEqual(mparticle.uploadInterval, 100)
}

func testUserAttributesForUserIdRequestDataFromBackendController() {
backendController.userAttributesReturnValue = ["key": "value"]
let dictionary = mparticle.userAttributes(forUserId: 1)
XCTAssertEqual(dictionary?["key"] as? String, "value")
XCTAssertTrue(backendController.userAttributesCalled)
XCTAssertEqual(backendController.userAttributesUserIdParam, 1)
}

func testSetOptOutOptOutValueIsDifferentItShouldBeChangedAndDeliveredToBackendController() {
XCTAssertFalse(state.optOut)
mparticle.optOut = true
XCTAssertTrue(state.optOut)
XCTAssertTrue(backendController.setOptOutCalled)
XCTAssertEqual(backendController.setOptOutOptOutStatusParam, true)
XCTAssertNotNil(backendController.setOptOutCompletionHandler)
backendController.setOptOutCompletionHandler?(true, .success)
}
}
86 changes: 86 additions & 0 deletions UnitTests/SwiftTests/Core/MParticleSessionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//
// MParticleSessionTests.swift
// mParticle-Apple-SDK
//
// Created by Nick Dimitrakas on 11/3/25.
//

import Foundation

import XCTest
#if MPARTICLE_LOCATION_DISABLE
import mParticle_Apple_SDK_NoLocation
#else
import mParticle_Apple_SDK
#endif

final class MParticleSessionTests: MParticleTestBase {

func testBeginSessionTempSessionAvailableSessionTempSessionShouldNotBeCreated() {
backendController.session = nil
backendController.tempSessionReturnValue = MParticleSession()
mparticle.beginSession()
XCTAssertFalse(backendController.createTempSessionCalled)
}

func testBeginSessionSessionAvailableSessionTempSessionShouldNotBeCreated() {
backendController.session = MPSession()
backendController.tempSessionReturnValue = nil
mparticle.beginSession()
XCTAssertFalse(backendController.createTempSessionCalled)
}

func testBeginSessionSessionUnavailable() {
backendController.session = nil
backendController.tempSessionReturnValue = nil
mparticle.beginSession()
XCTAssertTrue(executor.executeOnMessageQueueAsync)
XCTAssertTrue(backendController.createTempSessionCalled)
XCTAssertTrue(backendController.beginSessionCalled)
XCTAssertEqual(backendController.beginSessionIsManualParam, true)
XCTAssertNotNil(backendController.beginSessionDateParam)
}

func testEndSessionNoSession() {
backendController.session = nil
mparticle.endSession()
XCTAssertEqual(executor.executeOnMessageQueueAsync, true)
XCTAssertFalse(backendController.endSessionWithIsManualCalled)
}

func testEndSessionWithSession() {
backendController.session = MPSession()
mparticle.endSession()
XCTAssertEqual(executor.executeOnMessageQueueAsync, true)
XCTAssertTrue(backendController.endSessionWithIsManualCalled)
XCTAssertEqual(backendController.endSessionIsManualParam, true)
}

func testSessionDidBegin() {
kitContainer.forwardSDKCallExpectation = XCTestExpectation()
mparticle.sessionDidBegin(MPSession())

wait(for: [kitContainer.forwardSDKCallExpectation!], timeout: 1.0)

XCTAssertTrue(kitContainer.forwardSDKCallCalled)
XCTAssertEqual(kitContainer.forwardSDKCallSelectorParam?.description, "beginSession")
XCTAssertEqual(kitContainer.forwardSDKCallMessageTypeParam, .sessionStart)
XCTAssertNil(kitContainer.forwardSDKCallEventParam)
XCTAssertNil(kitContainer.forwardSDKCallParametersParam)
XCTAssertNil(kitContainer.forwardSDKCallUserInfoParam)
}

func testSessionDidEnd() {
kitContainer.forwardSDKCallExpectation = XCTestExpectation()
mparticle.sessionDidEnd(MPSession())

wait(for: [kitContainer.forwardSDKCallExpectation!], timeout: 1.0)

XCTAssertTrue(kitContainer.forwardSDKCallCalled)
XCTAssertEqual(kitContainer.forwardSDKCallSelectorParam?.description, "endSession")
XCTAssertEqual(kitContainer.forwardSDKCallMessageTypeParam, .sessionEnd)
XCTAssertNil(kitContainer.forwardSDKCallEventParam)
XCTAssertNil(kitContainer.forwardSDKCallParametersParam)
XCTAssertNil(kitContainer.forwardSDKCallUserInfoParam)
}
}
Loading
Loading