From 94bb0098bcb8e501eb9040c40199442e858c4e8a Mon Sep 17 00:00:00 2001 From: Sasha Kuznetsov Date: Thu, 26 Mar 2020 12:08:17 -0700 Subject: Move gnss files to server/location/gnss Test: build and run cuttlefish Bug: 150810542 Change-Id: I0b83ba5850249e3111b80fcb774dee8121d0a048 Merged-In: I7d966b3e26f1d5e5aadf0085d0d0bfe51b9dfc1a --- .../location/GnssAntennaInfoProviderTest.java | 94 -------------- .../server/location/GnssBatchingProviderTest.java | 83 ------------- .../server/location/GnssGeofenceProviderTest.java | 112 ----------------- .../location/GnssMeasurementsProviderTest.java | 83 ------------- .../GnssNavigationMessageProviderTest.java | 82 ------------- .../server/location/GnssPositionModeTest.java | 53 -------- .../location/GnssSatelliteBlacklistHelperTest.java | 119 ------------------ .../android/server/location/NtpTimeHelperTest.java | 100 --------------- .../location/gnss/GnssAntennaInfoProviderTest.java | 95 ++++++++++++++ .../location/gnss/GnssBatchingProviderTest.java | 99 +++++++++++++++ .../location/gnss/GnssGeofenceProviderTest.java | 128 +++++++++++++++++++ .../gnss/GnssMeasurementsProviderTest.java | 100 +++++++++++++++ .../gnss/GnssNavigationMessageProviderTest.java | 99 +++++++++++++++ .../server/location/gnss/GnssPositionModeTest.java | 69 +++++++++++ .../gnss/GnssSatelliteBlacklistHelperTest.java | 136 +++++++++++++++++++++ .../server/location/gnss/NtpTimeHelperTest.java | 116 ++++++++++++++++++ 16 files changed, 842 insertions(+), 726 deletions(-) delete mode 100644 services/robotests/src/com/android/server/location/GnssAntennaInfoProviderTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssBatchingProviderTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssGeofenceProviderTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssMeasurementsProviderTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssNavigationMessageProviderTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssPositionModeTest.java delete mode 100644 services/robotests/src/com/android/server/location/GnssSatelliteBlacklistHelperTest.java delete mode 100644 services/robotests/src/com/android/server/location/NtpTimeHelperTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssAntennaInfoProviderTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssBatchingProviderTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssGeofenceProviderTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssMeasurementsProviderTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssNavigationMessageProviderTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssPositionModeTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/GnssSatelliteBlacklistHelperTest.java create mode 100644 services/robotests/src/com/android/server/location/gnss/NtpTimeHelperTest.java (limited to 'services/robotests/src') diff --git a/services/robotests/src/com/android/server/location/GnssAntennaInfoProviderTest.java b/services/robotests/src/com/android/server/location/GnssAntennaInfoProviderTest.java deleted file mode 100644 index 76f7ad646bb1..000000000000 --- a/services/robotests/src/com/android/server/location/GnssAntennaInfoProviderTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright (C) 2020 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.os.Handler; -import android.os.Looper; -import android.platform.test.annotations.Presubmit; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** - * Unit tests for {@link GnssAntennaInfoProvider}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssAntennaInfoProviderTest { - @Mock - private GnssAntennaInfoProvider.GnssAntennaInfoProviderNative mMockNative; - private GnssAntennaInfoProvider mTestProvider; - - /** Setup. */ - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mMockNative.startAntennaInfoListening()).thenReturn(true); - when(mMockNative.stopAntennaInfoListening()).thenReturn(true); - - mTestProvider = new GnssAntennaInfoProvider(RuntimeEnvironment.application, - new Handler(Looper.myLooper()), mMockNative) { - @Override - public boolean isGpsEnabled() { - return true; - } - }; - } - - /** - * Test that registerWithService calls the native startAntennaInfoListening method. - */ - @Test - public void register_nativeStarted() { - mTestProvider.registerWithService(); - verify(mMockNative, times(1)).startAntennaInfoListening(); - } - - /** - * Test that unregisterFromService calls the native stopAntennaInfoListening method. - */ - @Test - public void unregister_nativeStopped() { - mTestProvider.registerWithService(); - mTestProvider.unregisterFromService(); - verify(mMockNative, times(1)).stopAntennaInfoListening(); - } - - /** - * Test that GnssAntennaInfoProvider.isAntennaInfoSupported() returns the result of the - * native isAntennaInfoSupported method. - */ - @Test - public void isSupported_nativeIsSupported() { - when(mMockNative.isAntennaInfoSupported()).thenReturn(true); - assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); - - when(mMockNative.isAntennaInfoSupported()).thenReturn(false); - assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssBatchingProviderTest.java b/services/robotests/src/com/android/server/location/GnssBatchingProviderTest.java deleted file mode 100644 index d58c3f73b8e8..000000000000 --- a/services/robotests/src/com/android/server/location/GnssBatchingProviderTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.platform.test.annotations.Presubmit; - -import com.android.server.location.GnssBatchingProvider.GnssBatchingProviderNative; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; - -/** - * Unit tests for {@link GnssBatchingProvider}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssBatchingProviderTest { - - private static final long PERIOD_NANOS = (long) 1e9; - private static final boolean WAKE_ON_FIFO_FULL = true; - private static final int BATCH_SIZE = 3; - @Mock - private GnssBatchingProviderNative mMockNative; - private GnssBatchingProvider mTestProvider; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mMockNative.initBatching()).thenReturn(true); - when(mMockNative.startBatch(anyLong(), anyBoolean())).thenReturn(true); - when(mMockNative.stopBatch()).thenReturn(true); - when(mMockNative.getBatchSize()).thenReturn(BATCH_SIZE); - mTestProvider = new GnssBatchingProvider(mMockNative); - mTestProvider.enable(); - mTestProvider.start(PERIOD_NANOS, WAKE_ON_FIFO_FULL); - } - - @Test - public void start_nativeStarted() { - verify(mMockNative).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); - } - - @Test - public void stop_nativeStopped() { - mTestProvider.stop(); - verify(mMockNative).stopBatch(); - } - - @Test - public void flush_nativeFlushed() { - mTestProvider.flush(); - verify(mMockNative).flushBatch(); - } - - @Test - public void getBatchSize_nativeGetBatchSize() { - assertThat(mTestProvider.getBatchSize()).isEqualTo(BATCH_SIZE); - } - - @Test - public void started_resume_started() { - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(2)).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); - } - - @Test - public void stopped_resume_notStarted() { - mTestProvider.stop(); - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(1)).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssGeofenceProviderTest.java b/services/robotests/src/com/android/server/location/GnssGeofenceProviderTest.java deleted file mode 100644 index 30c73368da15..000000000000 --- a/services/robotests/src/com/android/server/location/GnssGeofenceProviderTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.android.server.location; - -import static org.mockito.ArgumentMatchers.anyDouble; -import static org.mockito.ArgumentMatchers.anyInt; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.os.RemoteException; -import android.platform.test.annotations.Presubmit; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; - -/** - * Unit tests for {@link GnssGeofenceProvider}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssGeofenceProviderTest { - private static final int GEOFENCE_ID = 12345; - private static final double LATITUDE = 10.0; - private static final double LONGITUDE = 20.0; - private static final double RADIUS = 5.0; - private static final int LAST_TRANSITION = 0; - private static final int MONITOR_TRANSITIONS = 0; - private static final int NOTIFICATION_RESPONSIVENESS = 0; - private static final int UNKNOWN_TIMER = 0; - @Mock - private GnssGeofenceProvider.GnssGeofenceProviderNative mMockNative; - private GnssGeofenceProvider mTestProvider; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mMockNative.addGeofence(anyInt(), anyDouble(), anyDouble(), anyDouble(), anyInt(), - anyInt(), anyInt(), anyInt())).thenReturn(true); - when(mMockNative.pauseGeofence(anyInt())).thenReturn(true); - when(mMockNative.removeGeofence(anyInt())).thenReturn(true); - when(mMockNative.resumeGeofence(anyInt(), anyInt())).thenReturn(true); - mTestProvider = new GnssGeofenceProvider(mMockNative); - mTestProvider.addCircularHardwareGeofence(GEOFENCE_ID, LATITUDE, - LONGITUDE, RADIUS, LAST_TRANSITION, MONITOR_TRANSITIONS, - NOTIFICATION_RESPONSIVENESS, - UNKNOWN_TIMER); - } - - @Test - public void addGeofence_nativeAdded() { - verify(mMockNative).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), - eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), - eq(NOTIFICATION_RESPONSIVENESS), - eq(UNKNOWN_TIMER)); - } - - @Test - public void pauseGeofence_nativePaused() { - mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); - verify(mMockNative).pauseGeofence(eq(GEOFENCE_ID)); - } - - @Test - public void removeGeofence_nativeRemoved() { - mTestProvider.removeHardwareGeofence(GEOFENCE_ID); - verify(mMockNative).removeGeofence(eq(GEOFENCE_ID)); - } - - @Test - public void resumeGeofence_nativeResumed() { - mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); - mTestProvider.resumeHardwareGeofence(GEOFENCE_ID, MONITOR_TRANSITIONS); - verify(mMockNative).resumeGeofence(eq(GEOFENCE_ID), eq(MONITOR_TRANSITIONS)); - } - - @Test - public void addGeofence_restart_added() throws RemoteException { - mTestProvider.resumeIfStarted(); - - verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), - eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), - eq(NOTIFICATION_RESPONSIVENESS), - eq(UNKNOWN_TIMER)); - } - - @Test - public void removeGeofence_restart_notAdded() throws RemoteException { - mTestProvider.removeHardwareGeofence(GEOFENCE_ID); - mTestProvider.resumeIfStarted(); - - verify(mMockNative, times(1)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), - eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), - eq(NOTIFICATION_RESPONSIVENESS), - eq(UNKNOWN_TIMER)); - } - - @Test - public void pauseGeofence_restart_paused() throws RemoteException { - mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); - mTestProvider.resumeIfStarted(); - - verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), - eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), - eq(NOTIFICATION_RESPONSIVENESS), - eq(UNKNOWN_TIMER)); - verify(mMockNative, times(2)).pauseGeofence(eq(GEOFENCE_ID)); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssMeasurementsProviderTest.java b/services/robotests/src/com/android/server/location/GnssMeasurementsProviderTest.java deleted file mode 100644 index b349b67dab0c..000000000000 --- a/services/robotests/src/com/android/server/location/GnssMeasurementsProviderTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.anyBoolean; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.os.Handler; -import android.os.Looper; -import android.platform.test.annotations.Presubmit; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** - * Unit tests for {@link GnssMeasurementsProvider}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssMeasurementsProviderTest { - @Mock - private GnssMeasurementsProvider.GnssMeasurementProviderNative mMockNative; - private GnssMeasurementsProvider mTestProvider; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mMockNative.startMeasurementCollection(anyBoolean())).thenReturn(true); - when(mMockNative.stopMeasurementCollection()).thenReturn(true); - - mTestProvider = new GnssMeasurementsProvider(RuntimeEnvironment.application, - new Handler(Looper.myLooper()), mMockNative) { - @Override - public boolean isGpsEnabled() { - return true; - } - }; - } - - @Test - public void register_nativeStarted() { - mTestProvider.registerWithService(); - verify(mMockNative).startMeasurementCollection(anyBoolean()); - } - - @Test - public void unregister_nativeStopped() { - mTestProvider.registerWithService(); - mTestProvider.unregisterFromService(); - verify(mMockNative).stopMeasurementCollection(); - } - - @Test - public void isSupported_nativeIsSupported() { - when(mMockNative.isMeasurementSupported()).thenReturn(true); - assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); - - when(mMockNative.isMeasurementSupported()).thenReturn(false); - assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); - } - - @Test - public void register_resume_started() { - mTestProvider.registerWithService(); - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(2)).startMeasurementCollection(anyBoolean()); - } - - @Test - public void unregister_resume_notStarted() { - mTestProvider.registerWithService(); - mTestProvider.unregisterFromService(); - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(1)).startMeasurementCollection(anyBoolean()); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssNavigationMessageProviderTest.java b/services/robotests/src/com/android/server/location/GnssNavigationMessageProviderTest.java deleted file mode 100644 index aa2a96e6fad4..000000000000 --- a/services/robotests/src/com/android/server/location/GnssNavigationMessageProviderTest.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import android.os.Handler; -import android.os.Looper; -import android.platform.test.annotations.Presubmit; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** - * Unit tests for {@link GnssNavigationMessageProvider}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssNavigationMessageProviderTest { - @Mock - private GnssNavigationMessageProvider.GnssNavigationMessageProviderNative mMockNative; - private GnssNavigationMessageProvider mTestProvider; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(mMockNative.startNavigationMessageCollection()).thenReturn(true); - when(mMockNative.stopNavigationMessageCollection()).thenReturn(true); - - mTestProvider = new GnssNavigationMessageProvider(RuntimeEnvironment.application, - new Handler(Looper.myLooper()), mMockNative) { - @Override - public boolean isGpsEnabled() { - return true; - } - }; - } - - @Test - public void register_nativeStarted() { - mTestProvider.registerWithService(); - verify(mMockNative).startNavigationMessageCollection(); - } - - @Test - public void unregister_nativeStopped() { - mTestProvider.registerWithService(); - mTestProvider.unregisterFromService(); - verify(mMockNative).stopNavigationMessageCollection(); - } - - @Test - public void isSupported_nativeIsSupported() { - when(mMockNative.isNavigationMessageSupported()).thenReturn(true); - assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); - - when(mMockNative.isNavigationMessageSupported()).thenReturn(false); - assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); - } - - @Test - public void register_resume_started() { - mTestProvider.registerWithService(); - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(2)).startNavigationMessageCollection(); - } - - @Test - public void unregister_resume_notStarted() { - mTestProvider.registerWithService(); - mTestProvider.unregisterFromService(); - mTestProvider.resumeIfStarted(); - verify(mMockNative, times(1)).startNavigationMessageCollection(); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssPositionModeTest.java b/services/robotests/src/com/android/server/location/GnssPositionModeTest.java deleted file mode 100644 index f37f50e76ae5..000000000000 --- a/services/robotests/src/com/android/server/location/GnssPositionModeTest.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import android.platform.test.annotations.Presubmit; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -import java.util.HashSet; - -/** - * Unit tests for {@link GnssPositionMode}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssPositionModeTest { - - private GnssPositionMode positionMode1 = createGnssPositionMode(0, 1000); - private GnssPositionMode positionMode2 = createGnssPositionMode(0, 1000); - private GnssPositionMode positionMode3 = createGnssPositionMode(1, 1000); - - @Test - public void testHashCode() { - assertThat(positionMode1.hashCode()).isEqualTo(positionMode2.hashCode()); - assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); - assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); - - HashSet hashSet = new HashSet<>(); - hashSet.add(positionMode1.hashCode()); - hashSet.add(positionMode2.hashCode()); - assertThat(hashSet.size()).isEqualTo(1); - hashSet.add(positionMode3.hashCode()); - assertThat(hashSet.size()).isEqualTo(2); - } - - @Test - public void checkIfEqualsImpliesSameHashCode() { - assertTEqualsImpliesSameHashCode(positionMode1, positionMode2); - assertTEqualsImpliesSameHashCode(positionMode2, positionMode3); - } - - private void assertTEqualsImpliesSameHashCode(GnssPositionMode mode1, GnssPositionMode mode2) { - if (mode1.equals(mode2)) { - assertThat(mode1.hashCode()).isEqualTo(mode2.hashCode()); - } - } - - private GnssPositionMode createGnssPositionMode(int mode, int minInterval) { - return new GnssPositionMode(mode, 0, minInterval, 0, 0, true); - } -} diff --git a/services/robotests/src/com/android/server/location/GnssSatelliteBlacklistHelperTest.java b/services/robotests/src/com/android/server/location/GnssSatelliteBlacklistHelperTest.java deleted file mode 100644 index ba4a753e4813..000000000000 --- a/services/robotests/src/com/android/server/location/GnssSatelliteBlacklistHelperTest.java +++ /dev/null @@ -1,119 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; - -import android.content.ContentResolver; -import android.content.Context; -import android.database.ContentObserver; -import android.os.Looper; -import android.platform.test.annotations.Presubmit; -import android.provider.Settings; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.Shadows; - -import java.util.Collection; -import java.util.List; - -/** - * Unit tests for {@link GnssSatelliteBlacklistHelper}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class GnssSatelliteBlacklistHelperTest { - - private ContentResolver mContentResolver; - @Mock - private GnssSatelliteBlacklistHelper.GnssSatelliteBlacklistCallback mCallback; - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - Context context = RuntimeEnvironment.application; - mContentResolver = context.getContentResolver(); - new GnssSatelliteBlacklistHelper(context, Looper.myLooper(), mCallback); - } - - @Test - public void blacklistOf2Satellites_callbackIsCalled() { - String blacklist = "3,0,5,24"; - updateBlacklistAndVerifyCallbackIsCalled(blacklist); - } - - @Test - public void blacklistWithSpaces_callbackIsCalled() { - String blacklist = "3, 11"; - updateBlacklistAndVerifyCallbackIsCalled(blacklist); - } - - @Test - public void emptyBlacklist_callbackIsCalled() { - String blacklist = ""; - updateBlacklistAndVerifyCallbackIsCalled(blacklist); - } - - @Test - public void blacklistWithOddNumberOfValues_callbackIsNotCalled() { - String blacklist = "3,0,5"; - updateBlacklistAndNotifyContentObserver(blacklist); - verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); - } - - @Test - public void blacklistWithNegativeValue_callbackIsNotCalled() { - String blacklist = "3,-11"; - updateBlacklistAndNotifyContentObserver(blacklist); - verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); - } - - @Test - public void blacklistWithNonDigitCharacter_callbackIsNotCalled() { - String blacklist = "3,1a,5,11"; - updateBlacklistAndNotifyContentObserver(blacklist); - verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); - } - - private void updateBlacklistAndNotifyContentObserver(String blacklist) { - Settings.Global.putString(mContentResolver, - Settings.Global.GNSS_SATELLITE_BLACKLIST, blacklist); - notifyContentObserverFor(Settings.Global.GNSS_SATELLITE_BLACKLIST); - } - - private void updateBlacklistAndVerifyCallbackIsCalled(String blacklist) { - updateBlacklistAndNotifyContentObserver(blacklist); - - ArgumentCaptor constellationsCaptor = ArgumentCaptor.forClass(int[].class); - ArgumentCaptor svIdsCaptor = ArgumentCaptor.forClass(int[].class); - verify(mCallback).onUpdateSatelliteBlacklist(constellationsCaptor.capture(), - svIdsCaptor.capture()); - - int[] constellations = constellationsCaptor.getValue(); - int[] svIds = svIdsCaptor.getValue(); - List values = GnssSatelliteBlacklistHelper.parseSatelliteBlacklist(blacklist); - assertThat(values.size()).isEqualTo(constellations.length * 2); - assertThat(svIds.length).isEqualTo(constellations.length); - for (int i = 0; i < constellations.length; i++) { - assertThat(constellations[i]).isEqualTo(values.get(i * 2)); - assertThat(svIds[i]).isEqualTo(values.get(i * 2 + 1)); - } - } - - private static void notifyContentObserverFor(String globalSetting) { - Collection contentObservers = - Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()) - .getContentObservers(Settings.Global.getUriFor(globalSetting)); - assertThat(contentObservers).isNotEmpty(); - contentObservers.iterator().next().onChange(false /* selfChange */); - } -} diff --git a/services/robotests/src/com/android/server/location/NtpTimeHelperTest.java b/services/robotests/src/com/android/server/location/NtpTimeHelperTest.java deleted file mode 100644 index 9c5d4ad6ceeb..000000000000 --- a/services/robotests/src/com/android/server/location/NtpTimeHelperTest.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.android.server.location; - -import static com.google.common.truth.Truth.assertThat; - -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; - -import android.os.Looper; -import android.os.SystemClock; -import android.platform.test.annotations.Presubmit; -import android.util.NtpTrustedTime; - -import com.android.server.location.NtpTimeHelper.InjectNtpTimeCallback; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; -import org.robolectric.shadows.ShadowLooper; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - -/** - * Unit tests for {@link NtpTimeHelper}. - */ -@RunWith(RobolectricTestRunner.class) -@Presubmit -public class NtpTimeHelperTest { - - private static final long MOCK_NTP_TIME = 1519930775453L; - @Mock - private NtpTrustedTime mMockNtpTrustedTime; - private NtpTimeHelper mNtpTimeHelper; - private CountDownLatch mCountDownLatch; - - @Before - public void setUp() throws Exception { - MockitoAnnotations.initMocks(this); - mCountDownLatch = new CountDownLatch(1); - InjectNtpTimeCallback callback = - (time, timeReference, uncertainty) -> { - assertThat(time).isEqualTo(MOCK_NTP_TIME); - mCountDownLatch.countDown(); - }; - mNtpTimeHelper = new NtpTimeHelper(RuntimeEnvironment.application, - Looper.myLooper(), - callback, mMockNtpTrustedTime); - } - - @Test - public void handleInjectNtpTime_cachedAgeLow_injectTime() throws InterruptedException { - NtpTrustedTime.TimeResult result = mock(NtpTrustedTime.TimeResult.class); - doReturn(NtpTimeHelper.NTP_INTERVAL - 1).when(result).getAgeMillis(); - doReturn(MOCK_NTP_TIME).when(result).getTimeMillis(); - doReturn(result).when(mMockNtpTrustedTime).getCachedTimeResult(); - - mNtpTimeHelper.retrieveAndInjectNtpTime(); - - waitForTasksToBePostedOnHandlerAndRunThem(); - assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); - } - - @Test - public void handleInjectNtpTime_injectTimeFailed_injectTimeDelayed() - throws InterruptedException { - NtpTrustedTime.TimeResult result1 = mock(NtpTrustedTime.TimeResult.class); - doReturn(NtpTimeHelper.NTP_INTERVAL + 1).when(result1).getAgeMillis(); - doReturn(result1).when(mMockNtpTrustedTime).getCachedTimeResult(); - doReturn(false).when(mMockNtpTrustedTime).forceRefresh(); - - mNtpTimeHelper.retrieveAndInjectNtpTime(); - waitForTasksToBePostedOnHandlerAndRunThem(); - assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isFalse(); - - doReturn(true).when(mMockNtpTrustedTime).forceRefresh(); - NtpTrustedTime.TimeResult result2 = mock(NtpTrustedTime.TimeResult.class); - doReturn(1L).when(result2).getAgeMillis(); - doReturn(MOCK_NTP_TIME).when(result2).getTimeMillis(); - doReturn(result2).when(mMockNtpTrustedTime).getCachedTimeResult(); - SystemClock.sleep(NtpTimeHelper.RETRY_INTERVAL); - - waitForTasksToBePostedOnHandlerAndRunThem(); - assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); - } - - /** - * Since a thread is created in {@link NtpTimeHelper#retrieveAndInjectNtpTime} and the task to - * be verified is posted in the thread, we have to wait for the task to be posted and then it - * can be run. - */ - private void waitForTasksToBePostedOnHandlerAndRunThem() throws InterruptedException { - mCountDownLatch.await(1, TimeUnit.SECONDS); - ShadowLooper.runUiThreadTasks(); - } -} - diff --git a/services/robotests/src/com/android/server/location/gnss/GnssAntennaInfoProviderTest.java b/services/robotests/src/com/android/server/location/gnss/GnssAntennaInfoProviderTest.java new file mode 100644 index 000000000000..d8acd6ea6948 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssAntennaInfoProviderTest.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.os.Handler; +import android.os.Looper; +import android.platform.test.annotations.Presubmit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +/** + * Unit tests for {@link GnssAntennaInfoProvider}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssAntennaInfoProviderTest { + @Mock + private GnssAntennaInfoProvider.GnssAntennaInfoProviderNative + mMockNative; + private GnssAntennaInfoProvider mTestProvider; + + /** Setup. */ + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockNative.startAntennaInfoListening()).thenReturn(true); + when(mMockNative.stopAntennaInfoListening()).thenReturn(true); + + mTestProvider = new GnssAntennaInfoProvider(RuntimeEnvironment.application, + new Handler(Looper.myLooper()), mMockNative) { + @Override + public boolean isGpsEnabled() { + return true; + } + }; + } + + /** + * Test that registerWithService calls the native startAntennaInfoListening method. + */ + @Test + public void register_nativeStarted() { + mTestProvider.registerWithService(); + verify(mMockNative, times(1)).startAntennaInfoListening(); + } + + /** + * Test that unregisterFromService calls the native stopAntennaInfoListening method. + */ + @Test + public void unregister_nativeStopped() { + mTestProvider.registerWithService(); + mTestProvider.unregisterFromService(); + verify(mMockNative, times(1)).stopAntennaInfoListening(); + } + + /** + * Test that GnssAntennaInfoProvider.isAntennaInfoSupported() returns the result of the + * native isAntennaInfoSupported method. + */ + @Test + public void isSupported_nativeIsSupported() { + when(mMockNative.isAntennaInfoSupported()).thenReturn(true); + assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); + + when(mMockNative.isAntennaInfoSupported()).thenReturn(false); + assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssBatchingProviderTest.java b/services/robotests/src/com/android/server/location/gnss/GnssBatchingProviderTest.java new file mode 100644 index 000000000000..25d6aa4dae29 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssBatchingProviderTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.platform.test.annotations.Presubmit; + +import com.android.server.location.gnss.GnssBatchingProvider.GnssBatchingProviderNative; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; + +/** + * Unit tests for {@link GnssBatchingProvider}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssBatchingProviderTest { + + private static final long PERIOD_NANOS = (long) 1e9; + private static final boolean WAKE_ON_FIFO_FULL = true; + private static final int BATCH_SIZE = 3; + @Mock + private GnssBatchingProviderNative mMockNative; + private GnssBatchingProvider mTestProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockNative.initBatching()).thenReturn(true); + when(mMockNative.startBatch(anyLong(), anyBoolean())).thenReturn(true); + when(mMockNative.stopBatch()).thenReturn(true); + when(mMockNative.getBatchSize()).thenReturn(BATCH_SIZE); + mTestProvider = new GnssBatchingProvider(mMockNative); + mTestProvider.enable(); + mTestProvider.start(PERIOD_NANOS, WAKE_ON_FIFO_FULL); + } + + @Test + public void start_nativeStarted() { + verify(mMockNative).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); + } + + @Test + public void stop_nativeStopped() { + mTestProvider.stop(); + verify(mMockNative).stopBatch(); + } + + @Test + public void flush_nativeFlushed() { + mTestProvider.flush(); + verify(mMockNative).flushBatch(); + } + + @Test + public void getBatchSize_nativeGetBatchSize() { + assertThat(mTestProvider.getBatchSize()).isEqualTo(BATCH_SIZE); + } + + @Test + public void started_resume_started() { + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(2)).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); + } + + @Test + public void stopped_resume_notStarted() { + mTestProvider.stop(); + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(1)).startBatch(eq(PERIOD_NANOS), eq(WAKE_ON_FIFO_FULL)); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssGeofenceProviderTest.java b/services/robotests/src/com/android/server/location/gnss/GnssGeofenceProviderTest.java new file mode 100644 index 000000000000..4a533aac01db --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssGeofenceProviderTest.java @@ -0,0 +1,128 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static org.mockito.ArgumentMatchers.anyDouble; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.os.RemoteException; +import android.platform.test.annotations.Presubmit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; + +/** + * Unit tests for {@link GnssGeofenceProvider}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssGeofenceProviderTest { + private static final int GEOFENCE_ID = 12345; + private static final double LATITUDE = 10.0; + private static final double LONGITUDE = 20.0; + private static final double RADIUS = 5.0; + private static final int LAST_TRANSITION = 0; + private static final int MONITOR_TRANSITIONS = 0; + private static final int NOTIFICATION_RESPONSIVENESS = 0; + private static final int UNKNOWN_TIMER = 0; + @Mock + private GnssGeofenceProvider.GnssGeofenceProviderNative mMockNative; + private GnssGeofenceProvider mTestProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockNative.addGeofence(anyInt(), anyDouble(), anyDouble(), anyDouble(), anyInt(), + anyInt(), anyInt(), anyInt())).thenReturn(true); + when(mMockNative.pauseGeofence(anyInt())).thenReturn(true); + when(mMockNative.removeGeofence(anyInt())).thenReturn(true); + when(mMockNative.resumeGeofence(anyInt(), anyInt())).thenReturn(true); + mTestProvider = new GnssGeofenceProvider(mMockNative); + mTestProvider.addCircularHardwareGeofence(GEOFENCE_ID, LATITUDE, + LONGITUDE, RADIUS, LAST_TRANSITION, MONITOR_TRANSITIONS, + NOTIFICATION_RESPONSIVENESS, + UNKNOWN_TIMER); + } + + @Test + public void addGeofence_nativeAdded() { + verify(mMockNative).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), + eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), + eq(NOTIFICATION_RESPONSIVENESS), + eq(UNKNOWN_TIMER)); + } + + @Test + public void pauseGeofence_nativePaused() { + mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); + verify(mMockNative).pauseGeofence(eq(GEOFENCE_ID)); + } + + @Test + public void removeGeofence_nativeRemoved() { + mTestProvider.removeHardwareGeofence(GEOFENCE_ID); + verify(mMockNative).removeGeofence(eq(GEOFENCE_ID)); + } + + @Test + public void resumeGeofence_nativeResumed() { + mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); + mTestProvider.resumeHardwareGeofence(GEOFENCE_ID, MONITOR_TRANSITIONS); + verify(mMockNative).resumeGeofence(eq(GEOFENCE_ID), eq(MONITOR_TRANSITIONS)); + } + + @Test + public void addGeofence_restart_added() throws RemoteException { + mTestProvider.resumeIfStarted(); + + verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), + eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), + eq(NOTIFICATION_RESPONSIVENESS), + eq(UNKNOWN_TIMER)); + } + + @Test + public void removeGeofence_restart_notAdded() throws RemoteException { + mTestProvider.removeHardwareGeofence(GEOFENCE_ID); + mTestProvider.resumeIfStarted(); + + verify(mMockNative, times(1)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), + eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), + eq(NOTIFICATION_RESPONSIVENESS), + eq(UNKNOWN_TIMER)); + } + + @Test + public void pauseGeofence_restart_paused() throws RemoteException { + mTestProvider.pauseHardwareGeofence(GEOFENCE_ID); + mTestProvider.resumeIfStarted(); + + verify(mMockNative, times(2)).addGeofence(eq(GEOFENCE_ID), eq(LATITUDE), eq(LONGITUDE), + eq(RADIUS), eq(LAST_TRANSITION), eq(MONITOR_TRANSITIONS), + eq(NOTIFICATION_RESPONSIVENESS), + eq(UNKNOWN_TIMER)); + verify(mMockNative, times(2)).pauseGeofence(eq(GEOFENCE_ID)); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssMeasurementsProviderTest.java b/services/robotests/src/com/android/server/location/gnss/GnssMeasurementsProviderTest.java new file mode 100644 index 000000000000..e7d9ef810e51 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssMeasurementsProviderTest.java @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.os.Handler; +import android.os.Looper; +import android.platform.test.annotations.Presubmit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +/** + * Unit tests for {@link GnssMeasurementsProvider}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssMeasurementsProviderTest { + @Mock + private GnssMeasurementsProvider.GnssMeasurementProviderNative + mMockNative; + private GnssMeasurementsProvider mTestProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockNative.startMeasurementCollection(anyBoolean())).thenReturn(true); + when(mMockNative.stopMeasurementCollection()).thenReturn(true); + + mTestProvider = new GnssMeasurementsProvider(RuntimeEnvironment.application, + new Handler(Looper.myLooper()), mMockNative) { + @Override + public boolean isGpsEnabled() { + return true; + } + }; + } + + @Test + public void register_nativeStarted() { + mTestProvider.registerWithService(); + verify(mMockNative).startMeasurementCollection(anyBoolean()); + } + + @Test + public void unregister_nativeStopped() { + mTestProvider.registerWithService(); + mTestProvider.unregisterFromService(); + verify(mMockNative).stopMeasurementCollection(); + } + + @Test + public void isSupported_nativeIsSupported() { + when(mMockNative.isMeasurementSupported()).thenReturn(true); + assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); + + when(mMockNative.isMeasurementSupported()).thenReturn(false); + assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); + } + + @Test + public void register_resume_started() { + mTestProvider.registerWithService(); + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(2)).startMeasurementCollection(anyBoolean()); + } + + @Test + public void unregister_resume_notStarted() { + mTestProvider.registerWithService(); + mTestProvider.unregisterFromService(); + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(1)).startMeasurementCollection(anyBoolean()); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssNavigationMessageProviderTest.java b/services/robotests/src/com/android/server/location/gnss/GnssNavigationMessageProviderTest.java new file mode 100644 index 000000000000..c21db73fb56c --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssNavigationMessageProviderTest.java @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.os.Handler; +import android.os.Looper; +import android.platform.test.annotations.Presubmit; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; + +/** + * Unit tests for {@link GnssNavigationMessageProvider}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssNavigationMessageProviderTest { + @Mock + private GnssNavigationMessageProvider.GnssNavigationMessageProviderNative + mMockNative; + private GnssNavigationMessageProvider mTestProvider; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(mMockNative.startNavigationMessageCollection()).thenReturn(true); + when(mMockNative.stopNavigationMessageCollection()).thenReturn(true); + + mTestProvider = new GnssNavigationMessageProvider(RuntimeEnvironment.application, + new Handler(Looper.myLooper()), mMockNative) { + @Override + public boolean isGpsEnabled() { + return true; + } + }; + } + + @Test + public void register_nativeStarted() { + mTestProvider.registerWithService(); + verify(mMockNative).startNavigationMessageCollection(); + } + + @Test + public void unregister_nativeStopped() { + mTestProvider.registerWithService(); + mTestProvider.unregisterFromService(); + verify(mMockNative).stopNavigationMessageCollection(); + } + + @Test + public void isSupported_nativeIsSupported() { + when(mMockNative.isNavigationMessageSupported()).thenReturn(true); + assertThat(mTestProvider.isAvailableInPlatform()).isTrue(); + + when(mMockNative.isNavigationMessageSupported()).thenReturn(false); + assertThat(mTestProvider.isAvailableInPlatform()).isFalse(); + } + + @Test + public void register_resume_started() { + mTestProvider.registerWithService(); + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(2)).startNavigationMessageCollection(); + } + + @Test + public void unregister_resume_notStarted() { + mTestProvider.registerWithService(); + mTestProvider.unregisterFromService(); + mTestProvider.resumeIfStarted(); + verify(mMockNative, times(1)).startNavigationMessageCollection(); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssPositionModeTest.java b/services/robotests/src/com/android/server/location/gnss/GnssPositionModeTest.java new file mode 100644 index 000000000000..7117ff95b401 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssPositionModeTest.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import android.platform.test.annotations.Presubmit; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.util.HashSet; + +/** + * Unit tests for {@link GnssPositionMode}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssPositionModeTest { + + private GnssPositionMode positionMode1 = createGnssPositionMode(0, 1000); + private GnssPositionMode positionMode2 = createGnssPositionMode(0, 1000); + private GnssPositionMode positionMode3 = createGnssPositionMode(1, 1000); + + @Test + public void testHashCode() { + assertThat(positionMode1.hashCode()).isEqualTo(positionMode2.hashCode()); + assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); + assertThat(positionMode1.hashCode()).isNotEqualTo(positionMode3.hashCode()); + + HashSet hashSet = new HashSet<>(); + hashSet.add(positionMode1.hashCode()); + hashSet.add(positionMode2.hashCode()); + assertThat(hashSet.size()).isEqualTo(1); + hashSet.add(positionMode3.hashCode()); + assertThat(hashSet.size()).isEqualTo(2); + } + + @Test + public void checkIfEqualsImpliesSameHashCode() { + assertTEqualsImpliesSameHashCode(positionMode1, positionMode2); + assertTEqualsImpliesSameHashCode(positionMode2, positionMode3); + } + + private void assertTEqualsImpliesSameHashCode(GnssPositionMode mode1, GnssPositionMode mode2) { + if (mode1.equals(mode2)) { + assertThat(mode1.hashCode()).isEqualTo(mode2.hashCode()); + } + } + + private GnssPositionMode createGnssPositionMode(int mode, int minInterval) { + return new GnssPositionMode(mode, 0, minInterval, 0, 0, true); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/GnssSatelliteBlacklistHelperTest.java b/services/robotests/src/com/android/server/location/gnss/GnssSatelliteBlacklistHelperTest.java new file mode 100644 index 000000000000..7c73a2f92f71 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/GnssSatelliteBlacklistHelperTest.java @@ -0,0 +1,136 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; + +import android.content.ContentResolver; +import android.content.Context; +import android.database.ContentObserver; +import android.os.Looper; +import android.platform.test.annotations.Presubmit; +import android.provider.Settings; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.Shadows; + +import java.util.Collection; +import java.util.List; + +/** + * Unit tests for {@link GnssSatelliteBlacklistHelper}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class GnssSatelliteBlacklistHelperTest { + + private ContentResolver mContentResolver; + @Mock + private GnssSatelliteBlacklistHelper.GnssSatelliteBlacklistCallback + mCallback; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + Context context = RuntimeEnvironment.application; + mContentResolver = context.getContentResolver(); + new GnssSatelliteBlacklistHelper(context, Looper.myLooper(), mCallback); + } + + @Test + public void blacklistOf2Satellites_callbackIsCalled() { + String blacklist = "3,0,5,24"; + updateBlacklistAndVerifyCallbackIsCalled(blacklist); + } + + @Test + public void blacklistWithSpaces_callbackIsCalled() { + String blacklist = "3, 11"; + updateBlacklistAndVerifyCallbackIsCalled(blacklist); + } + + @Test + public void emptyBlacklist_callbackIsCalled() { + String blacklist = ""; + updateBlacklistAndVerifyCallbackIsCalled(blacklist); + } + + @Test + public void blacklistWithOddNumberOfValues_callbackIsNotCalled() { + String blacklist = "3,0,5"; + updateBlacklistAndNotifyContentObserver(blacklist); + verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); + } + + @Test + public void blacklistWithNegativeValue_callbackIsNotCalled() { + String blacklist = "3,-11"; + updateBlacklistAndNotifyContentObserver(blacklist); + verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); + } + + @Test + public void blacklistWithNonDigitCharacter_callbackIsNotCalled() { + String blacklist = "3,1a,5,11"; + updateBlacklistAndNotifyContentObserver(blacklist); + verify(mCallback, never()).onUpdateSatelliteBlacklist(any(int[].class), any(int[].class)); + } + + private void updateBlacklistAndNotifyContentObserver(String blacklist) { + Settings.Global.putString(mContentResolver, + Settings.Global.GNSS_SATELLITE_BLACKLIST, blacklist); + notifyContentObserverFor(Settings.Global.GNSS_SATELLITE_BLACKLIST); + } + + private void updateBlacklistAndVerifyCallbackIsCalled(String blacklist) { + updateBlacklistAndNotifyContentObserver(blacklist); + + ArgumentCaptor constellationsCaptor = ArgumentCaptor.forClass(int[].class); + ArgumentCaptor svIdsCaptor = ArgumentCaptor.forClass(int[].class); + verify(mCallback).onUpdateSatelliteBlacklist(constellationsCaptor.capture(), + svIdsCaptor.capture()); + + int[] constellations = constellationsCaptor.getValue(); + int[] svIds = svIdsCaptor.getValue(); + List values = GnssSatelliteBlacklistHelper.parseSatelliteBlacklist(blacklist); + assertThat(values.size()).isEqualTo(constellations.length * 2); + assertThat(svIds.length).isEqualTo(constellations.length); + for (int i = 0; i < constellations.length; i++) { + assertThat(constellations[i]).isEqualTo(values.get(i * 2)); + assertThat(svIds[i]).isEqualTo(values.get(i * 2 + 1)); + } + } + + private static void notifyContentObserverFor(String globalSetting) { + Collection contentObservers = + Shadows.shadowOf(RuntimeEnvironment.application.getContentResolver()) + .getContentObservers(Settings.Global.getUriFor(globalSetting)); + assertThat(contentObservers).isNotEmpty(); + contentObservers.iterator().next().onChange(false /* selfChange */); + } +} diff --git a/services/robotests/src/com/android/server/location/gnss/NtpTimeHelperTest.java b/services/robotests/src/com/android/server/location/gnss/NtpTimeHelperTest.java new file mode 100644 index 000000000000..9b59aaddc4d4 --- /dev/null +++ b/services/robotests/src/com/android/server/location/gnss/NtpTimeHelperTest.java @@ -0,0 +1,116 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.location.gnss; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +import android.os.Looper; +import android.os.SystemClock; +import android.platform.test.annotations.Presubmit; +import android.util.NtpTrustedTime; + +import com.android.server.location.gnss.NtpTimeHelper.InjectNtpTimeCallback; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.shadows.ShadowLooper; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +/** + * Unit tests for {@link NtpTimeHelper}. + */ +@RunWith(RobolectricTestRunner.class) +@Presubmit +public class NtpTimeHelperTest { + + private static final long MOCK_NTP_TIME = 1519930775453L; + @Mock + private NtpTrustedTime mMockNtpTrustedTime; + private NtpTimeHelper mNtpTimeHelper; + private CountDownLatch mCountDownLatch; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + mCountDownLatch = new CountDownLatch(1); + InjectNtpTimeCallback callback = + (time, timeReference, uncertainty) -> { + assertThat(time).isEqualTo(MOCK_NTP_TIME); + mCountDownLatch.countDown(); + }; + mNtpTimeHelper = new NtpTimeHelper(RuntimeEnvironment.application, + Looper.myLooper(), + callback, mMockNtpTrustedTime); + } + + @Test + public void handleInjectNtpTime_cachedAgeLow_injectTime() throws InterruptedException { + NtpTrustedTime.TimeResult result = mock(NtpTrustedTime.TimeResult.class); + doReturn(NtpTimeHelper.NTP_INTERVAL - 1).when(result).getAgeMillis(); + doReturn(MOCK_NTP_TIME).when(result).getTimeMillis(); + doReturn(result).when(mMockNtpTrustedTime).getCachedTimeResult(); + + mNtpTimeHelper.retrieveAndInjectNtpTime(); + + waitForTasksToBePostedOnHandlerAndRunThem(); + assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); + } + + @Test + public void handleInjectNtpTime_injectTimeFailed_injectTimeDelayed() + throws InterruptedException { + NtpTrustedTime.TimeResult result1 = mock(NtpTrustedTime.TimeResult.class); + doReturn(NtpTimeHelper.NTP_INTERVAL + 1).when(result1).getAgeMillis(); + doReturn(result1).when(mMockNtpTrustedTime).getCachedTimeResult(); + doReturn(false).when(mMockNtpTrustedTime).forceRefresh(); + + mNtpTimeHelper.retrieveAndInjectNtpTime(); + waitForTasksToBePostedOnHandlerAndRunThem(); + assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isFalse(); + + doReturn(true).when(mMockNtpTrustedTime).forceRefresh(); + NtpTrustedTime.TimeResult result2 = mock(NtpTrustedTime.TimeResult.class); + doReturn(1L).when(result2).getAgeMillis(); + doReturn(MOCK_NTP_TIME).when(result2).getTimeMillis(); + doReturn(result2).when(mMockNtpTrustedTime).getCachedTimeResult(); + SystemClock.sleep(NtpTimeHelper.RETRY_INTERVAL); + + waitForTasksToBePostedOnHandlerAndRunThem(); + assertThat(mCountDownLatch.await(2, TimeUnit.SECONDS)).isTrue(); + } + + /** + * Since a thread is created in {@link NtpTimeHelper#retrieveAndInjectNtpTime} and the task to + * be verified is posted in the thread, we have to wait for the task to be posted and then it + * can be run. + */ + private void waitForTasksToBePostedOnHandlerAndRunThem() throws InterruptedException { + mCountDownLatch.await(1, TimeUnit.SECONDS); + ShadowLooper.runUiThreadTasks(); + } +} + -- cgit v1.2.3-59-g8ed1b