summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/storage/FileSourceTestUtils.kt
blob: 040b288aa21488d88d667de908803f08e169739a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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()
  }
}