summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/test/java/com/mapbox/mapboxsdk/location/LocationComponentActivationOptionsTest.java
blob: 2660c819d484b295257f2184a17a2ce4ee9c3b9e (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
package com.mapbox.mapboxsdk.location;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.maps.Style;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;

import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class LocationComponentActivationOptionsTest {

  @Mock
  private Context context;
  @Mock
  private TypedArray array;
  @Mock
  private Resources resources;
  @Mock
  private Style style;

  @Rule
  public MockitoRule mockitoRule = MockitoJUnit.rule();

  @NonNull
  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Before
  public void setUp() throws Exception {
    when(context.obtainStyledAttributes(R.style.mapbox_LocationComponent, R.styleable.mapbox_LocationComponent))
      .thenReturn(array);
    when(array.getResourceId(R.styleable.mapbox_LocationComponent_mapbox_foregroundDrawable, -1))
      .thenReturn(R.drawable.mapbox_user_icon);
    when(context.getResources()).thenReturn(resources);
  }

  @Test
  public void sanity() throws Exception {
    when(style.isFullyLoaded()).thenReturn(true);

    LocationComponentOptions locationComponentOptions = LocationComponentOptions.builder(context)
      .accuracyAlpha(0.5f)
      .build();
    assertNotNull(locationComponentOptions);

    LocationComponentActivationOptions locationComponentActivationOptions =
      LocationComponentActivationOptions.builder(context, style)
        .locationComponentOptions(locationComponentOptions)
        .useDefaultLocationEngine(true)
        .build();
    assertNotNull(locationComponentActivationOptions);
  }

  @Test
  public void includingBothStyleResAndComponentOptions_causesExceptionToBeThrown() throws Exception {

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("You've provided both a style resource and a LocationComponentOptions"
      + " object to the LocationComponentActivationOptions builder. You can't use both and "
      + "you must choose one of the two to style the LocationComponent.");

    LocationComponentOptions locationComponentOptions = LocationComponentOptions.builder(context)
      .accuracyAlpha(0.5f)
      .build();

    LocationComponentActivationOptions.builder(context, style)
      .locationComponentOptions(locationComponentOptions)
      .styleRes(R.style.mapbox_LocationComponent)
      .build();
  }

  @Test
  public void nullContext_causesExceptionToBeThrown() throws Exception {
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("Context in LocationComponentActivationOptions is null.");

    LocationComponentActivationOptions.builder(null, style)
      .build();
  }

  @Test
  public void nullStyle_causesExceptionToBeThrown() throws Exception {
    thrown.expect(NullPointerException.class);
    thrown.expectMessage("Style in LocationComponentActivationOptions is null. Make sure the Style object isn't null."
      + " Wait for the map to fully load before passing the Style object to LocationComponentActivationOptions.");

    LocationComponentActivationOptions.builder(context, null)
      .build();
  }

  @Test
  public void locationComponent_exceptionThrownWithDefaultLocationEngineButNotFullyLoadedStyle() throws Exception {

    when(style.isFullyLoaded()).thenReturn(false);

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Style in LocationComponentActivationOptions isn't fully loaded. Wait for the "
        + "map to fully load before passing the Style object to "
        + "LocationComponentActivationOptions.");

    LocationComponentActivationOptions.builder(context, style)
        .build();
  }
}