Home | History | Annotate | Download | only in location
      1 package com.android.server.location;
      2 
      3 import static com.google.common.truth.Truth.assertThat;
      4 import static org.mockito.ArgumentMatchers.anyBoolean;
      5 import static org.mockito.Mockito.times;
      6 import static org.mockito.Mockito.verify;
      7 import static org.mockito.Mockito.when;
      8 
      9 import android.os.Handler;
     10 import android.os.Looper;
     11 import android.platform.test.annotations.Presubmit;
     12 
     13 import com.android.server.testing.FrameworkRobolectricTestRunner;
     14 import com.android.server.testing.SystemLoaderPackages;
     15 
     16 import org.junit.Before;
     17 import org.junit.Test;
     18 import org.junit.runner.RunWith;
     19 import org.mockito.Mock;
     20 import org.mockito.MockitoAnnotations;
     21 import org.robolectric.RuntimeEnvironment;
     22 import org.robolectric.annotation.Config;
     23 
     24 /**
     25  * Unit tests for {@link GnssMeasurementsProvider}.
     26  */
     27 @RunWith(FrameworkRobolectricTestRunner.class)
     28 @Config(
     29         manifest = Config.NONE,
     30         sdk = 27
     31 )
     32 @SystemLoaderPackages({"com.android.server.location"})
     33 @Presubmit
     34 public class GnssMeasurementsProviderTest {
     35     @Mock
     36     private GnssMeasurementsProvider.GnssMeasurementProviderNative mMockNative;
     37     private GnssMeasurementsProvider mTestProvider;
     38 
     39     @Before
     40     public void setUp() {
     41         MockitoAnnotations.initMocks(this);
     42         when(mMockNative.startMeasurementCollection(anyBoolean())).thenReturn(true);
     43         when(mMockNative.stopMeasurementCollection()).thenReturn(true);
     44 
     45         mTestProvider = new GnssMeasurementsProvider(RuntimeEnvironment.application,
     46                 new Handler(Looper.myLooper()), mMockNative) {
     47             @Override
     48             public boolean isGpsEnabled() {
     49                 return true;
     50             }
     51         };
     52     }
     53 
     54     @Test
     55     public void register_nativeStarted() {
     56         mTestProvider.registerWithService();
     57         verify(mMockNative).startMeasurementCollection(anyBoolean());
     58     }
     59 
     60     @Test
     61     public void unregister_nativeStopped() {
     62         mTestProvider.registerWithService();
     63         mTestProvider.unregisterFromService();
     64         verify(mMockNative).stopMeasurementCollection();
     65     }
     66 
     67     @Test
     68     public void isSupported_nativeIsSupported() {
     69         when(mMockNative.isMeasurementSupported()).thenReturn(true);
     70         assertThat(mTestProvider.isAvailableInPlatform()).isTrue();
     71 
     72         when(mMockNative.isMeasurementSupported()).thenReturn(false);
     73         assertThat(mTestProvider.isAvailableInPlatform()).isFalse();
     74     }
     75 
     76     @Test
     77     public void register_resume_started() {
     78         mTestProvider.registerWithService();
     79         mTestProvider.resumeIfStarted();
     80         verify(mMockNative, times(2)).startMeasurementCollection(anyBoolean());
     81     }
     82 
     83     @Test
     84     public void unregister_resume_notStarted() {
     85         mTestProvider.registerWithService();
     86         mTestProvider.unregisterFromService();
     87         mTestProvider.resumeIfStarted();
     88         verify(mMockNative, times(1)).startMeasurementCollection(anyBoolean());
     89     }
     90 }
     91