Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] add unit tests for #13947
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Apr 4, 2019
1 parent af74d00 commit 98d3f42
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 41 deletions.
Expand Up @@ -231,7 +231,7 @@ protected void onPostExecute(String[] paths) {
* @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 {
Expand Down
@@ -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()
}
}

This file was deleted.

@@ -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()
}
}

0 comments on commit 98d3f42

Please sign in to comment.