1 /* 2 * Copyright (C) 2023 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.am; 18 19 import static android.app.ActivityManager.FOREGROUND_SERVICE_API_TYPE_CAMERA; 20 import static android.app.ActivityManager.FOREGROUND_SERVICE_API_TYPE_MICROPHONE; 21 22 import static com.android.server.am.ForegroundServiceTypeLoggerModule.FGS_API_BEGIN_WITH_FGS; 23 import static com.android.server.am.ForegroundServiceTypeLoggerModule.FGS_API_END_WITHOUT_FGS; 24 import static com.android.server.am.ForegroundServiceTypeLoggerModule.FGS_API_END_WITH_FGS; 25 import static com.android.server.am.ForegroundServiceTypeLoggerModule.FGS_STATE_CHANGED_API_CALL; 26 27 import static org.mockito.ArgumentMatchers.any; 28 import static org.mockito.ArgumentMatchers.anyInt; 29 import static org.mockito.ArgumentMatchers.anyLong; 30 import static org.mockito.ArgumentMatchers.eq; 31 import static org.mockito.Mockito.doNothing; 32 import static org.mockito.Mockito.mock; 33 import static org.mockito.Mockito.reset; 34 import static org.mockito.Mockito.spy; 35 import static org.mockito.Mockito.times; 36 import static org.mockito.Mockito.verify; 37 38 import android.content.pm.ServiceInfo; 39 import android.platform.test.annotations.Presubmit; 40 41 import androidx.test.filters.SmallTest; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 46 /** 47 * Test class for {@link ForegroundServiceTypeLoggerModule}. 48 * 49 * Build/Install/Run: 50 * atest FrameworksServicesTests:FgsLoggerTest 51 */ 52 @Presubmit 53 @SmallTest 54 public class FgsLoggerTest { 55 56 ForegroundServiceTypeLoggerModule mFgsLogger; 57 58 @Before setUp()59 public void setUp() { 60 ForegroundServiceTypeLoggerModule logger = new ForegroundServiceTypeLoggerModule(); 61 mFgsLogger = spy(logger); 62 doNothing().when(mFgsLogger) 63 .logFgsApiEvent(any(ServiceRecord.class), 64 anyInt(), anyInt(), anyInt(), anyLong()); 65 doNothing().when(mFgsLogger) 66 .logFgsApiEventWithNoFgs(anyInt(), 67 anyInt(), anyInt(), anyLong()); 68 } 69 70 @Test testFgsStartThenApiStart()71 public void testFgsStartThenApiStart() { 72 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 73 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 74 mFgsLogger.logForegroundServiceStart(1, 1, record); 75 mFgsLogger.logForegroundServiceApiEventBegin(1, 1, 1, "aPackageHasNoName"); 76 int expectedTypes = 1; 77 verify(mFgsLogger).logFgsApiEvent(any(ServiceRecord.class), 78 eq(FGS_STATE_CHANGED_API_CALL), eq(FGS_API_BEGIN_WITH_FGS), 79 eq(expectedTypes), anyLong()); 80 reset(mFgsLogger); 81 mFgsLogger.logForegroundServiceApiEventEnd(1, 1, 1); 82 83 resetAndVerifyZeroInteractions(); 84 85 mFgsLogger.logForegroundServiceStop(1, record); 86 verify(mFgsLogger).logFgsApiEvent(any(ServiceRecord.class), 87 eq(FGS_STATE_CHANGED_API_CALL), eq(FGS_API_END_WITH_FGS), 88 eq(expectedTypes), anyLong()); 89 } 90 91 @Test testApiStartThenFgsStart()92 public void testApiStartThenFgsStart() { 93 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 94 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 95 mFgsLogger.logForegroundServiceApiEventBegin(1, 1, 1, "aPackageHasNoName"); 96 97 resetAndVerifyZeroInteractions(); 98 99 mFgsLogger.logForegroundServiceStart(1, 1, record); 100 int expectedTypes = 1; 101 verify(mFgsLogger).logFgsApiEvent(any(ServiceRecord.class), 102 eq(FGS_STATE_CHANGED_API_CALL), eq(FGS_API_BEGIN_WITH_FGS), 103 eq(expectedTypes), anyLong()); 104 reset(mFgsLogger); 105 mFgsLogger.logForegroundServiceApiEventEnd(1, 1, 1); 106 107 resetAndVerifyZeroInteractions(); 108 109 mFgsLogger.logForegroundServiceStop(1, record); 110 verify(mFgsLogger).logFgsApiEvent(any(ServiceRecord.class), 111 eq(FGS_STATE_CHANGED_API_CALL), eq(FGS_API_END_WITH_FGS), 112 eq(expectedTypes), anyLong()); 113 } 114 115 @Test testFgsStartApiStartFgsStopApiStop()116 public void testFgsStartApiStartFgsStopApiStop() { 117 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 118 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 119 reset(mFgsLogger); 120 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 121 1, 1, "aPackageHasNoName"); 122 123 resetAndVerifyZeroInteractions(); 124 125 int expectedTypes = 1; 126 127 mFgsLogger.logForegroundServiceStart(1, 1, record); 128 verify(mFgsLogger).logFgsApiEvent(any(ServiceRecord.class), 129 eq(FGS_STATE_CHANGED_API_CALL), eq(FGS_API_BEGIN_WITH_FGS), 130 eq(expectedTypes), anyLong()); 131 reset(mFgsLogger); 132 mFgsLogger.logForegroundServiceStop(1, record); 133 134 resetAndVerifyZeroInteractions(); 135 136 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 137 verify(mFgsLogger).logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 138 eq(expectedTypes), anyLong()); 139 } 140 141 @Test testApiStartStopNoFgs()142 public void testApiStartStopNoFgs() throws InterruptedException { 143 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 144 1, 1, "aPackageHasNoName"); 145 Thread.sleep(2000); 146 147 resetAndVerifyZeroInteractions(); 148 149 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 150 151 resetAndVerifyZeroInteractions(); 152 } 153 154 @Test testApiStartStopFgs()155 public void testApiStartStopFgs() throws InterruptedException { 156 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 157 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 158 159 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 160 1, 1, "aPackageHasNoName"); 161 Thread.sleep(2000); 162 163 resetAndVerifyZeroInteractions(); 164 165 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 166 167 resetAndVerifyZeroInteractions(); 168 169 mFgsLogger.logForegroundServiceStart(1, 1, record); 170 171 resetAndVerifyZeroInteractions(); 172 } 173 174 @Test testFgsStartStopApiStartStop()175 public void testFgsStartStopApiStartStop() throws InterruptedException { 176 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 177 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 178 179 mFgsLogger.logForegroundServiceStart(1, 1, record); 180 181 resetAndVerifyZeroInteractions(); 182 183 mFgsLogger.logForegroundServiceStop(1, record); 184 185 resetAndVerifyZeroInteractions(); 186 187 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 188 1, 1, "aPackageHasNoName"); 189 Thread.sleep(2000); 190 191 resetAndVerifyZeroInteractions(); 192 193 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 194 195 resetAndVerifyZeroInteractions(); 196 } 197 198 @Test testMultipleStartStopApis()199 public void testMultipleStartStopApis() throws InterruptedException { 200 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 201 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 202 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin( 203 FOREGROUND_SERVICE_API_TYPE_CAMERA, 204 1, 1, "aPackageHasNoName"); 205 Thread.sleep(1000); 206 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 207 1, 1, "aPackageHasNoName"); 208 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 209 1, 1, "aPackageHasNoName"); 210 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 211 1, 1, "aPackageHasNoName"); 212 Thread.sleep(1000); 213 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 214 1, 1, "aPackageHasNoName"); 215 216 resetAndVerifyZeroInteractions(); 217 218 // now start an FGS 219 mFgsLogger.logForegroundServiceStart(1, 1, record); 220 // now we should see exactly one call logged 221 // and this should be the very first call 222 // we also try to verify the time as being the very first call 223 int expectedTypes = 1; 224 long expectedTimestamp = timeStamp; 225 226 verify(mFgsLogger, times(1)) 227 .logFgsApiEvent(any(ServiceRecord.class), 228 eq(FGS_STATE_CHANGED_API_CALL), 229 eq(FGS_API_BEGIN_WITH_FGS), 230 eq(expectedTypes), 231 eq(expectedTimestamp)); 232 reset(mFgsLogger); 233 // now we do multiple stops 234 // only the last one should be logged 235 mFgsLogger.logForegroundServiceStop(1, record); 236 237 resetAndVerifyZeroInteractions(); 238 239 // log the API stops 240 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 241 Thread.sleep(1000); 242 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 243 Thread.sleep(1000); 244 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 245 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 246 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 247 1, 1); 248 expectedTimestamp = timeStamp; 249 verify(mFgsLogger, times(1)) 250 .logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 251 eq(expectedTypes), 252 eq(expectedTimestamp)); 253 } 254 255 @Test testMultipleStartStops()256 public void testMultipleStartStops() throws InterruptedException { 257 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 258 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 259 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin( 260 FOREGROUND_SERVICE_API_TYPE_CAMERA, 261 1, 1, "aPackageHasNoName"); 262 Thread.sleep(1000); 263 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 264 1, 1, "aPackageHasNoName"); 265 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 266 1, 1, "aPackageHasNoName"); 267 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 268 1, 1, "aPackageHasNoName"); 269 Thread.sleep(1000); 270 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 271 1, 1, "aPackageHasNoName"); 272 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 273 1, 1); 274 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 275 1, 1, "aPackageHasNoName"); 276 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 277 1, 1); 278 279 resetAndVerifyZeroInteractions(); 280 281 // now start an FGS 282 mFgsLogger.logForegroundServiceStart(1, 1, record); 283 // now we should see exactly one call logged 284 // and this should be the very first call 285 // we also try to verify the time as being the very first call 286 int expectedTypes = 1; 287 long expectedTimestamp = timeStamp; 288 289 verify(mFgsLogger, times(1)) 290 .logFgsApiEvent(any(ServiceRecord.class), 291 eq(FGS_STATE_CHANGED_API_CALL), 292 eq(FGS_API_BEGIN_WITH_FGS), 293 eq(expectedTypes), 294 eq(expectedTimestamp)); 295 reset(mFgsLogger); 296 // now we do multiple stops 297 // only the last one should be logged 298 mFgsLogger.logForegroundServiceStop(1, record); 299 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 300 "aPackageHasNoName"); 301 302 resetAndVerifyZeroInteractions(); 303 304 // log the API stops 305 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 306 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 307 "aPackageHasNoName"); 308 Thread.sleep(1000); 309 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 310 Thread.sleep(1000); 311 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 312 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 313 1, 1); 314 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 315 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 316 expectedTimestamp = timeStamp; 317 verify(mFgsLogger, times(1)) 318 .logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 319 eq(expectedTypes), 320 eq(expectedTimestamp)); 321 } 322 323 @Test testMultiStartStopThroughout()324 public void testMultiStartStopThroughout() throws InterruptedException { 325 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 326 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 327 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin( 328 FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, "aPackageHasNoName"); 329 Thread.sleep(1000); 330 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 331 "aPackageHasNoName"); 332 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 333 "aPackageHasNoName"); 334 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 335 "aPackageHasNoName"); 336 Thread.sleep(1000); 337 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 338 "aPackageHasNoName"); 339 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 340 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 341 "aPackageHasNoName"); 342 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 343 344 resetAndVerifyZeroInteractions(); 345 346 // now start an FGS 347 mFgsLogger.logForegroundServiceStart(1, 1, record); 348 // now we should see exactly one call logged 349 // and this should be the very first call 350 // we also try to verify the time as being the very first call 351 int expectedTypes = 1; 352 long expectedTimestamp = timeStamp; 353 354 verify(mFgsLogger, times(1)) 355 .logFgsApiEvent(any(ServiceRecord.class), 356 eq(FGS_STATE_CHANGED_API_CALL), 357 eq(FGS_API_BEGIN_WITH_FGS), 358 eq(expectedTypes), 359 eq(expectedTimestamp)); 360 reset(mFgsLogger); 361 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 362 "aPackageHasNoName"); 363 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 364 365 resetAndVerifyZeroInteractions(); 366 367 // now we do multiple stops 368 // only the last one should be logged 369 mFgsLogger.logForegroundServiceStop(1, record); 370 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 371 "aPackageHasNoName"); 372 resetAndVerifyZeroInteractions(); 373 // log the API stops 374 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 375 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 376 "aPackageHasNoName"); 377 Thread.sleep(1000); 378 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 379 Thread.sleep(1000); 380 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 381 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 382 1, 1); 383 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 384 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 385 expectedTimestamp = timeStamp; 386 verify(mFgsLogger, times(1)) 387 .logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 388 eq(expectedTypes), 389 eq(expectedTimestamp)); 390 } 391 392 @Test testMultipleFGS()393 public void testMultipleFGS() throws InterruptedException { 394 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 395 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 396 ActivityManagerService ams = mock(ActivityManagerService.class); 397 ServiceRecord record2 = ServiceRecord.newEmptyInstanceForTest(ams); 398 record2.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 399 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin( 400 FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, "aPackageHasNoName"); 401 Thread.sleep(1000); 402 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 403 "aPackageHasNoName"); 404 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 405 "aPackageHasNoName"); 406 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 407 "aPackageHasNoName"); 408 Thread.sleep(1000); 409 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 410 "aPackageHasNoName"); 411 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 412 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 413 "aPackageHasNoName"); 414 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 415 resetAndVerifyZeroInteractions(); 416 // now start an FGS 417 mFgsLogger.logForegroundServiceStart(1, 1, record); 418 mFgsLogger.logForegroundServiceStart(1, 1, record2); 419 // now we should see exactly one call logged 420 // and this should be the very first call 421 // we also try to verify the time as being the very first call 422 int expectedTypes = 1; 423 long expectedTimestamp = timeStamp; 424 425 verify(mFgsLogger, times(1)) 426 .logFgsApiEvent(any(ServiceRecord.class), 427 eq(FGS_STATE_CHANGED_API_CALL), 428 eq(FGS_API_BEGIN_WITH_FGS), 429 eq(expectedTypes), 430 eq(expectedTimestamp)); 431 reset(mFgsLogger); 432 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 433 "aPackageHasNoName"); 434 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 435 436 resetAndVerifyZeroInteractions(); 437 438 // now we do multiple stops 439 // only the last one should be logged 440 mFgsLogger.logForegroundServiceStop(1, record); 441 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 442 "aPackageHasNoName"); 443 444 resetAndVerifyZeroInteractions(); 445 446 // log the API stops 447 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 448 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 449 "aPackageHasNoName"); 450 Thread.sleep(1000); 451 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 452 Thread.sleep(1000); 453 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 454 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(1, 1, 1); 455 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 456 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 457 expectedTimestamp = timeStamp; 458 verify(mFgsLogger, times(1)) 459 .logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 460 eq(expectedTypes), 461 eq(expectedTimestamp)); 462 } 463 464 @Test testMultipleUid()465 public void testMultipleUid() throws InterruptedException { 466 int expectedTypes = 1; 467 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 468 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 469 ActivityManagerService ams = mock(ActivityManagerService.class); 470 ServiceRecord record2 = ServiceRecord.newEmptyInstanceForTest(ams); 471 record2.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 472 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin(1, 1, 1, "aPackageHasNoName"); 473 Thread.sleep(1000); 474 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 475 "aPackageHasNoName"); 476 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 477 "aPackageHasNoName"); 478 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 479 "aPackageHasNoName"); 480 Thread.sleep(1000); 481 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 482 "aPackageHasNoName"); 483 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 484 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 485 "aPackageHasNoName"); 486 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 487 488 ServiceRecord recordUid2 = ServiceRecord.newEmptyInstanceForTest(null); 489 recordUid2.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 490 ServiceRecord recordUid22 = ServiceRecord.newEmptyInstanceForTest(ams); 491 recordUid22.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 492 long timeStamp2 = mFgsLogger.logForegroundServiceApiEventBegin(1, 2, 1, 493 "aPackageHasNoName"); 494 Thread.sleep(1000); 495 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1, 496 "aPackageHasNoName"); 497 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1, 498 "aPackageHasNoName"); 499 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1, 500 "aPackageHasNoName"); 501 Thread.sleep(1000); 502 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1, 503 "aPackageHasNoName"); 504 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1); 505 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1, 506 "aPackageHasNoName"); 507 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1); 508 509 resetAndVerifyZeroInteractions(); 510 511 // now start an FGS 512 mFgsLogger.logForegroundServiceStart(1, 1, record); 513 mFgsLogger.logForegroundServiceStart(1, 1, record2); 514 515 mFgsLogger.logForegroundServiceStart(2, 1, recordUid2); 516 mFgsLogger.logForegroundServiceStart(2, 1, recordUid22); 517 // now we should see exactly two calls logged 518 // and this should be the very first call 519 // we also try to verify the time as being the very first call 520 verify(mFgsLogger, times(2)) 521 .logFgsApiEvent(any(ServiceRecord.class), 522 eq(FGS_STATE_CHANGED_API_CALL), 523 eq(FGS_API_BEGIN_WITH_FGS), 524 eq(expectedTypes), anyLong()); 525 reset(mFgsLogger); 526 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 527 "aPackageHasNoName"); 528 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 529 530 resetAndVerifyZeroInteractions(); 531 532 // now we do multiple stops 533 // only the last one should be logged 534 mFgsLogger.logForegroundServiceStop(1, record); 535 mFgsLogger.logForegroundServiceStop(2, recordUid2); 536 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 537 "aPackageHasNoName"); 538 539 resetAndVerifyZeroInteractions(); 540 541 // log the API stops 542 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 543 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 544 "aPackageHasNoName"); 545 Thread.sleep(1000); 546 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 547 Thread.sleep(1000); 548 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 549 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 550 1, 1); 551 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 552 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 553 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1); 554 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1); 555 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 2, 1); 556 timeStamp2 = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 557 2, 1); 558 long expectedTimestamp = timeStamp; 559 long expectedTimestamp2 = timeStamp2; 560 verify(mFgsLogger, times(1)) 561 .logFgsApiEventWithNoFgs(eq(1), eq(FGS_API_END_WITHOUT_FGS), 562 eq(expectedTypes), 563 eq(expectedTimestamp)); 564 verify(mFgsLogger, times(1)) 565 .logFgsApiEventWithNoFgs(eq(2), eq(FGS_API_END_WITHOUT_FGS), 566 eq(expectedTypes), 567 eq(expectedTimestamp2)); 568 } 569 570 @Test testMultipleStartStopWithinFgsWindow()571 public void testMultipleStartStopWithinFgsWindow() throws InterruptedException { 572 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 573 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA; 574 575 resetAndVerifyZeroInteractions(); 576 577 // now start an FGS 578 mFgsLogger.logForegroundServiceStart(1, 1, record); 579 long timeStamp = mFgsLogger.logForegroundServiceApiEventBegin( 580 FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, "aPackageHasNoName"); 581 Thread.sleep(1000); 582 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 583 "aPackageHasNoName"); 584 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 585 "aPackageHasNoName"); 586 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 587 "aPackageHasNoName"); 588 Thread.sleep(1000); 589 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 590 "aPackageHasNoName"); 591 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 592 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 593 "aPackageHasNoName"); 594 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 595 // now we should see exactly one call logged 596 // and this should be the very first call 597 // we also try to verify the time as being the very first call 598 int expectedTypes = 1; 599 long expectedTimestamp = timeStamp; 600 601 verify(mFgsLogger, times(1)) 602 .logFgsApiEvent(any(ServiceRecord.class), 603 eq(FGS_STATE_CHANGED_API_CALL), 604 eq(FGS_API_BEGIN_WITH_FGS), 605 eq(expectedTypes), 606 eq(expectedTimestamp)); 607 reset(mFgsLogger); 608 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 609 "aPackageHasNoName"); 610 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 611 612 resetAndVerifyZeroInteractions(); 613 614 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 615 "aPackageHasNoName"); 616 617 resetAndVerifyZeroInteractions(); 618 619 // log the API stops 620 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 621 mFgsLogger.logForegroundServiceApiEventBegin(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, 622 "aPackageHasNoName"); 623 Thread.sleep(1000); 624 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 625 Thread.sleep(1000); 626 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 627 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 628 mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1); 629 timeStamp = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 630 1, 1); 631 632 resetAndVerifyZeroInteractions(); 633 634 // now we do multiple stops 635 // only the last one should be logged 636 mFgsLogger.logForegroundServiceStop(1, record); 637 expectedTimestamp = timeStamp; 638 verify(mFgsLogger, times(1)) 639 .logFgsApiEvent(any(ServiceRecord.class), 640 eq(FGS_STATE_CHANGED_API_CALL), 641 eq(FGS_API_END_WITH_FGS), 642 eq(expectedTypes), 643 eq(expectedTimestamp)); 644 } 645 646 @Test multipleTypesOneFgsTest()647 public void multipleTypesOneFgsTest() { 648 ServiceRecord record = ServiceRecord.newEmptyInstanceForTest(null); 649 record.foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_CAMERA 650 | ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE; 651 652 long timestamp1 = mFgsLogger.logForegroundServiceApiEventBegin( 653 FOREGROUND_SERVICE_API_TYPE_CAMERA, 1, 1, "aPackageHasNoName"); 654 long timestamp2 = mFgsLogger.logForegroundServiceApiEventBegin( 655 FOREGROUND_SERVICE_API_TYPE_MICROPHONE, 656 1, 1, "aPackageHasNoName"); 657 int expectedTypes = 1; 658 int expectedType2 = FOREGROUND_SERVICE_API_TYPE_MICROPHONE; 659 long expectedTimestamp = timestamp1; 660 long expectedTimestamp2 = timestamp2; 661 mFgsLogger.logForegroundServiceStart(1, 1, record); 662 663 verify(mFgsLogger, times(1)) 664 .logFgsApiEvent(any(ServiceRecord.class), 665 eq(FGS_STATE_CHANGED_API_CALL), 666 eq(FGS_API_BEGIN_WITH_FGS), 667 eq(expectedTypes), 668 eq(expectedTimestamp)); 669 670 verify(mFgsLogger, times(1)) 671 .logFgsApiEvent(any(ServiceRecord.class), 672 eq(FGS_STATE_CHANGED_API_CALL), 673 eq(FGS_API_BEGIN_WITH_FGS), 674 eq(expectedType2), 675 eq(expectedTimestamp2)); 676 677 reset(mFgsLogger); 678 resetAndVerifyZeroInteractions(); 679 680 timestamp1 = mFgsLogger.logForegroundServiceApiEventEnd(FOREGROUND_SERVICE_API_TYPE_CAMERA, 681 1, 1); 682 timestamp2 = mFgsLogger.logForegroundServiceApiEventEnd( 683 FOREGROUND_SERVICE_API_TYPE_MICROPHONE, 684 1, 1); 685 686 mFgsLogger.logForegroundServiceStop(1, record); 687 688 expectedTimestamp = timestamp1; 689 expectedTimestamp2 = timestamp2; 690 691 verify(mFgsLogger, times(1)) 692 .logFgsApiEvent(any(ServiceRecord.class), 693 eq(FGS_STATE_CHANGED_API_CALL), 694 eq(FGS_API_END_WITH_FGS), 695 eq(expectedTypes), 696 eq(expectedTimestamp)); 697 verify(mFgsLogger, times(1)) 698 .logFgsApiEvent(any(ServiceRecord.class), 699 eq(FGS_STATE_CHANGED_API_CALL), 700 eq(FGS_API_END_WITH_FGS), 701 eq(expectedType2), 702 eq(expectedTimestamp2)); 703 704 } 705 resetAndVerifyZeroInteractions()706 private void resetAndVerifyZeroInteractions() { 707 doNothing().when(mFgsLogger) 708 .logFgsApiEvent(any(ServiceRecord.class), 709 anyInt(), anyInt(), anyInt(), anyLong()); 710 doNothing().when(mFgsLogger) 711 .logFgsApiEventWithNoFgs(anyInt(), anyInt(), anyInt(), anyLong()); 712 verify(mFgsLogger, times(0)) 713 .logFgsApiEvent(any(ServiceRecord.class), 714 anyInt(), anyInt(), anyInt(), anyLong()); 715 verify(mFgsLogger, times(0)) 716 .logFgsApiEventWithNoFgs(anyInt(), anyInt(), anyInt(), anyLong()); 717 } 718 } 719