1
+ package org.rekotlinrouter
2
+
3
+
4
+ import org.assertj.core.api.Assertions.assertThat
5
+ import org.junit.Test
6
+ // import org.junit.jupiter.api.DisplayName
7
+ // import org.junit.jupiter.api.Test
8
+ import tw.geothings.rekotlin.StateType
9
+
10
+
11
+ /* *
12
+ * Created by mkaratadipalayam on 27/09/17.
13
+ */
14
+
15
+ internal class AppState : StateType
16
+
17
+ internal class ReKotlinRouterUnitTests {
18
+
19
+ val mainActivityIdentifier = " MainActivity"
20
+ val counterActivityIdentifier = " CounterActivity"
21
+ val statsActivityIdentifier = " StatsActivity"
22
+ val infoActivityIdentifier = " InfoActivity"
23
+
24
+
25
+ @Test
26
+ // @DisplayName("calculates transitions from an empty route to a multi segment route")
27
+ fun test_transition_from_empty_to_multi_segment_route (){
28
+
29
+ // Given
30
+ val oldRoute: Route = arrayListOf ()
31
+ val newRoute = arrayListOf (mainActivityIdentifier, statsActivityIdentifier)
32
+
33
+ // When
34
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
35
+
36
+ // Then
37
+ var action1Correct: Boolean = false
38
+ var action2Correct: Boolean = false
39
+
40
+ routingActions.forEach { routingAction ->
41
+ when (routingAction) {
42
+ is push -> {
43
+ if (routingAction.responsibleRoutableIndex== 0 && routingAction.segmentToBePushed == mainActivityIdentifier ){
44
+ action1Correct = true
45
+ }
46
+ if (routingAction.responsibleRoutableIndex== 1 && routingAction.segmentToBePushed == statsActivityIdentifier ){
47
+ action2Correct = true
48
+ }
49
+ }
50
+ }
51
+ }
52
+ assertThat(action1Correct).isTrue()
53
+ assertThat(action2Correct).isTrue()
54
+ assertThat(routingActions.count()).isEqualTo(2 )
55
+ }
56
+
57
+
58
+ @Test
59
+ // @DisplayName("generates a Change action on the last common subroute")
60
+ fun test_change_action_on_last_common_subroute (){
61
+
62
+ // Given
63
+ val oldRoute = arrayListOf (mainActivityIdentifier,counterActivityIdentifier)
64
+ val newRoute = arrayListOf (mainActivityIdentifier,statsActivityIdentifier)
65
+
66
+ // When
67
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
68
+
69
+ // Then
70
+ var controllerIndex: Int = - 1
71
+ var toBeReplaced: RouteElementIdentifier = " "
72
+ var new: RouteElementIdentifier = " "
73
+ routingActions.forEach { routingAction ->
74
+
75
+ when (routingAction) {
76
+ is change -> {
77
+ controllerIndex = routingAction.responsibleRoutableIndex
78
+ toBeReplaced = routingAction.segmentToBeReplaced
79
+ new = routingAction.newSegment
80
+ }
81
+ }
82
+ }
83
+ assertThat(controllerIndex).isEqualTo(1 )
84
+ assertThat(toBeReplaced).isEqualTo(counterActivityIdentifier)
85
+ assertThat(new).isEqualTo(statsActivityIdentifier)
86
+ }
87
+
88
+ @Test
89
+ // @DisplayName("generates a Change action on the last common subroute, also for routes of different length")
90
+ fun test_change_action_on_last_common_subroute_plus_routes_of_different_length (){
91
+
92
+ // Given
93
+ val oldRoute = arrayListOf (mainActivityIdentifier,counterActivityIdentifier)
94
+ val newRoute = arrayListOf (mainActivityIdentifier,statsActivityIdentifier,infoActivityIdentifier)
95
+
96
+ // When
97
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
98
+
99
+ // Then
100
+ var action1Correct: Boolean = false
101
+ var action2Correct: Boolean = false
102
+
103
+ routingActions.forEach { routingAction ->
104
+ when (routingAction) {
105
+ is change -> {
106
+ if (routingAction.responsibleRoutableIndex== 1
107
+ && routingAction.segmentToBeReplaced == counterActivityIdentifier
108
+ && routingAction.newSegment == statsActivityIdentifier){
109
+ action1Correct = true
110
+ }
111
+ }
112
+ is push -> {
113
+ if (routingAction.responsibleRoutableIndex== 2 && routingAction.segmentToBePushed == infoActivityIdentifier ){
114
+ action2Correct = true
115
+ }
116
+
117
+ }
118
+
119
+ }
120
+ }
121
+
122
+ assertThat(action1Correct).isTrue()
123
+ assertThat(action2Correct).isTrue()
124
+ assertThat(routingActions.count()).isEqualTo(2 )
125
+ }
126
+
127
+ @Test
128
+ // @DisplayName("generates a Change action on root when root element changes")
129
+ fun test_change_action_on_root_when_root_element_changes (){
130
+
131
+ // Given
132
+ val oldRoute = arrayListOf (mainActivityIdentifier)
133
+ val newRoute = arrayListOf (statsActivityIdentifier)
134
+
135
+ // When
136
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
137
+
138
+ // Then
139
+ var controllerIndex: Int = - 1
140
+ var toBeReplaced: RouteElementIdentifier = " "
141
+ var new: RouteElementIdentifier = " "
142
+ routingActions.forEach { routingAction ->
143
+
144
+ when (routingAction) {
145
+ is change -> {
146
+ controllerIndex = routingAction.responsibleRoutableIndex
147
+ toBeReplaced = routingAction.segmentToBeReplaced
148
+ new = routingAction.newSegment
149
+ }
150
+ }
151
+ }
152
+ assertThat(controllerIndex).isEqualTo(0 )
153
+ assertThat(routingActions.count()).isEqualTo(1 )
154
+ assertThat(toBeReplaced).isEqualTo(mainActivityIdentifier)
155
+ assertThat(new).isEqualTo(statsActivityIdentifier)
156
+ }
157
+
158
+ @Test
159
+ // @DisplayName("calculates no actions for transition from empty route to empty route")
160
+ fun test_no_action_when_transistion_from_empty_to_empty_route (){
161
+
162
+ // Given
163
+ // val oldRoute: Route = emptyArray()
164
+ // val newRoute: Route = emptyArray()
165
+
166
+ val oldRoute: Route = arrayListOf ()
167
+ val newRoute: Route = arrayListOf ()
168
+ // When
169
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
170
+
171
+ // Then
172
+ assertThat(routingActions.count()).isEqualTo(0 )
173
+ }
174
+
175
+ @Test
176
+ // @DisplayName("calculates no actions for transitions between identical, non-empty routes")
177
+ fun test_no_action_when_transistion_from_identical_non_empty_routes (){
178
+
179
+ // Given
180
+ val oldRoute = arrayListOf (mainActivityIdentifier,counterActivityIdentifier)
181
+ val newRoute = arrayListOf (mainActivityIdentifier,counterActivityIdentifier)
182
+
183
+ // When
184
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
185
+
186
+ // Then
187
+ assertThat(routingActions.count()).isEqualTo(0 )
188
+ }
189
+
190
+ @Test
191
+ // @DisplayName("calculates transitions with multiple pops")
192
+ fun test_transistion_with_multiple_pops (){
193
+
194
+ // Given
195
+ val oldRoute = arrayListOf (mainActivityIdentifier,statsActivityIdentifier,counterActivityIdentifier)
196
+ val newRoute = arrayListOf (mainActivityIdentifier)
197
+
198
+ // When
199
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
200
+
201
+ // Then
202
+ var action1Correct: Boolean = false
203
+ var action2Correct: Boolean = false
204
+ routingActions.forEach { routingAction ->
205
+ when (routingAction) {
206
+ is pop -> {
207
+ if (routingAction.responsibleRoutableIndex== 2 && routingAction.segmentToBePopped == counterActivityIdentifier ){
208
+ action1Correct = true
209
+ }
210
+ if (routingAction.responsibleRoutableIndex== 1 && routingAction.segmentToBePopped == statsActivityIdentifier ){
211
+ action2Correct = true
212
+ }
213
+ }
214
+ }
215
+ }
216
+ assertThat(action1Correct).isTrue()
217
+ assertThat(action2Correct).isTrue()
218
+ assertThat(routingActions.count()).isEqualTo(2 )
219
+ }
220
+
221
+ @Test
222
+ // @DisplayName("calculates transitions with multiple pushes")
223
+ fun test_transistions_with_multiple_pushes (){
224
+
225
+ // Given
226
+ val oldRoute = arrayListOf (mainActivityIdentifier)
227
+ val newRoute = arrayListOf (mainActivityIdentifier,statsActivityIdentifier,counterActivityIdentifier)
228
+
229
+ // When
230
+ val routingActions = Router .routingActionsForTransitionFrom(oldRoute, newRoute)
231
+
232
+ // Then
233
+ var action1Correct: Boolean = false
234
+ var action2Correct: Boolean = false
235
+ routingActions.forEach { routingAction ->
236
+ when (routingAction) {
237
+ is push -> {
238
+ if (routingAction.responsibleRoutableIndex== 1 && routingAction.segmentToBePushed == statsActivityIdentifier ){
239
+ action1Correct = true
240
+ }
241
+ if (routingAction.responsibleRoutableIndex== 2 && routingAction.segmentToBePushed == counterActivityIdentifier ){
242
+ action2Correct = true
243
+ }
244
+
245
+ }
246
+ }
247
+ }
248
+ assertThat(action1Correct).isTrue()
249
+ assertThat(action2Correct).isTrue()
250
+ assertThat(routingActions.count()).isEqualTo(2 )
251
+ }
252
+ }
0 commit comments