@@ -6,6 +6,7 @@ import androidx.core.app.ComponentActivity
6
6
import androidx.lifecycle.DefaultLifecycleObserver
7
7
import androidx.lifecycle.Lifecycle
8
8
import androidx.lifecycle.LifecycleRegistry
9
+ import androidx.lifecycle.testing.TestLifecycleOwner
9
10
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
10
11
import com.mapbox.navigation.base.options.NavigationOptions
11
12
import com.mapbox.navigation.core.MapboxNavigation
@@ -60,13 +61,13 @@ class MapboxNavigationAppDelegateTest {
60
61
fun `verify onAttached and onDetached when multiple lifecycles have started` () {
61
62
mapboxNavigationApp.setup { navigationOptions }
62
63
63
- val testLifecycleOwnerA = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
64
- val testLifecycleOwnerB = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
64
+ val testLifecycleOwnerA = TestLifecycleOwner (initialState = Lifecycle . State . CREATED )
65
+ val testLifecycleOwnerB = TestLifecycleOwner (initialState = Lifecycle . State . CREATED )
65
66
mapboxNavigationApp.attach(testLifecycleOwnerA)
66
67
mapboxNavigationApp.attach(testLifecycleOwnerB)
67
68
68
- testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
69
- testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
69
+ testLifecycleOwnerA.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
70
+ testLifecycleOwnerB.handleLifecycleEvent( Lifecycle .Event . ON_DESTROY )
70
71
71
72
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
72
73
mapboxNavigationApp.registerObserver(observer)
@@ -87,10 +88,10 @@ class MapboxNavigationAppDelegateTest {
87
88
mapboxNavigationApp.registerObserver(firstObserver)
88
89
mapboxNavigationApp.registerObserver(secondObserver)
89
90
90
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
91
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
91
92
mapboxNavigationApp.attach(testLifecycleOwner)
92
93
93
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
94
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
94
95
95
96
verify(exactly = 1 ) { firstObserver.onAttached(any()) }
96
97
verify(exactly = 1 ) { secondObserver.onAttached(any()) }
@@ -105,10 +106,10 @@ class MapboxNavigationAppDelegateTest {
105
106
mapboxNavigationApp.registerObserver(firstObserver)
106
107
mapboxNavigationApp.registerObserver(secondObserver)
107
108
108
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
109
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
109
110
mapboxNavigationApp.attach(testLifecycleOwner)
110
111
111
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
112
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
112
113
mapboxNavigationApp.setup { navigationOptions }
113
114
114
115
verify(exactly = 1 ) { firstObserver.onAttached(any()) }
@@ -127,10 +128,10 @@ class MapboxNavigationAppDelegateTest {
127
128
}
128
129
mapboxNavigationApp.registerObserver(observer)
129
130
130
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
131
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
131
132
mapboxNavigationApp.attach(testLifecycleOwner)
132
133
mapboxNavigationApp.setup { navigationOptions }
133
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
134
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
134
135
mapboxNavigationApp.setup { navigationOptions }
135
136
136
137
assertEquals(2 , attachedSlot.size)
@@ -218,13 +219,13 @@ class MapboxNavigationAppDelegateTest {
218
219
fun `verify detaching all LifecycleOwners detaches all observers` () {
219
220
mapboxNavigationApp.setup { navigationOptions }
220
221
221
- val testLifecycleOwnerA = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
222
- val testLifecycleOwnerB = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
222
+ val testLifecycleOwnerA = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
223
+ val testLifecycleOwnerB = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
223
224
mapboxNavigationApp.attach(testLifecycleOwnerA)
224
225
mapboxNavigationApp.attach(testLifecycleOwnerB)
225
226
226
- testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
227
- testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
227
+ testLifecycleOwnerA.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
228
+ testLifecycleOwnerB.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
228
229
229
230
val firstObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
230
231
val secondObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -246,13 +247,13 @@ class MapboxNavigationAppDelegateTest {
246
247
fun `verify disable will call observers onDetached` () {
247
248
mapboxNavigationApp.setup { navigationOptions }
248
249
249
- val testLifecycleOwnerA = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
250
- val testLifecycleOwnerB = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
250
+ val testLifecycleOwnerA = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
251
+ val testLifecycleOwnerB = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
251
252
mapboxNavigationApp.attach(testLifecycleOwnerA)
252
253
mapboxNavigationApp.attach(testLifecycleOwnerB)
253
254
254
- testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
255
- testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
255
+ testLifecycleOwnerA.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
256
+ testLifecycleOwnerB.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
256
257
257
258
val firstObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
258
259
val secondObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -273,9 +274,9 @@ class MapboxNavigationAppDelegateTest {
273
274
fun `verify disable will prevent mapboxNavigation from restarting` () {
274
275
mapboxNavigationApp.setup { navigationOptions }
275
276
276
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
277
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
277
278
mapboxNavigationApp.attach(testLifecycleOwner)
278
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
279
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
279
280
280
281
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
281
282
mapboxNavigationApp.registerObserver(observer)
@@ -292,9 +293,9 @@ class MapboxNavigationAppDelegateTest {
292
293
fun `verify disable will detach and current becomes null` () {
293
294
mapboxNavigationApp.setup { navigationOptions }
294
295
295
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
296
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
296
297
mapboxNavigationApp.attach(testLifecycleOwner)
297
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
298
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
298
299
299
300
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
300
301
mapboxNavigationApp.registerObserver(observer)
@@ -309,13 +310,13 @@ class MapboxNavigationAppDelegateTest {
309
310
fun `verify current is null when all lifecycle owners are destroyed` () {
310
311
mapboxNavigationApp.setup { navigationOptions }
311
312
312
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
313
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
313
314
mapboxNavigationApp.attach(testLifecycleOwner)
314
315
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
315
316
mapboxNavigationApp.registerObserver(observer)
316
317
317
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
318
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . DESTROYED
318
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
319
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_DESTROY )
319
320
320
321
assertNull(mapboxNavigationApp.current())
321
322
}
@@ -324,21 +325,21 @@ class MapboxNavigationAppDelegateTest {
324
325
fun `verify current is set after LifecycleOwner is created` () {
325
326
mapboxNavigationApp.setup { navigationOptions }
326
327
327
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
328
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
328
329
mapboxNavigationApp.attach(testLifecycleOwner)
329
330
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
330
331
mapboxNavigationApp.registerObserver(observer)
331
332
332
333
assertNull(mapboxNavigationApp.current())
333
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . CREATED
334
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_CREATE )
334
335
assertNotNull(mapboxNavigationApp.current())
335
336
}
336
337
337
338
@Test
338
339
fun `verify MapboxNavigationObserver lifecycles are called once` () {
339
340
mapboxNavigationApp.setup { navigationOptions }
340
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
341
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
341
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
342
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
342
343
mapboxNavigationApp.attach(testLifecycleOwner)
343
344
344
345
val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -481,8 +482,8 @@ class MapboxNavigationAppDelegateTest {
481
482
fun `verify isConfigurationChanging is false for setup changes` () {
482
483
mockMultiMapboxNavigation()
483
484
mapboxNavigationApp.setup { navigationOptions }
484
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
485
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
485
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
486
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
486
487
mapboxNavigationApp.attach(testLifecycleOwner)
487
488
488
489
val onAttachedSlot = mutableListOf<Boolean >()
@@ -497,19 +498,20 @@ class MapboxNavigationAppDelegateTest {
497
498
}
498
499
}
499
500
mapboxNavigationApp.registerObserver(observerFirst)
500
- mapboxNavigationApp.setup { mockk { every { accessToken } returns " test_access_token_1" } }
501
- mapboxNavigationApp.setup { mockk { every { accessToken } returns " test_access_token_2" } }
501
+ mapboxNavigationApp.setup { mockk() }
502
+ mapboxNavigationApp.setup { mockk() }
503
+ mapboxNavigationApp.disable()
502
504
503
- assertArrayEquals(onAttachedSlot.toBooleanArray(), booleanArrayOf(false , false , false ))
504
- assertArrayEquals(onDetachedSlot.toBooleanArray(), booleanArrayOf( false , false ))
505
+ assertArrayEquals(booleanArrayOf(false , false , false ), onAttachedSlot.toBooleanArray( ))
506
+ assertArrayEquals(booleanArrayOf( false , false , false ), onDetachedSlot.toBooleanArray( ))
505
507
}
506
508
507
509
@Test
508
510
fun `verify isOptionsChanging is true while the setup changes` () {
509
511
mockMultiMapboxNavigation()
510
512
mapboxNavigationApp.setup { navigationOptions }
511
- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
512
- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
513
+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
514
+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
513
515
mapboxNavigationApp.attach(testLifecycleOwner)
514
516
515
517
val onAttachedSlot = mutableListOf<Boolean >()
@@ -524,13 +526,12 @@ class MapboxNavigationAppDelegateTest {
524
526
}
525
527
}
526
528
mapboxNavigationApp.registerObserver(observerFirst)
527
- mapboxNavigationApp.setup { mockk { every { accessToken } returns " test_access_token_1" } }
528
- mapboxNavigationApp.setup { mockk { every { accessToken } returns " test_access_token_2" } }
529
+ mapboxNavigationApp.setup { mockk() }
530
+ mapboxNavigationApp.setup { mockk() }
531
+ mapboxNavigationApp.disable()
529
532
530
- assertEquals(3 , onAttachedSlot.size)
531
- assertEquals(2 , onDetachedSlot.size)
532
- assertArrayEquals(onAttachedSlot.toBooleanArray(), booleanArrayOf(false , true , true ))
533
- assertArrayEquals(onDetachedSlot.toBooleanArray(), booleanArrayOf(true , true ))
533
+ assertArrayEquals(booleanArrayOf(false , true , true ), onAttachedSlot.toBooleanArray())
534
+ assertArrayEquals(booleanArrayOf(true , true , false ), onDetachedSlot.toBooleanArray())
534
535
}
535
536
536
537
private fun mockActivityLifecycle (): Pair <ComponentActivity , LifecycleRegistry > {
0 commit comments