summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java
blob: e072f07fb9a01a84c66179d59c8622b66d4b83c5 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package com.mapbox.mapboxsdk.geometry;

import android.os.Parcelable;

import com.mapbox.mapboxsdk.constants.GeometryConstants;
import com.mapbox.mapboxsdk.exceptions.InvalidLatLngBoundsException;
import com.mapbox.mapboxsdk.utils.MockParcel;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.ArrayList;
import java.util.List;

import static junit.framework.Assert.assertNotNull;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class LatLngBoundsTest {

  private static final double DELTA = 1e-15;

  private LatLngBounds latLngBounds;
  private static final LatLng LAT_LNG_NULL_ISLAND = new LatLng(0, 0);
  private static final LatLng LAT_LNG_NOT_NULL_ISLAND = new LatLng(2, 2);

  @Before
  public void beforeTest() {
    latLngBounds = new LatLngBounds.Builder()
      .include(LAT_LNG_NULL_ISLAND)
      .include(LAT_LNG_NOT_NULL_ISLAND)
      .build();
  }

  @Test
  public void testSanity() {
    LatLngBounds.Builder latLngBoundsBuilder = new LatLngBounds.Builder();
    latLngBoundsBuilder.include(LAT_LNG_NULL_ISLAND).include(LAT_LNG_NOT_NULL_ISLAND);
    assertNotNull("latLng  should not be null", latLngBoundsBuilder.build());
  }

  @Test(expected = InvalidLatLngBoundsException.class)
  public void noLatLngs() {
    new LatLngBounds.Builder().build();
  }

  @Test(expected = InvalidLatLngBoundsException.class)
  public void oneLatLngs() {
    new LatLngBounds.Builder().include(LAT_LNG_NULL_ISLAND).build();
  }

  @Test
  public void latitiudeSpan() {
    assertEquals("Span should be the same", 2, latLngBounds.getLatitudeSpan(), DELTA);
  }

  @Test
  public void longitudeSpan() {
    assertEquals("Span should be the same", 2, latLngBounds.getLongitudeSpan(), DELTA);
  }

  @Test
  public void coordinateSpan() {
    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be the same", new LatLngSpan(2, 2), latLngSpan);
  }

  @Test
  public void dateLineSpanBuilder1() {
    latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, -170))
      .include(new LatLng(-10, 170))
      .build();

    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 20),
      latLngSpan);
  }

  @Test
  public void dateLineSpanBuilder2() {
    latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(-10, -170))
      .include(new LatLng(10, 170))
      .build();

    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 20),
      latLngSpan);
  }

  @Test
  public void dateLineSpanFrom1() {
    latLngBounds = LatLngBounds.from(10, -170, -10, 170);
    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 20),
      latLngSpan);
  }

  @Test
  public void dateLineSpanFrom2() {
    latLngBounds = LatLngBounds.from(10, 170, -10, -170);
    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 340),
      latLngSpan);
  }

  @Test
  public void zeroLongitudeSpan() {
    latLngBounds = LatLngBounds.from(10, 10, -10, 10);
    LatLngSpan latLngSpan = latLngBounds.getSpan();
    assertEquals("LatLngSpan should be shortest distance", new LatLngSpan(20, 0),
      latLngSpan);
  }

  @Test
  public void nearDateLineCenter1() {
    latLngBounds = LatLngBounds.from(10, -175, -10, 165);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, 175), center);
  }

  @Test
  public void nearDateLineCenter2() {
    latLngBounds = LatLngBounds.from(10, -165, -10, 175);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, -175), center);
  }

  @Test
  public void nearDateLineCenter3() {
    latLngBounds = LatLngBounds.from(10, -170, -10, 170);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, -180), center);
  }

  @Test
  public void nearDateLineCenter4() {
    latLngBounds = LatLngBounds.from(10, -180, -10, 0);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, 90), center);
  }

  @Test
  public void nearDateLineCenter5() {
    latLngBounds = LatLngBounds.from(10, 180, -10, 0);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, 90), center);
  }

  @Test
  public void centerForBoundsWithSameLongitude() {
    latLngBounds = LatLngBounds.from(10, 10, -10, 10);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(0, 10), center);
  }

  @Test
  public void centerForBoundsWithSameLatitude() {
    latLngBounds = LatLngBounds.from(10, 10, 10, -10);
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(10, 0), center);
  }

  @Test
  public void center() {
    LatLng center = latLngBounds.getCenter();
    assertEquals("Center should match", new LatLng(1, 1), center);
  }

  @Test
  public void notEmptySpan() {
    latLngBounds = new LatLngBounds.Builder()
      .include(LAT_LNG_NOT_NULL_ISLAND)
      .include(LAT_LNG_NULL_ISLAND)
      .build();
    assertFalse("Should not be empty", latLngBounds.isEmptySpan());
  }

  @Test
  public void toLatLngs() {
    latLngBounds = new LatLngBounds.Builder()
      .include(LAT_LNG_NOT_NULL_ISLAND)
      .include(LAT_LNG_NULL_ISLAND)
      .build();

    assertArrayEquals("LatLngs should match",
      new LatLng[] {LAT_LNG_NOT_NULL_ISLAND, LAT_LNG_NULL_ISLAND},
      latLngBounds.toLatLngs());
  }

  @Test
  public void include() {
    assertTrue("LatLng should be included", latLngBounds.contains(new LatLng(1, 1)));
  }

  @Test
  public void includes() {
    List<LatLng> points = new ArrayList<>();
    points.add(LAT_LNG_NULL_ISLAND);
    points.add(LAT_LNG_NOT_NULL_ISLAND);

    LatLngBounds latLngBounds1 = new LatLngBounds.Builder()
      .includes(points)
      .build();

    LatLngBounds latLngBounds2 = new LatLngBounds.Builder()
      .include(LAT_LNG_NULL_ISLAND)
      .include(LAT_LNG_NOT_NULL_ISLAND)
      .build();

    assertEquals("LatLngBounds should match", latLngBounds1, latLngBounds2);
  }

  @Test
  public void includesOrderDoesNotMatter() {
    LatLngBounds sameLongitudeFirst = new LatLngBounds.Builder()
      .include(new LatLng(50, 10))  // southWest
      .include(new LatLng(60, 10))
      .include(new LatLng(60, 20))  // northEast
      .include(new LatLng(50, 20))
      .include(new LatLng(50, 10))  // southWest again
      .build();

    LatLngBounds sameLatitudeFirst = new LatLngBounds.Builder()
      .include(new LatLng(50, 20))
      .include(new LatLng(50, 10))  // southWest
      .include(new LatLng(60, 10))
      .include(new LatLng(60, 20))  // northEast
      .include(new LatLng(50, 20))
      .build();

    assertEquals(sameLatitudeFirst, sameLongitudeFirst);
  }

  @Test
  public void includesOverDateline1() {

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, -170))
      .include(new LatLng(-10, -175))
      .include(new LatLng(0, 170))
      .build();

    assertEquals("LatLngSpan should be the same",
      new LatLngSpan(20, 20), latLngBounds.getSpan());
  }

  @Test
  public void includesOverDateline2() {

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, 170))
      .include(new LatLng(-10, 175))
      .include(new LatLng(0, -170))
      .build();

    assertEquals("LatLngSpan should be the same",
      new LatLngSpan(20, 20), latLngBounds.getSpan());
  }

  @Test
  public void includesOverDateline3() {

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, 170))
      .include(new LatLng(-10, -170))
      .include(new LatLng(0, -180))
      .include(new LatLng(5, 180))
      .build();

    assertEquals("LatLngSpan should be the same",
      new LatLngSpan(20, 20), latLngBounds.getSpan());
  }

  @Test
  public void containsNot() {
    assertFalse("LatLng should not be included", latLngBounds.contains(new LatLng(3, 1)));
  }

  @Test
  public void containsBoundsInWorld() {
    assertTrue("LatLngBounds should be contained in the world", LatLngBounds.world().contains(latLngBounds));
  }

  @Test
  public void worldSpan() {
    assertEquals("LatLngBounds world span should be 180, 360",
      GeometryConstants.LATITUDE_SPAN, LatLngBounds.world().getLatitudeSpan(), DELTA);
    assertEquals("LatLngBounds world span should be 180, 360",
      GeometryConstants.LONGITUDE_SPAN, LatLngBounds.world().getLongitudeSpan(), DELTA);
  }

  @Test
  public void emptySpan() {
    LatLngBounds latLngBounds = LatLngBounds.from(GeometryConstants.MIN_LATITUDE, GeometryConstants.MAX_LONGITUDE,
      GeometryConstants.MIN_LATITUDE, GeometryConstants.MAX_LONGITUDE);
    assertTrue("LatLngBounds empty span", latLngBounds.isEmptySpan());
  }

  @Test
  public void containsBounds() {
    LatLngBounds inner = new LatLngBounds.Builder()
      .include(new LatLng(-5, -5))
      .include(new LatLng(5, 5))
      .build();
    LatLngBounds outer = new LatLngBounds.Builder()
      .include(new LatLng(-10, -10))
      .include(new LatLng(10, 10))
      .build();
    assertTrue(outer.contains(inner));
    assertFalse(inner.contains(outer));
  }

  @Test
  public void testHashCode() {
    assertEquals(2147483647, latLngBounds.hashCode(), -1946419200);
  }

  @Test
  public void equality() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(LAT_LNG_NULL_ISLAND)
      .include(LAT_LNG_NOT_NULL_ISLAND)
      .build();
    assertEquals("equality should match", this.latLngBounds, latLngBounds);
    assertEquals("not equal to a different object type", this.latLngBounds.equals(LAT_LNG_NOT_NULL_ISLAND), false);
  }

  @Test
  public void testToString() {
    assertEquals(latLngBounds.toString(), "N:2.0; E:2.0; S:0.0; W:0.0");
  }

  @Test
  public void intersect() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(1, 1))
      .include(LAT_LNG_NULL_ISLAND)
      .build();
    assertEquals("intersect should match", latLngBounds, latLngBounds.intersect(this.latLngBounds.getLatNorth(),
      this.latLngBounds.getLonEast(), this.latLngBounds.getLatSouth(), this.latLngBounds.getLonWest()));
  }

  @Test
  public void intersectNot() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, 10))
      .include(new LatLng(9, 8))
      .build();
    assertNull(latLngBounds.intersect(this.latLngBounds));
  }

  @Test
  public void innerUnion() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(1, 1))
      .include(LAT_LNG_NULL_ISLAND)
      .build();
    assertEquals("union should match", latLngBounds, latLngBounds.intersect(this.latLngBounds));
  }

  @Test
  public void outerUnion() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, 10))
      .include(new LatLng(9, 8))
      .build();
    assertEquals("outer union should match",
      latLngBounds.union(this.latLngBounds),
      new LatLngBounds.Builder()
        .include(new LatLng(10, 10))
        .include(LAT_LNG_NULL_ISLAND)
        .build());
  }

  @Test
  public void unionOverDateLine() {
    LatLngBounds latLngBounds1 = new LatLngBounds.Builder()
      .include(new LatLng(10, 170))
      .include(new LatLng(0, 160))
      .build();

    LatLngBounds latLngBounds2 = new LatLngBounds.Builder()
      .include(new LatLng(0, -170))
      .include(new LatLng(-10, -160))
      .build();

    assertEquals("outer union should match",
      latLngBounds1.union(latLngBounds2),
      new LatLngBounds.Builder()
        .include(new LatLng(10, 160))
        .include(new LatLng(-10, -160))
        .build());
  }


  @Test
  public void northWest() {
    double minLat = 5;
    double minLon = 6;
    double maxLat = 20;
    double maxLon = 21;

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(minLat, minLon))
      .include(new LatLng(maxLat, maxLon))
      .build();

    assertEquals("NorthWest should match", latLngBounds.getNorthWest(), new LatLng(maxLat, minLon));
  }

  @Test
  public void southWest() {
    double minLat = 5;
    double minLon = 6;
    double maxLat = 20;
    double maxLon = 21;

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(minLat, minLon))
      .include(new LatLng(maxLat, maxLon))
      .build();

    assertEquals("SouthWest should match", latLngBounds.getSouthWest(), new LatLng(minLat, minLon));
  }

  @Test
  public void northEast() {
    double minLat = 5;
    double minLon = 6;
    double maxLat = 20;
    double maxLon = 21;

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(minLat, minLon))
      .include(new LatLng(maxLat, maxLon))
      .build();

    assertEquals("NorthEast should match", latLngBounds.getNorthEast(), new LatLng(maxLat, maxLon));
  }

  @Test
  public void southEast() {
    double minLat = 5;
    double minLon = 6;
    double maxLat = 20;
    double maxLon = 21;

    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(minLat, minLon))
      .include(new LatLng(maxLat, maxLon))
      .build();

    assertEquals("SouthEast should match", latLngBounds.getSouthEast(), new LatLng(minLat, maxLon));
  }

  @Test
  public void testParcelable() {
    LatLngBounds latLngBounds = new LatLngBounds.Builder()
      .include(new LatLng(10, 10))
      .include(new LatLng(9, 8))
      .build();
    Parcelable parcel = MockParcel.obtain(latLngBounds);
    assertEquals("Parcel should match original object", parcel, latLngBounds);
  }

  @Test
  public void fromTileID() {
    LatLngBounds bounds = LatLngBounds.from(0, 0, 0);
    assertEquals(GeometryConstants.MIN_LONGITUDE, bounds.getLonWest(), DELTA);
    assertEquals(GeometryConstants.MIN_MERCATOR_LATITUDE, bounds.getLatSouth(), DELTA);
    assertEquals(GeometryConstants.MAX_LONGITUDE, bounds.getLonEast(), DELTA);
    assertEquals(GeometryConstants.MAX_MERCATOR_LATITUDE, bounds.getLatNorth(), DELTA);

    bounds = LatLngBounds.from(10, 288, 385);
    assertEquals(-78.75, bounds.getLonWest(), DELTA);
    assertEquals(40.446947059600497, bounds.getLatSouth(), DELTA);
    assertEquals(-78.3984375, bounds.getLonEast(), DELTA);
    assertEquals(40.713955826286039, bounds.getLatNorth(), DELTA);

  }

  @Rule
  public final ExpectedException exception = ExpectedException.none();

  @Test
  public void testConstructorChecksNorthLatitudeNaN() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must not be NaN");
    LatLngBounds.from(Double.NaN, 0, -20, -20);
  }

  @Test
  public void testConstructorChecksEastLongitudeNaN() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("longitude must not be NaN");
    LatLngBounds.from(0, Double.NaN, -20, -20);
  }

  @Test
  public void testConstructorChecksNorthLatitudeGreaterThan90() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must be between -90 and 90");
    LatLngBounds.from(95, 0, -20, -20);
  }

  @Test
  public void testConstructorChecksNorthLatitudeLessThanThanNegative90() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must be between -90 and 90");
    LatLngBounds.from(-95, 0, -20, -20);
  }

  @Test
  public void testConstructorChecksEastLongitudeInfinity() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("longitude must not be infinite");
    LatLngBounds.from(0, Double.POSITIVE_INFINITY, -20, -20);
  }


  @Test
  public void testConstructorChecksSouthLatitudeNaN() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must not be NaN");
    LatLngBounds.from(20, 20, Double.NaN, 0);
  }

  @Test
  public void testConstructorChecksWesttLongitudeNaN() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("longitude must not be NaN");
    LatLngBounds.from(20, 20, 0, Double.NaN);
  }

  @Test
  public void testConstructorChecksSouthLatitudeGreaterThan90() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must be between -90 and 90");
    LatLngBounds.from(20, 20,95, 0);
  }

  @Test
  public void testConstructorChecksSouthLatitudeLessThanThanNegative90() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("latitude must be between -90 and 90");
    LatLngBounds.from(20, 20, -95, 0);
  }

  @Test
  public void testConstructorChecksWestLongitudeInfinity() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("longitude must not be infinite");
    LatLngBounds.from(20, 20, 0, Double.POSITIVE_INFINITY);
  }

  @Test
  public void testConstructorCheckLatSouthGreaterLatNorth() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("LatSouth cannot be less than latNorth");
    LatLngBounds.from(0, 20, 20, 0);
  }
}