Merge "Fix wrong user in personal profile" into udc-dev am: ce7ad1f8a0

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Settings/+/22307474

Change-Id: Ib735a7e0015c217ff1c9067c5bab3cdd1fe8cb2c
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Wilson Wu
2023-03-31 10:33:22 +00:00
committed by Automerger Merge Worker
2 changed files with 41 additions and 8 deletions

View File

@@ -55,7 +55,10 @@ public class AvailableVirtualKeyboardFragment extends DashboardFragment
@VisibleForTesting
InputMethodSettingValuesWrapper mInputMethodSettingValues;
private Context mUserAwareContext;
@VisibleForTesting
Context mUserAwareContext;
private int mUserId;
@Override
@@ -82,9 +85,16 @@ public class AvailableVirtualKeyboardFragment extends DashboardFragment
break;
}
case ProfileSelectFragment.ProfileType.PERSONAL: {
final UserHandle primaryUser = userManager.getPrimaryUser().getUserHandle();
newUserId = primaryUser.getIdentifier();
newUserAwareContext = context.createContextAsUser(primaryUser, 0);
// Use the parent user of the current user if the current user is profile.
final UserHandle currentUser = UserHandle.of(currentUserId);
final UserHandle userProfileParent = userManager.getProfileParent(currentUser);
if (userProfileParent != null) {
newUserId = userProfileParent.getIdentifier();
newUserAwareContext = context.createContextAsUser(userProfileParent, 0);
} else {
newUserId = currentUserId;
newUserAwareContext = context;
}
break;
}
default:

View File

@@ -27,6 +27,7 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import android.content.Context;
import android.content.pm.ApplicationInfo;
@@ -34,6 +35,7 @@ import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
@@ -46,7 +48,6 @@ import com.android.settings.R;
import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
import com.android.settings.testutils.shadow.ShadowInputMethodManagerWithMethodList;
import com.android.settings.testutils.shadow.ShadowSecureSettings;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.inputmethod.InputMethodPreference;
import com.android.settingslib.inputmethod.InputMethodSettingValuesWrapper;
@@ -66,14 +67,15 @@ import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {
ShadowSecureSettings.class,
ShadowInputMethodManagerWithMethodList.class,
ShadowUserManager.class
ShadowInputMethodManagerWithMethodList.class
})
public class AvailableVirtualKeyboardFragmentTest {
@Mock
private InputMethodManager mInputMethodManager;
@Mock
private UserManager mUserManager;
@Mock
private InputMethodSettingValuesWrapper mValuesWrapper;
@Mock
private PreferenceScreen mPreferenceScreen;
@@ -92,6 +94,27 @@ public class AvailableVirtualKeyboardFragmentTest {
initMock();
}
@Test
public void onAttachPersonalProfile_noProfileParent() {
doReturn(null).when(mUserManager).getProfileParent(any(UserHandle.class));
mFragment.onAttach(mContext);
assertThat(mFragment.mUserAwareContext).isEqualTo(mContext);
}
@Test
public void onAttachPersonalProfile_hasProfileParent() {
final UserHandle profileParent = new UserHandle(0);
final Context mockContext = mock(Context.class);
doReturn(profileParent).when(mUserManager).getProfileParent(any(UserHandle.class));
doReturn(mockContext).when(mContext).createContextAsUser(any(UserHandle.class), anyInt());
mFragment.onAttach(mContext);
assertThat(mFragment.mUserAwareContext).isEqualTo(mockContext);
}
@Test
public void onCreatePreferences_shouldAddResource() {
mFragment.onAttach(mContext);
@@ -175,7 +198,7 @@ public class AvailableVirtualKeyboardFragmentTest {
when(mFragment.getPreferenceScreen()).thenReturn(mPreferenceScreen);
when(mPreferenceManager.getContext()).thenReturn(mContext);
when(mContext.getSystemService(InputMethodManager.class)).thenReturn(mInputMethodManager);
doReturn(mContext).when(mContext).createContextAsUser(any(UserHandle.class), anyInt());
when(mContext.getSystemService(UserManager.class)).thenReturn(mUserManager);
}
private List<InputMethodInfo> createFakeInputMethodInfoList(final String name, int num) {