Skip to content

Commit 54a2f39

Browse files
natiginfogithub-actions[bot]
authored andcommitted
[maps-android] fix extension-style-app tests (#11776)
GitOrigin-RevId: f6b5e6c5ce514eaa62f40262a8127deca3e81a36
1 parent a3b3b77 commit 54a2f39

File tree

9 files changed

+86
-84
lines changed

9 files changed

+86
-84
lines changed

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/BaseStyleTest.kt

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.mapbox.maps.testapp.style
22

33
import androidx.appcompat.app.AppCompatActivity
4-
import androidx.test.ext.junit.rules.ActivityScenarioRule
4+
import androidx.test.core.app.ActivityScenario
55
import androidx.test.ext.junit.runners.AndroidJUnit4
66
import androidx.test.filters.LargeTest
77
import androidx.test.platform.app.InstrumentationRegistry
@@ -27,44 +27,64 @@ import com.mapbox.maps.extension.style.precipitations.generated.setSnow
2727
import com.mapbox.maps.extension.style.sources.addSource
2828
import com.mapbox.maps.extension.style.terrain.generated.setTerrain
2929
import org.junit.After
30+
import org.junit.AfterClass
3031
import org.junit.Before
31-
import org.junit.Rule
32+
import org.junit.BeforeClass
3233
import org.junit.runner.RunWith
3334
import java.util.concurrent.CountDownLatch
3435
import java.util.concurrent.TimeUnit
3536
import java.util.concurrent.TimeoutException
3637

3738
/**
3839
* Instrumentation test for Layers to test Layer properties.
40+
*
41+
* The [ActivityScenario] is launched once per test class (in [setupClass]) to save time.
42+
* A fresh [MapView] and [MapboxMap] are created for each test (in [before]) for isolation.
43+
* UI thread work is dispatched via [runOnUiThread] ([Instrumentation.runOnMainSync]) to
44+
* avoid depending on the Activity not being destroyed between tests.
3945
*/
4046
@RunWith(AndroidJUnit4::class)
4147
@LargeTest
4248
abstract class BaseStyleTest {
4349

4450
private lateinit var mapView: MapView
45-
protected lateinit var mapboxMap: MapboxMap
4651
private lateinit var style: Style
52+
protected lateinit var mapboxMap: MapboxMap
53+
54+
companion object {
55+
private lateinit var _scenario: ActivityScenario<AppCompatActivity>
56+
57+
@BeforeClass
58+
@JvmStatic
59+
fun setupClass() {
60+
_scenario = ActivityScenario.launch(AppCompatActivity::class.java)
61+
}
62+
63+
@AfterClass
64+
@JvmStatic
65+
fun tearDownClass() {
66+
_scenario.close()
67+
}
68+
}
4769

48-
@get:Rule
49-
var rule = ActivityScenarioRule(AppCompatActivity::class.java)
70+
protected fun runOnUiThread(block: () -> Unit) {
71+
InstrumentationRegistry.getInstrumentation().runOnMainSync(block)
72+
}
5073

5174
@Before
5275
fun before() {
5376
val latch = CountDownLatch(1)
54-
rule.scenario.onActivity {
55-
it.runOnUiThread {
56-
val context = InstrumentationRegistry.getInstrumentation().targetContext
57-
mapView = MapView(context)
58-
59-
mapboxMap = mapView.mapboxMap
60-
mapboxMap.loadStyle(
61-
"mapbox://styles/mapbox/empty-v9"
62-
) { style ->
63-
this@BaseStyleTest.style = style
64-
latch.countDown()
65-
}
66-
mapView.onStart()
77+
InstrumentationRegistry.getInstrumentation().runOnMainSync {
78+
val context = InstrumentationRegistry.getInstrumentation().targetContext
79+
mapView = MapView(context)
80+
mapboxMap = mapView.mapboxMap
81+
mapboxMap.loadStyle(
82+
"mapbox://styles/mapbox/empty-v9"
83+
) { style ->
84+
this@BaseStyleTest.style = style
85+
latch.countDown()
6786
}
87+
mapView.onStart()
6888
}
6989
if (!latch.await(10000, TimeUnit.MILLISECONDS)) {
7090
throw TimeoutException()
@@ -73,16 +93,9 @@ abstract class BaseStyleTest {
7393

7494
@After
7595
fun tearDown() {
76-
val latch = CountDownLatch(1)
77-
rule.scenario.onActivity {
78-
it.runOnUiThread {
79-
mapView.onStop()
80-
mapView.onDestroy()
81-
latch.countDown()
82-
}
83-
}
84-
if (!latch.await(10000, TimeUnit.MILLISECONDS)) {
85-
throw TimeoutException()
96+
InstrumentationRegistry.getInstrumentation().runOnMainSync {
97+
mapView.onStop()
98+
mapView.onDestroy()
8699
}
87100
}
88101

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/layers/generated/FillLayerTest.kt

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/layers/generated/LineLayerTest.kt

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/layers/generated/SkyLayerTest.kt

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/layers/generated/SymbolLayerTest.kt

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/light/generated/DirectionalLightTest.kt

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension-style-app/src/androidTest/java/com/mapbox/maps/testapp/style/sources/RasterArraySourceTest.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ class RasterArraySourceTest : BaseStyleTest() {
1919
@Test
2020
fun rasterLayersTest() {
2121
val latch = CountDownLatch(1)
22-
rule.scenario.onActivity {
23-
it.runOnUiThread {
24-
mapboxMap.loadStyle(
22+
runOnUiThread {
23+
mapboxMap.loadStyle(
2524
style = """
2625
{
2726
"version": 8,
@@ -108,7 +107,6 @@ class RasterArraySourceTest : BaseStyleTest() {
108107
)
109108
latch.countDown()
110109
}
111-
}
112110
}
113111

114112
if (!latch.await(10000, TimeUnit.MILLISECONDS)) {

0 commit comments

Comments
 (0)