Skip to content

Commit

Permalink
Add test cases to verify annotations update
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Li committed Jun 29, 2021
1 parent b725fd5 commit 36a4dd0
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package com.mapbox.maps.testapp.annotation

import android.graphics.Color
import android.os.Handler
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import com.google.gson.JsonPrimitive
import com.mapbox.geojson.Point
import com.mapbox.maps.Style
import com.mapbox.maps.extension.style.utils.ColorUtils
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.*
import com.mapbox.maps.testapp.BaseMapTest
import junit.framework.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

/**
* Instrumented test for verifying annotation updating while changing style
*/
@RunWith(AndroidJUnit4::class)
@LargeTest
class UpdateAnnotationWithMultiManagersTest : BaseMapTest() {
private val latch = CountDownLatch(4)
private lateinit var pointAnnotationManager: PointAnnotationManager
private lateinit var circleAnnotationManager: CircleAnnotationManager
private lateinit var polygonAnnotationManager: PolygonAnnotationManager
private lateinit var polylineAnnotationManager: PolylineAnnotationManager
private lateinit var pointAnnotation: PointAnnotation
private lateinit var circleAnnotation: CircleAnnotation
private lateinit var polylineAnnotation: PolylineAnnotation
private lateinit var polygonAnnotation: PolygonAnnotation
private lateinit var handler: Handler
private val updateTimes = 100
private val updateSteps = 0.1
private val points = mutableListOf(
mutableListOf(
Point.fromLngLat(-3.363937, -10.733102),
Point.fromLngLat(1.754703, -19.716317),
Point.fromLngLat(-15.747196, -21.085074),
Point.fromLngLat(-3.363937, -10.733102)
)
)
private val polylinePoints = mutableListOf(
Point.fromLngLat(-4.375974, -2.178992),
Point.fromLngLat(-7.639772, -4.107888),
Point.fromLngLat(-11.439207, 2.798737),
)

@Test
fun testUpdateAnnotation() {
rule.scenario.onActivity {
handler = Handler(it.mainLooper)
it.runOnUiThread {
mapboxMap.loadStyleUri(Style.MAPBOX_STREETS) {
pointAnnotationManager = mapView.annotations.createPointAnnotationManager(mapView)
pointAnnotationManager.textFont = listOf("Open Sans Regular")
pointAnnotation = pointAnnotationManager.create(
PointAnnotationOptions()
.withIconColor(ColorUtils.colorToRgbaString(Color.RED))
.withIconImage("car-15")
.withPoint(Point.fromLngLat(0.0, 0.0))
)
circleAnnotationManager = mapView.annotations.createCircleAnnotationManager(mapView)
circleAnnotation = circleAnnotationManager.create(
CircleAnnotationOptions()
.withCircleColor(Color.RED)
.withCircleRadius(5.0)
.withPoint(Point.fromLngLat(0.0, 0.0))
)

polygonAnnotationManager = mapView.annotations.createPolygonAnnotationManager(mapView)
polygonAnnotation = polygonAnnotationManager.create(
PolygonAnnotationOptions()
.withPoints(points)
.withData(JsonPrimitive("Foobar"))
.withFillColor(Color.RED)
)

polylineAnnotationManager = mapView.annotations.createPolylineAnnotationManager(mapView)
polylineAnnotation = polylineAnnotationManager.create(
PolylineAnnotationOptions()
.withPoints(polylinePoints)
.withLineColor(Color.RED)
.withLineWidth(5.0)
)

Thread {
for (i in 0..updateTimes) {
pointAnnotation.point = Point.fromLngLat(
pointAnnotation.point.longitude() + updateSteps,
pointAnnotation.point.latitude() + updateSteps
)
pointAnnotationManager.update(pointAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()

Thread {
for (i in 0..updateTimes) {
circleAnnotation.point = Point.fromLngLat(
circleAnnotation.point.longitude() + updateSteps,
circleAnnotation.point.latitude() + updateSteps
)
circleAnnotationManager.update(circleAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()

Thread {
for (i in 0..updateTimes) {
points[0][0] = Point.fromLngLat(
points[0][0].longitude() + updateSteps,
points[0][0].latitude() + updateSteps
)
polygonAnnotation.points = points
polygonAnnotationManager.update(polygonAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()
Thread {
for (i in 0..updateTimes) {
polylinePoints[0] = Point.fromLngLat(
polylinePoints[0].longitude() + updateSteps,
polylinePoints[0].latitude() + updateSteps
)
polylineAnnotation.points = polylinePoints
polylineAnnotationManager.update(polylineAnnotation)
Thread.sleep(10)
}
latch.countDown()
}.start()
}
}
}
if (!latch.await(3000, TimeUnit.MILLISECONDS)) {
throw TimeoutException()
}
assertEquals(pointAnnotation.point.latitude(), updateSteps * updateTimes, 0.1)
assertEquals(pointAnnotation.point.longitude(), updateSteps * updateTimes, 0.1)
assertEquals(circleAnnotation.point.longitude(), updateSteps * updateTimes, 0.1)
assertEquals(circleAnnotation.point.latitude(), updateSteps * updateTimes, 0.1)
assertEquals(polylineAnnotation.points[0].longitude(), polylinePoints[0].longitude(), 0.1)
assertEquals(polylineAnnotation.points[0].latitude(), polylinePoints[0].latitude(), 0.1)
assertEquals(polygonAnnotation.points[0][0].longitude(), points[0][0].longitude(), 0.1)
assertEquals(polygonAnnotation.points[0][0].latitude(), points[0][0].latitude(), 0.1)
}
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".examples.annotation.PolyfillAnnotationActivity"
android:name=".examples.annotation.PolygoneAnnotationActivity"
android:description="@string/description_annotation_fill"
android:exported="true"
android:label="@string/activity_fill">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import kotlinx.android.synthetic.main.activity_annotation.*
import java.util.*

/**
* Example showing how to add Fill annotations
* Example showing how to add Polygone annotations
*/
class PolyfillAnnotationActivity : AppCompatActivity() {
class PolygoneAnnotationActivity : AppCompatActivity() {
private val random = Random()
private var polygonAnnotationManager: PolygonAnnotationManager? = null
private var index: Int = 0
Expand All @@ -37,7 +37,7 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
polygonAnnotationManager = annotationPlugin.createPolygonAnnotationManager(mapView).apply {
addClickListener(
OnPolygonAnnotationClickListener {
Toast.makeText(this@PolyfillAnnotationActivity, "click ${it.id}", Toast.LENGTH_SHORT)
Toast.makeText(this@PolygoneAnnotationActivity, "click ${it.id}", Toast.LENGTH_SHORT)
.show()
false
}
Expand All @@ -46,15 +46,15 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
addInteractionListener(object : OnPolygonAnnotationInteractionListener {
override fun onSelectAnnotation(annotation: PolygonAnnotation) {
Toast.makeText(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"onSelectAnnotation ${annotation.id}",
Toast.LENGTH_SHORT
).show()
}

override fun onDeselectAnnotation(annotation: PolygonAnnotation) {
Toast.makeText(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"onDeselectAnnotation ${annotation.id}",
Toast.LENGTH_SHORT
).show()
Expand Down Expand Up @@ -89,7 +89,7 @@ class PolyfillAnnotationActivity : AppCompatActivity() {
create(polygonAnnotationOptionsList)

AnnotationUtils.loadStringFromAssets(
this@PolyfillAnnotationActivity,
this@PolygoneAnnotationActivity,
"annotations.json"
)?.let {
create(FeatureCollection.fromJson(it))
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 36a4dd0

Please sign in to comment.