Merge "Fix crash in clicking Default Home gear icon."

am: 0c724f5181

Change-Id: I53b6c18077d0bcf5a7bc13fcc425c6c1ca4143ad
This commit is contained in:
Doris Ling
2018-08-25 23:12:24 -07:00
committed by android-build-merger
2 changed files with 29 additions and 22 deletions

View File

@@ -106,7 +106,9 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
.setPackage(packageName)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
return mPackageManager.queryIntentActivities(intent, 0).size() == 1 ? intent : null;
return intent.resolveActivity(mPackageManager.getPackageManager()) != null
? intent
: null;
}
public static boolean hasHomePreference(String pkg, Context context) {

View File

@@ -17,8 +17,8 @@
package com.android.settings.applications.defaultapps;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
@@ -29,6 +29,9 @@ import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.UserManager;
import android.support.v7.preference.Preference;
@@ -52,6 +55,9 @@ import java.util.Collections;
@RunWith(SettingsRobolectricTestRunner.class)
public class DefaultHomePreferenceControllerTest {
private static final String TEST_PACKAGE = "test.pkg";
private static final String TEST_CLASS = "class";
@Mock
private UserManager mUserManager;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
@@ -106,14 +112,14 @@ public class DefaultHomePreferenceControllerTest {
@Test
public void testIsHomeDefault_noDefaultSet_shouldReturnTrue() {
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
assertThat(DefaultHomePreferenceController.isHomeDefault("test.pkg", mPackageManager))
assertThat(DefaultHomePreferenceController.isHomeDefault(TEST_PACKAGE, mPackageManager))
.isTrue();
}
@Test
public void testIsHomeDefault_defaultSetToPkg_shouldReturnTrue() {
final String pkgName = "test.pkg";
final ComponentName defaultHome = new ComponentName(pkgName, "class");
final String pkgName = TEST_PACKAGE;
final ComponentName defaultHome = new ComponentName(pkgName, TEST_CLASS);
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
@@ -123,8 +129,8 @@ public class DefaultHomePreferenceControllerTest {
@Test
public void testIsHomeDefault_defaultSetToOtherPkg_shouldReturnFalse() {
final String pkgName = "test.pkg";
final ComponentName defaultHome = new ComponentName("not" + pkgName, "class");
final String pkgName = TEST_PACKAGE;
final ComponentName defaultHome = new ComponentName("not" + pkgName, TEST_CLASS);
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
@@ -135,29 +141,28 @@ public class DefaultHomePreferenceControllerTest {
@Test
public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
when(mPackageManager.getHomeActivities(anyList()))
.thenReturn(new ComponentName("test.pkg", "class"));
.thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
when(mPackageManager.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
.thenReturn(null);
assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
}
@Test
public void testGetSettingIntent_homeHasOneSetting_shouldReturnSettingIntent() {
when(mPackageManager.getHomeActivities(anyList()))
.thenReturn(new ComponentName("test.pkg", "class"));
when(mPackageManager.queryIntentActivities(any(), eq(0)))
.thenReturn(Collections.singletonList(mock(ResolveInfo.class)));
.thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
final ResolveInfo info = mock(ResolveInfo.class);
info.activityInfo = mock(ActivityInfo.class);
info.activityInfo.name = TEST_CLASS;
info.activityInfo.applicationInfo = mock(ApplicationInfo.class);
info.activityInfo.applicationInfo.packageName = TEST_PACKAGE;
when(mPackageManager.getPackageManager().resolveActivity(any(Intent.class), anyInt()))
.thenReturn(info);
Intent intent = mController.getSettingIntent(mController.getDefaultAppInfo());
assertThat(intent).isNotNull();
assertThat(intent.getPackage()).isEqualTo("test.pkg");
}
@Test
public void testGetSettingIntent_homeHasMultipleSettings_shouldNotReturnSettingIntent() {
when(mPackageManager.getHomeActivities(anyList()))
.thenReturn(new ComponentName("test.pkg", "class"));
when(mPackageManager.queryIntentActivities(any(), eq(0)))
.thenReturn(Arrays.asList(mock(ResolveInfo.class), mock(ResolveInfo.class)));
assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
assertThat(intent.getPackage()).isEqualTo(TEST_PACKAGE);
}
@Test