This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
/
PopularRepositoriesSpec.kt
237 lines (184 loc) · 6.84 KB
/
PopularRepositoriesSpec.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.freeletics.rxredux
import com.freeletics.rxredux.businesslogic.pagination.PaginationStateMachine
import io.reactivex.Observable
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert
import timber.log.Timber
import java.util.concurrent.TimeUnit
/**
* Abstraction layer that shows what a user can do on the screen
*/
interface Screen {
/**
* Scroll the list to the item at position
*/
fun scrollToEndOfList()
/**
* Action on the screen: Clicks on the retry button to retry loading the first page
*/
fun retryLoadingFirstPage()
/**
* Launches the screen.
* After having this called, the screen is visible
*/
fun loadFirstPage()
}
/**
* Can record states over time.
*/
interface StateRecorder {
/**
* Observable of recorded States
*/
fun renderedStates(): Observable<PaginationStateMachine.State>
}
/**
* Keep the whole history of all states over time
*/
class StateHistory(private val stateRecorder: StateRecorder) {
/**
* All states that has been captured and asserted in an `on`cl
*/
private var stateHistory: List<PaginationStateMachine.State> = emptyList()
/**
* Waits until the next state is rendered and then retruns a [StateHistorySnapshot]
* or if a timeout happens then a TimeOutException will be thrown
*/
internal fun waitUntilNextRenderedState(): StateHistorySnapshot {
val recordedStates = stateRecorder.renderedStates()
.take(stateHistory.size + 1L)
.toList()
.timeout(1, TimeUnit.MINUTES)
.doOnError { it.printStackTrace() }
.blockingGet()
val history = stateHistory
stateHistory = recordedStates
return StateHistorySnapshot(
actualRecordedStates = recordedStates,
verifiedHistory = history
)
}
/**
* A Snapshot in time
*/
internal data class StateHistorySnapshot(
/**
* The actual full recorded history of states
*/
val actualRecordedStates: List<PaginationStateMachine.State>,
/**
* full history of all states that we have already verified / validated and
* are sure that this list of states is correct
*/
val verifiedHistory: List<PaginationStateMachine.State>
)
}
private data class Given(
private val screen: Screen,
private val stateHistory: StateHistory,
private val composedMessage: String
) {
inner class On(private val composedMessage: String) {
inner class It(private val composedMessage: String) {
internal fun assertStateRendered(expectedState: PaginationStateMachine.State) {
val (recordedStates, verifiedHistory) = stateHistory.waitUntilNextRenderedState()
val expectedStates = verifiedHistory + expectedState
Assert.assertEquals(
composedMessage,
expectedStates,
recordedStates
)
Timber.d("✅ $composedMessage")
}
}
infix fun String.byRendering(expectedState: PaginationStateMachine.State) {
val message = this
val it = It("$composedMessage *IT* $message")
it.assertStateRendered(expectedState)
}
}
fun on(message: String, block: On.() -> Unit) {
val on = On("*GIVEN* $composedMessage *ON* $message")
on.block()
}
}
/**
* A simple holder object for all required configuration
*/
data class ScreenConfig(
val mockWebServer: MockWebServer
)
class PopularRepositoriesSpec(
private val screen: Screen,
private val stateHistory: StateHistory,
private val config: ScreenConfig
) {
private fun given(message: String, block: Given.() -> Unit) {
val given = Given(screen, stateHistory, message)
given.block()
}
fun runTests() {
val server = config.mockWebServer
val connectionErrorMessage = "Failed to connect to /127.0.0.1:$MOCK_WEB_SERVER_PORT"
given("the device is offline") {
server.shutdown()
on("loading first page") {
screen.loadFirstPage()
"shows loading first page" byRendering PaginationStateMachine.State.LoadingFirstPageState
"shows error loading first page" byRendering
PaginationStateMachine.State.ErrorLoadingFirstPageState(
connectionErrorMessage
)
}
}
given("device is online (was offline before)") {
server.enqueue200(FIRST_PAGE)
server.start(MOCK_WEB_SERVER_PORT)
Thread.sleep(5000)
on("user clicks retry loading first page") {
screen.retryLoadingFirstPage()
"shows loading" byRendering PaginationStateMachine.State.LoadingFirstPageState
"shows first page" byRendering PaginationStateMachine.State.ShowContentState(
items = FIRST_PAGE,
page = 1
)
}
server.enqueue200(SECOND_PAGE)
on("scrolling to the end of the first page") {
screen.scrollToEndOfList()
"shows loading next page" byRendering
PaginationStateMachine.State.ShowContentAndLoadNextPageState(
items = FIRST_PAGE,
page = 1
)
"shows next page content" byRendering
PaginationStateMachine.State.ShowContentState(
items = FIRST_PAGE + SECOND_PAGE,
page = 2
)
}
}
given("device is offline again (was online before)") {
server.shutdown()
on("scrolling to end of second page") {
screen.scrollToEndOfList()
"shows loading next page" byRendering
PaginationStateMachine.State.ShowContentAndLoadNextPageState(
items = FIRST_PAGE + SECOND_PAGE,
page = 2
)
"shows error info for few seconds on top of the list of items" byRendering
PaginationStateMachine.State.ShowContentAndLoadNextPageErrorState(
items = FIRST_PAGE + SECOND_PAGE,
page = 2,
errorMessage = connectionErrorMessage
)
"hides error information and shows items only" byRendering
PaginationStateMachine.State.ShowContentState(
items = FIRST_PAGE + SECOND_PAGE,
page = 2
)
}
}
}
}