1package com.android.server.location;
2
3import static org.mockito.Matchers.anyDouble;
4import static org.mockito.Matchers.anyInt;
5import static org.mockito.Matchers.eq;
6import static org.mockito.Mockito.times;
7import static org.mockito.Mockito.verify;
8import static org.mockito.Mockito.when;
9
10import android.os.Looper;
11import android.os.RemoteException;
12import android.platform.test.annotations.Presubmit;
13
14import com.android.server.testing.FrameworkRobolectricTestRunner;
15import com.android.server.testing.SystemLoaderPackages;
16
17import org.junit.Before;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.mockito.Mock;
21import org.mockito.MockitoAnnotations;
22import org.robolectric.annotation.Config;
23
24/**
25 * Unit tests for {@link GnssGeofenceProvider}.
26 */
27@RunWith(FrameworkRobolectricTestRunner.class)
28@Config(
29        manifest = Config.NONE,
30        sdk = 27
31)
32@SystemLoaderPackages({"com.android.server.location"})
33@Presubmit
34public class GnssGeofenceProviderTest {
35    private static final int GEOFENCE_ID = 12345;
36    private static final double LATITUDE = 10.0;
37    private static final double LONGITUDE = 20.0;
38    private static final double RADIUS = 5.0;
39    private static final int LAST_TRANSITION = 0;
40    private static final int MONITOR_TRANSITIONS = 0;
41    private static final int NOTIFICATION_RESPONSIVENESS = 0;
42    private static final int UNKNOWN_TIMER = 0;
43    @Mock
44    private GnssGeofenceProvider.GnssGeofenceProviderNative mMockNative;
45    private GnssGeofenceProvider mTestProvider;
46
47    @Before
48    public void setUp() {
49        MockitoAnnotations.initMocks(this);
50        when(mMockNative.addGeofence(anyInt(), anyDouble(), anyDouble(), anyDouble(), anyInt(),
51                anyInt(), anyInt(), anyInt())).thenReturn(true);
52        when(mMockNative.pauseGeofence(anyInt())).thenReturn(true);
53        when(mMockNative.removeGeofence(anyInt())).thenReturn(true);
54        when(mMockNative.resumeGeofence(anyInt(), anyInt())).thenReturn(true);
55        mTestProvider = new GnssGeofenceProvider(Looper.myLooper(), mMockNative);
56        mTestProvider.addCircularHardwareGeofence(GEOFENCE_ID, LATITUDE,
57                LONGITUDE, RADIUS, LAST_TRANSITION, MONITOR_TRANSITIONS,
58                NOTIFICATION_RESPONSIVENESS,
59                UNKNOWN_TIMER);
60    }
61
62    @Test
63    public void addGeofence_nativeAdded() {
64        verify(mMockNative).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE),
65                eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS),
66                eq(NOTIFICATION_RESPONSIVENESS),
67                eq(UNKNOWN_TIMER));
68    }
69
70    @Test
71    public void pauseGeofence_nativePaused() {
72        mTestProvider.pauseHardwareGeofence(GEOFENCE_ID);
73        verify(mMockNative).pauseGeofence(eq(GEOFENCE_ID));
74    }
75
76    @Test
77    public void removeGeofence_nativeRemoved() {
78        mTestProvider.removeHardwareGeofence(GEOFENCE_ID);
79        verify(mMockNative).removeGeofence(eq(GEOFENCE_ID));
80    }
81
82    @Test
83    public void resumeGeofence_nativeResumed() {
84        mTestProvider.pauseHardwareGeofence(GEOFENCE_ID);
85        mTestProvider.resumeHardwareGeofence(GEOFENCE_ID, MONITOR_TRANSITIONS);
86        verify(mMockNative).resumeGeofence(eq(GEOFENCE_ID), eq(MONITOR_TRANSITIONS));
87    }
88
89    @Test
90    public void addGeofence_restart_added() throws RemoteException {
91        mTestProvider.resumeIfStarted();
92
93        verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE),
94                eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS),
95                eq(NOTIFICATION_RESPONSIVENESS),
96                eq(UNKNOWN_TIMER));
97    }
98
99    @Test
100    public void removeGeofence_restart_notAdded() throws RemoteException {
101        mTestProvider.removeHardwareGeofence(GEOFENCE_ID);
102        mTestProvider.resumeIfStarted();
103
104        verify(mMockNative, times(1)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE),
105                eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS),
106                eq(NOTIFICATION_RESPONSIVENESS),
107                eq(UNKNOWN_TIMER));
108    }
109
110    @Test
111    public void pauseGeofence_restart_paused() throws RemoteException {
112        mTestProvider.pauseHardwareGeofence(GEOFENCE_ID);
113        mTestProvider.resumeIfStarted();
114
115        verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE),
116                eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS),
117                eq(NOTIFICATION_RESPONSIVENESS),
118                eq(UNKNOWN_TIMER));
119        verify(mMockNative, times(2)).pauseGeofence(eq(GEOFENCE_ID));
120    }
121}
122