Kotlin testapp fixes (#753)

This commit is contained in:
Bart Louwers
2023-01-31 17:29:25 +01:00
committed by GitHub
parent 2e121db75f
commit ac2e9b10ab
15 changed files with 172 additions and 166 deletions

View File

@@ -50,7 +50,7 @@ open class MapSnapshotter(context: Context, options: Options) {
*
* @see MapSnapshotter.start
*/
interface SnapshotReadyCallback {
fun interface SnapshotReadyCallback {
/**
* Called when the snapshot is complete.
*
@@ -65,7 +65,7 @@ open class MapSnapshotter(context: Context, options: Options) {
*
* @see MapSnapshotter.start
*/
interface ErrorHandler {
fun interface ErrorHandler {
/**
* Called on error. Snapshotting will not
* continue

View File

@@ -51,7 +51,7 @@ android {
}
namespace 'com.mapbox.mapboxsdk.testapp'
testNamespace '${applicationId}'
testNamespace 'org.maplibre.testapp.test'
lint {
abortOnError false
baseline file('lint-baseline-local.xml')

View File

@@ -1,9 +1,9 @@
package com.mapbox.mapboxsdk
import com.mapbox.mapboxsdk.testapp.MapboxApplication
import com.mapbox.mapboxsdk.testapp.MapLibreApplication
class InstrumentationApplication : MapboxApplication() {
override fun initializeLeakCanary(): Boolean {
class InstrumentationApplication : MapLibreApplication() {
fun initializeLeakCanary(): Boolean {
// do not initialize leak canary during instrumentation tests
return true
}

View File

@@ -1,5 +1,9 @@
package com.mapbox.mapboxsdk.maps;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import androidx.test.annotation.UiThreadTest;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

View File

@@ -61,7 +61,7 @@ public class CompassViewTest extends EspressoTest {
onView(withTagValue(is("compassView"))).check(matches(not(isDisplayed())));
invoke(mapboxMap, (uiController, mapboxMap) -> {
CameraPosition cameraPosition = mapboxMap.getCameraPosition();
assertEquals("Camera bearing should face north, ", 0, cameraPosition.getBearing(), TestConstants.BEARING_DELTA);
assertEquals("Camera bearing should face north, ", 0, cameraPosition.bearing, TestConstants.BEARING_DELTA);
});
}
}

View File

@@ -59,12 +59,12 @@ class OfflineManagerTest : AppCenter() {
OfflineManager.getInstance(context).mergeOfflineRegions(
FileSource.getResourcesCachePath(rule.activity) + "/" + TEST_DB_FILE_NAME,
object : OfflineManager.MergeOfflineRegionsCallback {
override fun onMerge(offlineRegions: Array<out OfflineRegion>?) {
override fun onMerge(offlineRegions: Array<OfflineRegion>?) {
assert(offlineRegions?.size == 1)
latch.countDown()
}
override fun onError(error: String?) {
override fun onError(error: String) {
throw RuntimeException("Unable to merge external offline database. $error")
}
}
@@ -78,13 +78,13 @@ class OfflineManagerTest : AppCenter() {
val latch = CountDownLatch(1)
rule.activity.runOnUiThread {
OfflineManager.getInstance(context).listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
override fun onList(offlineRegions: Array<out OfflineRegion>?) {
override fun onList(offlineRegions: Array<OfflineRegion>?) {
assert(offlineRegions?.size == 1)
mergedRegion = offlineRegions!![0]
latch.countDown()
}
override fun onError(error: String?) {
override fun onError(error: String) {
throw RuntimeException("Unable to merge external offline database. $error")
}
})
@@ -101,7 +101,7 @@ class OfflineManagerTest : AppCenter() {
latch.countDown()
}
override fun onError(error: String?) {
override fun onError(error: String) {
throw RuntimeException("Unable to delete region")
}
})
@@ -118,7 +118,7 @@ class OfflineManagerTest : AppCenter() {
latch.countDown()
}
override fun onError(error: String?) {
override fun onError(error: String) {
throw RuntimeException("Unable to delete region")
}
})

View File

@@ -1,42 +0,0 @@
package com.mapbox.mapboxsdk.testapp.offline;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import com.mapbox.mapboxsdk.AppCenter;
import com.mapbox.mapboxsdk.testapp.utils.OfflineUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import static com.mapbox.mapboxsdk.testapp.activity.offline.OfflineActivity.JSON_CHARSET;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@RunWith(AndroidJUnit4ClassRunner.class)
public class OfflineUtilsTest extends AppCenter {
private static final String REGION_NAME = "hello world";
private static final String CONVERTED_REGION_NAME = "{\"FIELD_REGION_NAME\":\"hello world\"}";
@Test
public void testOfflineUtilsConvertToBytes() throws UnsupportedEncodingException {
byte[] expected = CONVERTED_REGION_NAME.getBytes(JSON_CHARSET);
byte[] actual = OfflineUtils.convertRegionName(REGION_NAME);
assertTrue("Bytes arrays should match", Arrays.equals(expected, actual));
}
@Test
public void testOfflineUtilsConvertToString() throws UnsupportedEncodingException {
String actual = OfflineUtils.convertRegionName(CONVERTED_REGION_NAME.getBytes(JSON_CHARSET));
assertEquals("Strings should match", REGION_NAME, actual);
}
@Test
public void testOfflineUtilsConvertNoOp() {
String convertNoOp = OfflineUtils.convertRegionName(OfflineUtils.convertRegionName(REGION_NAME));
assertEquals("Strings should match", REGION_NAME, convertNoOp);
}
}

View File

@@ -0,0 +1,41 @@
package com.mapbox.mapboxsdk.testapp.offline
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.mapbox.mapboxsdk.AppCenter
import com.mapbox.mapboxsdk.testapp.activity.offline.OfflineActivity
import com.mapbox.mapboxsdk.testapp.utils.OfflineUtils.convertRegionName
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import java.io.UnsupportedEncodingException
import java.util.*
@RunWith(AndroidJUnit4ClassRunner::class)
class OfflineUtilsTest : AppCenter() {
@Test
@Throws(UnsupportedEncodingException::class)
fun testOfflineUtilsConvertToBytes() {
val expected = CONVERTED_REGION_NAME.toByteArray(OfflineActivity.JSON_CHARSET)
val actual = convertRegionName(REGION_NAME)
Assert.assertTrue("Bytes arrays should match", Arrays.equals(expected, actual))
}
@Test
@Throws(UnsupportedEncodingException::class)
fun testOfflineUtilsConvertToString() {
val actual =
convertRegionName(CONVERTED_REGION_NAME.toByteArray(OfflineActivity.JSON_CHARSET))
Assert.assertEquals("Strings should match", REGION_NAME, actual)
}
@Test
fun testOfflineUtilsConvertNoOp() {
val convertNoOp = convertRegionName(convertRegionName(REGION_NAME)!!)
Assert.assertEquals("Strings should match", REGION_NAME, convertNoOp)
}
companion object {
private const val REGION_NAME = "hello world"
private const val CONVERTED_REGION_NAME = "{\"FIELD_REGION_NAME\":\"hello world\"}"
}
}

View File

@@ -7,6 +7,7 @@ import com.mapbox.mapboxsdk.AppCenter
import com.mapbox.mapboxsdk.storage.FileSource
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test

View File

@@ -2,16 +2,16 @@ package com.mapbox.mapboxsdk.testapp.style
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.THREAD_POOL_LIMIT
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.THREAD_PREFIX
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.Companion.THREAD_POOL_LIMIT
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource.Companion.THREAD_PREFIX
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke
import com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationLandscape
import com.mapbox.mapboxsdk.testapp.action.OrientationAction.orientationPortrait
import com.mapbox.mapboxsdk.testapp.action.WaitAction
import com.mapbox.mapboxsdk.testapp.activity.BaseTest
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.ID_GRID_LAYER
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.ID_GRID_SOURCE
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.Companion.ID_GRID_LAYER
import com.mapbox.mapboxsdk.testapp.activity.style.GridSourceActivity.Companion.ID_GRID_SOURCE
import com.mapbox.mapboxsdk.testapp.utils.TestingAsyncUtils
import org.junit.Assert
import org.junit.Assert.*
@@ -58,12 +58,12 @@ class CustomGeometrySourceTest : BaseTest() {
fun threadsRestartedWhenSourceReAddedTest() {
validateTestSetup()
invoke(mapboxMap) { uiController, mapboxMap ->
mapboxMap.style!!.removeLayer((rule.activity as GridSourceActivity).layer)
mapboxMap.style!!.removeLayer((rule.activity as GridSourceActivity).layer!!)
TestingAsyncUtils.waitForLayer(uiController, mapView)
mapboxMap.style!!.removeSource(ID_GRID_SOURCE)
TestingAsyncUtils.waitForLayer(uiController, mapView)
mapboxMap.style!!.addSource((rule.activity as GridSourceActivity).source)
mapboxMap.style!!.addLayer((rule.activity as GridSourceActivity).layer)
mapboxMap.style!!.addSource((rule.activity as GridSourceActivity).source!!)
mapboxMap.style!!.addLayer((rule.activity as GridSourceActivity).layer!!)
TestingAsyncUtils.waitForLayer(uiController, mapView)
Assert.assertTrue(
"Threads should be restarted when the source is re-added to the map.",

View File

@@ -1,60 +0,0 @@
package com.mapbox.mapboxsdk.testapp.style;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest;
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils;
import org.junit.Test;
import java.io.IOException;
import static com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke;
import static org.junit.Assert.assertEquals;
/**
* Tests around style loading
*/
public class StyleLoaderTest extends EspressoTest {
@Test
public void testSetGetStyleJsonString() {
validateTestSetup();
invoke(mapboxMap, (uiController, mapboxMap) -> {
try {
String expected = ResourceUtils.readRawResource(rule.getActivity(), R.raw.local_style);
mapboxMap.setStyle(new Style.Builder().fromJson(expected));
String actual = mapboxMap.getStyle().getJson();
assertEquals("Style json should match", expected, actual);
} catch (IOException exception) {
exception.printStackTrace();
}
});
}
@Test
public void testDefaultStyleLoadWithActivityLifecycleChange() {
validateTestSetup();
invoke(mapboxMap, (uiController, mapboxMap) -> {
try {
String expected = ResourceUtils.readRawResource(rule.getActivity(), R.raw.local_style);
mapboxMap.setStyle(new Style.Builder().fromJson(expected));
// fake activity stop/start
MapView mapView = (MapView) rule.getActivity().findViewById(R.id.mapView);
mapView.onPause();
mapView.onStop();
mapView.onStart();
mapView.onResume();
String actual = mapboxMap.getStyle().getJson();
assertEquals("Style URL should be empty", "", mapboxMap.getStyle().getUri());
assertEquals("Style json should match", expected, actual);
} catch (IOException exception) {
exception.printStackTrace();
}
});
}
}

View File

@@ -0,0 +1,74 @@
package com.mapbox.mapboxsdk.testapp.style
import android.view.View
import androidx.test.espresso.UiController
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest
import com.mapbox.mapboxsdk.testapp.utils.ResourceUtils.readRawResource
import org.junit.Assert
import org.junit.Test
import java.io.IOException
/**
* Tests around style loading
*/
class StyleLoaderTest : EspressoTest() {
@Test
fun testSetGetStyleJsonString() {
validateTestSetup()
MapboxMapAction.invoke(
mapboxMap
) { uiController: UiController?, mapboxMap: MapboxMap ->
try {
val expected =
readRawResource(
rule.activity,
R.raw.local_style
)
mapboxMap.setStyle(Style.Builder().fromJson(expected))
val actual = mapboxMap.style!!.json
Assert.assertEquals("Style json should match", expected, actual)
} catch (exception: IOException) {
exception.printStackTrace()
}
}
}
@Test
fun testDefaultStyleLoadWithActivityLifecycleChange() {
validateTestSetup()
MapboxMapAction.invoke(
mapboxMap
) { uiController: UiController?, mapboxMap: MapboxMap ->
try {
val expected =
readRawResource(
rule.activity,
R.raw.local_style
)
mapboxMap.setStyle(Style.Builder().fromJson(expected))
// fake activity stop/start
val mapView =
rule.activity.findViewById<View>(R.id.mapView) as MapView
mapView.onPause()
mapView.onStop()
mapView.onStart()
mapView.onResume()
val actual = mapboxMap.style!!.json
Assert.assertEquals(
"Style URL should be empty",
"",
mapboxMap.style!!.uri
)
Assert.assertEquals("Style json should match", expected, actual)
} catch (exception: IOException) {
exception.printStackTrace()
}
}
}
}

View File

@@ -1,43 +0,0 @@
package com.mapbox.mapboxsdk.utils;
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
@RunWith(AndroidJUnit4ClassRunner.class)
public class FontUtilsTest {
@Test
public void testExtractedFontShouldMatchDefault() {
String[] fonts = new String[] {"foo", "bar"};
String actual = FontUtils.extractValidFont(fonts);
assertEquals("Selected font should match", MapboxConstants.DEFAULT_FONT, actual);
}
@Test
public void testExtractedFontShouldMatchMonospace() {
String expected = "monospace";
String[] fonts = new String[] {"foo", expected};
String actual = FontUtils.extractValidFont(fonts);
assertEquals("Selected font should match", expected, actual);
}
@Test
public void testExtractedFontArrayShouldBeNull() {
String[] fonts = null;
String actual = FontUtils.extractValidFont(fonts);
assertNull(actual);
}
@Test
public void testExtractedFontShouldBeNull() {
String actual = FontUtils.extractValidFont(null);
assertNull(actual);
}
}

View File

@@ -0,0 +1,31 @@
package com.mapbox.mapboxsdk.utils
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.mapbox.mapboxsdk.constants.MapboxConstants
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4ClassRunner::class)
class FontUtilsTest {
@Test
fun testExtractedFontShouldMatchDefault() {
val fonts = arrayOf("foo", "bar")
val actual = FontUtils.extractValidFont(*fonts)
Assert.assertEquals("Selected font should match", MapboxConstants.DEFAULT_FONT, actual)
}
@Test
fun testExtractedFontShouldMatchMonospace() {
val expected = "monospace"
val fonts = arrayOf("foo", expected)
val actual = FontUtils.extractValidFont(*fonts)
Assert.assertEquals("Selected font should match", expected, actual)
}
@Test
fun testExtractedFontShouldBeNull() {
val actual = FontUtils.extractValidFont(null)
Assert.assertNull(actual)
}
}

View File

@@ -22,7 +22,7 @@ import timber.log.Timber.DebugTree
* Initialises components as LeakCanary, Strictmode, Timber and Mapbox
*
*/
class MapLibreApplication : MultiDexApplication() {
open class MapLibreApplication : MultiDexApplication() {
override fun onCreate() {
super.onCreate()
initializeLogger()