1 package com.android.server.location; 2 3 import static com.google.common.truth.Truth.assertThat; 4 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 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 import org.robolectric.RuntimeEnvironment; 20 21 /** 22 * Unit tests for {@link GnssNavigationMessageProvider}. 23 */ 24 @RunWith(RobolectricTestRunner.class) 25 @Presubmit 26 public class GnssNavigationMessageProviderTest { 27 @Mock 28 private GnssNavigationMessageProvider.GnssNavigationMessageProviderNative mMockNative; 29 private GnssNavigationMessageProvider mTestProvider; 30 31 @Before setUp()32 public void setUp() { 33 MockitoAnnotations.initMocks(this); 34 when(mMockNative.startNavigationMessageCollection()).thenReturn(true); 35 when(mMockNative.stopNavigationMessageCollection()).thenReturn(true); 36 37 mTestProvider = new GnssNavigationMessageProvider(RuntimeEnvironment.application, 38 new Handler(Looper.myLooper()), mMockNative) { 39 @Override 40 public boolean isGpsEnabled() { 41 return true; 42 } 43 }; 44 } 45 46 @Test register_nativeStarted()47 public void register_nativeStarted() { 48 mTestProvider.registerWithService(); 49 verify(mMockNative).startNavigationMessageCollection(); 50 } 51 52 @Test unregister_nativeStopped()53 public void unregister_nativeStopped() { 54 mTestProvider.registerWithService(); 55 mTestProvider.unregisterFromService(); 56 verify(mMockNative).stopNavigationMessageCollection(); 57 } 58 59 @Test isSupported_nativeIsSupported()60 public void isSupported_nativeIsSupported() { 61 when(mMockNative.isNavigationMessageSupported()).thenReturn(true); 62 assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); 63 64 when(mMockNative.isNavigationMessageSupported()).thenReturn(false); 65 assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); 66 } 67 68 @Test register_resume_started()69 public void register_resume_started() { 70 mTestProvider.registerWithService(); 71 mTestProvider.resumeIfStarted(); 72 verify(mMockNative, times(2)).startNavigationMessageCollection(); 73 } 74 75 @Test unregister_resume_notStarted()76 public void unregister_resume_notStarted() { 77 mTestProvider.registerWithService(); 78 mTestProvider.unregisterFromService(); 79 mTestProvider.resumeIfStarted(); 80 verify(mMockNative, times(1)).startNavigationMessageCollection(); 81 } 82 } 83