Update SecondaryUserControllerTest to sdk 26

Test: make RunSettingsRoboTests -j40
Change-Id: I4aa035a46180ad791602200d066ffb8b06f25b93
This commit is contained in:
jeffreyhuang
2017-12-04 12:12:48 -08:00
parent 6ffe277c01
commit 5b43113287

View File

@@ -21,6 +21,7 @@ import static com.android.settings.utils.FileSizeFormatter.MEGABYTE_IN_BYTES;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -55,7 +56,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@RunWith(SettingsRobolectricTestRunner.class) @RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION_O)
public class SecondaryUserControllerTest { public class SecondaryUserControllerTest {
private static final String TEST_NAME = "Fred"; private static final String TEST_NAME = "Fred";
private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users"; private static final String TARGET_PREFERENCE_GROUP_KEY = "pref_secondary_users";
@@ -90,7 +91,7 @@ public class SecondaryUserControllerTest {
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getTitle()).isEqualTo(TEST_NAME); assertThat(preference.getTitle()).isEqualTo(TEST_NAME);
} }
@@ -103,17 +104,17 @@ public class SecondaryUserControllerTest {
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("0.01 GB"); assertThat(preference.getSummary()).isEqualTo("0.01 GB");
} }
@Test @Test
public void noSecondaryUserAddedIfNoneExist() throws Exception { public void noSecondaryUserAddedIfNoneExist() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -123,15 +124,15 @@ public class SecondaryUserControllerTest {
@Test @Test
public void secondaryUserAddedIfHasDistinctId() throws Exception { public void secondaryUserAddedIfHasDistinctId() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo(); final UserInfo secondaryUser = new UserInfo();
secondaryUser.id = 10; secondaryUser.id = 10;
secondaryUser.profileGroupId = 101010; // this just has to be something not 0 secondaryUser.profileGroupId = 101010; // this just has to be something not 0
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser); userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -140,15 +141,15 @@ public class SecondaryUserControllerTest {
@Test @Test
public void profilesOfPrimaryUserAreNotIgnored() throws Exception { public void profilesOfPrimaryUserAreNotIgnored() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
UserInfo secondaryUser = new UserInfo(); final UserInfo secondaryUser = new UserInfo();
secondaryUser.id = mPrimaryUser.id; secondaryUser.id = mPrimaryUser.id;
userInfos.add(mPrimaryUser); userInfos.add(mPrimaryUser);
userInfos.add(secondaryUser); userInfos.add(secondaryUser);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(2); assertThat(controllers).hasSize(2);
@@ -161,9 +162,9 @@ public class SecondaryUserControllerTest {
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
StorageAsyncLoader.AppsStorageResult userResult = final StorageAsyncLoader.AppsStorageResult userResult =
new StorageAsyncLoader.AppsStorageResult(); new StorageAsyncLoader.AppsStorageResult();
SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>(); final SparseArray<StorageAsyncLoader.AppsStorageResult> result = new SparseArray<>();
userResult.externalStats = userResult.externalStats =
new StorageStatsSource.ExternalStorageStats( new StorageStatsSource.ExternalStorageStats(
MEGABYTE_IN_BYTES * 30, MEGABYTE_IN_BYTES * 30,
@@ -175,23 +176,23 @@ public class SecondaryUserControllerTest {
mController.handleResult(result); mController.handleResult(result);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getSummary()).isEqualTo("0.03 GB"); assertThat(preference.getSummary()).isEqualTo("0.03 GB");
} }
@Test @Test
public void dontAddPrimaryProfileAsASecondaryProfile() throws Exception { public void dontAddPrimaryProfileAsASecondaryProfile() throws Exception {
ArrayList<UserInfo> userInfos = new ArrayList<>(); final ArrayList<UserInfo> userInfos = new ArrayList<>();
// The primary UserInfo may be a different object with a different name... but represent the // The primary UserInfo may be a different object with a different name... but represent the
// same user! // same user!
UserInfo primaryUserRenamed = new UserInfo(); final UserInfo primaryUserRenamed = new UserInfo();
primaryUserRenamed.name = "Owner"; primaryUserRenamed.name = "Owner";
primaryUserRenamed.flags = UserInfo.FLAG_PRIMARY; primaryUserRenamed.flags = UserInfo.FLAG_PRIMARY;
userInfos.add(primaryUserRenamed); userInfos.add(primaryUserRenamed);
when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser); when(mUserManager.getPrimaryUser()).thenReturn(mPrimaryUser);
when(mUserManager.getUsers()).thenReturn(userInfos); when(mUserManager.getUsers()).thenReturn(userInfos);
List<AbstractPreferenceController> controllers = final List<AbstractPreferenceController> controllers =
SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager); SecondaryUserController.getSecondaryUserControllers(mContext, mUserManager);
assertThat(controllers).hasSize(1); assertThat(controllers).hasSize(1);
@@ -201,34 +202,29 @@ public class SecondaryUserControllerTest {
@Test @Test
public void iconCallbackChangesPreferenceIcon() throws Exception { public void iconCallbackChangesPreferenceIcon() throws Exception {
SparseArray<Drawable> icons = new SparseArray<>(); final SparseArray<Drawable> icons = new SparseArray<>();
Bitmap userBitmap = final UserIconDrawable drawable = mock(UserIconDrawable.class);
BitmapFactory.decodeResource( when(drawable.mutate()).thenReturn(drawable);
RuntimeEnvironment.application.getResources(), R.drawable.home);
UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
icons.put(10, drawable);
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
icons.put(mPrimaryUser.id, drawable);
mController.displayPreference(mScreen); mController.displayPreference(mScreen);
mController.handleUserIcons(icons); mController.handleUserIcons(icons);
final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class); final ArgumentCaptor<Preference> argumentCaptor = ArgumentCaptor.forClass(Preference.class);
verify(mGroup).addPreference(argumentCaptor.capture()); verify(mGroup).addPreference(argumentCaptor.capture());
Preference preference = argumentCaptor.getValue(); final Preference preference = argumentCaptor.getValue();
assertThat(preference.getIcon()).isEqualTo(drawable); assertThat(preference.getIcon()).isEqualTo(drawable);
} }
@Test @Test
public void setIcon_doesntNpeOnNullPreference() throws Exception { public void setIcon_doesntNpeOnNullPreference() throws Exception {
SparseArray<Drawable> icons = new SparseArray<>(); final SparseArray<Drawable> icons = new SparseArray<>();
Bitmap userBitmap = final UserIconDrawable drawable = mock(UserIconDrawable.class);
BitmapFactory.decodeResource(
RuntimeEnvironment.application.getResources(), R.drawable.home);
UserIconDrawable drawable = new UserIconDrawable(100 /* size */).setIcon(userBitmap).bake();
icons.put(10, drawable);
mPrimaryUser.name = TEST_NAME; mPrimaryUser.name = TEST_NAME;
mPrimaryUser.id = 10; mPrimaryUser.id = 10;
icons.put(mPrimaryUser.id, drawable);
mController.handleUserIcons(icons); mController.handleUserIcons(icons);