summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/storage/FileSourceStandaloneTest.kt
blob: 08a15b0c54aee7cbaff9f7dab8a83f5543a0a385 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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.FeatureOverviewActivity
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 {

  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))

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