summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/MapGestureDetectorTest.kt
blob: 659c2e1b392b6d2d69691695c35b176f5ac0e365 (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
190
191
192
193
package com.mapbox.mapboxsdk.maps

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.matcher.ViewMatchers.withId
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.constants.MapboxConstants
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.GesturesUiTestUtils.move
import com.mapbox.mapboxsdk.maps.GesturesUiTestUtils.quickScale
import com.mapbox.mapboxsdk.testapp.R
import com.mapbox.mapboxsdk.testapp.activity.BaseTest
import com.mapbox.mapboxsdk.testapp.activity.maplayout.SimpleMapActivity
import org.junit.Assert
import org.junit.Before
import org.junit.Test

class MapGestureDetectorTest : BaseTest() {
  override fun getActivityClass() = SimpleMapActivity::class.java

  private var maxWidth: Int = 0
  private var maxHeight: Int = 0

  @Before
  fun setup() {
    maxWidth = mapView.width
    maxHeight = mapView.height
  }

  @Test
  fun sanity_quickZoom() {
    validateTestSetup()
    var initialZoom: Double? = null
    rule.runOnUiThread {
      initialZoom = mapboxMap.cameraPosition.zoom
    }
    onView(withId(R.id.mapView)).perform(quickScale(maxHeight / 2f, withVelocity = false))
    rule.runOnUiThread {
      Assert.assertTrue(mapboxMap.cameraPosition.zoom > initialZoom!!)
    }
  }

  @Test
  fun quickZoomDisabled_phantomQuickZoom_moveStillEnabled_15091() {
    // regression test for https://github.com/mapbox/mapbox-gl-native/issues/15091
    validateTestSetup()
    var initialCameraPosition: CameraPosition? = null
    rule.runOnUiThread {
      // zoom in so we can move vertically
      mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(4.0))
      initialCameraPosition = mapboxMap.cameraPosition
      mapboxMap.uiSettings.isQuickZoomGesturesEnabled = false
    }

    onView(withId(R.id.mapView)).perform(quickScale(maxHeight / 2f))
    rule.runOnUiThread {
      // camera did not move
      Assert.assertEquals(initialCameraPosition!!, mapboxMap.cameraPosition)
    }

    // move to expected target
    onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false))
    rule.runOnUiThread {
      Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0)
      Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0)
    }
  }

  @Test
  fun quickZoom_doNotMove_14227() {
    // test for https://github.com/mapbox/mapbox-gl-native/issues/14227
    validateTestSetup()
    var initialTarget: LatLng? = null
    rule.runOnUiThread {
      initialTarget = mapboxMap.cameraPosition.target
    }

    onView(withId(R.id.mapView)).perform(quickScale(maxHeight / 2f))
    rule.runOnUiThread {
      // camera did not move
      Assert.assertEquals(initialTarget!!.latitude, mapboxMap.cameraPosition.target.latitude, 1.0)
      Assert.assertEquals(initialTarget!!.longitude, mapboxMap.cameraPosition.target.longitude, 1.0)
    }
  }

  @Test
  fun quickZoom_interrupted_moveStillEnabled_14598() {
    // test for https://github.com/mapbox/mapbox-gl-native/issues/14598
    validateTestSetup()
    onView(withId(R.id.mapView)).perform(quickScale(maxHeight / 2f, interrupt = true))

    var initialCameraPosition: CameraPosition? = null
    rule.runOnUiThread {
      // zoom in so we can move vertically
      mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(4.0))
      initialCameraPosition = mapboxMap.cameraPosition
      mapboxMap.uiSettings.isQuickZoomGesturesEnabled = false
    }

    // move to expected target
    onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false))
    rule.runOnUiThread {
      Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0)
      Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0)
    }
  }

  @Test
  fun quickZoom_ignoreDoubleTap() {
    // test for https://github.com/mapbox/mapbox-gl-native/issues/14013
    validateTestSetup()
    var initialZoom: Double? = null
    rule.runOnUiThread {
      mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(2.0))
      initialZoom = mapboxMap.cameraPosition.zoom
    }
    onView(withId(R.id.mapView)).perform(quickScale(-(mapboxMap.gesturesManager.standardScaleGestureDetector.spanSinceStartThreshold * 2), withVelocity = false, duration = 1000L))
    R.id.mapView.loopFor(MapboxConstants.ANIMATION_DURATION.toLong())
    rule.runOnUiThread {
      Assert.assertTrue(mapboxMap.cameraPosition.zoom < initialZoom!!)
    }
  }

  @Test
  fun doubleTap_minimalMovement() {
    validateTestSetup()
    var initialZoom: Double? = null
    rule.runOnUiThread {
      initialZoom = mapboxMap.cameraPosition.zoom
    }
    onView(withId(R.id.mapView)).perform(quickScale(mapboxMap.gesturesManager.standardScaleGestureDetector.spanSinceStartThreshold / 2, withVelocity = false, duration = 50L))
    R.id.mapView.loopFor(MapboxConstants.ANIMATION_DURATION.toLong())
    rule.runOnUiThread {
      Assert.assertEquals(initialZoom!! + 1, mapboxMap.cameraPosition.zoom, 0.1)
    }
  }

  @Test
  fun doubleTap_overMaxThreshold_ignore_14013() {
    // test for https://github.com/mapbox/mapbox-gl-native/issues/14013
    validateTestSetup()
    var initialZoom: Double? = null
    rule.runOnUiThread {
      initialZoom = mapboxMap.cameraPosition.zoom
      mapboxMap.uiSettings.isQuickZoomGesturesEnabled = false
    }
    onView(withId(R.id.mapView)).perform(quickScale(mapboxMap.gesturesManager.standardScaleGestureDetector.spanSinceStartThreshold * 2, withVelocity = false, duration = 50L))
    R.id.mapView.loopFor(MapboxConstants.ANIMATION_DURATION.toLong())
    rule.runOnUiThread {
      Assert.assertEquals(initialZoom!!, mapboxMap.cameraPosition.zoom, 0.01)
    }
  }

  @Test
  fun doubleTap_interrupted_moveStillEnabled() {
    validateTestSetup()

    rule.runOnUiThread {
      mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(4.0))
    }

    onView(withId(R.id.mapView)).perform(quickScale(mapboxMap.gesturesManager.standardScaleGestureDetector.spanSinceStartThreshold / 2, withVelocity = false, duration = 50L, interrupt = true))

    var initialCameraPosition: CameraPosition? = null
    rule.runOnUiThread {
      // zoom in so we can move vertically
      mapboxMap.moveCamera(CameraUpdateFactory.zoomTo(4.0))
      initialCameraPosition = mapboxMap.cameraPosition
      mapboxMap.uiSettings.isQuickZoomGesturesEnabled = false
    }

    // move to expected target
    onView(withId(R.id.mapView)).perform(move(-maxWidth / 2f, -maxHeight / 2f, withVelocity = false))
    rule.runOnUiThread {
      Assert.assertNotEquals(initialCameraPosition!!.target.latitude, mapboxMap.cameraPosition.target.latitude, 1.0)
      Assert.assertNotEquals(initialCameraPosition!!.target.longitude, mapboxMap.cameraPosition.target.longitude, 1.0)
    }
  }

  @Test
  fun quickZoom_roundTripping() {
    validateTestSetup()
    rule.runOnUiThread {
      mapboxMap.moveCamera(CameraUpdateFactory.newLatLngZoom(LatLng(51.0, 16.0), 3.0))
    }
    onView(withId(R.id.mapView)).perform(quickScale(300f, withVelocity = false, duration = 750L))
    onView(withId(R.id.mapView)).perform(quickScale(-300f, withVelocity = false, duration = 750L))

    rule.runOnUiThread {
      Assert.assertEquals(3.0, mapboxMap.cameraPosition.zoom, 0.0001)
    }
  }
}