[android] - refactor idling resource from test
This commit is contained in:
@@ -1,59 +1,39 @@
|
||||
package com.mapbox.mapboxsdk.testapp.offline
|
||||
|
||||
import android.content.Context
|
||||
import android.support.test.espresso.Espresso
|
||||
import android.support.test.espresso.IdlingRegistry
|
||||
import android.support.test.espresso.UiController
|
||||
import android.support.test.espresso.idling.CountingIdlingResource
|
||||
import android.support.test.rule.ActivityTestRule
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap
|
||||
import com.mapbox.mapboxsdk.offline.OfflineManager
|
||||
import com.mapbox.mapboxsdk.offline.OfflineRegion
|
||||
import com.mapbox.mapboxsdk.storage.FileSource
|
||||
import com.mapbox.mapboxsdk.testapp.action.MapboxMapAction.invoke
|
||||
import com.mapbox.mapboxsdk.testapp.activity.EspressoTest
|
||||
import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity
|
||||
import com.mapbox.mapboxsdk.testapp.utils.FileUtils
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class OfflineManagerTest : EspressoTest() {
|
||||
class OfflineManagerTest {
|
||||
|
||||
companion object {
|
||||
private const val TEST_DB_FILE_NAME = "offline_test.db"
|
||||
}
|
||||
|
||||
@Rule
|
||||
@JvmField
|
||||
var rule = ActivityTestRule(FeatureOverviewActivity::class.java)
|
||||
|
||||
private val context: Context by lazy { rule.activity }
|
||||
|
||||
private lateinit var offlineIdlingResource: CountingIdlingResource
|
||||
|
||||
override fun beforeTest() {
|
||||
super.beforeTest()
|
||||
offlineIdlingResource = CountingIdlingResource("idling_resource")
|
||||
IdlingRegistry.getInstance().register(offlineIdlingResource)
|
||||
}
|
||||
private val latch: CountDownLatch = CountDownLatch(1)
|
||||
|
||||
@Test
|
||||
fun offlineMergeListDeleteTest() {
|
||||
validateTestSetup()
|
||||
|
||||
invoke(mapboxMap) { _: UiController, _: MapboxMap ->
|
||||
offlineIdlingResource.increment()
|
||||
rule.runOnUiThread {
|
||||
FileUtils.CopyFileFromAssetsTask(rule.activity, object : FileUtils.OnFileCopiedFromAssetsListener {
|
||||
override fun onFileCopiedFromAssets() {
|
||||
OfflineManager.getInstance(context).mergeOfflineRegions(
|
||||
FileSource.getResourcesCachePath(rule.activity) + "/" + TEST_DB_FILE_NAME,
|
||||
object : OfflineManager.MergeOfflineRegionsCallback {
|
||||
override fun onMerge(offlineRegions: Array<out OfflineRegion>?) {
|
||||
assert(offlineRegions?.size == 1)
|
||||
offlineIdlingResource.decrement()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to merge external offline database. $error")
|
||||
}
|
||||
})
|
||||
mergeRegion()
|
||||
}
|
||||
|
||||
override fun onError() {
|
||||
@@ -61,43 +41,45 @@ class OfflineManagerTest : EspressoTest() {
|
||||
}
|
||||
}).execute(TEST_DB_FILE_NAME, FileSource.getResourcesCachePath(rule.activity))
|
||||
}
|
||||
latch.await()
|
||||
}
|
||||
|
||||
invoke(mapboxMap) { _: UiController, _: MapboxMap ->
|
||||
offlineIdlingResource.increment()
|
||||
OfflineManager.getInstance(context).listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
|
||||
override fun onList(offlineRegions: Array<out OfflineRegion>?) {
|
||||
private fun mergeRegion() {
|
||||
OfflineManager.getInstance(context).mergeOfflineRegions(
|
||||
FileSource.getResourcesCachePath(rule.activity) + "/" + TEST_DB_FILE_NAME,
|
||||
object : OfflineManager.MergeOfflineRegionsCallback {
|
||||
override fun onMerge(offlineRegions: Array<out OfflineRegion>?) {
|
||||
assert(offlineRegions?.size == 1)
|
||||
if (offlineRegions != null) {
|
||||
for (region in offlineRegions) {
|
||||
offlineIdlingResource.increment()
|
||||
region.delete(object : OfflineRegion.OfflineRegionDeleteCallback {
|
||||
override fun onDelete() {
|
||||
offlineIdlingResource.decrement()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to delete region with ID: ${region.id}. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
throw RuntimeException("Unable to find merged region.")
|
||||
}
|
||||
offlineIdlingResource.decrement()
|
||||
listRegion()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to obtain offline regions list. $error")
|
||||
throw RuntimeException("Unable to merge external offline database. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// waiting for offline idling resource
|
||||
Espresso.onIdle()
|
||||
}
|
||||
|
||||
override fun afterTest() {
|
||||
super.afterTest()
|
||||
IdlingRegistry.getInstance().unregister(offlineIdlingResource)
|
||||
private fun listRegion() {
|
||||
OfflineManager.getInstance(context).listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {
|
||||
override fun onList(offlineRegions: Array<out OfflineRegion>?) {
|
||||
assert(offlineRegions?.size == 1)
|
||||
deleteRegion(offlineRegions!![0])
|
||||
}
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to merge external offline database. $error")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun deleteRegion(offlineRegion: OfflineRegion) {
|
||||
offlineRegion.delete(object : OfflineRegion.OfflineRegionDeleteCallback {
|
||||
override fun onDelete() {
|
||||
latch.countDown()
|
||||
}
|
||||
|
||||
override fun onError(error: String?) {
|
||||
throw RuntimeException("Unable to delete region")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user