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
This commit is contained in:
Felka Chang
2020-09-24 17:03:32 +08:00
parent eab871a7c3
commit 63a2ede196

View File

@@ -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(