|
1 | 1 | package com.mapbox.navigation.ui.app.internal.controller |
2 | 2 |
|
3 | 3 | import android.location.Location |
4 | | -import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI |
| 4 | +import android.location.LocationManager |
5 | 5 | import com.mapbox.navigation.core.MapboxNavigation |
6 | | -import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp |
| 6 | +import com.mapbox.navigation.core.trip.session.LocationMatcherResult |
7 | 7 | import com.mapbox.navigation.core.trip.session.LocationObserver |
8 | 8 | import com.mapbox.navigation.testing.MainCoroutineRule |
9 | 9 | import com.mapbox.navigation.ui.app.internal.location.LocationAction |
10 | 10 | import com.mapbox.navigation.ui.app.testing.TestStore |
11 | | -import com.mapbox.navigation.ui.app.testing.TestingUtil.makeLocationMatcherResult |
12 | | -import io.mockk.Runs |
13 | 11 | import io.mockk.every |
14 | | -import io.mockk.just |
15 | 12 | import io.mockk.mockk |
16 | | -import io.mockk.mockkObject |
17 | 13 | import io.mockk.slot |
18 | 14 | import io.mockk.spyk |
19 | | -import io.mockk.unmockkAll |
20 | 15 | import io.mockk.verify |
21 | | -import io.mockk.verifyOrder |
22 | 16 | import kotlinx.coroutines.ExperimentalCoroutinesApi |
23 | 17 | import kotlinx.coroutines.test.runBlockingTest |
24 | | -import org.junit.After |
25 | 18 | import org.junit.Assert.assertEquals |
26 | 19 | import org.junit.Before |
27 | 20 | import org.junit.Rule |
28 | 21 | import org.junit.Test |
29 | 22 |
|
30 | | -@OptIn(ExperimentalPreviewMapboxNavigationAPI::class, ExperimentalCoroutinesApi::class) |
| 23 | +@OptIn(ExperimentalCoroutinesApi::class) |
31 | 24 | class LocationStateControllerTest { |
32 | 25 |
|
33 | 26 | @get:Rule |
34 | 27 | var coroutineRule = MainCoroutineRule() |
35 | 28 |
|
36 | | - private val locationObserverSlot = slot<LocationObserver>() |
37 | | - private val store = spyk(TestStore()) |
38 | | - private val sut = LocationStateController(store) |
39 | | - |
40 | | - private fun mockMapboxNavigation(): MapboxNavigation { |
41 | | - val mapboxNavigation = mockk<MapboxNavigation>(relaxed = true) { |
42 | | - every { registerLocationObserver(capture(locationObserverSlot)) } just Runs |
43 | | - } |
44 | | - every { MapboxNavigationApp.current() } returns mapboxNavigation |
45 | | - return mapboxNavigation |
46 | | - } |
| 29 | + private lateinit var store: TestStore |
| 30 | + private lateinit var sut: LocationStateController |
| 31 | + private lateinit var mapboxNavigation: MapboxNavigation |
47 | 32 |
|
48 | 33 | @Before |
49 | | - fun setup() { |
50 | | - mockkObject(MapboxNavigationApp) |
51 | | - } |
52 | | - |
53 | | - @After |
54 | | - fun teardown() { |
55 | | - unmockkAll() |
| 34 | + fun setUp() { |
| 35 | + mapboxNavigation = mockk(relaxed = true) |
| 36 | + store = spyk(TestStore()) |
| 37 | + sut = LocationStateController(store) |
56 | 38 | } |
57 | 39 |
|
58 | 40 | @Test |
59 | | - fun `onAttached will registerLocationObserver onNewLocationMatcherResult updates state`() = |
| 41 | + fun `onAttached should subscribe as LocationObserver and dispatch location Update action`() = |
60 | 42 | runBlockingTest { |
61 | | - sut.onAttached(mockMapboxNavigation()) |
62 | | - val newLocationUpdate = makeLocationMatcherResult(1.0, 2.0, 0f) |
63 | | - locationObserverSlot.captured.onNewLocationMatcherResult(newLocationUpdate) |
| 43 | + val locationMatcherResult = locationMatcherResult(location(1.0, 2.0)) |
| 44 | + givenLocationUpdate(locationMatcherResult) |
64 | 45 |
|
65 | | - verify { |
66 | | - store.dispatch(LocationAction.Update(newLocationUpdate)) |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - @Test |
71 | | - fun `onAttached registerLocationObserver onNewLocationMatcherResult updates navigationLocationProvider`() = |
72 | | - runBlockingTest { |
73 | | - val mockLocation: Location = mockk { |
74 | | - every { longitude } returns -109.587335 |
75 | | - every { latitude } returns 38.731370 |
76 | | - } |
| 46 | + sut.onAttached(mapboxNavigation) |
77 | 47 |
|
78 | | - sut.onAttached(mockMapboxNavigation()) |
79 | | - locationObserverSlot.captured.onNewLocationMatcherResult( |
80 | | - mockk(relaxed = true) { |
81 | | - every { enhancedLocation } returns mockLocation |
82 | | - every { keyPoints } returns listOf(mockLocation) |
83 | | - } |
84 | | - ) |
85 | | - |
86 | | - with(sut.navigationLocationProvider.lastLocation!!) { |
87 | | - assertEquals(-109.587335, longitude, 0.00001) |
88 | | - assertEquals(38.731370, latitude, 0.00001) |
89 | | - } |
| 48 | + verify { store.dispatch(LocationAction.Update(locationMatcherResult)) } |
90 | 49 | } |
91 | 50 |
|
92 | 51 | @Test |
93 | | - fun `onAttached registerLocationObserver onNewLocationMatcherResult updates lastPoint`() = |
94 | | - runBlockingTest { |
95 | | - val mockLocation: Location = mockk { |
96 | | - every { longitude } returns -109.587335 |
97 | | - every { latitude } returns 38.731370 |
98 | | - } |
| 52 | + fun `process should process LocationAction and update store state`() { |
| 53 | + val action = LocationAction.Update( |
| 54 | + locationMatcherResult(location(2.0, 3.0)) |
| 55 | + ) |
99 | 56 |
|
100 | | - sut.onAttached(mockMapboxNavigation()) |
101 | | - locationObserverSlot.captured.onNewLocationMatcherResult( |
102 | | - mockk(relaxed = true) { |
103 | | - every { enhancedLocation } returns mockLocation |
104 | | - every { keyPoints } returns listOf(mockLocation) |
105 | | - } |
106 | | - ) |
| 57 | + val state = sut.process(store.state.value, action) |
| 58 | + |
| 59 | + assertEquals(action.result, state.location) |
| 60 | + } |
107 | 61 |
|
108 | | - with(sut.lastPoint!!) { |
109 | | - assertEquals(-109.587335, longitude(), 0.00001) |
110 | | - assertEquals(38.731370, latitude(), 0.00001) |
111 | | - } |
| 62 | + private fun givenLocationUpdate(result: LocationMatcherResult) { |
| 63 | + val locObserver = slot<LocationObserver>() |
| 64 | + every { |
| 65 | + mapboxNavigation.registerLocationObserver(capture(locObserver)) |
| 66 | + } answers { |
| 67 | + locObserver.captured.onNewRawLocation(result.enhancedLocation) |
| 68 | + locObserver.captured.onNewLocationMatcherResult(result) |
112 | 69 | } |
| 70 | + } |
113 | 71 |
|
114 | | - @Test |
115 | | - fun `onDetached will unregisterLocationObserver`() = runBlockingTest { |
116 | | - val mockMapboxNavigation = mockMapboxNavigation() |
117 | | - sut.onAttached(mockMapboxNavigation) |
118 | | - sut.onDetached(mockMapboxNavigation) |
| 72 | + private fun locationMatcherResult(location: Location) = mockk<LocationMatcherResult> { |
| 73 | + every { enhancedLocation } returns location |
| 74 | + every { keyPoints } returns emptyList() |
| 75 | + } |
119 | 76 |
|
120 | | - verifyOrder { |
121 | | - mockMapboxNavigation.registerLocationObserver(any()) |
122 | | - mockMapboxNavigation.unregisterLocationObserver(any()) |
123 | | - } |
| 77 | + private fun location(latitude: Double, longitude: Double) = Location( |
| 78 | + LocationManager.PASSIVE_PROVIDER |
| 79 | + ).apply { |
| 80 | + this.latitude = latitude |
| 81 | + this.longitude = longitude |
124 | 82 | } |
125 | 83 | } |
0 commit comments