Migrate settings robolectric tests to mockito 2

- Migrated ArgumentMatcher subclasses to lambdas
- Replaced any() with nullable() where tests were failing

Test: cd tests/robotests && mma
Bug: 38456058
Change-Id: Ice8c39b435c45b87f82dbbd9860e68f235314cf8
This commit is contained in:
Maurice Lam
2017-05-23 19:51:36 -07:00
parent be39d57762
commit 046400c2c4
38 changed files with 426 additions and 481 deletions

View File

@@ -16,6 +16,14 @@
package com.android.settings.enterprise;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
@@ -43,13 +51,6 @@ import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.List;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.when;
/**
* Tests for {@link EnterpriseSetDefaultAppsPreferenceController}.
*/
@@ -81,7 +82,7 @@ public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
final List<UserAppInfo> apps = new ArrayList<>(number);
apps.add(new UserAppInfo(new UserInfo(i, "user." + i, UserInfo.FLAG_ADMIN), appInfo));
when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(eq(i),
argThat(new MatchesIntents(intents)))).thenReturn(apps);
argThat(matchesIntents(intents)))).thenReturn(apps);
}
}
@@ -116,7 +117,7 @@ public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
@Test
public void testIsAvailable() {
when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(),
anyObject())).thenReturn(new ArrayList<UserAppInfo>());
any(Intent[].class))).thenReturn(new ArrayList<>());
assertThat(mController.isAvailable()).isFalse();
setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1);
@@ -136,28 +137,20 @@ public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
.isEqualTo("number_enterprise_set_default_apps");
}
private static class MatchesIntents extends ArgumentMatcher<Intent[]> {
private final Intent[] mExpectedIntents;
MatchesIntents(Intent[] intents) {
mExpectedIntents = intents;
}
@Override
public boolean matches(Object object) {
final Intent[] actualIntents = (Intent[]) object;
private ArgumentMatcher<Intent[]> matchesIntents(Intent[] intents) {
return (Intent[] actualIntents) -> {
if (actualIntents == null) {
return false;
}
if (actualIntents.length != mExpectedIntents.length) {
if (actualIntents.length != intents.length) {
return false;
}
for (int i = 0; i < mExpectedIntents.length; i++) {
if (!mExpectedIntents[i].filterEquals(actualIntents[i])) {
for (int i = 0; i < intents.length; i++) {
if (!intents[i].filterEquals(actualIntents[i])) {
return false;
}
}
return true;
}
};
}
}