[android] resume file source and wait for result to complete resources cache path change

This commit is contained in:
Łukasz Paczos
2019-05-14 14:55:17 +02:00
committed by Łukasz Paczos
parent 46ef2d86d2
commit 1bef3b7d1d
5 changed files with 143 additions and 32 deletions

View File

@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.storage
import android.os.Handler
import android.support.test.annotation.UiThreadTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
@@ -10,6 +9,7 @@ import org.junit.*
import org.junit.rules.TestName
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
class FileSourceStandaloneTest {
@@ -55,21 +55,59 @@ class FileSourceStandaloneTest {
fileSourceTestUtils.changePath(fileSourceTestUtils.testPath)
Assert.assertEquals(fileSourceTestUtils.testPath, FileSource.getResourcesCachePath(rule.activity))
// workaround for https://github.com/mapbox/mapbox-gl-native/issues/14334
val latch = CountDownLatch(1)
rule.activity.runOnUiThread {
fileSource.activate()
Handler().postDelayed({
fileSource.deactivate()
latch.countDown()
}, 2000)
}
latch.await()
fileSourceTestUtils.changePath(fileSourceTestUtils.originalPath)
Assert.assertEquals(fileSourceTestUtils.originalPath, FileSource.getResourcesCachePath(rule.activity))
}
@Test
fun overridePathChangeCallTest() {
val firstLatch = CountDownLatch(1)
val secondLatch = CountDownLatch(1)
rule.activity.runOnUiThread {
FileSource.setResourcesCachePath(
fileSourceTestUtils.testPath,
object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String) {
Assert.assertEquals(fileSourceTestUtils.testPath, path)
firstLatch.countDown()
}
override fun onError(message: String) {
Assert.fail("First attempt should succeed.")
}
})
FileSource.setResourcesCachePath(
fileSourceTestUtils.testPath2,
object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String) {
Assert.fail("Second attempt should fail because first one is in progress.")
}
override fun onError(message: String) {
Assert.assertEquals("Another resources cache path change is in progress", message)
secondLatch.countDown()
}
})
}
if (!secondLatch.await(5, TimeUnit.SECONDS)) {
rule.runOnUiThread {
// if we fail to call a callback, the file source is not going to be deactivated
fileSource.deactivate()
}
Assert.fail("Second attempt should fail.")
}
if (!firstLatch.await(5, TimeUnit.SECONDS)) {
rule.runOnUiThread {
// if we fail to call a callback, the file source is not going to be deactivated
fileSource.deactivate()
}
Assert.fail("First attempt should succeed.")
}
}
@After
fun cleanup() {
fileSourceTestUtils.cleanup()

View File

@@ -10,10 +10,15 @@ import java.util.concurrent.CountDownLatch
class FileSourceTestUtils(private val activity: Activity) {
val originalPath = FileSource.getResourcesCachePath(activity)
val testPath = "$originalPath/test"
val testPath2 = "$originalPath/test2"
private val paths = listOf(testPath, testPath2)
fun setup() {
val testFile = File(testPath)
testFile.mkdirs()
for (path in paths) {
val testFile = File(path)
testFile.mkdirs()
}
}
@WorkerThread
@@ -22,25 +27,29 @@ class FileSourceTestUtils(private val activity: Activity) {
if (currentPath != originalPath) {
changePath(originalPath)
}
val testFile = File(testPath)
if (testFile.exists()) {
testFile.deleteRecursively()
for (path in paths) {
val testFile = File(path)
if (testFile.exists()) {
testFile.deleteRecursively()
}
}
}
@WorkerThread
fun changePath(path: String) {
fun changePath(requestedPath: String) {
val latch = CountDownLatch(1)
activity.runOnUiThread {
FileSource.setResourcesCachePath(
path,
requestedPath,
object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String) {
Assert.assertEquals(requestedPath, path)
latch.countDown()
}
override fun onError(message: String) {
Assert.fail("Resource path change failed - path: $path, message: $message")
Assert.fail("Resource path change failed - path: $requestedPath, message: $message")
}
})
}