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