Add unit tests for AttentionManagerService. Make sure it won't crash when OnSwitchUser() is invoked while the bound service is null.

Bug: 130659721
Test: atest AttentionManagerServiceTest

Change-Id: I71e1252e92eabca02fe2ae9b21596377f22f064c
This commit is contained in:
Yi Jiang
2019-04-16 17:23:11 -07:00
parent b33a6c6067
commit fa323e7dcb
2 changed files with 9 additions and 1 deletions

View File

@@ -309,7 +309,8 @@ public class AttentionManagerService extends SystemService {
}
@GuardedBy("mLock")
private UserState getOrCreateUserStateLocked(int userId) {
@VisibleForTesting
protected UserState getOrCreateUserStateLocked(int userId) {
UserState result = mUserStates.get(userId);
if (result == null) {
result = new UserState(userId, mContext, mLock, mComponentName);

View File

@@ -132,6 +132,13 @@ public class AttentionManagerServiceTest {
verify(callback).onSuccess(anyInt(), anyLong());
}
@Test
public void testOnSwitchUser_noCrashCurrentServiceIsNull() {
final int userId = 10;
mSpyAttentionManager.getOrCreateUserStateLocked(userId);
mSpyAttentionManager.onSwitchUser(userId);
}
private class MockIAttentionService implements IAttentionService {
public void checkAttention(IAttentionCallback callback) throws RemoteException {
callback.onSuccess(0, 0);