summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/TransformTest.kt
blob: fddf7eeaff19d14e53fe8ad29dce4bcfc2fad462 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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: NativeMap
  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(), any()) } answers {}
    every { nativeMapView.easeTo(any(), any(), any(), any(), any(), any(), any()) } answers {}
    every { nativeMapView.flyTo(any(), 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, null) }
    verify { callback.onFinish() }
  }

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

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

    val expected = CameraPosition.DEFAULT
    val update = CameraUpdateFactory.newCameraPosition(expected)

    transform.moveCamera(mapboxMap, update, null) // Initialize camera position
    transform.moveCamera(mapboxMap, update, callback)

    verify(exactly = 1, verifyBlock = { nativeMapView.jumpTo(any(), any(), any(), any(), any()) })
    verify { callback.onFinish() }
  }

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

    every { mapView.addOnCameraDidChangeListener(any()) } answers { transform.onCameraDidChange(true) }
    every { mapView.removeOnCameraDidChangeListener(any()) } answers {}

    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.easeCamera(mapboxMap, update, 100, false, callback)

    verify { nativeMapView.easeTo(target, -1.0, -1.0, -1.0, null, 100, false) }
    verify { callback.onFinish() }
  }

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

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

    val expected = CameraPosition.DEFAULT
    val update = CameraUpdateFactory.newCameraPosition(expected)
    transform.moveCamera(mapboxMap, update, null)

    transform.easeCamera(mapboxMap, update, 100, false, callback)

    verify(exactly = 0, verifyBlock = { nativeMapView.easeTo(any(), any(), any(), any(), any(), any(), any()) })
    verify { callback.onFinish() }
  }

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

    every { mapView.addOnCameraDidChangeListener(any()) } answers { transform.onCameraDidChange(true) }
    every { mapView.removeOnCameraDidChangeListener(any()) } answers {}

    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.animateCamera(mapboxMap, update, 100, callback)

    verify { nativeMapView.flyTo(target, -1.0, -1.0, -1.0, null, 100) }
    verify { callback.onFinish() }
  }

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

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

    val expected = CameraPosition.DEFAULT
    val update = CameraUpdateFactory.newCameraPosition(expected)
    transform.moveCamera(mapboxMap, update, null)

    transform.animateCamera(mapboxMap, update, 100, callback)

    verify(exactly = 0, verifyBlock = { nativeMapView.flyTo(any(), any(), any(), any(), any(), any()) })
    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)
  }
}