-
Notifications
You must be signed in to change notification settings - Fork 7
/
FakePayloadFactory.kt
126 lines (108 loc) · 3.92 KB
/
FakePayloadFactory.kt
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
package io.embrace.android.embracesdk
import io.embrace.android.embracesdk.fakes.fakeSession
import io.embrace.android.embracesdk.fakes.fakeSessionMessage
import io.embrace.android.embracesdk.payload.Session
import io.embrace.android.embracesdk.payload.SessionMessage
import io.embrace.android.embracesdk.session.lifecycle.ProcessState
import io.embrace.android.embracesdk.session.message.PayloadFactory
internal class FakePayloadFactory : PayloadFactory {
val startSessionTimestamps = mutableListOf<Long>()
val endSessionTimestamps = mutableListOf<Long>()
var manualSessionEndCount = 0
var manualSessionStartCount = 0
var snapshotSessionCount = 0
private var activeSession: Session? = null
val endBaTimestamps = mutableListOf<Long>()
val startBaTimestamps = mutableListOf<Long>()
var baCrashId: String? = null
private var snapshotBaCount: Int = 0
override fun startPayloadWithState(
state: ProcessState,
timestamp: Long,
coldStart: Boolean
): Session {
return when (state) {
ProcessState.FOREGROUND -> startSessionWithState(timestamp)
ProcessState.BACKGROUND -> startBackgroundActivityWithState(timestamp)
}
}
override fun endPayloadWithState(
state: ProcessState,
timestamp: Long,
initial: Session
): SessionMessage {
return when (state) {
ProcessState.FOREGROUND -> endSessionWithState(timestamp)
ProcessState.BACKGROUND -> endBackgroundActivityWithState(timestamp)
}
}
override fun endPayloadWithCrash(
state: ProcessState,
timestamp: Long,
initial: Session,
crashId: String
): SessionMessage {
return when (state) {
ProcessState.FOREGROUND -> endSessionWithCrash(crashId)
ProcessState.BACKGROUND -> endBackgroundActivityWithCrash(crashId)
}
}
override fun snapshotPayload(
state: ProcessState,
timestamp: Long,
initial: Session
): SessionMessage? {
return when (state) {
ProcessState.FOREGROUND -> snapshotSession()
ProcessState.BACKGROUND -> snapshotBackgroundActivity()
}
}
private fun startBackgroundActivityWithState(timestamp: Long): Session {
startBaTimestamps.add(timestamp)
return fakeBackgroundActivity()
}
private fun endBackgroundActivityWithState(timestamp: Long): SessionMessage {
endBaTimestamps.add(timestamp)
return fakeBackgroundActivityMessage()
}
private fun endBackgroundActivityWithCrash(
crashId: String
): SessionMessage {
this.baCrashId = crashId
return fakeBackgroundActivityMessage()
}
private fun snapshotBackgroundActivity(): SessionMessage {
snapshotBaCount++
return fakeBackgroundActivityMessage()
}
private fun startSessionWithState(timestamp: Long): Session {
startSessionTimestamps.add(timestamp)
activeSession = fakeSession(startMs = timestamp)
return checkNotNull(activeSession)
}
override fun startSessionWithManual(timestamp: Long): Session {
manualSessionStartCount++
activeSession = fakeSession()
return checkNotNull(activeSession)
}
private fun endSessionWithState(timestamp: Long): SessionMessage {
endSessionTimestamps.add(timestamp)
activeSession = null
return fakeSessionMessage()
}
var crashId: String? = null
private fun endSessionWithCrash(crashId: String): SessionMessage {
this.crashId = crashId
activeSession = null
return fakeSessionMessage()
}
override fun endSessionWithManual(timestamp: Long, initial: Session): SessionMessage {
manualSessionEndCount++
activeSession = null
return fakeSessionMessage()
}
private fun snapshotSession(): SessionMessage? {
snapshotSessionCount++
return null
}
}