@@ -6,6 +6,7 @@ import androidx.core.app.ComponentActivity
66import androidx.lifecycle.DefaultLifecycleObserver
77import androidx.lifecycle.Lifecycle
88import androidx.lifecycle.LifecycleRegistry
9+ import androidx.lifecycle.testing.TestLifecycleOwner
910import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
1011import com.mapbox.navigation.base.options.NavigationOptions
1112import com.mapbox.navigation.core.MapboxNavigation
@@ -60,13 +61,13 @@ class MapboxNavigationAppDelegateTest {
6061 fun `verify onAttached and onDetached when multiple lifecycles have started` () {
6162 mapboxNavigationApp.setup { navigationOptions }
6263
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 )
6566 mapboxNavigationApp.attach(testLifecycleOwnerA)
6667 mapboxNavigationApp.attach(testLifecycleOwnerB)
6768
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 )
7071
7172 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
7273 mapboxNavigationApp.registerObserver(observer)
@@ -87,10 +88,10 @@ class MapboxNavigationAppDelegateTest {
8788 mapboxNavigationApp.registerObserver(firstObserver)
8889 mapboxNavigationApp.registerObserver(secondObserver)
8990
90- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
91+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
9192 mapboxNavigationApp.attach(testLifecycleOwner)
9293
93- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
94+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
9495
9596 verify(exactly = 1 ) { firstObserver.onAttached(any()) }
9697 verify(exactly = 1 ) { secondObserver.onAttached(any()) }
@@ -105,10 +106,10 @@ class MapboxNavigationAppDelegateTest {
105106 mapboxNavigationApp.registerObserver(firstObserver)
106107 mapboxNavigationApp.registerObserver(secondObserver)
107108
108- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
109+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
109110 mapboxNavigationApp.attach(testLifecycleOwner)
110111
111- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
112+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
112113 mapboxNavigationApp.setup { navigationOptions }
113114
114115 verify(exactly = 1 ) { firstObserver.onAttached(any()) }
@@ -127,10 +128,10 @@ class MapboxNavigationAppDelegateTest {
127128 }
128129 mapboxNavigationApp.registerObserver(observer)
129130
130- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
131+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
131132 mapboxNavigationApp.attach(testLifecycleOwner)
132133 mapboxNavigationApp.setup { navigationOptions }
133- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
134+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
134135 mapboxNavigationApp.setup { navigationOptions }
135136
136137 assertEquals(2 , attachedSlot.size)
@@ -218,13 +219,13 @@ class MapboxNavigationAppDelegateTest {
218219 fun `verify detaching all LifecycleOwners detaches all observers` () {
219220 mapboxNavigationApp.setup { navigationOptions }
220221
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 )
223224 mapboxNavigationApp.attach(testLifecycleOwnerA)
224225 mapboxNavigationApp.attach(testLifecycleOwnerB)
225226
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 )
228229
229230 val firstObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
230231 val secondObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -246,13 +247,13 @@ class MapboxNavigationAppDelegateTest {
246247 fun `verify disable will call observers onDetached` () {
247248 mapboxNavigationApp.setup { navigationOptions }
248249
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 )
251252 mapboxNavigationApp.attach(testLifecycleOwnerA)
252253 mapboxNavigationApp.attach(testLifecycleOwnerB)
253254
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 )
256257
257258 val firstObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
258259 val secondObserver = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -273,9 +274,9 @@ class MapboxNavigationAppDelegateTest {
273274 fun `verify disable will prevent mapboxNavigation from restarting` () {
274275 mapboxNavigationApp.setup { navigationOptions }
275276
276- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
277+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
277278 mapboxNavigationApp.attach(testLifecycleOwner)
278- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
279+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
279280
280281 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
281282 mapboxNavigationApp.registerObserver(observer)
@@ -292,9 +293,9 @@ class MapboxNavigationAppDelegateTest {
292293 fun `verify disable will detach and current becomes null` () {
293294 mapboxNavigationApp.setup { navigationOptions }
294295
295- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
296+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
296297 mapboxNavigationApp.attach(testLifecycleOwner)
297- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . RESUMED
298+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_RESUME )
298299
299300 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
300301 mapboxNavigationApp.registerObserver(observer)
@@ -309,13 +310,13 @@ class MapboxNavigationAppDelegateTest {
309310 fun `verify current is null when all lifecycle owners are destroyed` () {
310311 mapboxNavigationApp.setup { navigationOptions }
311312
312- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
313+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
313314 mapboxNavigationApp.attach(testLifecycleOwner)
314315 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
315316 mapboxNavigationApp.registerObserver(observer)
316317
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 )
319320
320321 assertNull(mapboxNavigationApp.current())
321322 }
@@ -324,21 +325,21 @@ class MapboxNavigationAppDelegateTest {
324325 fun `verify current is set after LifecycleOwner is created` () {
325326 mapboxNavigationApp.setup { navigationOptions }
326327
327- val testLifecycleOwner = CarAppLifecycleOwnerTest . TestLifecycleOwner ()
328+ val testLifecycleOwner = TestLifecycleOwner (initialState = Lifecycle . State . INITIALIZED )
328329 mapboxNavigationApp.attach(testLifecycleOwner)
329330 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
330331 mapboxNavigationApp.registerObserver(observer)
331332
332333 assertNull(mapboxNavigationApp.current())
333- testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle .State . CREATED
334+ testLifecycleOwner.handleLifecycleEvent( Lifecycle .Event . ON_CREATE )
334335 assertNotNull(mapboxNavigationApp.current())
335336 }
336337
337338 @Test
338339 fun `verify MapboxNavigationObserver lifecycles are called once` () {
339340 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 )
342343 mapboxNavigationApp.attach(testLifecycleOwner)
343344
344345 val observer = mockk<MapboxNavigationObserver >(relaxUnitFun = true )
@@ -481,8 +482,8 @@ class MapboxNavigationAppDelegateTest {
481482 fun `verify isConfigurationChanging is false for setup changes` () {
482483 mockMultiMapboxNavigation()
483484 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 )
486487 mapboxNavigationApp.attach(testLifecycleOwner)
487488
488489 val onAttachedSlot = mutableListOf<Boolean >()
@@ -497,19 +498,20 @@ class MapboxNavigationAppDelegateTest {
497498 }
498499 }
499500 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()
502504
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( ))
505507 }
506508
507509 @Test
508510 fun `verify isOptionsChanging is true while the setup changes` () {
509511 mockMultiMapboxNavigation()
510512 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 )
513515 mapboxNavigationApp.attach(testLifecycleOwner)
514516
515517 val onAttachedSlot = mutableListOf<Boolean >()
@@ -524,13 +526,12 @@ class MapboxNavigationAppDelegateTest {
524526 }
525527 }
526528 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()
529532
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())
534535 }
535536
536537 private fun mockActivityLifecycle (): Pair <ComponentActivity , LifecycleRegistry > {
0 commit comments