@@ -5,6 +5,17 @@ import XCTest
55 import mParticle_Apple_SDK
66#endif
77
8+ enum LogKitBatchData {
9+ static let invalidJSON = #"{"invalid": "json""#
10+ static let singleEvent = #"{"events":[{"id":1}]}"#
11+ static let multiEvent = #"{"events":[{"id":1},{"id":2}]}"#
12+ static let parsedSingleEvent : [ String : Any ] = [
13+ " events " : [
14+ [ " id " : 1 ]
15+ ]
16+ ]
17+ }
18+
819class MParticleTestsSwift : XCTestCase {
920 var receivedMessage : String ?
1021 var mparticle : MParticle !
@@ -16,6 +27,8 @@ class MParticleTestsSwift: XCTestCase {
1627 var notificationController : MPNotificationControllerMock !
1728 var appEnvironmentProvier : AppEnvironmentProviderMock !
1829 var appNotificationHandler : MPAppNotificationHandlerMock !
30+ var persistenceController : MPPersistenceControllerMock !
31+ var kit : MPKitMock !
1932
2033 func customLogger( _ message: String ) {
2134 receivedMessage = message
@@ -51,6 +64,11 @@ class MParticleTestsSwift: XCTestCase {
5164
5265 appNotificationHandler = MPAppNotificationHandlerMock ( )
5366 mparticle. appNotificationHandler = appNotificationHandler
67+
68+ persistenceController = MPPersistenceControllerMock ( )
69+ mparticle. persistenceController = persistenceController
70+
71+ kit = MPKitMock ( )
5472 }
5573
5674 override func tearDown( ) {
@@ -606,9 +624,6 @@ class MParticleTestsSwift: XCTestCase {
606624 func testResetForSwitchingWorkspaces( ) {
607625 let expectation = XCTestExpectation ( )
608626
609- let persistenceController = MPPersistenceControllerMock ( )
610- mparticle. persistenceController = persistenceController
611-
612627 mparticle. reset {
613628 expectation. fulfill ( )
614629 }
@@ -935,6 +950,114 @@ class MParticleTestsSwift: XCTestCase {
935950 XCTAssertTrue ( kitContainer. forwardSDKCallEventParam === transformedEvent)
936951 }
937952
953+ // MARK: - logKitBatch
954+
955+ func testLogKitBatch_withNilBatch_doesNotExecuteOrForward( ) {
956+ mparticle. logKitBatch ( nil )
957+
958+ XCTAssertEqual ( receivedMessage, " mParticle -> Cannot log nil batch! " )
959+ XCTAssertFalse ( executor. executeOnMessageQueueAsync)
960+ XCTAssertFalse ( kitContainer. hasKitBatchingKitsCalled)
961+ XCTAssertFalse ( executor. executeOnMainAsync)
962+ XCTAssertFalse ( kitContainer. forwardSDKCallCalled)
963+ XCTAssertFalse ( persistenceController. saveCalled)
964+ }
965+
966+ func testLogKitBatch_noBatchingKits_andKitsInitialized_doesNothing( ) {
967+ kitContainer. kitsInitialized = true
968+ kitContainer. hasKitBatchingKitsReturnValue = false
969+
970+ mparticle. logKitBatch ( LogKitBatchData . singleEvent)
971+
972+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
973+ XCTAssertFalse ( executor. executeOnMainAsync)
974+ XCTAssertFalse ( kitContainer. forwardSDKCallCalled)
975+ XCTAssertFalse ( persistenceController. saveCalled)
976+ }
977+
978+ func testLogKitBatch_kitsNotInitialized_defersWorkUntilInitialization( ) {
979+ kitContainer. kitsInitialized = false
980+ kitContainer. hasKitBatchingKitsReturnValue = true
981+
982+ mparticle. logKitBatch ( LogKitBatchData . singleEvent)
983+
984+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
985+
986+ // Should queue deferred block, not execute immediately
987+ XCTAssertFalse ( executor. executeOnMainAsync)
988+ XCTAssertFalse ( kitContainer. forwardSDKCallCalled)
989+ XCTAssertFalse ( persistenceController. saveCalled)
990+
991+ // Simulate kits becoming initialized
992+ kitContainer. kitsInitialized = true
993+ mparticle. executeKitsInitializedBlocks ( )
994+
995+ XCTAssertEqual ( listenerController. onAPICalledApiName? . description, " executeKitsInitializedBlocks " )
996+ XCTAssertTrue ( executor. executeOnMainAsync)
997+ XCTAssertTrue ( kitContainer. forwardSDKCallCalled)
998+ XCTAssertEqual ( kitContainer. forwardSDKCallSelectorParam? . description, " logBatch: " )
999+ XCTAssertEqual ( kitContainer. forwardSDKCallBatchParam? . count, 1 )
1000+ XCTAssertNotNil ( kitContainer. forwardSDKCallKitHandlerParam)
1001+ }
1002+
1003+ func testLogKitBatch_withBatchingKits_forwardsParsedBatch_andPersistsForwardRecords( ) {
1004+ kitContainer. kitsInitialized = true
1005+ kitContainer. hasKitBatchingKitsReturnValue = true
1006+
1007+ mparticle. logKitBatch ( LogKitBatchData . multiEvent)
1008+
1009+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
1010+ XCTAssertTrue ( executor. executeOnMainAsync)
1011+ XCTAssertTrue ( kitContainer. forwardSDKCallCalled)
1012+ XCTAssertEqual ( kitContainer. forwardSDKCallSelectorParam? . description, " logBatch: " )
1013+ XCTAssertEqual ( kitContainer. forwardSDKCallBatchParam? . count, 1 )
1014+ XCTAssertNotNil ( kitContainer. forwardSDKCallKitHandlerParam)
1015+ }
1016+
1017+ func testLogKitBatch_invokesKitHandler_andPersistsForwardRecords( ) {
1018+ kitContainer. kitsInitialized = true
1019+ kitContainer. hasKitBatchingKitsReturnValue = true
1020+
1021+ let forwardRecord = MPForwardRecord ( )
1022+ kit. logBatchReturnValue = [ forwardRecord]
1023+
1024+ mparticle. logKitBatch ( LogKitBatchData . singleEvent)
1025+
1026+ guard let kitHandler = kitContainer. forwardSDKCallKitHandlerParam else {
1027+ XCTFail ( " Expected kitHandler closure to be captured " )
1028+ return
1029+ }
1030+
1031+ // Simulate invoking the handler
1032+ let config = MPKitConfiguration ( )
1033+ kitHandler ( kit, LogKitBatchData . parsedSingleEvent, config)
1034+
1035+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
1036+ XCTAssertTrue ( executor. executeOnMainAsync)
1037+ XCTAssertTrue ( kitContainer. forwardSDKCallCalled)
1038+ XCTAssertEqual ( kitContainer. forwardSDKCallSelectorParam? . description, " logBatch: " )
1039+ XCTAssertEqual ( kitContainer. forwardSDKCallBatchParam? . count, 1 )
1040+ XCTAssertTrue ( kit. logBatchCalled)
1041+ XCTAssertEqual ( kit. logBatchParam ? [ " events " ] as? [ [ String : Int ] ] , [ [ " id " : 1 ] ] )
1042+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
1043+ XCTAssertTrue ( persistenceController. saveCalled)
1044+ XCTAssertTrue ( persistenceController. saveForwardRecordParam === forwardRecord)
1045+ }
1046+
1047+ func testLogKitBatch_invalidJSON_stillForwardsWithNilBatch( ) {
1048+ kitContainer. kitsInitialized = true
1049+ kitContainer. hasKitBatchingKitsReturnValue = true
1050+
1051+ mparticle. logKitBatch ( LogKitBatchData . invalidJSON)
1052+
1053+ XCTAssertTrue ( executor. executeOnMessageQueueAsync)
1054+ XCTAssertTrue ( executor. executeOnMainAsync)
1055+ XCTAssertTrue ( kitContainer. forwardSDKCallCalled)
1056+ XCTAssertEqual ( kitContainer. forwardSDKCallSelectorParam? . description, " logBatch: " )
1057+ XCTAssertEqual ( kitContainer. forwardSDKCallBatchParam? . count, 0 )
1058+ XCTAssertNotNil ( kitContainer. forwardSDKCallKitHandlerParam)
1059+ }
1060+
9381061 // MARK: - logCommerceEvent
9391062
9401063 func testLogCommerceEvent_assignsTimestampWhenNil( ) {
0 commit comments