Fix test cases for impending Robolectric merge
Bug: 121048884 Test: make -j56 RunSettingsRoboTests Change-Id: I13568f48b66c79ff6f7806d8bc2c54ba05e7f595
This commit is contained in:
@@ -109,10 +109,16 @@ public class DefaultBrowserPreferenceControllerTest {
|
||||
doReturn(null).when(spyController).getDefaultAppIcon();
|
||||
final List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
final CharSequence PACKAGE_NAME = "com.test.package";
|
||||
final ResolveInfo info1 = spy(createResolveInfo(PACKAGE_NAME.toString()));
|
||||
when(info1.loadLabel(mPackageManager)).thenReturn(PACKAGE_NAME);
|
||||
|
||||
// This ResolveInfo will return a non-null label from loadLabel.
|
||||
final ResolveInfo info1 = createResolveInfo(PACKAGE_NAME.toString());
|
||||
info1.nonLocalizedLabel = PACKAGE_NAME;
|
||||
resolveInfos.add(info1);
|
||||
resolveInfos.add(createResolveInfo(PACKAGE_NAME.toString()));
|
||||
|
||||
// This ResolveInfo will return a null label from loadLabel.
|
||||
final ResolveInfo info2 = createResolveInfo(PACKAGE_NAME.toString());
|
||||
resolveInfos.add(info2);
|
||||
|
||||
when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())).thenReturn(null);
|
||||
when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt()))
|
||||
.thenReturn(resolveInfos);
|
||||
|
@@ -34,6 +34,7 @@ import android.net.IConnectivityManager;
|
||||
import android.net.NetworkRequest;
|
||||
import android.os.IBinder;
|
||||
import android.os.UserHandle;
|
||||
import android.os.ServiceManager;
|
||||
import android.provider.SettingsSlicesContract;
|
||||
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
@@ -50,7 +51,6 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.shadows.ShadowServiceManager;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class VpnPreferenceControllerTest {
|
||||
@@ -78,7 +78,7 @@ public class VpnPreferenceControllerTest {
|
||||
.thenReturn(mConnectivityManager);
|
||||
when(mBinder.queryLocalInterface("android.net.IConnectivityManager"))
|
||||
.thenReturn(mConnectivityManagerService);
|
||||
ShadowServiceManager.addService(Context.CONNECTIVITY_SERVICE, mBinder);
|
||||
ServiceManager.addService(Context.CONNECTIVITY_SERVICE, mBinder);
|
||||
when(mScreen.findPreference(anyString())).thenReturn(mPreference);
|
||||
|
||||
mController = spy(new VpnPreferenceController(mContext));
|
||||
|
@@ -28,6 +28,7 @@ import android.content.pm.ResolveInfo;
|
||||
import android.content.pm.ServiceInfo;
|
||||
import android.service.trust.TrustAgentService;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@@ -96,7 +97,7 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
@Test
|
||||
public void onStart_noTrustAgent_shouldNotAddPreference() {
|
||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mController.onStart();
|
||||
@@ -109,15 +110,16 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
onStart_hasAUninstalledTrustAgent_shouldRemoveOnePreferenceAndLeaveTwoPreferences() {
|
||||
final List<ResolveInfo> availableAgents = createFakeAvailableAgents();
|
||||
final ResolveInfo uninstalledTrustAgent = availableAgents.get(0);
|
||||
|
||||
for (ResolveInfo rInfo : availableAgents) {
|
||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
||||
}
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mController.onStart();
|
||||
availableAgents.remove(uninstalledTrustAgent);
|
||||
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
mController.onStart();
|
||||
|
||||
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(2);
|
||||
@@ -131,13 +133,13 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
for (ResolveInfo rInfo : availableAgents) {
|
||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
||||
}
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mController.onStart();
|
||||
availableAgents.add(newTrustAgent);
|
||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(newTrustAgent);
|
||||
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
mController.onStart();
|
||||
|
||||
assertThat(mPreferenceScreen.getPreferenceCount()).isEqualTo(4);
|
||||
@@ -150,7 +152,7 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
for (ResolveInfo rInfo : availableAgents) {
|
||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
||||
}
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mController.onStart();
|
||||
@@ -169,7 +171,7 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
for (ResolveInfo rInfo : availableAgents) {
|
||||
ShadowTrustAgentManager.grantPermissionToResolveInfo(rInfo);
|
||||
}
|
||||
mPackageManager.addResolveInfoForIntent(TEST_INTENT, availableAgents);
|
||||
mPackageManager.setResolveInfosForIntent(TEST_INTENT, availableAgents);
|
||||
ShadowRestrictedLockUtilsInternal.setKeyguardDisabledFeatures(
|
||||
DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS);
|
||||
|
||||
@@ -213,8 +215,14 @@ public class TrustAgentsPreferenceControllerTest {
|
||||
private final static List<ResolveInfo> sPermissionGrantedList = new ArrayList<>();
|
||||
|
||||
@Implementation
|
||||
public boolean shouldProvideTrust(ResolveInfo resolveInfo, PackageManager pm) {
|
||||
return sPermissionGrantedList.contains(resolveInfo);
|
||||
protected boolean shouldProvideTrust(ResolveInfo resolveInfo, PackageManager pm) {
|
||||
for (ResolveInfo info : sPermissionGrantedList) {
|
||||
if (info.serviceInfo.equals(resolveInfo.serviceInfo)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void grantPermissionToResolveInfo(ResolveInfo rInfo) {
|
||||
|
@@ -118,13 +118,13 @@ public class CreateShortcutPreferenceControllerTest {
|
||||
ri2.activityInfo.applicationInfo = new ApplicationInfo();
|
||||
ri2.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
mPackageManager.addResolveInfoForIntent(
|
||||
mPackageManager.setResolveInfosForIntent(
|
||||
new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE),
|
||||
Arrays.asList(ri1, ri2));
|
||||
|
||||
final List<ResolveInfo> info = mController.queryShortcuts();
|
||||
assertThat(info).hasSize(1);
|
||||
assertThat(info.get(0)).isEqualTo(ri2);
|
||||
assertThat(info.get(0).activityInfo).isEqualTo(ri2.activityInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -143,13 +143,13 @@ public class CreateShortcutPreferenceControllerTest {
|
||||
ri2.activityInfo.applicationInfo = new ApplicationInfo();
|
||||
ri2.activityInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
|
||||
|
||||
mPackageManager.addResolveInfoForIntent(
|
||||
mPackageManager.setResolveInfosForIntent(
|
||||
new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE),
|
||||
Arrays.asList(ri1, ri2));
|
||||
|
||||
final List<ResolveInfo> info = mController.queryShortcuts();
|
||||
assertThat(info).hasSize(2);
|
||||
assertThat(info.get(0)).isEqualTo(ri2);
|
||||
assertThat(info.get(1)).isEqualTo(ri1);
|
||||
assertThat(info.get(0).activityInfo).isEqualTo(ri2.activityInfo);
|
||||
assertThat(info.get(1).activityInfo).isEqualTo(ri1.activityInfo);
|
||||
}
|
||||
}
|
||||
|
@@ -80,12 +80,14 @@ public class ShortcutsUpdateTaskTest {
|
||||
.setComponent(new ComponentName(
|
||||
mContext, Settings.ManageApplicationsActivity.class));
|
||||
final ResolveInfo ri1 = mock(ResolveInfo.class);
|
||||
ri1.nonLocalizedLabel = "label1";
|
||||
|
||||
final Intent shortcut2 = new Intent(CreateShortcutPreferenceController.SHORTCUT_PROBE)
|
||||
.setComponent(new ComponentName(
|
||||
mContext, Settings.SoundSettingsActivity.class));
|
||||
final ResolveInfo ri2 = mock(ResolveInfo.class);
|
||||
when(ri1.loadLabel(any(PackageManager.class))).thenReturn("label1");
|
||||
when(ri2.loadLabel(any(PackageManager.class))).thenReturn("label2");
|
||||
ri2.nonLocalizedLabel = "label2";
|
||||
|
||||
mPackageManager.addResolveInfoForIntent(shortcut1, ri1);
|
||||
mPackageManager.addResolveInfoForIntent(shortcut2, ri2);
|
||||
|
||||
|
@@ -39,6 +39,7 @@ import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageItemInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.UserInfo;
|
||||
import android.graphics.drawable.ColorDrawable;
|
||||
import android.os.UserManager;
|
||||
import android.webkit.UserPackage;
|
||||
|
||||
@@ -56,6 +57,8 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.shadows.ShadowPackageManager;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.Arrays;
|
||||
@@ -341,6 +344,11 @@ public class WebViewAppPickerTest {
|
||||
when(mPackageManager.getPackageInfo(eq(DEFAULT_PACKAGE_NAME), anyInt())).thenReturn(
|
||||
packageInfo);
|
||||
|
||||
// Subvert attempts to load an unbadged icon for the application.
|
||||
PackageManager pm = RuntimeEnvironment.application.getPackageManager();
|
||||
ShadowPackageManager spm = Shadows.shadowOf(pm);
|
||||
spm.setUnbadgedApplicationIcon(DEFAULT_PACKAGE_NAME, new ColorDrawable());
|
||||
|
||||
RadioButtonPreference mockPreference = mock(RadioButtonPreference.class);
|
||||
mPicker.bindPreference(mockPreference, DEFAULT_PACKAGE_NAME, webviewAppInfo, null);
|
||||
mPicker
|
||||
|
Reference in New Issue
Block a user