summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/maps/NativeMapViewTest.kt
blob: 409d956a132492953eb1c4c3d864760087eb8508 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.mapbox.mapboxsdk.maps

import android.content.Context
import android.graphics.PointF
import android.support.test.InstrumentationRegistry
import android.support.test.annotation.UiThreadTest
import android.support.test.runner.AndroidJUnit4
import com.mapbox.mapboxsdk.AppCenter
import com.mapbox.mapboxsdk.camera.CameraPosition
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.geometry.ProjectedMeters
import com.mapbox.mapboxsdk.maps.renderer.MapRenderer
import com.mapbox.mapboxsdk.style.layers.TransitionOptions
import com.mapbox.mapboxsdk.testapp.utils.TestConstants
import junit.framework.Assert.*
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class NativeMapViewTest : AppCenter() {

    private lateinit var nativeMapView: NativeMap

    companion object {
        const val DELTA = 0.000001
        const val DELTA_BIG = 1.0
        const val BEARING_TEST = 60.0
        const val PITCH_TEST = 40.0
        const val ZOOM_TEST = 16.0
        const val WIDTH = 500
        const val HEIGHT = WIDTH
        val LATLNG_TEST = LatLng(12.0, 34.0)
    }

    @Before
    @UiThreadTest
    fun before() {
        val context = InstrumentationRegistry.getContext()
        nativeMapView = NativeMapView(context, 2.0f, false, null, null, DummyRenderer(context))
        nativeMapView.resizeView(WIDTH, HEIGHT)
    }

    @After
    @UiThreadTest
    fun after() {
        nativeMapView.destroy()
    }

    @Test
    @UiThreadTest
    fun testSetStyleUrl() {
        val expected = Style.DARK
        nativeMapView.styleUri = expected
        val actual = nativeMapView.styleUri
        assertEquals("Style URL should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testSetStyleJson() {
        val expected = "{}"
        nativeMapView.styleJson = expected
        val actual = nativeMapView.styleJson
        assertEquals("Style JSON should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testBearing() {
        val expected = BEARING_TEST
        nativeMapView.setBearing(expected, 0)
        val actual = nativeMapView.bearing
        assertEquals("Bearing should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testLatLng() {
        val expected = LATLNG_TEST
        nativeMapView.setLatLng(expected, 0)
        val actual = nativeMapView.latLng
        assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA)
        assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA)
    }

    @Test
    @UiThreadTest
    fun testLatLngDefault() {
        val expected = LatLng()
        val actual = nativeMapView.latLng
        assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA)
        assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA)
    }


    @Test
    @UiThreadTest
    fun testBearingDefault() {
        val expected = 0.0
        val actual = nativeMapView.bearing
        assertEquals("Bearing should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testPitch() {
        val expected = PITCH_TEST
        nativeMapView.setPitch(expected, 0)
        val actual = nativeMapView.pitch
        assertEquals("Pitch should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testPitchDefault() {
        val expected = 0.0
        val actual = nativeMapView.pitch
        assertEquals("Pitch should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testZoom() {
        val expected = ZOOM_TEST
        nativeMapView.setZoom(expected, PointF(0.0f, 0.0f), 0)
        val actual = nativeMapView.zoom
        assertEquals("Zoom should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testZoomDefault() {
        val expected = 0.0
        val actual = nativeMapView.zoom
        assertEquals("Zoom should match", expected, actual, DELTA)
    }

    @Test
    @UiThreadTest
    fun testJumpTo() {
        val expected = CameraPosition.Builder()
                .bearing(BEARING_TEST)
                .target(LATLNG_TEST)
                .tilt(PITCH_TEST)
                .zoom(ZOOM_TEST)
                .build()
        nativeMapView.jumpTo(LATLNG_TEST, ZOOM_TEST, PITCH_TEST, BEARING_TEST)
        val actual = nativeMapView.cameraPosition
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, DELTA)
        assertEquals("Bearing should match", expected.bearing, actual.bearing, DELTA)
        assertEquals("Pitch should match", expected.tilt, actual.tilt, DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, DELTA)
    }

    @Test
    @UiThreadTest
    fun testLatLngForPixel() {
        val expected = LATLNG_TEST
        nativeMapView.setLatLng(LATLNG_TEST,0)
        val actual = nativeMapView.latLngForPixel(
                PointF((WIDTH / 2).toFloat(), (HEIGHT / 2).toFloat())
        )
        assertEquals("Latitude should match", expected.latitude, actual.latitude, DELTA_BIG)
        assertEquals("Longitude should match", expected.longitude, actual.longitude, DELTA_BIG)
    }

    @Test
    @UiThreadTest
    fun testPixelForLatLng() {
        val expected = PointF((WIDTH / 2).toFloat(), (HEIGHT / 2).toFloat())
        nativeMapView.setLatLng(LATLNG_TEST, 0)
        val actual = nativeMapView.pixelForLatLng(LATLNG_TEST)
        assertEquals("X should match", expected.x.toDouble(), actual.x.toDouble(), DELTA_BIG)
        assertEquals("Y should match", expected.y.toDouble(), actual.y.toDouble(), DELTA_BIG)
    }

    @Test
    @UiThreadTest
    fun testPrefetchTilesTrue() {
        val expected = true
        nativeMapView.prefetchTiles = true
        val actual = nativeMapView.prefetchTiles
        assertEquals("Flag should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testPrefetchTilesFalse() {
        val expected = false
        nativeMapView.prefetchTiles = false
        val actual = nativeMapView.prefetchTiles
        assertEquals("Flag should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testPrefetchTilesDefault() {
        val expected = true
        val actual = nativeMapView.prefetchTiles
        assertEquals("Flag should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testSetContentPadding() {
        val expected = floatArrayOf(1.0f, 2.0f, 3.0f, 4.0f)
        nativeMapView.contentPadding = expected
        val actual = nativeMapView.contentPadding
        assertEquals("Left should match", expected[0], actual[0])
        assertEquals("Top should match", expected[1], actual[1])
        assertEquals("Right should match", expected[2], actual[2])
        assertEquals("Bottom should match", expected[3], actual[3])
    }

    @Test
    @UiThreadTest
    fun testSetMinZoom() {
        val expected = 12.0
        nativeMapView.minZoom = expected
        val actual = nativeMapView.minZoom
        assertEquals("Min zoom should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testSetMaxZoom() {
        val expected = 12.0
        nativeMapView.maxZoom = expected
        val actual = nativeMapView.maxZoom
        assertEquals("Max zoom should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testGetProjectedMetersAtLatitude() {
        val expected = 38986.83510557766
        val actual = nativeMapView.getMetersPerPixelAtLatitude(5.0)
        assertEquals("Get projected meters should match", expected, actual)
    }

    @Test
    @UiThreadTest
    fun testLatLngForProjectedMeters() {
        val expected = LatLng(0.01796630538796444, 0.02694945852363162)
        val actual = nativeMapView.latLngForProjectedMeters(ProjectedMeters(2000.0, 3000.0))
        assertEquals("Lat for projected meters", expected.latitude, actual.latitude, DELTA)
        assertEquals("Lng for projected meters", expected.longitude, actual.longitude, DELTA)
    }

    @Test
    @UiThreadTest
    fun testFlyTo() {
        val expected = CameraPosition.Builder()
                .zoom(12.0)
                .tilt(30.0)
                .target(LatLng(12.0, 14.0))
                .bearing(20.0)
                .build()
        nativeMapView.flyTo(expected.target, expected.zoom, expected.bearing, expected.tilt, 0)
        val actual = nativeMapView.cameraPosition
        assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA)
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA)
    }

    @Test
    @UiThreadTest
    fun testEaseTo() {
        val expected = CameraPosition.Builder()
                .zoom(12.0)
                .tilt(30.0)
                .target(LatLng(12.0, 14.0))
                .bearing(20.0)
                .build()
        nativeMapView.easeTo(expected.target, expected.zoom, expected.bearing, expected.tilt, 0, false)
        val actual = nativeMapView.cameraPosition
        assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA)
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA)
    }

    @Test
    @UiThreadTest
    fun testResetPosition() {
        val expected = CameraPosition.Builder()
                .zoom(0.0)
                .tilt(0.0)
                .target(LatLng(0.0, 0.0))
                .bearing(0.0)
                .build()
        nativeMapView.jumpTo(LatLng(1.0, 2.0), 12.0, 23.0, 1.0)
        nativeMapView.resetPosition()
        val actual = nativeMapView.cameraPosition
        assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA)
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA)
    }

    @Test
    @UiThreadTest
    fun testGetCameraForLatLngBounds() {
        val expected = CameraPosition.Builder()
                .zoom(3.5258764777024005)
                .tilt(0.0)
                .target(LatLng(23.182767623652808, 13.999999999994088))
                .bearing(0.0)
                .build()
        val actual = nativeMapView.getCameraForLatLngBounds(
                LatLngBounds.from(30.0, 16.0, 16.0, 12.0),
                intArrayOf(0, 0, 0, 0),
                0.0,
                0.0
        )
        assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA)
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA)
    }

    @Test
    @UiThreadTest
    fun testMoveBy() {
        val expected = CameraPosition.Builder()
                .zoom(0.0)
                .tilt(0.0)
                .target(LatLng(4.21494310024160, -4.218749958739409))
                .bearing(0.0)
                .build()
        nativeMapView.moveBy(12.0, 12.0, 0)
        val actual = nativeMapView.cameraPosition
        assertEquals("Bearing should match", expected.bearing, actual.bearing, TestConstants.BEARING_DELTA)
        assertEquals("Latitude should match", expected.target.latitude, actual.target.latitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Longitude should match", expected.target.longitude, actual.target.longitude, TestConstants.LAT_LNG_DELTA)
        assertEquals("Tilt should match", expected.tilt, actual.tilt, TestConstants.TILT_DELTA)
        assertEquals("Zoom should match", expected.zoom, actual.zoom, TestConstants.ZOOM_DELTA)
    }

    @Test
    @UiThreadTest
    fun testTransitionOptions() {
        val transitionOptions = TransitionOptions(500, 500)
        nativeMapView.transitionOptions = transitionOptions
        assertTrue(transitionOptions.isEnablePlacementTransitions)
        assertEquals(transitionOptions, nativeMapView.transitionOptions)
    }

    @Test
    @UiThreadTest
    fun testTransitionOptions_disablePlacementTransitions() {
        val transitionOptions = TransitionOptions(500, 500, false)
        nativeMapView.transitionOptions = transitionOptions
        assertFalse(transitionOptions.isEnablePlacementTransitions)
        assertEquals(transitionOptions, nativeMapView.transitionOptions)
    }

    class DummyRenderer(context: Context) : MapRenderer(context, null) {

        override fun requestRender() {
            //no-op
        }

        override fun queueEvent(runnable: Runnable?) {
            //no-op
        }
    }
}