From fa323e7dcb263ac699d30a6722774ef186a168f2 Mon Sep 17 00:00:00 2001 From: Yi Jiang Date: Tue, 16 Apr 2019 17:23:11 -0700 Subject: [PATCH] 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 --- .../java/com/android/server/attention/AttentionManagerService.java | 3 ++- .../com/android/server/attention/AttentionManagerServiceTest.java | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/services/core/java/com/android/server/attention/AttentionManagerService.java b/services/core/java/com/android/server/attention/AttentionManagerService.java index d7d4851bf33f..b50af28b12b3 100644 --- a/services/core/java/com/android/server/attention/AttentionManagerService.java +++ b/services/core/java/com/android/server/attention/AttentionManagerService.java @@ -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); diff --git a/services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java index 8426a0bb2c11..bb9f49e6f37f 100644 --- a/services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/attention/AttentionManagerServiceTest.java @@ -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); -- 2.11.0