Skip to content

Commit d8f325b

Browse files
committed
Improve tests
1 parent 427154d commit d8f325b

File tree

4 files changed

+59
-65
lines changed

4 files changed

+59
-65
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Mapbox welcomes participation and contributions from everyone.
77
#### Bug fixes and improvements
88
- Improved precision of congestion visualization and independent leg styling transitions, especially for long routes. [#6476](https://github.com/mapbox/mapbox-navigation-android/pull/6476)
99
- Fixed an issue with congestion visualization on the route line that occurred during route refreshes. [#6476](https://github.com/mapbox/mapbox-navigation-android/pull/6476)
10-
- Add ability to check when `NavigationOptions` are changing with `MapboxNavigationApp.isOptionsChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
11-
- Add ability to check when Lifecycle events are triggered by configuration changes with `MapboxNavigationApp.isConfigurationChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
10+
- Added ability to check when `NavigationOptions` are changing with `MapboxNavigationApp.isOptionsChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
11+
- Added ability to check when Lifecycle events are triggered by configuration changes with `MapboxNavigationApp.isConfigurationChanging`. [#6484](https://github.com/mapbox/mapbox-navigation-android/pull/6484)
1212

1313
## Mapbox Navigation SDK 2.9.0-beta.2 - 14 October, 2022
1414
### Changelog

libnavigation-core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ dependencies {
6262
testImplementation project(':libtesting-utils')
6363
testImplementation project(':libtesting-navigation-base')
6464
testImplementation project(':libtesting-navigation-util')
65+
testImplementation(dependenciesList.androidXLifecycleTesting)
6566
apply from: "${rootDir}/gradle/unit-testing-dependencies.gradle"
6667
testImplementation dependenciesList.commonsIO
6768

libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/CarAppLifecycleOwnerTest.kt

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import androidx.fragment.app.Fragment
88
import androidx.fragment.app.FragmentActivity
99
import androidx.lifecycle.DefaultLifecycleObserver
1010
import androidx.lifecycle.Lifecycle
11-
import androidx.lifecycle.LifecycleOwner
12-
import androidx.lifecycle.LifecycleRegistry
11+
import androidx.lifecycle.testing.TestLifecycleOwner
1312
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
1413
import com.mapbox.navigation.testing.LoggingFrontendTestRule
1514
import io.mockk.every
@@ -405,8 +404,8 @@ class CarAppLifecycleOwnerTest {
405404
val testLifecycleOwner = TestLifecycleOwner()
406405
carAppLifecycleOwner.attach(testLifecycleOwner)
407406

408-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
409-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
407+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
408+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
410409

411410
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
412411
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -422,10 +421,10 @@ class CarAppLifecycleOwnerTest {
422421
carAppLifecycleOwner.attach(testLifecycleOwnerA)
423422
carAppLifecycleOwner.attach(testLifecycleOwnerB)
424423

425-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
426-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
427-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
428-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
424+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
425+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
426+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
427+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
429428

430429
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
431430
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -441,10 +440,10 @@ class CarAppLifecycleOwnerTest {
441440
carAppLifecycleOwner.attach(testLifecycleOwnerA)
442441
carAppLifecycleOwner.attach(testLifecycleOwnerB)
443442

444-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
445-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.STARTED
446-
testLifecycleOwnerA.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
447-
testLifecycleOwnerB.lifecycleRegistry.currentState = Lifecycle.State.DESTROYED
443+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
444+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
445+
testLifecycleOwnerA.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
446+
testLifecycleOwnerB.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
448447

449448
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
450449
verify(exactly = 1) { testLifecycleObserver.onStart(any()) }
@@ -458,7 +457,7 @@ class CarAppLifecycleOwnerTest {
458457
val testLifecycleOwner = TestLifecycleOwner()
459458

460459
carAppLifecycleOwner.attach(testLifecycleOwner)
461-
testLifecycleOwner.lifecycleRegistry.currentState = Lifecycle.State.RESUMED
460+
testLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
462461
carAppLifecycleOwner.detach(testLifecycleOwner)
463462

464463
verify(exactly = 1) { testLifecycleObserver.onCreate(any()) }
@@ -468,13 +467,6 @@ class CarAppLifecycleOwnerTest {
468467
verify(exactly = 0) { testLifecycleObserver.onDestroy(any()) }
469468
}
470469

471-
class TestLifecycleOwner : LifecycleOwner {
472-
val lifecycleRegistry = LifecycleRegistry(this)
473-
.also { it.currentState = Lifecycle.State.INITIALIZED }
474-
475-
override fun getLifecycle(): Lifecycle = lifecycleRegistry
476-
}
477-
478470
private fun mockActivity(isChangingConfig: Boolean = false): FragmentActivity = mockk {
479471
every { isChangingConfigurations } returns isChangingConfig
480472
}

libnavigation-core/src/test/java/com/mapbox/navigation/core/lifecycle/MapboxNavigationAppDelegateTest.kt

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.core.app.ComponentActivity
66
import androidx.lifecycle.DefaultLifecycleObserver
77
import androidx.lifecycle.Lifecycle
88
import androidx.lifecycle.LifecycleRegistry
9+
import androidx.lifecycle.testing.TestLifecycleOwner
910
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
1011
import com.mapbox.navigation.base.options.NavigationOptions
1112
import 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

Comments
 (0)