Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 1 | /* |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 17 | package com.android.server.location.gnss; |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyBoolean; |
| 23 | import static org.mockito.ArgumentMatchers.anyInt; |
| 24 | import static org.mockito.ArgumentMatchers.anyLong; |
| 25 | import static org.mockito.ArgumentMatchers.anyString; |
| 26 | import static org.mockito.ArgumentMatchers.eq; |
| 27 | import static org.mockito.Mockito.mock; |
| 28 | import static org.mockito.Mockito.times; |
| 29 | import static org.mockito.Mockito.verify; |
| 30 | import static org.mockito.Mockito.when; |
| 31 | import static org.testng.Assert.assertThrows; |
| 32 | |
| 33 | import android.Manifest; |
| 34 | import android.app.ActivityManager; |
| 35 | import android.app.AppOpsManager; |
| 36 | import android.content.Context; |
| 37 | import android.content.pm.PackageManager; |
| 38 | import android.location.GnssClock; |
| 39 | import android.location.GnssMeasurementCorrections; |
| 40 | import android.location.GnssMeasurementsEvent; |
| 41 | import android.location.GnssNavigationMessage; |
| 42 | import android.location.GnssSingleSatCorrection; |
| 43 | import android.location.IBatchedLocationCallback; |
| 44 | import android.location.IGnssMeasurementsListener; |
| 45 | import android.location.IGnssNavigationMessageListener; |
| 46 | import android.location.IGnssStatusListener; |
| 47 | import android.location.INetInitiatedListener; |
| 48 | import android.location.Location; |
| 49 | import android.os.Handler; |
| 50 | import android.os.IBinder; |
| 51 | import android.os.IInterface; |
| 52 | import android.os.Message; |
| 53 | import android.os.RemoteException; |
| 54 | |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 55 | import com.android.server.LocationManagerService; |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 56 | import com.android.server.location.GnssBatchingProvider; |
| 57 | import com.android.server.location.GnssCapabilitiesProvider; |
| 58 | import com.android.server.location.GnssLocationProvider; |
| 59 | import com.android.server.location.GnssMeasurementCorrectionsProvider; |
| 60 | import com.android.server.location.GnssMeasurementsProvider; |
| 61 | import com.android.server.location.GnssMeasurementsProvider.GnssMeasurementProviderNative; |
| 62 | import com.android.server.location.GnssNavigationMessageProvider; |
| 63 | import com.android.server.location.GnssNavigationMessageProvider.GnssNavigationMessageProviderNative; |
| 64 | import com.android.server.location.GnssStatusListenerHelper; |
Soonil Nagarkar | 3f12840 | 2019-12-12 08:31:27 -0800 | [diff] [blame] | 65 | import com.android.server.location.LocationUsageLogger; |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 66 | |
| 67 | import org.junit.Before; |
| 68 | import org.junit.Test; |
| 69 | import org.mockito.AdditionalMatchers; |
| 70 | import org.mockito.Mock; |
| 71 | import org.mockito.Mockito; |
| 72 | import org.mockito.MockitoAnnotations; |
| 73 | import org.mockito.invocation.InvocationOnMock; |
| 74 | |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 75 | import java.util.ArrayList; |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 76 | import java.util.Arrays; |
| 77 | import java.util.List; |
| 78 | |
| 79 | /** |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 80 | * Unit tests for {@link com.android.server.location.gnss.GnssManagerService}. |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 81 | */ |
| 82 | public class GnssManagerServiceTest { |
| 83 | |
| 84 | // Gnss Providers |
| 85 | @Mock |
| 86 | private GnssLocationProvider mMockGnssLocationProvider; |
| 87 | @Mock |
| 88 | private GnssBatchingProvider mMockGnssBatchingProvider; |
| 89 | @Mock |
| 90 | private GnssLocationProvider.GnssSystemInfoProvider mMockGnssSystemInfoProvider; |
| 91 | @Mock |
| 92 | private GnssCapabilitiesProvider mMockGnssCapabilitiesProvider; |
| 93 | @Mock |
| 94 | private GnssMeasurementCorrectionsProvider mMockGnssMeasurementCorrectionsProvider; |
| 95 | @Mock |
| 96 | private INetInitiatedListener mMockNetInitiatedListener; |
| 97 | private GnssMeasurementsProvider mTestGnssMeasurementsProvider; |
| 98 | private GnssStatusListenerHelper mTestGnssStatusProvider; |
| 99 | private GnssNavigationMessageProvider mTestGnssNavigationMessageProvider; |
| 100 | |
| 101 | // Managers and services |
| 102 | @Mock |
| 103 | private AppOpsManager mMockAppOpsManager; |
| 104 | @Mock |
| 105 | private ActivityManager mMockActivityManager; |
| 106 | @Mock |
| 107 | private LocationManagerService mMockLocationManagerService; |
| 108 | |
| 109 | // Context and handler |
| 110 | @Mock |
| 111 | private Handler mMockHandler; |
| 112 | @Mock |
| 113 | private Context mMockContext; |
| 114 | |
| 115 | // Class under test |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 116 | private com.android.server.location.gnss.GnssManagerService mGnssManagerService; |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 117 | |
| 118 | @Before |
| 119 | public void setUp() { |
| 120 | MockitoAnnotations.initMocks(this); |
| 121 | GnssLocationProvider.setIsSupportedForTest(true); |
| 122 | |
| 123 | // Set up mock context |
| 124 | when(mMockContext.getSystemServiceName(AppOpsManager.class)).thenReturn( |
| 125 | Context.APP_OPS_SERVICE); |
| 126 | when(mMockContext.getSystemServiceName(ActivityManager.class)).thenReturn( |
| 127 | Context.ACTIVITY_SERVICE); |
| 128 | when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn( |
| 129 | mMockAppOpsManager); |
| 130 | when(mMockContext.getSystemService( |
| 131 | eq(Context.ACTIVITY_SERVICE))).thenReturn( |
| 132 | mMockActivityManager); |
| 133 | enableLocationPermissions(); |
| 134 | |
| 135 | // Mock Handler will execute posted runnables immediately |
| 136 | when(mMockHandler.sendMessageAtTime(any(Message.class), anyLong())).thenAnswer( |
| 137 | (InvocationOnMock invocation) -> { |
| 138 | Message msg = (Message) (invocation.getArguments()[0]); |
| 139 | msg.getCallback().run(); |
| 140 | return null; |
| 141 | }); |
| 142 | |
| 143 | // Setup providers |
| 144 | mTestGnssMeasurementsProvider = createGnssMeasurementsProvider( |
| 145 | mMockContext, mMockHandler); |
| 146 | mTestGnssStatusProvider = createGnssStatusListenerHelper( |
| 147 | mMockContext, mMockHandler); |
| 148 | mTestGnssNavigationMessageProvider = createGnssNavigationMessageProvider( |
| 149 | mMockContext, mMockHandler); |
| 150 | |
| 151 | // Setup GnssLocationProvider to return providers |
| 152 | when(mMockGnssLocationProvider.getGnssStatusProvider()).thenReturn( |
| 153 | mTestGnssStatusProvider); |
| 154 | when(mMockGnssLocationProvider.getGnssBatchingProvider()).thenReturn( |
| 155 | mMockGnssBatchingProvider); |
| 156 | when(mMockGnssLocationProvider.getGnssCapabilitiesProvider()).thenReturn( |
| 157 | mMockGnssCapabilitiesProvider); |
| 158 | when(mMockGnssLocationProvider.getGnssSystemInfoProvider()).thenReturn( |
| 159 | mMockGnssSystemInfoProvider); |
| 160 | when(mMockGnssLocationProvider.getGnssMeasurementCorrectionsProvider()).thenReturn( |
| 161 | mMockGnssMeasurementCorrectionsProvider); |
| 162 | when(mMockGnssLocationProvider.getGnssMeasurementsProvider()).thenReturn( |
| 163 | mTestGnssMeasurementsProvider); |
| 164 | when(mMockGnssLocationProvider.getGnssNavigationMessageProvider()).thenReturn( |
| 165 | mTestGnssNavigationMessageProvider); |
| 166 | when(mMockGnssLocationProvider.getNetInitiatedListener()).thenReturn( |
| 167 | mMockNetInitiatedListener); |
| 168 | |
| 169 | // Setup GnssBatching provider |
| 170 | when(mMockGnssBatchingProvider.start(anyLong(), anyBoolean())).thenReturn(true); |
| 171 | when(mMockGnssBatchingProvider.stop()).thenReturn(true); |
| 172 | |
| 173 | // Create GnssManagerService |
| 174 | mGnssManagerService = new GnssManagerService(mMockLocationManagerService, mMockContext, |
| 175 | mMockGnssLocationProvider, new LocationUsageLogger()); |
| 176 | } |
| 177 | |
| 178 | private void overrideAsBinder(IInterface mockListener) { |
| 179 | IBinder mockBinder = mock(IBinder.class); |
| 180 | when(mockListener.asBinder()).thenReturn(mockBinder); |
| 181 | } |
| 182 | |
| 183 | private IGnssStatusListener createMockGnssStatusListener() { |
| 184 | IGnssStatusListener mockListener = mock(IGnssStatusListener.class); |
| 185 | overrideAsBinder(mockListener); |
| 186 | return mockListener; |
| 187 | } |
| 188 | |
| 189 | private IGnssMeasurementsListener createMockGnssMeasurementsListener() { |
| 190 | IGnssMeasurementsListener mockListener = mock( |
| 191 | IGnssMeasurementsListener.class); |
| 192 | overrideAsBinder(mockListener); |
| 193 | return mockListener; |
| 194 | } |
| 195 | |
| 196 | private IBatchedLocationCallback createMockBatchedLocationCallback() { |
| 197 | IBatchedLocationCallback mockedCallback = mock(IBatchedLocationCallback.class); |
| 198 | overrideAsBinder(mockedCallback); |
| 199 | return mockedCallback; |
| 200 | } |
| 201 | |
| 202 | private IGnssNavigationMessageListener createMockGnssNavigationMessageListener() { |
| 203 | IGnssNavigationMessageListener mockListener = mock(IGnssNavigationMessageListener.class); |
| 204 | overrideAsBinder(mockListener); |
| 205 | return mockListener; |
| 206 | } |
| 207 | |
| 208 | private GnssMeasurementCorrections createDummyGnssMeasurementCorrections() { |
| 209 | GnssSingleSatCorrection gnssSingleSatCorrection = |
| 210 | new GnssSingleSatCorrection.Builder().build(); |
| 211 | return |
| 212 | new GnssMeasurementCorrections.Builder().setSingleSatelliteCorrectionList( |
| 213 | Arrays.asList(gnssSingleSatCorrection)).build(); |
| 214 | } |
| 215 | |
| 216 | private void enableLocationPermissions() { |
| 217 | Mockito.doThrow(new SecurityException()).when( |
| 218 | mMockContext).enforceCallingPermission( |
| 219 | AdditionalMatchers.and( |
| 220 | AdditionalMatchers.not(eq(Manifest.permission.LOCATION_HARDWARE)), |
| 221 | AdditionalMatchers.not(eq(Manifest.permission.ACCESS_FINE_LOCATION))), |
| 222 | anyString()); |
| 223 | when(mMockContext.checkPermission( |
| 224 | eq(android.Manifest.permission.LOCATION_HARDWARE), anyInt(), anyInt())).thenReturn( |
| 225 | PackageManager.PERMISSION_GRANTED); |
| 226 | |
| 227 | // AppOpsManager will return true if OP_FINE_LOCATION is checked |
| 228 | when(mMockAppOpsManager.checkOp(anyInt(), anyInt(), anyString())).thenAnswer( |
| 229 | (InvocationOnMock invocation) -> { |
| 230 | int code = (int) (invocation.getArguments()[0]); |
| 231 | if (code == AppOpsManager.OP_FINE_LOCATION) { |
| 232 | return AppOpsManager.MODE_ALLOWED; |
| 233 | } |
| 234 | return AppOpsManager.MODE_ERRORED; |
| 235 | }); |
| 236 | } |
| 237 | |
| 238 | private void disableLocationPermissions() { |
| 239 | Mockito.doThrow(new SecurityException()).when( |
| 240 | mMockContext).enforceCallingPermission(anyString(), anyString()); |
| 241 | Mockito.doThrow(new SecurityException()).when( |
| 242 | mMockContext).checkPermission(anyString(), anyInt(), anyInt()); |
| 243 | |
| 244 | when(mMockAppOpsManager.checkOp(anyInt(), anyInt(), |
| 245 | anyString())).thenReturn(AppOpsManager.MODE_ERRORED); |
| 246 | } |
| 247 | |
| 248 | private GnssStatusListenerHelper createGnssStatusListenerHelper(Context context, |
| 249 | Handler handler) { |
| 250 | return new GnssStatusListenerHelper( |
| 251 | context, handler) { |
| 252 | @Override |
| 253 | protected boolean isAvailableInPlatform() { |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | protected boolean isGpsEnabled() { |
| 259 | return true; |
| 260 | } |
| 261 | }; |
| 262 | } |
| 263 | |
| 264 | private GnssMeasurementsProvider createGnssMeasurementsProvider(Context context, |
| 265 | Handler handler) { |
| 266 | GnssMeasurementProviderNative |
| 267 | mockGnssMeasurementProviderNative = mock(GnssMeasurementProviderNative.class); |
| 268 | return new GnssMeasurementsProvider( |
| 269 | context, handler, mockGnssMeasurementProviderNative) { |
| 270 | @Override |
| 271 | protected boolean isGpsEnabled() { |
| 272 | return true; |
| 273 | } |
| 274 | }; |
| 275 | } |
| 276 | |
| 277 | private GnssNavigationMessageProvider createGnssNavigationMessageProvider(Context context, |
| 278 | Handler handler) { |
| 279 | GnssNavigationMessageProviderNative mockGnssNavigationMessageProviderNative = mock( |
| 280 | GnssNavigationMessageProviderNative.class); |
| 281 | return new GnssNavigationMessageProvider(context, handler, |
| 282 | mockGnssNavigationMessageProviderNative) { |
| 283 | @Override |
| 284 | protected boolean isGpsEnabled() { |
| 285 | return true; |
| 286 | } |
| 287 | }; |
| 288 | } |
| 289 | |
| 290 | @Test |
| 291 | public void getGnssYearOfHardwareTest() { |
| 292 | final int gnssYearOfHardware = 2012; |
| 293 | when(mMockGnssSystemInfoProvider.getGnssYearOfHardware()).thenReturn(gnssYearOfHardware); |
| 294 | enableLocationPermissions(); |
| 295 | |
| 296 | assertThat(mGnssManagerService.getGnssYearOfHardware()).isEqualTo(gnssYearOfHardware); |
| 297 | } |
| 298 | |
| 299 | @Test |
| 300 | public void getGnssHardwareModelNameTest() { |
| 301 | final String gnssHardwareModelName = "hardwarename"; |
| 302 | when(mMockGnssSystemInfoProvider.getGnssHardwareModelName()).thenReturn( |
| 303 | gnssHardwareModelName); |
| 304 | enableLocationPermissions(); |
| 305 | |
| 306 | assertThat(mGnssManagerService.getGnssHardwareModelName()).isEqualTo( |
| 307 | gnssHardwareModelName); |
| 308 | } |
| 309 | |
| 310 | @Test |
| 311 | public void getGnssCapabilitiesWithoutPermissionsTest() { |
| 312 | disableLocationPermissions(); |
| 313 | |
| 314 | assertThrows(SecurityException.class, |
| 315 | () -> mGnssManagerService.getGnssCapabilities("com.android.server")); |
| 316 | } |
| 317 | |
| 318 | @Test |
| 319 | public void getGnssCapabilitiesWithPermissionsTest() { |
| 320 | final long mGnssCapabilities = 23132L; |
| 321 | when(mMockGnssCapabilitiesProvider.getGnssCapabilities()).thenReturn(mGnssCapabilities); |
| 322 | enableLocationPermissions(); |
| 323 | |
| 324 | assertThat(mGnssManagerService.getGnssCapabilities("com.android.server")).isEqualTo( |
| 325 | mGnssCapabilities); |
| 326 | } |
| 327 | |
| 328 | @Test |
| 329 | public void getGnssBatchSizeWithoutPermissionsTest() { |
| 330 | disableLocationPermissions(); |
| 331 | |
| 332 | assertThrows(SecurityException.class, |
| 333 | () -> mGnssManagerService.getGnssBatchSize("com.android.server")); |
| 334 | } |
| 335 | |
| 336 | @Test |
| 337 | public void getGnssBatchSizeWithPermissionsTest() { |
| 338 | final int gnssBatchSize = 10; |
| 339 | when(mMockGnssBatchingProvider.getBatchSize()).thenReturn(gnssBatchSize); |
| 340 | enableLocationPermissions(); |
| 341 | |
| 342 | assertThat(mGnssManagerService.getGnssBatchSize("com.android.server")).isEqualTo( |
| 343 | gnssBatchSize); |
| 344 | } |
| 345 | |
| 346 | @Test |
| 347 | public void startGnssBatchWithoutPermissionsTest() { |
| 348 | final long periodNanos = 100L; |
| 349 | final boolean wakeOnFifoFull = true; |
| 350 | |
| 351 | disableLocationPermissions(); |
| 352 | |
| 353 | assertThrows(SecurityException.class, |
| 354 | () -> mGnssManagerService.startGnssBatch(periodNanos, wakeOnFifoFull, |
| 355 | "com.android.server")); |
| 356 | verify(mMockGnssBatchingProvider, times(0)).start(periodNanos, wakeOnFifoFull); |
| 357 | } |
| 358 | |
| 359 | @Test |
| 360 | public void startGnssBatchWithPermissionsTest() { |
| 361 | final long periodNanos = 100L; |
| 362 | final boolean wakeOnFifoFull = true; |
| 363 | |
| 364 | enableLocationPermissions(); |
| 365 | |
| 366 | assertThat(mGnssManagerService.startGnssBatch(periodNanos, wakeOnFifoFull, |
| 367 | "com.android.server")) |
| 368 | .isEqualTo( |
| 369 | true); |
| 370 | verify(mMockGnssBatchingProvider, times(1)).start(100L, true); |
| 371 | } |
| 372 | |
| 373 | @Test |
| 374 | public void addGnssBatchCallbackWithoutPermissionsTest() throws RemoteException { |
| 375 | IBatchedLocationCallback mockBatchedLocationCallback = createMockBatchedLocationCallback(); |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 376 | List<Location> mockLocationList = new ArrayList<>(); |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 377 | |
| 378 | disableLocationPermissions(); |
| 379 | |
| 380 | assertThrows(SecurityException.class, () -> mGnssManagerService.addGnssBatchingCallback( |
| 381 | mockBatchedLocationCallback, "com.android.server", "abcd123", |
| 382 | "TestBatchedLocationCallback")); |
| 383 | |
| 384 | mGnssManagerService.onReportLocation(mockLocationList); |
| 385 | |
| 386 | verify(mockBatchedLocationCallback, times(0)).onLocationBatch(mockLocationList); |
| 387 | } |
| 388 | |
| 389 | @Test |
| 390 | public void addGnssBatchCallbackWithPermissionsTest() throws RemoteException { |
| 391 | IBatchedLocationCallback mockBatchedLocationCallback = createMockBatchedLocationCallback(); |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 392 | List<Location> mockLocationList = new ArrayList<>(); |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 393 | |
| 394 | enableLocationPermissions(); |
| 395 | |
| 396 | assertThat(mGnssManagerService.addGnssBatchingCallback( |
| 397 | mockBatchedLocationCallback, "com.android.server", |
| 398 | "abcd123", "TestBatchedLocationCallback")).isEqualTo(true); |
| 399 | |
| 400 | mGnssManagerService.onReportLocation(mockLocationList); |
| 401 | |
| 402 | verify(mockBatchedLocationCallback, times(1)).onLocationBatch(mockLocationList); |
| 403 | } |
| 404 | |
| 405 | @Test |
| 406 | public void replaceGnssBatchCallbackTest() throws RemoteException { |
| 407 | IBatchedLocationCallback mockBatchedLocationCallback1 = createMockBatchedLocationCallback(); |
| 408 | IBatchedLocationCallback mockBatchedLocationCallback2 = createMockBatchedLocationCallback(); |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 409 | List<Location> mockLocationList = new ArrayList<>(); |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 410 | |
| 411 | enableLocationPermissions(); |
| 412 | |
| 413 | assertThat(mGnssManagerService.addGnssBatchingCallback( |
| 414 | mockBatchedLocationCallback1, "com.android.server", |
| 415 | "abcd123", "TestBatchedLocationCallback")).isEqualTo(true); |
| 416 | assertThat(mGnssManagerService.addGnssBatchingCallback( |
| 417 | mockBatchedLocationCallback2, "com.android.server", |
| 418 | "abcd123", "TestBatchedLocationCallback")).isEqualTo(true); |
| 419 | |
| 420 | mGnssManagerService.onReportLocation(mockLocationList); |
| 421 | |
| 422 | verify(mockBatchedLocationCallback1, times(0)).onLocationBatch(mockLocationList); |
| 423 | verify(mockBatchedLocationCallback2, times(1)).onLocationBatch(mockLocationList); |
| 424 | } |
| 425 | |
| 426 | @Test |
| 427 | public void flushGnssBatchWithoutPermissionsTest() { |
| 428 | disableLocationPermissions(); |
| 429 | |
| 430 | assertThrows(SecurityException.class, |
| 431 | () -> mGnssManagerService.flushGnssBatch("com.android.server")); |
| 432 | verify(mMockGnssBatchingProvider, times(0)).flush(); |
| 433 | } |
| 434 | |
| 435 | @Test |
| 436 | public void flushGnssBatchWithPermissionsTest() { |
| 437 | enableLocationPermissions(); |
| 438 | mGnssManagerService.flushGnssBatch("com.android.server"); |
| 439 | |
| 440 | verify(mMockGnssBatchingProvider, times(1)).flush(); |
| 441 | } |
| 442 | |
| 443 | @Test |
| 444 | public void removeGnssBatchingCallbackWithoutPermissionsTest() throws RemoteException { |
| 445 | IBatchedLocationCallback mockBatchedLocationCallback = createMockBatchedLocationCallback(); |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 446 | List<Location> mockLocationList = new ArrayList<>(); |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 447 | |
| 448 | enableLocationPermissions(); |
| 449 | |
| 450 | mGnssManagerService.addGnssBatchingCallback(mockBatchedLocationCallback, |
| 451 | "com.android.server", "abcd123", "TestBatchedLocationCallback"); |
| 452 | |
| 453 | disableLocationPermissions(); |
| 454 | |
| 455 | assertThrows(SecurityException.class, |
| 456 | () -> mGnssManagerService.removeGnssBatchingCallback()); |
| 457 | |
| 458 | mGnssManagerService.onReportLocation(mockLocationList); |
| 459 | |
| 460 | verify(mockBatchedLocationCallback, times(1)).onLocationBatch(mockLocationList); |
| 461 | } |
| 462 | |
| 463 | @Test |
| 464 | public void removeGnssBatchingCallbackWithPermissionsTest() throws RemoteException { |
| 465 | IBatchedLocationCallback mockBatchedLocationCallback = createMockBatchedLocationCallback(); |
Soonil Nagarkar | f20d10e | 2020-01-27 11:05:40 -0800 | [diff] [blame] | 466 | List<Location> mockLocationList = new ArrayList<>(); |
Sasha Kuznetsov | b9f26b4 | 2019-10-03 17:30:46 -0700 | [diff] [blame] | 467 | |
| 468 | enableLocationPermissions(); |
| 469 | |
| 470 | mGnssManagerService.addGnssBatchingCallback(mockBatchedLocationCallback, |
| 471 | "com.android.server", "abcd123", "TestBatchedLocationCallback"); |
| 472 | |
| 473 | mGnssManagerService.removeGnssBatchingCallback(); |
| 474 | |
| 475 | mGnssManagerService.onReportLocation(mockLocationList); |
| 476 | |
| 477 | verify(mockBatchedLocationCallback, times(0)).onLocationBatch(mockLocationList); |
| 478 | } |
| 479 | |
| 480 | @Test |
| 481 | public void stopGnssBatchWithoutPermissionsTest() { |
| 482 | disableLocationPermissions(); |
| 483 | |
| 484 | assertThrows(SecurityException.class, () -> mGnssManagerService.stopGnssBatch()); |
| 485 | verify(mMockGnssBatchingProvider, times(0)).stop(); |
| 486 | } |
| 487 | |
| 488 | @Test |
| 489 | public void stopGnssBatchWithPermissionsTest() { |
| 490 | enableLocationPermissions(); |
| 491 | |
| 492 | assertThat(mGnssManagerService.stopGnssBatch()).isEqualTo(true); |
| 493 | verify(mMockGnssBatchingProvider, times(1)).stop(); |
| 494 | } |
| 495 | |
| 496 | @Test |
| 497 | public void registerGnssStatusCallbackWithoutPermissionsTest() throws RemoteException { |
| 498 | final int timeToFirstFix = 20000; |
| 499 | IGnssStatusListener mockGnssStatusListener = createMockGnssStatusListener(); |
| 500 | |
| 501 | disableLocationPermissions(); |
| 502 | |
| 503 | assertThrows(SecurityException.class, () -> mGnssManagerService |
| 504 | .registerGnssStatusCallback( |
| 505 | mockGnssStatusListener, "com.android.server", "abcd123")); |
| 506 | |
| 507 | mTestGnssStatusProvider.onFirstFix(timeToFirstFix); |
| 508 | |
| 509 | verify(mockGnssStatusListener, times(0)).onFirstFix(timeToFirstFix); |
| 510 | } |
| 511 | |
| 512 | @Test |
| 513 | public void registerGnssStatusCallbackWithPermissionsTest() throws RemoteException { |
| 514 | final int timeToFirstFix = 20000; |
| 515 | IGnssStatusListener mockGnssStatusListener = createMockGnssStatusListener(); |
| 516 | |
| 517 | enableLocationPermissions(); |
| 518 | |
| 519 | assertThat(mGnssManagerService.registerGnssStatusCallback( |
| 520 | mockGnssStatusListener, "com.android.server", "abcd123")).isEqualTo(true); |
| 521 | |
| 522 | mTestGnssStatusProvider.onFirstFix(timeToFirstFix); |
| 523 | |
| 524 | verify(mockGnssStatusListener, times(1)).onFirstFix(timeToFirstFix); |
| 525 | } |
| 526 | |
| 527 | @Test |
| 528 | public void unregisterGnssStatusCallbackWithPermissionsTest() throws RemoteException { |
| 529 | final int timeToFirstFix = 20000; |
| 530 | IGnssStatusListener mockGnssStatusListener = createMockGnssStatusListener(); |
| 531 | |
| 532 | enableLocationPermissions(); |
| 533 | |
| 534 | mGnssManagerService.registerGnssStatusCallback( |
| 535 | mockGnssStatusListener, "com.android.server", "abcd123"); |
| 536 | |
| 537 | mGnssManagerService.unregisterGnssStatusCallback(mockGnssStatusListener); |
| 538 | |
| 539 | mTestGnssStatusProvider.onFirstFix(timeToFirstFix); |
| 540 | |
| 541 | verify(mockGnssStatusListener, times(0)).onFirstFix(timeToFirstFix); |
| 542 | } |
| 543 | |
| 544 | @Test |
| 545 | public void addGnssMeasurementsListenerWithoutPermissionsTest() throws RemoteException { |
| 546 | IGnssMeasurementsListener mockGnssMeasurementsListener = |
| 547 | createMockGnssMeasurementsListener(); |
| 548 | GnssMeasurementsEvent gnssMeasurementsEvent = new GnssMeasurementsEvent(new GnssClock(), |
| 549 | null); |
| 550 | |
| 551 | disableLocationPermissions(); |
| 552 | |
| 553 | assertThrows(SecurityException.class, |
| 554 | () -> mGnssManagerService.addGnssMeasurementsListener( |
| 555 | mockGnssMeasurementsListener, |
| 556 | "com.android.server", "abcd123", "TestGnssMeasurementsListener")); |
| 557 | |
| 558 | mTestGnssMeasurementsProvider.onMeasurementsAvailable(gnssMeasurementsEvent); |
| 559 | verify(mockGnssMeasurementsListener, times(0)).onGnssMeasurementsReceived( |
| 560 | gnssMeasurementsEvent); |
| 561 | } |
| 562 | |
| 563 | @Test |
| 564 | public void addGnssMeasurementsListenerWithPermissionsTest() throws RemoteException { |
| 565 | IGnssMeasurementsListener mockGnssMeasurementsListener = |
| 566 | createMockGnssMeasurementsListener(); |
| 567 | GnssMeasurementsEvent gnssMeasurementsEvent = new GnssMeasurementsEvent(new GnssClock(), |
| 568 | null); |
| 569 | |
| 570 | enableLocationPermissions(); |
| 571 | |
| 572 | assertThat(mGnssManagerService.addGnssMeasurementsListener(mockGnssMeasurementsListener, |
| 573 | "com.android.server", "abcd123", "TestGnssMeasurementsListener")).isEqualTo(true); |
| 574 | |
| 575 | mTestGnssMeasurementsProvider.onMeasurementsAvailable(gnssMeasurementsEvent); |
| 576 | verify(mockGnssMeasurementsListener, times(1)).onGnssMeasurementsReceived( |
| 577 | gnssMeasurementsEvent); |
| 578 | } |
| 579 | |
| 580 | @Test |
| 581 | public void injectGnssMeasurementCorrectionsWithoutPermissionsTest() { |
| 582 | GnssMeasurementCorrections gnssMeasurementCorrections = |
| 583 | createDummyGnssMeasurementCorrections(); |
| 584 | |
| 585 | disableLocationPermissions(); |
| 586 | |
| 587 | assertThrows(SecurityException.class, |
| 588 | () -> mGnssManagerService.injectGnssMeasurementCorrections( |
| 589 | gnssMeasurementCorrections, "com.android.server")); |
| 590 | verify(mMockGnssMeasurementCorrectionsProvider, times(0)) |
| 591 | .injectGnssMeasurementCorrections( |
| 592 | gnssMeasurementCorrections); |
| 593 | } |
| 594 | |
| 595 | @Test |
| 596 | public void injectGnssMeasurementCorrectionsWithPermissionsTest() { |
| 597 | GnssMeasurementCorrections gnssMeasurementCorrections = |
| 598 | createDummyGnssMeasurementCorrections(); |
| 599 | |
| 600 | enableLocationPermissions(); |
| 601 | |
| 602 | mGnssManagerService.injectGnssMeasurementCorrections( |
| 603 | gnssMeasurementCorrections, "com.android.server"); |
| 604 | verify(mMockGnssMeasurementCorrectionsProvider, times(1)) |
| 605 | .injectGnssMeasurementCorrections( |
| 606 | gnssMeasurementCorrections); |
| 607 | } |
| 608 | |
| 609 | @Test |
| 610 | public void removeGnssMeasurementsListenerWithoutPermissionsTest() throws RemoteException { |
| 611 | IGnssMeasurementsListener mockGnssMeasurementsListener = |
| 612 | createMockGnssMeasurementsListener(); |
| 613 | GnssMeasurementsEvent gnssMeasurementsEvent = new GnssMeasurementsEvent(new GnssClock(), |
| 614 | null); |
| 615 | |
| 616 | enableLocationPermissions(); |
| 617 | |
| 618 | mGnssManagerService.addGnssMeasurementsListener(mockGnssMeasurementsListener, |
| 619 | "com.android.server", "abcd123", "TestGnssMeasurementsListener"); |
| 620 | |
| 621 | disableLocationPermissions(); |
| 622 | |
| 623 | mGnssManagerService.removeGnssMeasurementsListener( |
| 624 | mockGnssMeasurementsListener); |
| 625 | |
| 626 | mTestGnssMeasurementsProvider.onMeasurementsAvailable(gnssMeasurementsEvent); |
| 627 | verify(mockGnssMeasurementsListener, times(0)).onGnssMeasurementsReceived( |
| 628 | gnssMeasurementsEvent); |
| 629 | } |
| 630 | |
| 631 | @Test |
| 632 | public void removeGnssMeasurementsListenerWithPermissionsTest() throws RemoteException { |
| 633 | IGnssMeasurementsListener mockGnssMeasurementsListener = |
| 634 | createMockGnssMeasurementsListener(); |
| 635 | GnssMeasurementsEvent gnssMeasurementsEvent = new GnssMeasurementsEvent(new GnssClock(), |
| 636 | null); |
| 637 | |
| 638 | enableLocationPermissions(); |
| 639 | |
| 640 | mGnssManagerService.addGnssMeasurementsListener(mockGnssMeasurementsListener, |
| 641 | "com.android.server", "abcd123", "TestGnssMeasurementsListener"); |
| 642 | |
| 643 | disableLocationPermissions(); |
| 644 | |
| 645 | mGnssManagerService.removeGnssMeasurementsListener( |
| 646 | mockGnssMeasurementsListener); |
| 647 | |
| 648 | mTestGnssMeasurementsProvider.onMeasurementsAvailable(gnssMeasurementsEvent); |
| 649 | verify(mockGnssMeasurementsListener, times(0)).onGnssMeasurementsReceived( |
| 650 | gnssMeasurementsEvent); |
| 651 | } |
| 652 | |
| 653 | @Test |
| 654 | public void addGnssNavigationMessageListenerWithoutPermissionsTest() throws RemoteException { |
| 655 | IGnssNavigationMessageListener mockGnssNavigationMessageListener = |
| 656 | createMockGnssNavigationMessageListener(); |
| 657 | GnssNavigationMessage gnssNavigationMessage = new GnssNavigationMessage(); |
| 658 | |
| 659 | disableLocationPermissions(); |
| 660 | |
| 661 | assertThrows(SecurityException.class, |
| 662 | () -> mGnssManagerService.addGnssNavigationMessageListener( |
| 663 | mockGnssNavigationMessageListener, "com.android.server", |
| 664 | "abcd123", "TestGnssNavigationMessageListener")); |
| 665 | |
| 666 | mTestGnssNavigationMessageProvider.onNavigationMessageAvailable(gnssNavigationMessage); |
| 667 | |
| 668 | verify(mockGnssNavigationMessageListener, times(0)).onGnssNavigationMessageReceived( |
| 669 | gnssNavigationMessage); |
| 670 | } |
| 671 | |
| 672 | @Test |
| 673 | public void addGnssNavigationMessageListenerWithPermissionsTest() throws RemoteException { |
| 674 | IGnssNavigationMessageListener mockGnssNavigationMessageListener = |
| 675 | createMockGnssNavigationMessageListener(); |
| 676 | GnssNavigationMessage gnssNavigationMessage = new GnssNavigationMessage(); |
| 677 | |
| 678 | enableLocationPermissions(); |
| 679 | |
| 680 | assertThat(mGnssManagerService.addGnssNavigationMessageListener( |
| 681 | mockGnssNavigationMessageListener, "com.android.server", |
| 682 | "abcd123", "TestGnssNavigationMessageListener")).isEqualTo(true); |
| 683 | |
| 684 | mTestGnssNavigationMessageProvider.onNavigationMessageAvailable(gnssNavigationMessage); |
| 685 | |
| 686 | verify(mockGnssNavigationMessageListener, times(1)).onGnssNavigationMessageReceived( |
| 687 | gnssNavigationMessage); |
| 688 | } |
| 689 | |
| 690 | @Test |
| 691 | public void removeGnssNavigationMessageListenerWithoutPermissionsTest() throws RemoteException { |
| 692 | IGnssNavigationMessageListener mockGnssNavigationMessageListener = |
| 693 | createMockGnssNavigationMessageListener(); |
| 694 | GnssNavigationMessage gnssNavigationMessage = new GnssNavigationMessage(); |
| 695 | |
| 696 | enableLocationPermissions(); |
| 697 | |
| 698 | mGnssManagerService.addGnssNavigationMessageListener( |
| 699 | mockGnssNavigationMessageListener, "com.android.server", |
| 700 | "abcd123", "TestGnssNavigationMessageListener"); |
| 701 | |
| 702 | disableLocationPermissions(); |
| 703 | |
| 704 | mGnssManagerService.removeGnssNavigationMessageListener( |
| 705 | mockGnssNavigationMessageListener); |
| 706 | |
| 707 | mTestGnssNavigationMessageProvider.onNavigationMessageAvailable(gnssNavigationMessage); |
| 708 | |
| 709 | verify(mockGnssNavigationMessageListener, times(0)).onGnssNavigationMessageReceived( |
| 710 | gnssNavigationMessage); |
| 711 | } |
| 712 | |
| 713 | @Test |
| 714 | public void removeGnssNavigationMessageListenerWithPermissionsTest() throws RemoteException { |
| 715 | IGnssNavigationMessageListener mockGnssNavigationMessageListener = |
| 716 | createMockGnssNavigationMessageListener(); |
| 717 | GnssNavigationMessage gnssNavigationMessage = new GnssNavigationMessage(); |
| 718 | |
| 719 | enableLocationPermissions(); |
| 720 | |
| 721 | mGnssManagerService.addGnssNavigationMessageListener( |
| 722 | mockGnssNavigationMessageListener, "com.android.server", |
| 723 | "abcd123", "TestGnssNavigationMessageListener"); |
| 724 | |
| 725 | mGnssManagerService.removeGnssNavigationMessageListener( |
| 726 | mockGnssNavigationMessageListener); |
| 727 | |
| 728 | mTestGnssNavigationMessageProvider.onNavigationMessageAvailable(gnssNavigationMessage); |
| 729 | |
| 730 | verify(mockGnssNavigationMessageListener, times(0)).onGnssNavigationMessageReceived( |
| 731 | gnssNavigationMessage); |
| 732 | } |
| 733 | |
| 734 | @Test |
| 735 | public void sendNiResponseWithPermissionsTest() throws RemoteException { |
| 736 | when(mMockNetInitiatedListener.sendNiResponse(anyInt(), anyInt())).thenReturn(true); |
| 737 | |
| 738 | int notifId = 0; |
| 739 | int userResponse = 0; |
| 740 | enableLocationPermissions(); |
| 741 | |
| 742 | assertThat(mGnssManagerService.sendNiResponse(notifId, userResponse)).isEqualTo(true); |
| 743 | |
| 744 | verify(mMockNetInitiatedListener, times(1)).sendNiResponse(notifId, userResponse); |
| 745 | } |
| 746 | } |