@@ -25,13 +25,12 @@ import com.mapbox.navigation.copilot.internal.PushStatus
25
25
import com.mapbox.navigation.copilot.internal.PushStatusObserver
26
26
import com.mapbox.navigation.copilot.work.HistoryUploadWorker
27
27
import com.mapbox.navigation.copilot.work.HistoryUploadWorker.Companion.putCopilotSession
28
- import com.mapbox.navigation.utils.internal.logD
28
+ import com.mapbox.navigation.testing.LoggingFrontendTestRule
29
29
import io.mockk.CapturingSlot
30
30
import io.mockk.coEvery
31
31
import io.mockk.every
32
32
import io.mockk.mockk
33
33
import io.mockk.mockkObject
34
- import io.mockk.mockkStatic
35
34
import io.mockk.slot
36
35
import io.mockk.unmockkAll
37
36
import io.mockk.verify
@@ -41,6 +40,7 @@ import org.junit.Assert.assertEquals
41
40
import org.junit.Assert.assertNotNull
42
41
import org.junit.Assert.assertTrue
43
42
import org.junit.Before
43
+ import org.junit.Rule
44
44
import org.junit.Test
45
45
import java.io.File
46
46
@@ -49,6 +49,9 @@ import java.io.File
49
49
@OptIn(ExperimentalPreviewMapboxNavigationAPI ::class )
50
50
class HistoryUploadWorkerTest {
51
51
52
+ @get:Rule
53
+ val loggerRule = LoggingFrontendTestRule ()
54
+
52
55
private lateinit var mockedContext: Context
53
56
private lateinit var mockedUploadServiceInterface: HttpServiceInterface
54
57
private val mockedPushStatusObserver = mockk<PushStatusObserver >(relaxUnitFun = true )
@@ -74,12 +77,6 @@ class HistoryUploadWorkerTest {
74
77
75
78
@Before
76
79
fun setup () {
77
- mockkStatic(" com.mapbox.navigation.utils.internal.LoggerProviderKt" )
78
- every { logD(msg = any(), category = any()) } answers {
79
- // // uncomment to verify log printout
80
- // println(secondArg<String?>() + " " + firstArg())
81
- }
82
-
83
80
// Mock File operations
84
81
mockkObject(HistoryAttachmentsUtils )
85
82
coEvery { rename(any(), any()) } coAnswers {
@@ -191,25 +188,6 @@ class HistoryUploadWorkerTest {
191
188
)
192
189
}
193
190
194
- @Test
195
- fun `remove history file not called - FAILED` () = runBlocking {
196
- givenUploadServiceAnswer(
197
- uploadStatus(
198
- state = TransferState .FAILED ,
199
- error = mockk<TransferError >(relaxed = true ) {
200
- every { code } returns mockk<TransferErrorCode >()
201
- },
202
- ),
203
- )
204
-
205
- HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession))
206
- .doWork()
207
-
208
- verify(exactly = 0 ) {
209
- delete(any())
210
- }
211
- }
212
-
213
191
@Test
214
192
fun `Result retry - FAILED` () = runBlocking {
215
193
givenUploadServiceAnswer(
@@ -280,7 +258,55 @@ class HistoryUploadWorkerTest {
280
258
}
281
259
282
260
@Test
283
- fun `remove history file - runAttemptCount greater or equal than MAX_RUN_ATTEMPT_COUNT` () =
261
+ fun `remove history file not called - FAILED, runAttemptCount = 1` () = runBlocking {
262
+ givenUploadServiceAnswer(
263
+ uploadStatus(
264
+ state = TransferState .FAILED ,
265
+ error = mockk<TransferError >(relaxed = true ) {
266
+ every { code } returns mockk<TransferErrorCode >()
267
+ },
268
+ ),
269
+ )
270
+
271
+ HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, runAttemptCount = 1 ))
272
+ .doWork()
273
+
274
+ verify(exactly = 0 ) {
275
+ delete(any())
276
+ }
277
+ }
278
+
279
+ @Test
280
+ fun `remove history file - FAILED, runAttemptCount = MAX_RUN_ATTEMPT_COUNT` () =
281
+ runBlocking {
282
+ givenUploadServiceAnswer(
283
+ uploadStatus(
284
+ state = TransferState .FAILED ,
285
+ error = mockk<TransferError >(relaxed = true ) {
286
+ every { code } returns mockk<TransferErrorCode >()
287
+ },
288
+ ),
289
+ )
290
+
291
+ HistoryUploadWorker (
292
+ mockedContext,
293
+ workerParams(
294
+ stubCopilotSession,
295
+ runAttemptCount = HistoryUploadWorker .MAX_RUN_ATTEMPT_COUNT ,
296
+ ),
297
+ ).doWork()
298
+
299
+ val deletedFiles = mutableListOf<File >()
300
+ verify(exactly = 2 ) {
301
+ delete(capture(deletedFiles))
302
+ }
303
+ assertEquals(2 , deletedFiles.size)
304
+ assertNotNull(deletedFiles.first { it.name.endsWith(" pbf.gz" ) })
305
+ assertNotNull(deletedFiles.first { it.name.endsWith(" metadata.json" ) })
306
+ }
307
+
308
+ @Test
309
+ fun `remove history file - FINISHED 204` () =
284
310
runBlocking {
285
311
givenUploadServiceAnswer(
286
312
uploadStatus(
@@ -290,7 +316,7 @@ class HistoryUploadWorkerTest {
290
316
),
291
317
)
292
318
293
- HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, 8 ))
319
+ HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession))
294
320
.doWork()
295
321
296
322
val deletedFiles = mutableListOf<File >()
@@ -314,8 +340,10 @@ class HistoryUploadWorkerTest {
314
340
),
315
341
)
316
342
317
- val result = HistoryUploadWorker (mockedContext, workerParams(stubCopilotSession, 8 ))
318
- .doWork()
343
+ val result = HistoryUploadWorker (
344
+ mockedContext,
345
+ workerParams(stubCopilotSession, HistoryUploadWorker .MAX_RUN_ATTEMPT_COUNT ),
346
+ ).doWork()
319
347
320
348
assertTrue(result is ListenableWorker .Result .Failure )
321
349
}
0 commit comments