Skip to content

Commit

Permalink
[locationcomponent] Emit the last indicator state when new listeners …
Browse files Browse the repository at this point in the history
…are added to the location component. MAPSAND-570 (#1827)

* Emit the last indicator state when listeners are added.

* Add unit tests.

* Update change log.

* Temporarily ignore transitionToDefaultTransition test on API 23 devices.
  • Loading branch information
pengdev authored Nov 14, 2022
1 parent 395d262 commit 0a2438b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Mapbox welcomes participation and contributions from everyone.
## Bug fixes 🐞
* Trigger repaint after `BitmapWidget` is updated. ([1797](https://github.com/mapbox/mapbox-maps-android/pull/1797))
* Fix a crash after removing the view annotation if view has an attached animation or transition. ([1831](https://github.com/mapbox/mapbox-maps-android/pull/1831))
* Emit the last indicator state when new listeners are added to the location component. ([1827](https://github.com/mapbox/mapbox-maps-android/pull/1827))

# 10.9.1 November 7, 2022

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.maps.testapp.viewport

import android.os.Build
import android.os.Handler
import android.os.Looper
import androidx.test.ext.junit.runners.AndroidJUnit4
Expand All @@ -15,6 +16,7 @@ import com.mapbox.maps.plugin.viewport.viewport
import com.mapbox.maps.testapp.BaseMapTest
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -68,6 +70,10 @@ class ViewportPluginTest : BaseMapTest() {

@Test
fun transitionToDefaultTransition() {
assumeTrue(
"Can only run on API Level 24 or newer because of difference of animator behaviour.",
Build.VERSION.SDK_INT > 23
)
handler.post {
viewportPlugin.transitionTo(followPuckViewportState)
assertEquals(0.0, mapView.getMapboxMap().cameraState.bearing, EPS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
@VisibleForTesting(otherwise = PRIVATE)
internal var isLocationComponentActivated = false

private var lastIndicatorPosition: Point? = null

private var lastIndicatorBearing: Double? = null

private var lastIndicatorAccuracyRadius: Double? = null

private val onIndicatorPositionChangedListeners =
CopyOnWriteArraySet<OnIndicatorPositionChangedListener>()

Expand All @@ -54,6 +60,9 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
*/
override fun addOnIndicatorPositionChangedListener(listener: OnIndicatorPositionChangedListener) {
onIndicatorPositionChangedListeners.add(listener)
lastIndicatorPosition?.let {
listener.onIndicatorPositionChanged(it)
}
}

/**
Expand All @@ -67,6 +76,7 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,

@VisibleForTesting(otherwise = PRIVATE)
internal val indicatorPositionChangedListener = OnIndicatorPositionChangedListener {
lastIndicatorPosition = it
for (listener in onIndicatorPositionChangedListeners) {
listener.onIndicatorPositionChanged(it)
}
Expand All @@ -79,6 +89,9 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
*/
override fun addOnIndicatorBearingChangedListener(listener: OnIndicatorBearingChangedListener) {
onIndicatorBearingChangedListeners.add(listener)
lastIndicatorBearing?.let {
listener.onIndicatorBearingChanged(it)
}
}

/**
Expand All @@ -97,6 +110,9 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,
*/
override fun addOnIndicatorAccuracyRadiusChangedListener(listener: OnIndicatorAccuracyRadiusChangedListener) {
onIndicatorAccuracyRadiusChangedListeners.add(listener)
lastIndicatorAccuracyRadius?.let {
listener.onIndicatorAccuracyRadiusChanged(it)
}
}

/**
Expand Down Expand Up @@ -140,13 +156,15 @@ class LocationComponentPluginImpl : LocationComponentPlugin2, LocationConsumer2,

@VisibleForTesting(otherwise = PRIVATE)
internal val indicatorBearingChangedListener = OnIndicatorBearingChangedListener {
lastIndicatorBearing = it
for (listener in onIndicatorBearingChangedListeners) {
listener.onIndicatorBearingChanged(it)
}
}

@VisibleForTesting(otherwise = PRIVATE)
internal val indicatorAccuracyRadiusChangedListener = OnIndicatorAccuracyRadiusChangedListener {
lastIndicatorAccuracyRadius = it
for (listener in onIndicatorAccuracyRadiusChangedListeners) {
listener.onIndicatorAccuracyRadiusChanged(it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,23 @@ class LocationComponentPluginImplTest {
@Test
fun testAddOnIndicatorPositionChangedListener() {
val positionListener = mockk<OnIndicatorPositionChangedListener>(relaxed = true)
locationComponentPlugin.addOnIndicatorPositionChangedListener(positionListener)
locationComponentPlugin.indicatorPositionChangedListener.onIndicatorPositionChanged(
Point.fromLngLat(
0.0,
0.0
)
)
verify { positionListener.onIndicatorPositionChanged(Point.fromLngLat(0.0, 0.0)) }
locationComponentPlugin.addOnIndicatorPositionChangedListener(positionListener)
// Verify the newly added listener will get the last updated indicator position
verify(exactly = 1) { positionListener.onIndicatorPositionChanged(Point.fromLngLat(0.0, 0.0)) }
locationComponentPlugin.indicatorPositionChangedListener.onIndicatorPositionChanged(
Point.fromLngLat(
1.0,
0.0
)
)
// Verify the listener will get notified when new location arrives.
verify(exactly = 1) { positionListener.onIndicatorPositionChanged(Point.fromLngLat(1.0, 0.0)) }
}

@Test
Expand All @@ -175,9 +184,13 @@ class LocationComponentPluginImplTest {
@Test
fun testAddOnIndicatorBearingChangedListener() {
val bearingListener = mockk<OnIndicatorBearingChangedListener>(relaxed = true)
locationComponentPlugin.addOnIndicatorBearingChangedListener(bearingListener)
locationComponentPlugin.indicatorBearingChangedListener.onIndicatorBearingChanged(0.0)
verify { bearingListener.onIndicatorBearingChanged(0.0) }
locationComponentPlugin.addOnIndicatorBearingChangedListener(bearingListener)
// Verify the newly added listener will get the last updated indicator bearing
verify(exactly = 1) { bearingListener.onIndicatorBearingChanged(0.0) }
// Verify the listener will get notified when new bearing arrives.
locationComponentPlugin.indicatorBearingChangedListener.onIndicatorBearingChanged(1.0)
verify(exactly = 1) { bearingListener.onIndicatorBearingChanged(1.0) }
}

@Test
Expand All @@ -189,6 +202,27 @@ class LocationComponentPluginImplTest {
verify(exactly = 0) { bearingListener.onIndicatorBearingChanged(0.0) }
}

@Test
fun testAddOnIndicatorAccuracyRadiusChangedListener() {
val accuracyListener = mockk<OnIndicatorAccuracyRadiusChangedListener>(relaxed = true)
locationComponentPlugin.indicatorAccuracyRadiusChangedListener.onIndicatorAccuracyRadiusChanged(0.0)
locationComponentPlugin.addOnIndicatorAccuracyRadiusChangedListener(accuracyListener)
// Verify the newly added listener will get the last updated indicator accuracy radius
verify(exactly = 1) { accuracyListener.onIndicatorAccuracyRadiusChanged(0.0) }
// Verify the listener will get notified when new accuracy radius arrives.
locationComponentPlugin.indicatorAccuracyRadiusChangedListener.onIndicatorAccuracyRadiusChanged(1.0)
verify(exactly = 1) { accuracyListener.onIndicatorAccuracyRadiusChanged(1.0) }
}

@Test
fun testRemoveOnIndicatorAccuracyRadiusChangedListener() {
val accuracyRadiusListener = mockk<OnIndicatorAccuracyRadiusChangedListener>(relaxed = true)
locationComponentPlugin.addOnIndicatorAccuracyRadiusChangedListener(accuracyRadiusListener)
locationComponentPlugin.removeOnIndicatorAccuracyRadiusChangedListener(accuracyRadiusListener)
locationComponentPlugin.indicatorAccuracyRadiusChangedListener.onIndicatorAccuracyRadiusChanged(.0)
verify(exactly = 0) { accuracyRadiusListener.onIndicatorAccuracyRadiusChanged(0.0) }
}

@Test
fun testSetLocationProvider() {
val mockLocationProvider = mockk<LocationProvider>(relaxed = true)
Expand Down

0 comments on commit 0a2438b

Please sign in to comment.