Skip to content

Commit dec0c71

Browse files
committed
Add tests
1 parent df0463b commit dec0c71

File tree

2 files changed

+108
-1
lines changed

2 files changed

+108
-1
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ internal class SessionGenerator(collectEvents: Boolean) {
4747
// Generates a new Session ID. If there was already a generated Session ID
4848
// from the last session during the app's lifecycle, it will also set the last Session ID
4949
fun generateNewSession() {
50-
// val newSessionId = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
5150
val newSessionId = UUID.randomUUID().toString().replace("-", "").lowercase()
5251

5352
// If firstSessionId is set, use it. Otherwise set it to the
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 kotlin.time.Duration
21+
import kotlin.time.Duration.Companion.minutes
22+
import org.junit.Test
23+
24+
class SessionGeneratorTest {
25+
fun isValidSessionId(sessionId: String): Boolean {
26+
if (sessionId.length != 32) {
27+
// assertionFailure("Session ID isn't 32 characters long")
28+
return false
29+
}
30+
if (sessionId.contains("-")) {
31+
// assertionFailure("Session ID contains a dash")
32+
return false
33+
}
34+
if (sessionId.lowercase() != sessionId) {
35+
// assertionFailure("Session ID is not lowercase")
36+
return false
37+
}
38+
return true
39+
}
40+
41+
// This test case isn't important behavior. Nothing should access
42+
// currentSession before generateNewSession has been called. This test just
43+
// ensures it has consistent behavior.
44+
@Test
45+
fun currentSession_beforeGenerateReturnsDefault() {
46+
val sessionGenerator = SessionGenerator(false)
47+
48+
assertThat(sessionGenerator.currentSession.sessionId).isEqualTo("")
49+
assertThat(sessionGenerator.currentSession.firstSessionId).isEqualTo("")
50+
assertThat(sessionGenerator.currentSession.shouldDispatchEvents).isEqualTo(false)
51+
assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(-1)
52+
}
53+
54+
@Test
55+
fun generateNewSessionID_generatesValidSessionInfo() {
56+
val sessionGenerator = SessionGenerator(true)
57+
58+
sessionGenerator.generateNewSession()
59+
60+
assertThat(isValidSessionId(sessionGenerator.currentSession.sessionId)).isEqualTo(true)
61+
assertThat(isValidSessionId(sessionGenerator.currentSession.firstSessionId)).isEqualTo(true)
62+
assertThat(sessionGenerator.currentSession.firstSessionId).isEqualTo(sessionGenerator.currentSession.sessionId)
63+
assertThat(sessionGenerator.currentSession.shouldDispatchEvents).isEqualTo(true)
64+
assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(0)
65+
}
66+
67+
// Ensures that generating a Session ID multiple times results in the fist
68+
// Session ID being set in the firstSessionId field
69+
@Test
70+
fun generateNewSessionID_incrementsSessionIndex_keepsFirstSessionId() {
71+
val sessionGenerator = SessionGenerator(true)
72+
73+
sessionGenerator.generateNewSession()
74+
75+
val firstSessionInfo = sessionGenerator.currentSession
76+
77+
assertThat(isValidSessionId(firstSessionInfo.sessionId)).isEqualTo(true)
78+
assertThat(isValidSessionId(firstSessionInfo.firstSessionId)).isEqualTo(true)
79+
assertThat(firstSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
80+
assertThat(firstSessionInfo.sessionIndex).isEqualTo(0)
81+
82+
sessionGenerator.generateNewSession()
83+
val secondSessionInfo = sessionGenerator.currentSession
84+
85+
assertThat(isValidSessionId(secondSessionInfo.sessionId)).isEqualTo(true)
86+
assertThat(isValidSessionId(secondSessionInfo.firstSessionId)).isEqualTo(true)
87+
// Ensure the new firstSessionId is equal to the first Session ID from earlier
88+
assertThat(secondSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
89+
// Session Index should increase
90+
assertThat(secondSessionInfo.sessionIndex).isEqualTo(1)
91+
92+
// Do a third round just in case
93+
sessionGenerator.generateNewSession()
94+
val thirdSessionInfo = sessionGenerator.currentSession
95+
96+
assertThat(isValidSessionId(thirdSessionInfo.sessionId)).isEqualTo(true)
97+
assertThat(isValidSessionId(thirdSessionInfo.firstSessionId)).isEqualTo(true)
98+
// Ensure the new firstSessionId is equal to the first Session ID from earlier
99+
assertThat(thirdSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
100+
// Session Index should increase
101+
assertThat(thirdSessionInfo.sessionIndex).isEqualTo(2)
102+
}
103+
104+
companion object {
105+
// private val SMALL_INTERVAL = 29.minutes // not enough time to initiate a new session
106+
// private val LARGE_INTERVAL = 31.minutes // enough to initiate another session
107+
}
108+
}

0 commit comments

Comments
 (0)