Merge "Fix NPE in AutoSyncWorkDataPreferenceController"

This commit is contained in:
Treehugger Robot
2018-02-07 22:01:51 +00:00
committed by Gerrit Code Review
2 changed files with 22 additions and 0 deletions

View File

@@ -17,6 +17,7 @@ package com.android.settings.accounts;
import android.app.Fragment;
import android.content.Context;
import android.os.UserHandle;
import com.android.settings.Utils;
@@ -34,4 +35,10 @@ public class AutoSyncWorkDataPreferenceController extends AutoSyncPersonalDataPr
public String getPreferenceKey() {
return KEY_AUTO_SYNC_WORK_ACCOUNT;
}
@Override
public boolean isAvailable() {
return mUserHandle != null && !mUserManager.isManagedProfile() && !mUserManager.isLinkedUser()
&& mUserManager.getProfiles(UserHandle.myUserId()).size() > 1;
}
}

View File

@@ -91,6 +91,21 @@ public class AutoSyncWorkDataPreferenceControllerTest {
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void checkIsAvailable_null_workProfileUserHandle_shouldNotDisplay() {
when(mUserManager.isManagedProfile()).thenReturn(false);
when(mUserManager.isLinkedUser()).thenReturn(false);
final List<UserInfo> infos = new ArrayList<>();
infos.add(new UserInfo(UserHandle.USER_SYSTEM, "user 1", 0 /* flags */));
infos.add(new UserInfo(999, "xspace", 800010));
when(mUserManager.getProfiles(eq(UserHandle.USER_SYSTEM))).thenReturn(infos);
mController = new AutoSyncWorkDataPreferenceController(mContext, mFragment);
assertThat(mController.mUserHandle).isEqualTo(null);
assertThat(mController.isAvailable()).isFalse();
}
@Test
public void multipleProfile_shouldInitWithWorkProfileUserHandle() {
when(mUserManager.isManagedProfile()).thenReturn(false);