summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt
blob: eb4e8c00e5a70a969d4c568383a5db9fe082e391 (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
184
185
186
187
188
189
package com.mapbox.mapboxsdk.maps

import android.graphics.PointF
import android.util.Pair
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.maps.MapGestureDetector.BOUND_REPEL_RATIO
import com.mapbox.mapboxsdk.utils.MathUtils
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class MapGestureDetectorTest {

  companion object {
    private val BOUNDS = LatLngBounds.Builder()
      .include(LatLng(10.0, -10.0))
      .include(LatLng(-10.0, 10.0))
      .build()
  }

  private lateinit var mapGestureDetector: MapGestureDetector
  private lateinit var transform: Transform
  private lateinit var projection: Projection
  private lateinit var uiSettings: UiSettings

  @Before
  fun setup() {
    transform = mockk(relaxed = true)
    projection = mockk(relaxed = true)
    uiSettings = mockk(relaxed = true)
    mapGestureDetector = MapGestureDetector(transform, projection, uiSettings, mockk(), mockk(), mockk())
  }

  @Test
  fun limitedOffset_returnWhenCenterOutside() {
    val outsideCoordinate = LatLng(25.0, 0.0)
    every { transform.getCenterCoordinate(false) } answers { outsideCoordinate }
    val limitedOffset =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 0f, 0f)
    assertNull(limitedOffset)
  }

  @Test
  fun limitedOffset_returnWhenWholeWorld() {
    val coordinate = LatLng(25.0, 0.0)
    every { transform.getCenterCoordinate(false) } answers { coordinate }
    val limitedOffset =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, LatLngBounds.world(), 0f, 0f)
    assertNull(limitedOffset)
  }

  @Test
  fun limitedOffset_referencePixelRepelledOfBound() {
    val coordinate = LatLng(10.0, 0.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(100f, 200f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(-100f, 300f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } answers { boundsCenterPoint }

    val expected = PointF(
      referencePixel.x + (boundsCenterPoint.x - referencePixel.x) / BOUND_REPEL_RATIO,
      referencePixel.y + (boundsCenterPoint.y - referencePixel.y) / BOUND_REPEL_RATIO)
    MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 0f, 0f)
    assertEquals(expected.x, referencePixel.x)
    assertEquals(expected.y, referencePixel.y)
  }

  @Test
  fun limitedOffset_noIntersection() {
    val coordinate = LatLng(1.0, -1.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(50f, 50f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(100f, 100f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } returns boundsCenterPoint
    every { projection.toScreenLocationRaw(BOUNDS.northWest) } returns PointF(0f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.northEast) } returns PointF(200f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.southEast) } returns PointF(200f, 200f)
    every { projection.toScreenLocationRaw(BOUNDS.southWest) } returns PointF(0f, 200f)
    val acceptableVector =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 25f, 30f)
    val target = PointF(referencePixel.x + 25f, referencePixel.y + 30f)
    assertEquals(Pair(target.x - referencePixel.x, target.y - referencePixel.y), acceptableVector)
  }

  @Test
  fun limitedOffset_northIntersection() {
    val coordinate = LatLng(1.0, -1.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(50f, 50f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(100f, 100f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } returns boundsCenterPoint
    every { projection.toScreenLocationRaw(BOUNDS.northWest) } returns PointF(0f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.northEast) } returns PointF(200f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.southEast) } returns PointF(200f, 200f)
    every { projection.toScreenLocationRaw(BOUNDS.southWest) } returns PointF(0f, 200f)
    val acceptableVector =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 25f, -200f)
    val target = PointF(referencePixel.x + 25f, referencePixel.y - 200f)
    val closestPoint = MathUtils.findSegmentsIntersection(
      referencePixel,
      target,
      projection.toScreenLocationRaw(BOUNDS.northWest),
      projection.toScreenLocationRaw(BOUNDS.northEast)
    )
    assertEquals(Pair(closestPoint!!.x - referencePixel.x, closestPoint.y - referencePixel.y), acceptableVector)
  }

  @Test
  fun limitedOffset_eastIntersection() {
    val coordinate = LatLng(1.0, -1.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(50f, 50f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(100f, 100f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } returns boundsCenterPoint
    every { projection.toScreenLocationRaw(BOUNDS.northWest) } returns PointF(0f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.northEast) } returns PointF(200f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.southEast) } returns PointF(200f, 200f)
    every { projection.toScreenLocationRaw(BOUNDS.southWest) } returns PointF(0f, 200f)
    val acceptableVector =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 200f, 25f)
    val target = PointF(referencePixel.x + 200f, referencePixel.y + 25f)
    val closestPoint = MathUtils.findSegmentsIntersection(
      referencePixel,
      target,
      projection.toScreenLocationRaw(BOUNDS.northEast),
      projection.toScreenLocationRaw(BOUNDS.southEast)
    )
    assertEquals(Pair(closestPoint!!.x - referencePixel.x, closestPoint.y - referencePixel.y), acceptableVector)
  }

  @Test
  fun limitedOffset_southIntersection() {
    val coordinate = LatLng(1.0, -1.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(50f, 50f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(100f, 100f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } returns boundsCenterPoint
    every { projection.toScreenLocationRaw(BOUNDS.northWest) } returns PointF(0f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.northEast) } returns PointF(200f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.southEast) } returns PointF(200f, 200f)
    every { projection.toScreenLocationRaw(BOUNDS.southWest) } returns PointF(0f, 200f)
    val acceptableVector =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, 25f, 200f)
    val target = PointF(referencePixel.x + 25f, referencePixel.y + 200f)
    val closestPoint = MathUtils.findSegmentsIntersection(
      referencePixel,
      target,
      projection.toScreenLocationRaw(BOUNDS.southEast),
      projection.toScreenLocationRaw(BOUNDS.southWest)
    )
    assertEquals(Pair(closestPoint!!.x - referencePixel.x, closestPoint.y - referencePixel.y), acceptableVector)
  }

  @Test
  fun limitedOffset_westIntersection() {
    val coordinate = LatLng(1.0, -1.0)
    every { transform.getCenterCoordinate(false) } returns coordinate
    val referencePixel = PointF(50f, 50f)
    every { projection.toScreenLocationRaw(coordinate) } returns referencePixel
    val boundsCenterPoint = PointF(100f, 100f)
    every { projection.toScreenLocationRaw(BOUNDS.center) } returns boundsCenterPoint
    every { projection.toScreenLocationRaw(BOUNDS.northWest) } returns PointF(0f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.northEast) } returns PointF(200f, 0f)
    every { projection.toScreenLocationRaw(BOUNDS.southEast) } returns PointF(200f, 200f)
    every { projection.toScreenLocationRaw(BOUNDS.southWest) } returns PointF(0f, 200f)
    val acceptableVector =
      MapGestureDetector.findBoundsLimitedOffset(projection, transform, BOUNDS, -200f, 25f)
    val target = PointF(referencePixel.x - 200f, referencePixel.y + 25f)
    val closestPoint = MathUtils.findSegmentsIntersection(
      referencePixel,
      target,
      projection.toScreenLocationRaw(BOUNDS.southWest),
      projection.toScreenLocationRaw(BOUNDS.northWest)
    )
    assertEquals(Pair(closestPoint!!.x - referencePixel.x, closestPoint.y - referencePixel.y), acceptableVector)
  }
}