[android] add unit tests for #13947
This commit is contained in:
committed by
Łukasz Paczos
parent
af74d00b05
commit
98d3f42e18
@@ -231,7 +231,7 @@ public class FileSource {
|
||||
* @param context the context to derive the files directory path from
|
||||
* @return the files directory path
|
||||
*/
|
||||
@Nullable
|
||||
@NonNull
|
||||
public static String getResourcesCachePath(@NonNull Context context) {
|
||||
resourcesCachePathLoaderLock.lock();
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.mapbox.mapboxsdk.testapp.storage
|
||||
|
||||
import android.support.test.annotation.UiThreadTest
|
||||
import android.support.test.rule.ActivityTestRule
|
||||
import android.support.test.runner.AndroidJUnit4
|
||||
import com.mapbox.mapboxsdk.storage.FileSource
|
||||
import com.mapbox.mapboxsdk.testapp.activity.espresso.EspressoTestActivity
|
||||
import junit.framework.Assert
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TestName
|
||||
import org.junit.runner.RunWith
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
open class FileSourceMapTest {
|
||||
|
||||
private lateinit var fileSourceTestUtils: FileSourceTestUtils
|
||||
|
||||
@get:Rule
|
||||
val rule = ActivityTestRule(EspressoTestActivity::class.java)
|
||||
|
||||
@get:Rule
|
||||
val testName = TestName()
|
||||
|
||||
@Before
|
||||
@UiThreadTest
|
||||
fun setup() {
|
||||
fileSourceTestUtils = FileSourceTestUtils(rule.activity)
|
||||
fileSourceTestUtils.setup()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun changeResourcesPathWhileMapVisible() {
|
||||
val latch = CountDownLatch(1)
|
||||
rule.activity.runOnUiThread {
|
||||
FileSource.setResourcesCachePath(rule.activity, fileSourceTestUtils.testPath, object : FileSource.ResourcesCachePathChangeCallback {
|
||||
override fun onSuccess(path: String?) {
|
||||
Assert.fail("Requested resources change while the map is running should fail")
|
||||
}
|
||||
|
||||
override fun onError(message: String?) {
|
||||
Assert.assertEquals("Cannot set path, file source is activated."
|
||||
+ " Make sure that the map or a resources download is not running.", message)
|
||||
latch.countDown()
|
||||
}
|
||||
})
|
||||
}
|
||||
latch.await()
|
||||
}
|
||||
|
||||
@After
|
||||
fun cleanup() {
|
||||
fileSourceTestUtils.cleanup()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
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
|
||||
import com.mapbox.mapboxsdk.storage.FileSource
|
||||
import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity
|
||||
import org.junit.*
|
||||
import org.junit.rules.TestName
|
||||
import org.junit.runner.RunWith
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class FileSourceStandaloneTest {
|
||||
|
||||
private lateinit var fileSourceTestUtils: FileSourceTestUtils
|
||||
private lateinit var fileSource: FileSource
|
||||
|
||||
@get:Rule
|
||||
val rule = ActivityTestRule(FeatureOverviewActivity::class.java)
|
||||
|
||||
@get:Rule
|
||||
val testName = TestName()
|
||||
|
||||
@Before
|
||||
@UiThreadTest
|
||||
fun setup() {
|
||||
fileSource = FileSource.getInstance(rule.activity)
|
||||
fileSourceTestUtils = FileSourceTestUtils(rule.activity)
|
||||
fileSourceTestUtils.setup()
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
fun testDefault() {
|
||||
Assert.assertFalse("FileSource should not be active", fileSource.isActivated)
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
fun testActivateDeactivate() {
|
||||
Assert.assertFalse("1) FileSource should not be active", fileSource.isActivated)
|
||||
fileSource.activate()
|
||||
Assert.assertTrue("2) FileSource should be active", fileSource.isActivated)
|
||||
fileSource.deactivate()
|
||||
Assert.assertFalse("3) FileSource should not be active", fileSource.isActivated)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun pathChangeTest() {
|
||||
Assert.assertFalse("FileSource should not be active", fileSource.isActivated)
|
||||
Assert.assertEquals(fileSourceTestUtils.originalPath, FileSource.getResourcesCachePath(rule.activity))
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
@After
|
||||
fun cleanup() {
|
||||
fileSourceTestUtils.cleanup()
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.mapbox.mapboxsdk.testapp.storage;
|
||||
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.annotation.UiThreadTest;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import com.mapbox.mapboxsdk.storage.FileSource;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static junit.framework.TestCase.assertFalse;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class FileSourceTest {
|
||||
|
||||
private FileSource fileSource;
|
||||
|
||||
@Before
|
||||
@UiThreadTest
|
||||
public void setUp() {
|
||||
fileSource = FileSource.getInstance(InstrumentationRegistry.getContext());
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
public void testDefault() {
|
||||
assertFalse("FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
|
||||
@Test
|
||||
@UiThreadTest
|
||||
public void testActivateDeactivate() {
|
||||
assertFalse("1) FileSource should not be active", fileSource.isActivated());
|
||||
fileSource.activate();
|
||||
assertTrue("2) FileSource should be active", fileSource.isActivated());
|
||||
fileSource.deactivate();
|
||||
assertFalse("3) FileSource should not be active", fileSource.isActivated());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.mapbox.mapboxsdk.testapp.storage
|
||||
|
||||
import android.app.Activity
|
||||
import android.support.annotation.WorkerThread
|
||||
import com.mapbox.mapboxsdk.storage.FileSource
|
||||
import junit.framework.Assert
|
||||
import java.io.File
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
class FileSourceTestUtils(private val activity: Activity) {
|
||||
val originalPath = FileSource.getResourcesCachePath(activity)
|
||||
val testPath = "$originalPath/test"
|
||||
|
||||
fun setup() {
|
||||
val testFile = File(testPath)
|
||||
testFile.mkdirs()
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun cleanup() {
|
||||
val currentPath = FileSource.getResourcesCachePath(activity)
|
||||
if (currentPath != originalPath) {
|
||||
changePath(originalPath)
|
||||
}
|
||||
val testFile = File(testPath)
|
||||
if (testFile.exists()) {
|
||||
testFile.deleteRecursively()
|
||||
}
|
||||
}
|
||||
|
||||
@WorkerThread
|
||||
fun changePath(path: String) {
|
||||
val latch = CountDownLatch(1)
|
||||
activity.runOnUiThread {
|
||||
FileSource.setResourcesCachePath(
|
||||
activity,
|
||||
path,
|
||||
object : FileSource.ResourcesCachePathChangeCallback {
|
||||
override fun onSuccess(path: String?) {
|
||||
latch.countDown()
|
||||
}
|
||||
|
||||
override fun onError(message: String?) {
|
||||
Assert.fail("Resource path change failed - path: $path, message: $message")
|
||||
}
|
||||
})
|
||||
}
|
||||
latch.await()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user