Skip to content

Commit 17e59c0

Browse files
committed
Generate a SessionEvent based on the SessionState
1 parent 4cc494c commit 17e59c0

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/FirebaseSessions.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import androidx.annotation.Discouraged
2222
import com.google.firebase.FirebaseApp
2323
import com.google.firebase.ktx.Firebase
2424
import com.google.firebase.ktx.app
25-
import com.google.firebase.sessions.EventType.SESSION_START
2625

2726
class FirebaseSessions internal constructor(firebaseApp: FirebaseApp) {
2827
private val sessionGenerator = SessionGenerator(collectEvents = true)
@@ -42,16 +41,7 @@ class FirebaseSessions internal constructor(firebaseApp: FirebaseApp) {
4241

4342
private fun initiateSessionStart() {
4443
val sessionState = sessionGenerator.generateNewSession()
45-
val sessionEvent =
46-
SessionEvent(
47-
eventType = SESSION_START,
48-
sessionData =
49-
SessionInfo(
50-
sessionState.sessionId,
51-
sessionState.firstSessionId,
52-
sessionState.sessionIndex
53-
),
54-
)
44+
val sessionEvent = SessionEvent.sessionStart(sessionState)
5545

5646
Log.i(TAG, "Initiate session start: $sessionEvent")
5747
}

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionEvent.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,20 @@ internal data class SessionEvent(
2929

3030
/** Information about the session triggering the event. */
3131
val sessionData: SessionInfo,
32-
)
32+
) {
33+
companion object {
34+
fun sessionStart(sessionState: SessionState) =
35+
SessionEvent(
36+
eventType = EventType.SESSION_START,
37+
sessionData =
38+
SessionInfo(
39+
sessionState.sessionId,
40+
sessionState.firstSessionId,
41+
sessionState.sessionIndex,
42+
),
43+
)
44+
}
45+
}
3346

3447
/** Enum denoting all possible session event types. */
3548
internal enum class EventType(val number: Int) {
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.sessions
18+
19+
import com.google.common.truth.Truth.assertThat
20+
import org.junit.Test
21+
22+
class SessionEventTest {
23+
@Test
24+
fun sessionStart_populatesSessionStateCorrectly() {
25+
val sessionState =
26+
SessionState(
27+
sessionId = "a1b2c3",
28+
firstSessionId = "a1a1a1",
29+
collectEvents = true,
30+
sessionIndex = 3,
31+
)
32+
33+
val sessionEvent = SessionEvent.sessionStart(sessionState)
34+
35+
assertThat(sessionEvent)
36+
.isEqualTo(
37+
SessionEvent(
38+
eventType = EventType.SESSION_START,
39+
sessionData =
40+
SessionInfo(
41+
sessionId = "a1b2c3",
42+
firstSessionId = "a1a1a1",
43+
sessionIndex = 3,
44+
)
45+
)
46+
)
47+
}
48+
}

0 commit comments

Comments
 (0)