From 63a2ede196d88d57a4d6da3f010973524b284571 Mon Sep 17 00:00:00 2001 From: Felka Chang Date: Thu, 24 Sep 2020 17:03:32 +0800 Subject: [PATCH] Fix the RemoteInputViewTest mock user context SystemUI needs to handle multiple user environment but the systemuitest doesn't simulate appropriately. To use spy mocks the Context.createContextAsUser. When the test is running, the real environment doesn't have the such user. That's why the IllegalStateException comes out. Fixes: 169302704 Test: TID="SystemUITests"; \ PACKAGE_NAME="com.android.systemui.statusbar.policy"; \ TEST_CLASS="RemoteInputViewTest"; \ atest "${TID}:${PACKAGE_NAME}.${TEST_CLASS}" Change-Id: I3041226496c641dad11b415173675e09186d318a --- .../statusbar/policy/RemoteInputViewTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java index e88b514ef2383..4fb85ad1bb4de 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/policy/RemoteInputViewTest.java @@ -17,9 +17,15 @@ package com.android.systemui.statusbar.policy; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + import android.app.ActivityManager; import android.app.PendingIntent; import android.app.RemoteInput; +import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.ShortcutManager; @@ -130,8 +136,18 @@ public class RemoteInputViewTest extends SysuiTestCase { private UserHandle getTargetInputMethodUser(UserHandle fromUser, UserHandle toUser) throws Exception { + /** + * RemoteInputView, Icon, and Bubble have the situation need to handle the other user. + * SystemUI cross multiple user but this test(com.android.systemui.tests) doesn't cross + * multiple user. It needs some of mocking multiple user environment to ensure the + * createContextAsUser without throwing IllegalStateException. + */ + Context contextSpy = spy(mContext); + doReturn(contextSpy).when(contextSpy).createContextAsUser(any(), anyInt()); + doReturn(toUser.getIdentifier()).when(contextSpy).getUserId(); + NotificationTestHelper helper = new NotificationTestHelper( - mContext, + contextSpy, mDependency, TestableLooper.get(this)); ExpandableNotificationRow row = helper.createRow(