summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/TransformTest.kt
blob: 8c985ca3a828294a4d307c64715f2ff15072eb36 (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
package com.mapbox.mapboxsdk.maps

import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.geometry.LatLng
import io.mockk.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class TransformTest {

  private lateinit var mapView: MapView
  private lateinit var nativeMapView: NativeMapView
  private lateinit var transform: Transform

  @Before
  fun setup() {
    val cameraChangeDispatcher = spyk<CameraChangeDispatcher>()
    mapView = mockk()
    nativeMapView = mockk()
    transform = Transform(mapView, nativeMapView, cameraChangeDispatcher)
    every { nativeMapView.isDestroyed } returns false
    every { nativeMapView.cameraPosition } returns CameraPosition.DEFAULT
    every { nativeMapView.cancelTransitions() } answers {}
    every { nativeMapView.jumpTo(any(), any(), any(), any()) } answers {}
    every { nativeMapView.flyTo(any(), any(), any(), any(), any()) } answers {}
    every { nativeMapView.minZoom = any() } answers {}
    every { nativeMapView.maxZoom = any() } answers {}
  }

  @Test
  fun testMoveCamera() {
    val mapboxMap = mockk<MapboxMap>()
    every { mapboxMap.cameraPosition } answers { CameraPosition.DEFAULT }

    val callback = mockk<MapboxMap.CancelableCallback>()
    every { callback.onFinish() } answers {}

    val target = LatLng(1.0, 2.0)
    val expected = CameraPosition.Builder().target(target).build()
    val update = CameraUpdateFactory.newCameraPosition(expected)
    transform.moveCamera(mapboxMap, update, callback)

    verify { nativeMapView.jumpTo(target, -1.0, -1.0, -1.0) }
    verify { callback.onFinish() }
  }

  @Test
  fun testMinZoom() {
    transform.minZoom = 10.0
    verify { nativeMapView.minZoom = 10.0 }
  }

  @Test
  fun testMaxZoom() {
    transform.maxZoom = 10.0
    verify { nativeMapView.maxZoom = 10.0 }
  }

  @Test
  fun testCancelNotInvokedFromOnFinish() {
    val slot = slot<MapView.OnCameraDidChangeListener>()
    every { mapView.addOnCameraDidChangeListener(capture(slot)) } answers { slot.captured.onCameraDidChange(true) }
    every { mapView.removeOnCameraDidChangeListener(any()) } answers {}
    // regression test for https://github.com/mapbox/mapbox-gl-native/issues/13735
    val mapboxMap = mockk<MapboxMap>()
    every { mapboxMap.cameraPosition } answers { CameraPosition.DEFAULT }

    val target = LatLng(1.0, 2.0)
    val expected = CameraPosition.Builder().target(target).build()

    val callback = object : MapboxMap.CancelableCallback {
      override fun onCancel() {
        throw IllegalStateException("onCancel shouldn't be called from onFinish")
      }

      override fun onFinish() {
        transform.animateCamera(mapboxMap, CameraUpdateFactory.newCameraPosition(expected), 500, null)
      }
    }
    transform.animateCamera(mapboxMap, CameraUpdateFactory.newCameraPosition(expected), 500, callback)
  }
}