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