summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/test/java/com/mapbox/mapboxsdk/annotations/MarkerViewTest.java
blob: 3c52c1642277c5c90c3fcdbf5fe126241c8b3a03 (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
package com.mapbox.mapboxsdk.annotations;

import android.os.Parcelable;

import com.mapbox.mapboxsdk.exceptions.InvalidMarkerPositionException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.utils.MockParcel;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class MarkerViewTest {

  @Mock
  MapboxMap mapboxMap;

  @Mock
  MarkerViewManager markerViewManager;

  @Before
  public void beforeTest() {
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testSanity() {
    MarkerViewOptions markerOptions = new MarkerViewOptions();
    assertNotNull("markerOptions should not be null", markerOptions);
  }

  @Test
  public void testMarker() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng());
    assertNotNull("marker should not be null", markerOptions.getMarker());
  }

  @Test(expected = InvalidMarkerPositionException.class)
  public void testInvalidMarker() {
    new MarkerViewOptions().getMarker();
  }

  @Test
  public void testPosition() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(10, 12));
    MarkerView marker = markerOptions.getMarker();
    assertEquals(marker.getPosition(), new LatLng(10, 12));
    assertEquals(markerOptions.getPosition(), new LatLng(10, 12));
  }

  @Test
  public void testSnippet() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().snippet("Mapbox").position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals(marker.getSnippet(), "Mapbox");
  }

  @Test
  public void testTitle() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().title("Mapbox").position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals(marker.getTitle(), "Mapbox");
    assertEquals(markerOptions.getTitle(), "Mapbox");
  }

  @Test
  public void testFlat() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().flat(true).position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertTrue("flat should be true", marker.isFlat());
  }

  @Test
  public void testFlatDefault() {
    assertFalse("default value of flat should be false", new MarkerViewOptions().position(
      new LatLng()).getMarker().isFlat());
  }

  @Test
  public void testAnchor() {
    float anchorU = 1;
    float anchorV = 1;
    MarkerViewOptions markerOptions = new MarkerViewOptions().anchor(anchorU, anchorV).position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals("anchorU should match ", anchorU, marker.getAnchorU(), 0);
    assertEquals("anchorU should match ", anchorV, marker.getAnchorV(), 0);
  }

  @Test
  public void testAnchorDefault() {
    MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
    assertEquals("anchorU should match ", 0.5, marker.getAnchorU(), 0);
    assertEquals("anchorU should match ", 1, marker.getAnchorV(), 0);
  }

  @Test
  public void testInfoWindowAnchor() {
    float anchorU = 1;
    float anchorV = 1;
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).infoWindowAnchor(anchorU, anchorV);
    MarkerView marker = markerOptions.getMarker();
    assertEquals("anchorU should match ", 1, marker.getInfoWindowAnchorU(), 0);
    assertEquals("anchorU should match ", 1, marker.getInfoWindowAnchorV(), 0);
  }

  @Test
  public void testInfoWindowAnchorDefault() {
    MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
    assertEquals("anchorU should match ", 0.5, marker.getInfoWindowAnchorU(), 0);
    assertEquals("anchorU should match ", 0, marker.getInfoWindowAnchorV(), 0);
  }

  @Test
  public void testRotation() {
    int rotation = 90;
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(rotation);
    MarkerView marker = markerOptions.getMarker();
    assertEquals("rotation should match ", rotation, marker.getRotation(), 0);
  }

  @Test
  public void testRotationAboveMax() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().rotation(390).position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals(marker.getRotation(), 30, 0);
  }

  @Test
  public void testRotationBelowMin() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().rotation(-10).position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals(marker.getRotation(), 350, 0);
  }

  @Test
  public void testRotationUpdatePositive() {
    float startRotation = 45;
    float endRotation = 180;

    // allow calls to our mock
    when(mapboxMap.getMarkerViewManager()).thenReturn(markerViewManager);

    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(startRotation);
    MarkerView marker = markerOptions.getMarker();
    marker.setMapboxMap(mapboxMap);

    marker.setRotation(endRotation);
    verify(markerViewManager, times(1)).animateRotationBy(marker, endRotation);
  }

  @Test
  public void testRotationUpdateNegative() {
    float startRotation = 10;
    float endRotation = 270;

    // allow calls to our mock
    when(mapboxMap.getMarkerViewManager()).thenReturn(markerViewManager);

    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(startRotation);
    MarkerView marker = markerOptions.getMarker();
    marker.setMapboxMap(mapboxMap);

    marker.setRotation(endRotation);
    verify(markerViewManager, times(1)).animateRotationBy(marker, endRotation);
  }

  @Test
  public void testRotationUpdateMax() {
    float startRotation = 359;
    float endRotation = 0;

    // allow calls to our mock
    when(mapboxMap.getMarkerViewManager()).thenReturn(markerViewManager);

    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(startRotation);
    MarkerView marker = markerOptions.getMarker();
    marker.setMapboxMap(mapboxMap);

    marker.setRotation(endRotation);
    verify(markerViewManager, times(1)).animateRotationBy(marker, 0);
  }

  @Test
  public void testRotationUpdateMin() {
    float startRotation = 0;
    float endRotation = 359;

    // allow calls to our mock
    when(mapboxMap.getMarkerViewManager()).thenReturn(markerViewManager);

    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).rotation(startRotation);
    MarkerView marker = markerOptions.getMarker();
    marker.setMapboxMap(mapboxMap);

    marker.setRotation(endRotation);
    verify(markerViewManager, times(1)).animateRotationBy(marker, endRotation);
  }

  @Test
  public void testVisible() {
    boolean visible = false;
    MarkerViewOptions markerOptions = new MarkerViewOptions().visible(visible).position(new LatLng());
    MarkerView marker = markerOptions.getMarker();
    assertEquals("visible should match ", visible, marker.isVisible());
  }

  @Test
  public void testVisibleDefault() {
    assertTrue(new MarkerViewOptions().position(new LatLng()).getMarker().isVisible());
  }

  @Test
  public void testBuilder() {
    MarkerView marker = new MarkerViewOptions().title("title").snippet("snippet").position(
      new LatLng(10, 12)).getMarker();
    assertEquals(marker.getSnippet(), "snippet");
    assertEquals(marker.getPosition(), new LatLng(10, 12));
  }

  @Test
  public void testHashCode() {
    MarkerView marker = new MarkerViewOptions().position(new LatLng()).getMarker();
    assertEquals("hash code should match", marker.hashCode(), 0);
  }

  @Test
  public void testHashCodeBuilder() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(10, 12));
    assertEquals("hash code should match", markerOptions.hashCode(), 0);
  }

  @Test
  public void testEquals() {
    MarkerView markerOne = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
    MarkerView markerTwo = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
    assertEquals(markerOne, markerTwo);
  }

  @Test
  public void testEqualsItself() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(0, 0));
    MarkerView marker = markerOptions.getMarker();
    assertEquals("MarkerView should match", marker, marker);
    assertEquals("MarkerViewOptions should match", markerOptions, markerOptions);
  }

  @Test
  public void testNotEquals() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng(0, 0));
    MarkerView marker = markerOptions.getMarker();
    assertNotEquals("MarkerViewOptions should match", markerOptions, new Object());
    assertNotEquals("MarkerView should match", marker, new Object());
  }

  @Test
  public void testEqualityBuilder() {
    MarkerViewOptions markerOne = new MarkerViewOptions().position(new LatLng(0, 0));
    MarkerViewOptions markerTwo = new MarkerViewOptions().position(new LatLng(0, 0));
    assertEquals(markerOne, markerTwo);
  }

  @Test
  public void testToString() {
    MarkerView marker = new MarkerViewOptions().position(new LatLng(0, 0)).getMarker();
    assertEquals(marker.toString(), "MarkerView [position["
      + "LatLng [latitude=0.0, longitude=0.0, altitude=0.0]" + "]]");
  }

  @Test
  public void testParcelable() {
    MarkerViewOptions markerOptions = new MarkerViewOptions().position(new LatLng()).title("t").snippet("s");
    Parcelable parcelable = MockParcel.obtain(markerOptions);
    assertEquals("Parcel should match original object", parcelable, markerOptions);
  }
}