-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathSessionMessagesTest.kt
More file actions
72 lines (68 loc) · 2.96 KB
/
Copy pathSessionMessagesTest.kt
File metadata and controls
72 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.mparticle
import android.os.Handler
import android.os.Looper
import com.mparticle.internal.AccessUtils
import com.mparticle.internal.AppStateManager
import com.mparticle.internal.Constants
import com.mparticle.networking.Matcher
import com.mparticle.networking.MockServer.JSONMatch
import com.mparticle.testutils.AndroidUtils
import com.mparticle.testutils.BaseCleanStartedEachTest
import org.junit.Assert
import org.junit.Before
import org.junit.Test
class SessionMessagesTest : BaseCleanStartedEachTest() {
private lateinit var mAppStateManager: AppStateManager
private lateinit var mHandler: Handler
@Before
fun before() {
mAppStateManager = MParticle.getInstance()?.mInternal?.appStateManager!!
mHandler = Handler(Looper.getMainLooper())
}
@Test
@Throws(Exception::class)
fun testSessionStartMessage() {
val sessionStartReceived = BooleanArray(1)
sessionStartReceived[0] = false
Assert.assertFalse(mAppStateManager.fetchSession().isActive)
val sessionId = AndroidUtils.Mutable<String?>(null)
mAppStateManager.ensureActiveSession()
sessionId.value = mAppStateManager.fetchSession().mSessionID
AccessUtils.awaitMessageHandler()
MParticle.getInstance()?.upload()
mServer.waitForVerify(
Matcher(mServer.Endpoints().eventsUrl).bodyMatch(
JSONMatch { jsonObject ->
try {
val jsonArray = jsonObject.optJSONArray(Constants.MessageKey.MESSAGES)
?: return@JSONMatch false
for (i in 0 until jsonArray.length()) {
val eventObject = jsonArray.getJSONObject(i)
if (eventObject.getString("dt") == Constants.MessageType.SESSION_START) {
Assert.assertEquals(
eventObject.getLong("ct").toFloat(),
mAppStateManager.fetchSession().mSessionStartTime.toFloat(),
1000f
)
Assert.assertEquals(
"""started sessionID = ${sessionId.value}
current sessionId = ${mAppStateManager.fetchSession().mSessionID}
sent sessionId = ${eventObject.getString("id")}""",
mAppStateManager.fetchSession().mSessionID,
eventObject.getString("id")
)
sessionStartReceived[0] = true
return@JSONMatch true
}
}
} catch (e: Exception) {
e.printStackTrace()
Assert.fail(e.message)
}
false
}
)
)
Assert.assertTrue(sessionStartReceived[0])
}
}