summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/geometry/LatLngBoundsTest.java
blob: bb96c9939d51116fc8eebbdf161500888161aea7 (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
package com.mapbox.mapboxsdk.geometry;

import android.os.Parcelable;

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

import org.junit.Before;
import org.junit.Test;

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 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 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 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 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);
  }
}