-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathAppStateManagerInstrumentedTest.kt
More file actions
153 lines (144 loc) · 5.83 KB
/
Copy pathAppStateManagerInstrumentedTest.kt
File metadata and controls
153 lines (144 loc) · 5.83 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.mparticle.internal
import android.content.Context
import android.util.MutableBoolean
import com.mparticle.MParticle
import com.mparticle.MParticleOptions
import com.mparticle.internal.database.services.AccessUtils
import com.mparticle.internal.database.services.MParticleDBManager
import com.mparticle.testutils.BaseCleanStartedEachTest
import com.mparticle.testutils.MPLatch
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import org.json.JSONException
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import java.util.concurrent.CountDownLatch
class AppStateManagerInstrumentedTest : BaseCleanStartedEachTest() {
var mAppStateManager: AppStateManager? = null
@Before
@Throws(Exception::class)
fun before() {
mAppStateManager = MParticle.getInstance()?.Internal()?.appStateManager
MParticle.getInstance()?.Internal()?.configManager?.setMpid(Constants.TEMPORARY_MPID, false)
}
@Test
@Throws(Exception::class)
fun testEndSessionMultipleMpids() {
val mpids: MutableSet<Long> = HashSet()
for (i in 0..4) {
mpids.add(ran.nextLong())
}
mAppStateManager?.ensureActiveSession()
for (mpid in mpids) {
mAppStateManager?.fetchSession()?.addMpid(mpid)
}
val checked = BooleanArray(1)
val latch: CountDownLatch = MPLatch(1)
AccessUtils.setMessageStoredListener(
MParticleDBManager.MessageListener { message ->
if (message.messageType == Constants.MessageType.SESSION_END) {
try {
val mpidsArray =
message.getJSONArray(Constants.MessageKey.SESSION_SPANNING_MPIDS)
Assert.assertEquals(mpidsArray.length().toLong(), mpids.size.toLong())
for (i in 0 until mpidsArray.length()) {
if (!mpids.contains(mpidsArray.getLong(i))) {
return@MessageListener
}
}
checked[0] = true
latch.countDown()
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
)
mAppStateManager?.endSession()
latch.await()
Assert.assertTrue(checked[0])
}
@Test
@Throws(Exception::class)
fun testDontIncludeDefaultMpidSessionEnd() {
val mpids: MutableSet<Long> = HashSet()
for (i in 0..4) {
mpids.add(ran.nextLong())
}
mpids.add(Constants.TEMPORARY_MPID)
mAppStateManager?.ensureActiveSession()
for (mpid in mpids) {
mAppStateManager?.fetchSession()?.addMpid(mpid)
}
val latch: CountDownLatch = MPLatch(1)
val checked = MutableBoolean(false)
AccessUtils.setMessageStoredListener(
MParticleDBManager.MessageListener { message ->
if (message.messageType == Constants.MessageType.SESSION_END) {
try {
val mpidsArray =
message.getJSONArray(Constants.MessageKey.SESSION_SPANNING_MPIDS)
if (mpidsArray.length() == mpids.size - 1) {
for (i in 0 until mpidsArray.length()) {
if (!mpids.contains(mpidsArray.getLong(i)) || mpidsArray.getLong(i) == Constants.TEMPORARY_MPID) {
return@MessageListener
}
}
checked.value = true
latch.countDown()
}
} catch (e: JSONException) {
e.printStackTrace()
}
}
}
)
mAppStateManager?.endSession()
latch.await()
Assert.assertTrue(checked.value)
}
@Test
@Throws(InterruptedException::class)
fun testOnApplicationForeground() = runTest(StandardTestDispatcher()) {
val latch: CountDownLatch = MPLatch(2)
val kitManagerTester = KitManagerTester(mContext, latch)
com.mparticle.AccessUtils.setKitManager(kitManagerTester)
goToBackground()
Assert.assertNull(mAppStateManager?.currentActivity)
Thread.sleep(AppStateManager.ACTIVITY_DELAY + 100)
goToForeground()
Assert.assertNotNull(mAppStateManager?.currentActivity?.get())
latch.await()
Assert.assertTrue(kitManagerTester.onApplicationBackgroundCalled)
Assert.assertTrue(kitManagerTester.onApplicationForegroundCalled)
}
internal inner class KitManagerTester(context: Context?, var latch: CountDownLatch) :
KitFrameworkWrapper(
context,
object : ReportingManager {
override fun log(message: JsonReportingMessage) {
// do nothing
}
override fun logAll(messageList: List<JsonReportingMessage>) {
// do nothing
}
},
MParticle.getInstance()?.Internal()?.configManager,
MParticle.getInstance()?.Internal()?.appStateManager,
MParticleOptions.builder(mContext).credentials("some", "key").build()
) {
var onApplicationBackgroundCalled = false
var onApplicationForegroundCalled = false
override fun onApplicationBackground() {
Assert.assertNull(currentActivity)
onApplicationBackgroundCalled = true
latch.countDown()
}
override fun onApplicationForeground() {
Assert.assertNotNull(currentActivity.get())
onApplicationForegroundCalled = true
latch.countDown()
}
}
}