summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/storage/FileSourceTestUtils.kt
blob: c79d3b2752e62937bf91b6a8e3ccd77c5565fd62 (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
51
52
53
54
55
56
57
58
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"
  val testPath2 = "$originalPath/test2"

  private val paths = listOf(testPath, testPath2)

  fun setup() {
    for (path in paths) {
      val testFile = File(path)
      testFile.mkdirs()
    }
  }

  @WorkerThread
  fun cleanup() {
    val currentPath = FileSource.getResourcesCachePath(activity)
    if (currentPath != originalPath) {
      changePath(originalPath)
    }

    for (path in paths) {
      val testFile = File(path)
      if (testFile.exists()) {
        testFile.deleteRecursively()
      }
    }
  }

  @WorkerThread
  fun changePath(requestedPath: String) {
    val latch = CountDownLatch(1)
    activity.runOnUiThread {
      FileSource.setResourcesCachePath(
        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: $requestedPath, message: $message")
          }
        })
    }
    latch.await()
  }
}