Merge "Use role for App info default SMS shortcut."

This commit is contained in:
TreeHugger Robot
2019-01-15 00:47:40 +00:00
committed by Android (Google) Code Review
4 changed files with 160 additions and 119 deletions

View File

@@ -18,11 +18,13 @@ package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;
import android.app.Activity;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.Intent;
import android.os.UserManager;
@@ -42,108 +44,173 @@ import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowApplication;
import org.robolectric.shadows.ShadowUserManager;
import java.util.Collections;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUserManager.class)
public class DefaultAppShortcutPreferenceControllerBaseTest {
private ShadowUserManager mShadowUserManager;
private static final String TEST_PREFERENCE_KEY = "TestKey";
private static final String TEST_ROLE_NAME = "TestRole";
private static final String TEST_PACKAGE_NAME = "TestPackage";
@Mock
private AppInfoDashboardFragment mFragment;
private RoleManager mRoleManager;
@Mock
private Preference mPreference;
private Activity mActivity;
private TestPreferenceController mController;
private ShadowUserManager mShadowUserManager;
private TestRolePreferenceController mController;
private TestLegacyPreferenceController mLegacyController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
ShadowApplication.getInstance().setSystemService(Context.ROLE_SERVICE, mRoleManager);
mActivity = Robolectric.setupActivity(Activity.class);
mShadowUserManager = shadowOf(mActivity.getSystemService(UserManager.class));
mController = new TestPreferenceController(mActivity, mFragment);
final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key);
mController = new TestRolePreferenceController(mActivity);
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
mLegacyController = new TestLegacyPreferenceController(mActivity);
}
@Test
public void getAvailabilityStatus_managedProfile_shouldReturnDisabled() {
public void getAvailabilityStatus_isManagedProfile_shouldReturnDisabled() {
mShadowUserManager.setManagedProfile(true);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(DefaultAppShortcutPreferenceControllerBase.DISABLED_FOR_USER);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
DefaultAppShortcutPreferenceControllerBase.DISABLED_FOR_USER);
}
@Test
public void getAvailabilityStatus_hasAppCapability_shouldReturnAvailable() {
mController.capable = true;
public void getAvailabilityStatus_roleIsAvailable_shouldReturnAvailable() {
mShadowUserManager.setManagedProfile(false);
when(mRoleManager.isRoleAvailable(eq(TEST_ROLE_NAME))).thenReturn(true);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(DefaultAppShortcutPreferenceControllerBase.AVAILABLE);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
DefaultAppShortcutPreferenceControllerBase.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noAppCapability_shouldReturnDisabled() {
mController.capable = false;
public void getAvailabilityStatus_roleNotAvailable_shouldReturnDisabled() {
mShadowUserManager.setManagedProfile(false);
when(mRoleManager.isRoleAvailable(eq(TEST_ROLE_NAME))).thenReturn(false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
DefaultAppShortcutPreferenceControllerBase.UNSUPPORTED_ON_DEVICE);
}
@Test
public void updateState_isDefaultApp_shouldSetSummaryToYes() {
mController.isDefault = true;
public void updateState_isRoleHolder_shouldSetSummaryToYes() {
when(mRoleManager.getRoleHolders(eq(TEST_ROLE_NAME))).thenReturn(Collections.singletonList(
TEST_PACKAGE_NAME));
final CharSequence yesText = mActivity.getText(R.string.yes);
mController.updateState(mPreference);
String yesString = mActivity.getString(R.string.yes);
verify(mPreference).setSummary(yesString);
verify(mPreference).setSummary(yesText);
}
@Test
public void updateState_notRoleHoler_shouldSetSummaryToNo() {
when(mRoleManager.getRoleHolders(eq(TEST_ROLE_NAME))).thenReturn(Collections.emptyList());
final CharSequence noText = mActivity.getText(R.string.no);
mController.updateState(mPreference);
verify(mPreference).setSummary(noText);
}
@Test
public void handlePreferenceTreeClick_shouldStartManageDefaultAppIntent() {
final ShadowActivity shadowActivity = shadowOf(mActivity);
mController.handlePreferenceTreeClick(mPreference);
final Intent intent = shadowActivity.getNextStartedActivity();
assertThat(intent).isNotNull();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MANAGE_DEFAULT_APP);
assertThat(intent.getStringExtra(Intent.EXTRA_ROLE_NAME)).isEqualTo(TEST_ROLE_NAME);
}
private class TestRolePreferenceController extends DefaultAppShortcutPreferenceControllerBase {
private TestRolePreferenceController(Context context) {
super(context, TEST_PREFERENCE_KEY, TEST_ROLE_NAME, TEST_PACKAGE_NAME);
}
}
// TODO: STOPSHIP(b/110557011): Remove following tests once we have all default apps migrated.
@Test
public void getAvailabilityStatus_hasAppCapability_shouldReturnAvailable() {
mShadowUserManager.setManagedProfile(false);
mLegacyController.mHasAppCapability = true;
assertThat(mLegacyController.getAvailabilityStatus()).isEqualTo(
DefaultAppShortcutPreferenceControllerBase.AVAILABLE);
}
@Test
public void getAvailabilityStatus_noAppCapability_shouldReturnDisabled() {
mShadowUserManager.setManagedProfile(false);
mLegacyController.mHasAppCapability = false;
assertThat(mLegacyController.getAvailabilityStatus()).isEqualTo(
DefaultAppShortcutPreferenceControllerBase.UNSUPPORTED_ON_DEVICE);
}
@Test
public void updateState_isDefaultApp_shouldSetSummaryToYes() {
mLegacyController.mIsDefaultApp = true;
final CharSequence yesText = mActivity.getText(R.string.yes);
mLegacyController.updateState(mPreference);
verify(mPreference).setSummary(yesText);
}
@Test
public void updateState_notDefaultApp_shouldSetSummaryToNo() {
mController.isDefault = false;
mLegacyController.mIsDefaultApp = false;
final CharSequence noText = mActivity.getText(R.string.no);
mController.updateState(mPreference);
String noString = mActivity.getString(R.string.no);
verify(mPreference).setSummary(noString);
mLegacyController.updateState(mPreference);
verify(mPreference).setSummary(noText);
}
@Test
public void handlePreferenceTreeClick_shouldStartDefaultAppSettings() {
final ShadowActivity shadowActivity = shadowOf(mActivity);
mController.handlePreferenceTreeClick(mPreference);
final Intent nextIntent = shadowActivity.getNextStartedActivity();
assertThat(nextIntent).isNotNull();
assertThat(nextIntent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
mLegacyController.handlePreferenceTreeClick(mPreference);
final Intent intent = shadowActivity.getNextStartedActivity();
assertThat(intent).isNotNull();
assertThat(intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT)).isEqualTo(
DefaultAppSettings.class.getName());
assertThat(
nextIntent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS).getString(
SettingsActivity.EXTRA_FRAGMENT_ARG_KEY)).isEqualTo("TestKey");
assertThat(intent.getBundleExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS).getString(
SettingsActivity.EXTRA_FRAGMENT_ARG_KEY)).isEqualTo(TEST_PREFERENCE_KEY);
}
private class TestPreferenceController extends DefaultAppShortcutPreferenceControllerBase {
private class TestLegacyPreferenceController
extends DefaultAppShortcutPreferenceControllerBase {
private boolean isDefault;
private boolean capable;
private boolean mIsDefaultApp;
private boolean mHasAppCapability;
private TestPreferenceController(Context context, AppInfoDashboardFragment parent) {
super(context, "TestKey", "TestPackage");
private TestLegacyPreferenceController(Context context) {
super(context, TEST_PREFERENCE_KEY, TEST_PACKAGE_NAME);
}
@Override
protected boolean hasAppCapability() {
return capable;
return mHasAppCapability;
}
@Override
protected boolean isDefaultApp() {
return isDefault;
return mIsDefaultApp;
}
}
}

View File

@@ -18,79 +18,28 @@ package com.android.settings.applications.appinfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
@RunWith(RobolectricTestRunner.class)
public class DefaultSmsShortcutPreferenceControllerTest {
@Mock
private PackageManager mPackageManager;
private static final String TEST_PACKAGE_NAME = "TestPackage";
private static final String PREFERENCE_KEY = "default_sms_app";
private Context mContext;
private DefaultSmsShortcutPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
mController = new DefaultSmsShortcutPreferenceController(mContext, "Package1");
mController = new DefaultSmsShortcutPreferenceController(RuntimeEnvironment.application,
TEST_PACKAGE_NAME);
}
@Test
public void getPreferenceKey_shouldReturnDefaultSms() {
assertThat(mController.getPreferenceKey()).isEqualTo("default_sms_app");
}
@Test
@Config(shadows = ShadowDefaultSmsPreferenceController.class)
public void hasAppCapability_hasSmsCapability_shouldReturnTrue() {
assertThat(mController.hasAppCapability()).isTrue();
}
@Test
public void hasAppCapability_noSmsCapability_shouldReturnFalse() {
assertThat(mController.hasAppCapability()).isFalse();
}
@Test
@Config(shadows = ShadowDefaultSmsPreferenceController.class)
public void isDefaultApp_isDefaultSms_shouldReturnTrue() {
assertThat(mController.isDefaultApp()).isTrue();
}
@Test
public void isDefaultApp_notDefaultSms_shouldReturnFalse() {
assertThat(mController.isDefaultApp()).isFalse();
}
@Implements(DefaultSmsPreferenceController.class)
public static class ShadowDefaultSmsPreferenceController {
@Implementation
protected static boolean hasSmsPreference(String pkg, Context context) {
return true;
}
@Implementation
protected static boolean isSmsDefault(String pkg, Context context) {
return true;
}
assertThat(mController.getPreferenceKey()).isEqualTo(PREFERENCE_KEY);
}
}