[android] - introduce Pixel dimension instrumentation test
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -5,12 +5,24 @@ import android.support.test.espresso.UiController
|
||||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke
|
||||
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest
|
||||
|
||||
import com.mapbox.mapboxsdk.testapp.activity.BaseTest
|
||||
import com.mapbox.mapboxsdk.testapp.activity.espresso.PixelTestActivity
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class VisibleRegionTest : EspressoTest() {
|
||||
class VisibleRegionTest : BaseTest() {
|
||||
|
||||
override fun getActivityClass(): Class<*> {
|
||||
return PixelTestActivity::class.java
|
||||
}
|
||||
|
||||
override
|
||||
fun beforeTest() {
|
||||
super.beforeTest()
|
||||
mapView = (rule.activity as PixelTestActivity).mapView
|
||||
}
|
||||
|
||||
@Test
|
||||
fun visibleRegionTest() {
|
||||
@@ -253,9 +265,9 @@ class VisibleRegionTest : EspressoTest() {
|
||||
@Test
|
||||
fun paddedTopVisibleRegionOverDatelineTest() {
|
||||
validateTestSetup()
|
||||
invoke(mapboxMap) { _: UiController, mapboxMap: MapboxMap ->
|
||||
invoke(mapboxMap) { ui: UiController, mapboxMap: MapboxMap ->
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 180.0), 8.0))
|
||||
|
||||
ui.loopMainThreadForAtLeast(5000)
|
||||
val latLngs = listOf(
|
||||
mapboxMap.getLatLngFromScreenCoords(0f, 0f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, 0f),
|
||||
@@ -289,7 +301,7 @@ class VisibleRegionTest : EspressoTest() {
|
||||
mapboxMap.getLatLngFromScreenCoords(0f, 0f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, 0f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width.toFloat(), 0f)
|
||||
.also { it.longitude += 360 },
|
||||
.also { it.longitude += 360 },
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width.toFloat(), mapView.height / 2f)
|
||||
.also { it.longitude += 360 },
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width.toFloat(), mapView.height.toFloat())
|
||||
@@ -345,11 +357,11 @@ class VisibleRegionTest : EspressoTest() {
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 0.0), 8.0))
|
||||
val d = Math.min(mapboxMap.width, mapboxMap.height) / 4;
|
||||
val latLngs = listOf(
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f - d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f + d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f - d / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f + d / 2f)
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f - d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f + d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f - d / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f + d / 2f)
|
||||
)
|
||||
|
||||
|
||||
@@ -361,29 +373,29 @@ class VisibleRegionTest : EspressoTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun visibleRotatedRegionOverDatelineTest() {
|
||||
validateTestSetup()
|
||||
invoke(mapboxMap) { _: UiController, mapboxMap: MapboxMap ->
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 180.0), 8.0))
|
||||
val d = Math.min(mapboxMap.width, mapboxMap.height) / 4;
|
||||
val latLngs = listOf(
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f - d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f + d / 2f, mapView.height / 2f)
|
||||
.also { it.longitude += 360 },
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f - d / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f + d / 2f)
|
||||
)
|
||||
@Test
|
||||
fun visibleRotatedRegionOverDatelineTest() {
|
||||
validateTestSetup()
|
||||
invoke(mapboxMap) { _: UiController, mapboxMap: MapboxMap ->
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(0.0, 180.0), 8.0))
|
||||
val d = Math.min(mapboxMap.width, mapboxMap.height) / 4;
|
||||
val latLngs = listOf(
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f - d / 2f, mapView.height / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f + d / 2f, mapView.height / 2f)
|
||||
.also { it.longitude += 360 },
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f - d / 2f),
|
||||
mapboxMap.getLatLngFromScreenCoords(mapView.width / 2f, mapView.height / 2f + d / 2f)
|
||||
)
|
||||
|
||||
|
||||
for (bearing in 45 until 360 step 45) {
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(bearing.toDouble()));
|
||||
val visibleRegion = mapboxMap.projection.visibleRegion
|
||||
assertTrue(latLngs.all { visibleRegion.latLngBounds.contains(it) })
|
||||
}
|
||||
}
|
||||
for (bearing in 45 until 360 step 45) {
|
||||
mapboxMap.moveCamera(CameraUpdateFactory.bearingTo(bearing.toDouble()));
|
||||
val visibleRegion = mapboxMap.projection.visibleRegion
|
||||
assertTrue(latLngs.all { visibleRegion.latLngBounds.contains(it) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun MapboxMap.getLatLngFromScreenCoords(x: Float, y: Float): LatLng {
|
||||
return this.projection.fromScreenLocation(PointF(x, y))
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.testapp.style;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import com.mapbox.mapboxsdk.testapp.R;
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction;
|
||||
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* CRUD tests around Image
|
||||
*/
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ImageTest extends EspressoTest {
|
||||
|
||||
private static final String IMAGE_ID = "test.image";
|
||||
|
||||
@Test
|
||||
public void testAddGetImage() {
|
||||
validateTestSetup();
|
||||
MapboxMapAction.invoke(mapboxMap, (uiController, mapboxMap) -> {
|
||||
Drawable drawable = rule.getActivity().getResources().getDrawable(R.drawable.ic_launcher_round);
|
||||
assertTrue(drawable instanceof BitmapDrawable);
|
||||
|
||||
Bitmap bitmapSet = ((BitmapDrawable) drawable).getBitmap();
|
||||
mapboxMap.getStyle().addImage(IMAGE_ID, bitmapSet);
|
||||
|
||||
// adding an image requires converting the image with an asynctask
|
||||
uiController.loopMainThreadForAtLeast(200);
|
||||
|
||||
Bitmap bitmapGet = mapboxMap.getStyle().getImage(IMAGE_ID);
|
||||
assertTrue(bitmapGet.sameAs(bitmapSet));
|
||||
|
||||
mapboxMap.getStyle().removeImage(IMAGE_ID);
|
||||
assertNull(mapboxMap.getStyle().getImage(IMAGE_ID));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.mapbox.mapboxsdk.testapp.style
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.drawable.BitmapDrawable
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
import com.mapbox.mapboxsdk.testapp.R
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction
|
||||
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* CRUD tests around Image
|
||||
*/
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ImageTest : EspressoTest() {
|
||||
|
||||
companion object {
|
||||
private const val IMAGE_ID = "test.image"
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testAddGetImage() {
|
||||
validateTestSetup()
|
||||
MapboxMapAction.invoke(mapboxMap) { uiController, mapboxMap ->
|
||||
val drawable = rule.activity.resources.getDrawable(R.drawable.ic_launcher_round)
|
||||
assertTrue(drawable is BitmapDrawable)
|
||||
|
||||
val bitmapSet = (drawable as BitmapDrawable).bitmap
|
||||
mapboxMap.style!!.addImage(IMAGE_ID, bitmapSet)
|
||||
|
||||
// adding an image requires converting the image with an asynctask
|
||||
uiController.loopMainThreadForAtLeast(200)
|
||||
|
||||
val bitmapGet = mapboxMap.style!!.getImage(IMAGE_ID)
|
||||
assertTrue(bitmapGet!!.similarTo(bitmapSet))
|
||||
|
||||
mapboxMap.style!!.removeImage(IMAGE_ID)
|
||||
assertNull(mapboxMap.style!!.getImage(IMAGE_ID))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative implementation of Bitmap.sameAs #14060
|
||||
*/
|
||||
fun Bitmap.similarTo(other: Bitmap): Boolean {
|
||||
if (invalidConfig(other)) {
|
||||
return false
|
||||
}
|
||||
|
||||
// Allocate arrays
|
||||
val argb = IntArray(width * height)
|
||||
val argbOther = IntArray(other.width * other.height)
|
||||
getPixels(argb, 0, width, 0, 0, width, height)
|
||||
other.getPixels(argbOther, 0, width, 0, 0, width, height)
|
||||
|
||||
// Alpha channel special check
|
||||
if (config == Bitmap.Config.ALPHA_8) {
|
||||
// in this case we have to manually compare the alpha channel as the rest is garbage.
|
||||
val length = width * height
|
||||
for (i in 0 until length) {
|
||||
if (argb[i] and -0x1000000 != argbOther[i] and -0x1000000) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return Arrays.equals(argb, argbOther)
|
||||
}
|
||||
|
||||
fun Bitmap.invalidConfig(other: Bitmap): Boolean = this.config != other.config || this.width != other.width || this.height != other.height
|
||||
@@ -923,6 +923,9 @@
|
||||
<activity
|
||||
android:name=".activity.espresso.EspressoTestActivity"
|
||||
android:screenOrientation="portrait" />
|
||||
<activity
|
||||
android:name=".activity.espresso.PixelTestActivity"
|
||||
android:screenOrientation="portrait" />
|
||||
<activity
|
||||
android:name=".activity.espresso.DeviceIndependentTestActivity"
|
||||
android:screenOrientation="portrait" />
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package com.mapbox.mapboxsdk.testapp.activity.espresso
|
||||
|
||||
import android.os.Bundle
|
||||
import android.support.v7.app.AppCompatActivity
|
||||
import com.mapbox.mapboxsdk.maps.MapView
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap
|
||||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback
|
||||
import com.mapbox.mapboxsdk.maps.Style
|
||||
import com.mapbox.mapboxsdk.testapp.R
|
||||
|
||||
/**
|
||||
* Test activity used for instrumentation tests that require a specific device size.
|
||||
*/
|
||||
class PixelTestActivity : AppCompatActivity(), OnMapReadyCallback {
|
||||
|
||||
lateinit var mapView: MapView
|
||||
lateinit var mapboxMap: MapboxMap
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_pixel_test)
|
||||
mapView = findViewById(R.id.mapView)
|
||||
mapView.onCreate(savedInstanceState)
|
||||
mapView.getMapAsync(this)
|
||||
}
|
||||
|
||||
override fun onMapReady(map: MapboxMap) {
|
||||
mapboxMap = map
|
||||
mapboxMap.setStyle(Style.MAPBOX_STREETS)
|
||||
}
|
||||
|
||||
public override fun onResume() {
|
||||
super.onResume()
|
||||
mapView.onResume()
|
||||
}
|
||||
|
||||
override fun onStart() {
|
||||
super.onStart()
|
||||
mapView.onStart()
|
||||
}
|
||||
|
||||
public override fun onPause() {
|
||||
super.onPause()
|
||||
mapView.onPause()
|
||||
}
|
||||
|
||||
override fun onStop() {
|
||||
super.onStop()
|
||||
mapView.onStop()
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
mapView.onSaveInstanceState(outState)
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
mapView.onDestroy()
|
||||
}
|
||||
|
||||
override fun onLowMemory() {
|
||||
super.onLowMemory()
|
||||
mapView.onLowMemory()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<FrameLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<com.mapbox.mapboxsdk.maps.MapView
|
||||
android:layout_gravity="center"
|
||||
android:id="@id/mapView"
|
||||
app:mapbox_cameraZoom="1"
|
||||
android:layout_width="1080px"
|
||||
android:layout_height="1920px"
|
||||
app:mapbox_pixelRatio="1"/>
|
||||
|
||||
</FrameLayout>
|
||||
Reference in New Issue
Block a user