17
17
package com.google.firebase.sessions
18
18
19
19
import com.google.common.truth.Truth.assertThat
20
+ import java.util.UUID
20
21
import org.junit.Test
21
22
22
23
class SessionGeneratorTest {
@@ -34,66 +35,119 @@ class SessionGeneratorTest {
34
35
}
35
36
36
37
// This test case isn't important behavior. Nothing should access
37
- // currentSession before generateNewSession has been called. This test just
38
- // ensures it has consistent behavior.
39
- @Test
40
- fun currentSession_beforeGenerateReturnsDefault () {
38
+ // currentSession before generateNewSession has been called.
39
+ @Test(expected = UninitializedPropertyAccessException ::class )
40
+ fun currentSession_beforeGenerate_throwsUninitialized () {
41
41
val sessionGenerator = SessionGenerator (collectEvents = false )
42
42
43
- assertThat(sessionGenerator.currentSession.sessionId).isEqualTo(" " )
44
- assertThat(sessionGenerator.currentSession.firstSessionId).isEqualTo(" " )
45
- assertThat(sessionGenerator.currentSession.collectEvents).isFalse()
46
- assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(- 1 )
43
+ sessionGenerator.currentSession
47
44
}
48
45
49
46
@Test
50
- fun generateNewSessionID_generatesValidSessionDetails () {
51
- val sessionGenerator = SessionGenerator (collectEvents = true )
47
+ fun generateNewSession_generatesValidSessionIds () {
48
+ val sessionGenerator = SessionGenerator (collectEvents = true ) // defaults to UUID::randomUUID
52
49
53
50
sessionGenerator.generateNewSession()
54
51
55
52
assertThat(isValidSessionId(sessionGenerator.currentSession.sessionId)).isTrue()
56
53
assertThat(isValidSessionId(sessionGenerator.currentSession.firstSessionId)).isTrue()
57
- assertThat(sessionGenerator.currentSession.firstSessionId)
58
- .isEqualTo(sessionGenerator.currentSession.sessionId)
59
- assertThat(sessionGenerator.currentSession.collectEvents).isTrue()
60
- assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(0 )
54
+
55
+ // Validate several random session ids.
56
+ repeat(16 ) {
57
+ assertThat(isValidSessionId(sessionGenerator.generateNewSession().sessionId)).isTrue()
58
+ }
61
59
}
62
60
63
- // Ensures that generating a Session ID multiple times results in the fist
64
- // Session ID being set in the firstSessionId field
65
61
@Test
66
- fun generateNewSessionID_incrementsSessionIndex_keepsFirstSessionId () {
67
- val sessionGenerator = SessionGenerator (collectEvents = true )
62
+ fun generateNewSession_generatesValidSessionDetails () {
63
+ val sessionGenerator = SessionGenerator (collectEvents = true , uuidGenerator = UUIDs ()::next )
68
64
69
65
sessionGenerator.generateNewSession()
70
66
71
- val firstSessionDetails = sessionGenerator.currentSession
67
+ assertThat(isValidSessionId(sessionGenerator.currentSession.sessionId)).isTrue()
68
+ assertThat(isValidSessionId(sessionGenerator.currentSession.firstSessionId)).isTrue()
69
+
70
+ assertThat(sessionGenerator.currentSession)
71
+ .isEqualTo(
72
+ SessionDetails (
73
+ sessionId = SESSION_ID_1 ,
74
+ firstSessionId = SESSION_ID_1 ,
75
+ collectEvents = true ,
76
+ sessionIndex = 0 ,
77
+ )
78
+ )
79
+ }
80
+
81
+ // Ensures that generating a Session ID multiple times results in the fist
82
+ // Session ID being set in the firstSessionId field
83
+ @Test
84
+ fun generateNewSession_incrementsSessionIndex_keepsFirstSessionId () {
85
+ val sessionGenerator = SessionGenerator (collectEvents = true , uuidGenerator = UUIDs ()::next)
86
+
87
+ val firstSessionDetails = sessionGenerator.generateNewSession()
72
88
73
89
assertThat(isValidSessionId(firstSessionDetails.sessionId)).isTrue()
74
90
assertThat(isValidSessionId(firstSessionDetails.firstSessionId)).isTrue()
75
- assertThat(firstSessionDetails.firstSessionId).isEqualTo(firstSessionDetails.sessionId)
76
- assertThat(firstSessionDetails.sessionIndex).isEqualTo(0 )
77
91
78
- sessionGenerator.generateNewSession()
79
- val secondSessionDetails = sessionGenerator.currentSession
92
+ assertThat(firstSessionDetails)
93
+ .isEqualTo(
94
+ SessionDetails (
95
+ sessionId = SESSION_ID_1 ,
96
+ firstSessionId = SESSION_ID_1 ,
97
+ collectEvents = true ,
98
+ sessionIndex = 0 ,
99
+ )
100
+ )
101
+
102
+ val secondSessionDetails = sessionGenerator.generateNewSession()
80
103
81
104
assertThat(isValidSessionId(secondSessionDetails.sessionId)).isTrue()
82
105
assertThat(isValidSessionId(secondSessionDetails.firstSessionId)).isTrue()
83
- // Ensure the new firstSessionId is equal to the first Session ID from earlier
84
- assertThat(secondSessionDetails.firstSessionId).isEqualTo(firstSessionDetails.sessionId)
85
- // Session Index should increase
86
- assertThat(secondSessionDetails.sessionIndex).isEqualTo(1 )
106
+
107
+ // Ensure the new firstSessionId is equal to the first sessionId, and sessionIndex increased
108
+ assertThat(secondSessionDetails)
109
+ .isEqualTo(
110
+ SessionDetails (
111
+ sessionId = SESSION_ID_2 ,
112
+ firstSessionId = SESSION_ID_1 ,
113
+ collectEvents = true ,
114
+ sessionIndex = 1 ,
115
+ )
116
+ )
87
117
88
118
// Do a third round just in case
89
- sessionGenerator.generateNewSession()
90
- val thirdSessionDetails = sessionGenerator.currentSession
119
+ val thirdSessionDetails = sessionGenerator.generateNewSession()
91
120
92
121
assertThat(isValidSessionId(thirdSessionDetails.sessionId)).isTrue()
93
122
assertThat(isValidSessionId(thirdSessionDetails.firstSessionId)).isTrue()
94
- // Ensure the new firstSessionId is equal to the first Session ID from earlier
95
- assertThat(thirdSessionDetails.firstSessionId).isEqualTo(firstSessionDetails.sessionId)
96
- // Session Index should increase
97
- assertThat(thirdSessionDetails.sessionIndex).isEqualTo(2 )
123
+
124
+ assertThat(thirdSessionDetails)
125
+ .isEqualTo(
126
+ SessionDetails (
127
+ sessionId = SESSION_ID_3 ,
128
+ firstSessionId = SESSION_ID_1 ,
129
+ collectEvents = true ,
130
+ sessionIndex = 2 ,
131
+ )
132
+ )
133
+ }
134
+
135
+ private class UUIDs (val names : List <String > = listOf(UUID_1 , UUID_2 , UUID_3 )) {
136
+ var index = - 1
137
+
138
+ fun next (): UUID {
139
+ index = (index + 1 ).coerceAtMost(names.size - 1 )
140
+ return UUID .fromString(names[index])
141
+ }
142
+ }
143
+
144
+ @Suppress(" SpellCheckingInspection" ) // UUIDs are not words.
145
+ companion object {
146
+ const val UUID_1 = " 11111111-1111-1111-1111-111111111111"
147
+ const val SESSION_ID_1 = " 11111111111111111111111111111111"
148
+ const val UUID_2 = " 22222222-2222-2222-2222-222222222222"
149
+ const val SESSION_ID_2 = " 22222222222222222222222222222222"
150
+ const val UUID_3 = " CCCCCCCC-CCCC-CCCC-CCCC-CCCCCCCCCCCC"
151
+ const val SESSION_ID_3 = " cccccccccccccccccccccccccccccccc"
98
152
}
99
153
}
0 commit comments