AR/FR: Tests using stubbed values.
Also a simplifying change to MasterClear. Test: Standard robotests Bug: 63937032 Change-Id: I54fe60c2e38d938148f89d043bf8a7f7698edc42
This commit is contained in:
@@ -76,7 +76,7 @@ public class MasterClear extends InstrumentedPreferenceFragment {
|
|||||||
private static final String TAG = "MasterClear";
|
private static final String TAG = "MasterClear";
|
||||||
|
|
||||||
private static final int KEYGUARD_REQUEST = 55;
|
private static final int KEYGUARD_REQUEST = 55;
|
||||||
private static final int CREDENTIAL_CONFIRM_REQUEST = 56;
|
@VisibleForTesting static final int CREDENTIAL_CONFIRM_REQUEST = 56;
|
||||||
|
|
||||||
static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
|
static final String ERASE_EXTERNAL_EXTRA = "erase_sd";
|
||||||
static final String ERASE_ESIMS_EXTRA = "erase_esim";
|
static final String ERASE_ESIMS_EXTRA = "erase_esim";
|
||||||
@@ -157,9 +157,12 @@ public class MasterClear extends InstrumentedPreferenceFragment {
|
|||||||
.setAction("android.accounts.action.PRE_FACTORY_RESET");
|
.setAction("android.accounts.action.PRE_FACTORY_RESET");
|
||||||
// Check to make sure that the intent is supported.
|
// Check to make sure that the intent is supported.
|
||||||
final PackageManager pm = context.getPackageManager();
|
final PackageManager pm = context.getPackageManager();
|
||||||
final List<ResolveInfo> resolutions =
|
final ResolveInfo resolution = pm.resolveActivity(requestAccountConfirmation, 0);
|
||||||
pm.queryIntentActivities(requestAccountConfirmation, 0);
|
if (resolution != null
|
||||||
if (resolutions != null && resolutions.size() > 0) {
|
&& resolution.activityInfo != null
|
||||||
|
&& packageName.equals(resolution.activityInfo.packageName)) {
|
||||||
|
// Note that we need to check the packagename to make sure that an Activity resolver
|
||||||
|
// wasn't returned.
|
||||||
getActivity().startActivityForResult(
|
getActivity().startActivityForResult(
|
||||||
requestAccountConfirmation, CREDENTIAL_CONFIRM_REQUEST);
|
requestAccountConfirmation, CREDENTIAL_CONFIRM_REQUEST);
|
||||||
return true;
|
return true;
|
||||||
|
@@ -19,16 +19,24 @@ package com.android.settings;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Mockito.any;
|
import static org.mockito.Mockito.any;
|
||||||
|
import static org.mockito.Mockito.eq;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
import static org.robolectric.Shadows.shadowOf;
|
import static org.robolectric.Shadows.shadowOf;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
|
import android.accounts.AccountManager;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.ActivityInfo;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.content.pm.ResolveInfo;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@@ -46,6 +54,7 @@ import org.mockito.Mock;
|
|||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.ShadowAccountManager;
|
||||||
import org.robolectric.shadows.ShadowActivity;
|
import org.robolectric.shadows.ShadowActivity;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@@ -55,6 +64,9 @@ import org.robolectric.shadows.ShadowActivity;
|
|||||||
shadows = {ShadowUtils.class}
|
shadows = {ShadowUtils.class}
|
||||||
)
|
)
|
||||||
public class MasterClearTest {
|
public class MasterClearTest {
|
||||||
|
private static final String TEST_ACCOUNT_TYPE = "android.test.account.type";
|
||||||
|
private static final String TEST_CONFIRMATION_PACKAGE = "android.test.confirmation.pkg";
|
||||||
|
private static final String TEST_ACCOUNT_NAME = "test@example.com";
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private MasterClear mMasterClear;
|
private MasterClear mMasterClear;
|
||||||
@@ -62,7 +74,18 @@ public class MasterClearTest {
|
|||||||
private ScrollView mScrollView;
|
private ScrollView mScrollView;
|
||||||
@Mock
|
@Mock
|
||||||
private LinearLayout mLinearLayout;
|
private LinearLayout mLinearLayout;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private PackageManager mPackageManager;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private AccountManager mAccountManager;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private Activity mMockActivity;
|
||||||
|
|
||||||
private ShadowActivity mShadowActivity;
|
private ShadowActivity mShadowActivity;
|
||||||
|
private ShadowAccountManager mShadowAccountManager;
|
||||||
private Activity mActivity;
|
private Activity mActivity;
|
||||||
private View mContentView;
|
private View mContentView;
|
||||||
|
|
||||||
@@ -86,6 +109,7 @@ public class MasterClearTest {
|
|||||||
mMasterClear = spy(new MasterClear());
|
mMasterClear = spy(new MasterClear());
|
||||||
mActivity = Robolectric.setupActivity(Activity.class);
|
mActivity = Robolectric.setupActivity(Activity.class);
|
||||||
mShadowActivity = shadowOf(mActivity);
|
mShadowActivity = shadowOf(mActivity);
|
||||||
|
// mShadowAccountManager = shadowOf(AccountManager.get(mActivity));
|
||||||
mContentView = LayoutInflater.from(mActivity).inflate(R.layout.master_clear, null);
|
mContentView = LayoutInflater.from(mActivity).inflate(R.layout.master_clear, null);
|
||||||
|
|
||||||
// Make scrollView only have one child
|
// Make scrollView only have one child
|
||||||
@@ -162,9 +186,61 @@ public class MasterClearTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTryShowAccountConfirmation_unsupported() {
|
public void testTryShowAccountConfirmation_unsupported() {
|
||||||
doReturn(mActivity).when(mMasterClear).getActivity();
|
when(mMasterClear.getActivity()).thenReturn(mActivity);
|
||||||
/* Using the default resources, account confirmation shouldn't trigger */
|
/* Using the default resources, account confirmation shouldn't trigger */
|
||||||
assertThat(mMasterClear.tryShowAccountConfirmation()).isFalse();
|
assertThat(mMasterClear.tryShowAccountConfirmation()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTryShowAccountConfirmation_no_relevant_accounts() {
|
||||||
|
when(mMasterClear.getActivity()).thenReturn(mMockActivity);
|
||||||
|
when(mMockActivity.getString(R.string.account_type)).thenReturn(TEST_ACCOUNT_TYPE);
|
||||||
|
when(mMockActivity.getString(R.string.account_confirmation_package)).thenReturn(TEST_CONFIRMATION_PACKAGE);
|
||||||
|
|
||||||
|
Account[] accounts = new Account[0];
|
||||||
|
when(mMockActivity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager);
|
||||||
|
when(mAccountManager.getAccountsByType(TEST_ACCOUNT_TYPE)).thenReturn(accounts);
|
||||||
|
assertThat(mMasterClear.tryShowAccountConfirmation()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTryShowAccountConfirmation_unresolved() {
|
||||||
|
when(mMasterClear.getActivity()).thenReturn(mMockActivity);
|
||||||
|
when(mMockActivity.getString(R.string.account_type)).thenReturn(TEST_ACCOUNT_TYPE);
|
||||||
|
when(mMockActivity.getString(R.string.account_confirmation_package)).thenReturn(TEST_CONFIRMATION_PACKAGE);
|
||||||
|
Account[] accounts = new Account[] { new Account(TEST_ACCOUNT_NAME, TEST_ACCOUNT_TYPE) };
|
||||||
|
when(mMockActivity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager);
|
||||||
|
when(mAccountManager.getAccountsByType(TEST_ACCOUNT_TYPE)).thenReturn(accounts);
|
||||||
|
// The package manager should not resolve the confirmation intent targeting the non-existent
|
||||||
|
// confirmation package.
|
||||||
|
when(mMockActivity.getPackageManager()).thenReturn(mPackageManager);
|
||||||
|
assertThat(mMasterClear.tryShowAccountConfirmation()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTryShowAccountConfirmation_ok() {
|
||||||
|
when(mMasterClear.getActivity()).thenReturn(mMockActivity);
|
||||||
|
// Only try to show account confirmation if the appropriate resource overlays are available.
|
||||||
|
when(mMockActivity.getString(R.string.account_type)).thenReturn(TEST_ACCOUNT_TYPE);
|
||||||
|
when(mMockActivity.getString(R.string.account_confirmation_package)).thenReturn(TEST_CONFIRMATION_PACKAGE);
|
||||||
|
// Add accounts to trigger the search for a resolving intent.
|
||||||
|
Account[] accounts = new Account[] { new Account(TEST_ACCOUNT_NAME, TEST_ACCOUNT_TYPE) };
|
||||||
|
when(mMockActivity.getSystemService(Context.ACCOUNT_SERVICE)).thenReturn(mAccountManager);
|
||||||
|
when(mAccountManager.getAccountsByType(TEST_ACCOUNT_TYPE)).thenReturn(accounts);
|
||||||
|
// The package manager should not resolve the confirmation intent targeting the non-existent
|
||||||
|
// confirmation package.
|
||||||
|
when(mMockActivity.getPackageManager()).thenReturn(mPackageManager);
|
||||||
|
|
||||||
|
ActivityInfo activityInfo = new ActivityInfo();
|
||||||
|
activityInfo.packageName = TEST_CONFIRMATION_PACKAGE;
|
||||||
|
ResolveInfo resolveInfo = new ResolveInfo();
|
||||||
|
resolveInfo.activityInfo = activityInfo;
|
||||||
|
when(mPackageManager.resolveActivity(any(), eq(0))).thenReturn(resolveInfo);
|
||||||
|
|
||||||
|
// Finally mock out the startActivityForResultCall
|
||||||
|
doNothing().when(mMockActivity).startActivityForResult(any(), eq(MasterClear.CREDENTIAL_CONFIRM_REQUEST));
|
||||||
|
|
||||||
|
assertThat(mMasterClear.tryShowAccountConfirmation()).isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initScrollView(int height, int scrollY, int childBottom) {
|
private void initScrollView(int height, int scrollY, int childBottom) {
|
||||||
|
Reference in New Issue
Block a user