Fix crash in clicking Default Home gear icon.
- change the way that default home query the resolve activity to only check for LAUNCHER activity. Change-Id: Ib53154afe7951f4e2c7c3ab147be4c691113e0e7 Merged-In: Ib53154afe7951f4e2c7c3ab147be4c691113e0e7 Fixes: 112249115 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -106,7 +106,9 @@ public class DefaultHomePreferenceController extends DefaultAppPreferenceControl
|
|||||||
Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
|
Intent intent = new Intent(Intent.ACTION_APPLICATION_PREFERENCES)
|
||||||
.setPackage(packageName)
|
.setPackage(packageName)
|
||||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
.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) {
|
public static boolean hasHomePreference(String pkg, Context context) {
|
||||||
|
@@ -17,8 +17,8 @@
|
|||||||
package com.android.settings.applications.defaultapps;
|
package com.android.settings.applications.defaultapps;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.Matchers.any;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.Matchers.anyInt;
|
||||||
import static org.mockito.Matchers.anyList;
|
import static org.mockito.Matchers.anyList;
|
||||||
import static org.mockito.Mockito.atLeastOnce;
|
import static org.mockito.Mockito.atLeastOnce;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@@ -29,6 +29,9 @@ import static org.mockito.Mockito.when;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
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.content.pm.ResolveInfo;
|
||||||
import android.os.UserManager;
|
import android.os.UserManager;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
@@ -52,6 +55,9 @@ import java.util.Collections;
|
|||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
public class DefaultHomePreferenceControllerTest {
|
public class DefaultHomePreferenceControllerTest {
|
||||||
|
|
||||||
|
private static final String TEST_PACKAGE = "test.pkg";
|
||||||
|
private static final String TEST_CLASS = "class";
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private UserManager mUserManager;
|
private UserManager mUserManager;
|
||||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
@@ -106,14 +112,14 @@ public class DefaultHomePreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testIsHomeDefault_noDefaultSet_shouldReturnTrue() {
|
public void testIsHomeDefault_noDefaultSet_shouldReturnTrue() {
|
||||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
when(mPackageManager.getHomeActivities(anyList())).thenReturn(null);
|
||||||
assertThat(DefaultHomePreferenceController.isHomeDefault("test.pkg", mPackageManager))
|
assertThat(DefaultHomePreferenceController.isHomeDefault(TEST_PACKAGE, mPackageManager))
|
||||||
.isTrue();
|
.isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsHomeDefault_defaultSetToPkg_shouldReturnTrue() {
|
public void testIsHomeDefault_defaultSetToPkg_shouldReturnTrue() {
|
||||||
final String pkgName = "test.pkg";
|
final String pkgName = TEST_PACKAGE;
|
||||||
final ComponentName defaultHome = new ComponentName(pkgName, "class");
|
final ComponentName defaultHome = new ComponentName(pkgName, TEST_CLASS);
|
||||||
|
|
||||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
||||||
|
|
||||||
@@ -123,8 +129,8 @@ public class DefaultHomePreferenceControllerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsHomeDefault_defaultSetToOtherPkg_shouldReturnFalse() {
|
public void testIsHomeDefault_defaultSetToOtherPkg_shouldReturnFalse() {
|
||||||
final String pkgName = "test.pkg";
|
final String pkgName = TEST_PACKAGE;
|
||||||
final ComponentName defaultHome = new ComponentName("not" + pkgName, "class");
|
final ComponentName defaultHome = new ComponentName("not" + pkgName, TEST_CLASS);
|
||||||
|
|
||||||
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
when(mPackageManager.getHomeActivities(anyList())).thenReturn(defaultHome);
|
||||||
|
|
||||||
@@ -135,29 +141,28 @@ public class DefaultHomePreferenceControllerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
|
public void testGetSettingIntent_homeHasNoSetting_shouldNotReturnSettingIntent() {
|
||||||
when(mPackageManager.getHomeActivities(anyList()))
|
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();
|
assertThat(mController.getSettingIntent(mController.getDefaultAppInfo())).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetSettingIntent_homeHasOneSetting_shouldReturnSettingIntent() {
|
public void testGetSettingIntent_homeHasOneSetting_shouldReturnSettingIntent() {
|
||||||
when(mPackageManager.getHomeActivities(anyList()))
|
when(mPackageManager.getHomeActivities(anyList()))
|
||||||
.thenReturn(new ComponentName("test.pkg", "class"));
|
.thenReturn(new ComponentName(TEST_PACKAGE, TEST_CLASS));
|
||||||
when(mPackageManager.queryIntentActivities(any(), eq(0)))
|
final ResolveInfo info = mock(ResolveInfo.class);
|
||||||
.thenReturn(Collections.singletonList(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());
|
Intent intent = mController.getSettingIntent(mController.getDefaultAppInfo());
|
||||||
assertThat(intent).isNotNull();
|
assertThat(intent).isNotNull();
|
||||||
assertThat(intent.getPackage()).isEqualTo("test.pkg");
|
assertThat(intent.getPackage()).isEqualTo(TEST_PACKAGE);
|
||||||
}
|
|
||||||
|
|
||||||
@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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user