forked from maplibre/maplibre-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
convert java to kotlin (10 last files within 'AndroidSDK..test') (map…
…libre#1926) Co-authored-by: abebeos <[email protected]>
- Loading branch information
abebeos
and
abebeos
authored
Dec 12, 2023
1 parent
884af02
commit f8e414a
Showing
20 changed files
with
1,149 additions
and
1,373 deletions.
There are no files selected for viewing
36 changes: 0 additions & 36 deletions
36
platform/android/MapboxGLAndroidSDK/src/test/java/org/maplibre/android/MapLibreInjector.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
platform/android/MapboxGLAndroidSDK/src/test/java/org/maplibre/android/MapLibreInjector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.maplibre.android | ||
|
||
import android.content.Context | ||
import org.maplibre.android.MapLibre | ||
import org.maplibre.android.util.TileServerOptions | ||
|
||
object MapLibreInjector { | ||
private const val FIELD_INSTANCE = "INSTANCE" | ||
@JvmStatic | ||
fun inject(context: Context, apiKey: String, | ||
options: TileServerOptions) { | ||
val maplibre = MapLibre(context, apiKey, options) | ||
try { | ||
val instance = MapLibre::class.java.getDeclaredField(FIELD_INSTANCE) | ||
instance.isAccessible = true | ||
instance[maplibre] = maplibre | ||
} catch (exception: Exception) { | ||
throw AssertionError() | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun clear() { | ||
try { | ||
val field = MapLibre::class.java.getDeclaredField(FIELD_INSTANCE) | ||
field.isAccessible = true | ||
field[field] = null | ||
} catch (exception: Exception) { | ||
throw AssertionError() | ||
} | ||
} | ||
} |
98 changes: 0 additions & 98 deletions
98
platform/android/MapboxGLAndroidSDK/src/test/java/org/maplibre/android/MapLibreTest.java
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
platform/android/MapboxGLAndroidSDK/src/test/java/org/maplibre/android/MapLibreTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.maplibre.android | ||
|
||
import android.content.Context | ||
import android.content.res.Resources | ||
import android.content.res.TypedArray | ||
import android.util.AttributeSet | ||
import android.util.DisplayMetrics | ||
import org.junit.After | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.ExpectedException | ||
import org.maplibre.android.MapLibreInjector.clear | ||
import org.maplibre.android.MapLibreInjector.inject | ||
import org.maplibre.android.exceptions.MapLibreConfigurationException | ||
import org.maplibre.android.maps.MapView | ||
import org.maplibre.android.utils.ConfigUtils.Companion.getMockedOptions | ||
import org.mockito.ArgumentMatchers | ||
import org.mockito.Mockito | ||
|
||
class MapLibreTest { | ||
private var context: Context? = null | ||
private var appContext: Context? = null | ||
|
||
@Rule | ||
@JvmField // J2K: https://stackoverflow.com/a/33449455 | ||
var expectedException = ExpectedException.none() | ||
@Before | ||
fun before() { | ||
context = Mockito.mock(Context::class.java) | ||
appContext = Mockito.mock(Context::class.java) | ||
// J2K: https://www.baeldung.com/kotlin/smart-cast-to-type-is-impossible#2-using-the-safe-call-operator--and-a-scope-function | ||
Mockito.`when`(context?.getApplicationContext()).thenReturn(appContext) | ||
} | ||
|
||
@Test | ||
fun testGetApiKey() { | ||
val apiKey = "pk.0000000001" | ||
inject(context!!, apiKey, getMockedOptions()) | ||
Assert.assertSame(apiKey, MapLibre.getApiKey()) | ||
} | ||
|
||
@Test | ||
fun testApplicationContext() { | ||
inject(context!!, "pk.0000000001", getMockedOptions()) | ||
Assert.assertNotNull(MapLibre.getApplicationContext()) | ||
Assert.assertNotEquals(context, appContext) | ||
Assert.assertEquals(appContext, appContext) | ||
} | ||
|
||
@Test | ||
fun testPlainTokenValid() { | ||
Assert.assertTrue(MapLibre.isApiKeyValid("apiKey")) | ||
} | ||
|
||
@Test | ||
fun testEmptyToken() { | ||
Assert.assertFalse(MapLibre.isApiKeyValid("")) | ||
} | ||
|
||
@Test | ||
fun testNullToken() { | ||
Assert.assertFalse(MapLibre.isApiKeyValid(null)) | ||
} | ||
|
||
@Test | ||
fun testNoInstance() { | ||
val displayMetrics = Mockito.mock(DisplayMetrics::class.java) | ||
val resources = Mockito.mock(Resources::class.java) | ||
Mockito.`when`(resources.displayMetrics).thenReturn(displayMetrics) | ||
Mockito.`when`(context!!.resources).thenReturn(resources) | ||
val typedArray = Mockito.mock(TypedArray::class.java) | ||
Mockito.`when`(context!!.obtainStyledAttributes(ArgumentMatchers.nullable(AttributeSet::class.java), ArgumentMatchers.any(IntArray::class.java), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt())) | ||
.thenReturn(typedArray) | ||
expectedException.expect(MapLibreConfigurationException::class.java) | ||
expectedException.expectMessage(""" | ||
Using MapView requires calling MapLibre.getInstance(Context context, String apiKey, WellKnownTileServer wellKnownTileServer) before inflating or creating the view. | ||
""".trimIndent()) | ||
MapView(context!!) | ||
} | ||
|
||
@After | ||
fun after() { | ||
clear() | ||
} | ||
} |
Oops, something went wrong.