Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f32d909d authored by Hongming Jin's avatar Hongming Jin Committed by Android (Google) Code Review
Browse files

Merge "Test: AccountManagerService APCT test."

parents 3c30e00a a0c4d1d6
Loading
Loading
Loading
Loading
+1296 −123

File changed.

Preview size limit exceeded, changes collapsed.

+16 −2
Original line number Diff line number Diff line
@@ -31,7 +31,8 @@ public final class AccountManagerServiceTestFixtures {
            "account_manager_service_test:account_status_token_key";
    public static final String KEY_ACCOUNT_PASSWORD =
            "account_manager_service_test:account_password_key";

    public static final String KEY_OPTIONS_BUNDLE =
            "account_manager_service_test:option_bundle_key";
    public static final String ACCOUNT_NAME_SUCCESS = "success_on_return@fixture.com";
    public static final String ACCOUNT_NAME_INTERVENE = "intervene@fixture.com";
    public static final String ACCOUNT_NAME_ERROR = "error@fixture.com";
@@ -47,7 +48,20 @@ public final class AccountManagerServiceTestFixtures {

    public static final String ACCOUNT_STATUS_TOKEN =
            "com.android.server.accounts.account_manager_service_test.account.status.token";

    public static final String AUTH_TOKEN_LABEL =
            "com.android.server.accounts.account_manager_service_test.auth.token.label";
    public static final String AUTH_TOKEN =
            "com.android.server.accounts.account_manager_service_test.auth.token";
    public static final String KEY_TOKEN_EXPIRY =
            "com.android.server.accounts.account_manager_service_test.auth.token.expiry";
    public static final String ACCOUNT_FEATURE1 =
            "com.android.server.accounts.account_manager_service_test.feature1";
    public static final String ACCOUNT_FEATURE2 =
            "com.android.server.accounts.account_manager_service_test.feature2";
    public static final String[] ACCOUNT_FEATURES =
            new String[]{ACCOUNT_FEATURE1, ACCOUNT_FEATURE2};
    public static final String CALLER_PACKAGE =
            "com.android.server.accounts.account_manager_service_test.caller.package";
    public static final String ACCOUNT_PASSWORD =
            "com.android.server.accounts.account_manager_service_test.account.password";
    public static final String KEY_RESULT = "account_manager_service_test:result";
+166 −14
Original line number Diff line number Diff line
@@ -45,8 +45,15 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator

    @Override
    public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
        throw new UnsupportedOperationException(
                "editProperties is not yet supported by the TestAccountAuthenticator");
        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME,
                AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE,
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1);
        result.putString(
                AccountManager.KEY_AUTHTOKEN,
                Integer.toString(mTokenCounter.incrementAndGet()));
        return result;
    }

    @Override
@@ -59,10 +66,38 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
        if (!mAccountType.equals(accountType)) {
            throw new IllegalArgumentException("Request to the wrong authenticator!");
        }
        String accountName = null;

        if (options != null) {
            accountName = options.getString(AccountManagerServiceTestFixtures.KEY_ACCOUNT_NAME);
        }

        Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, "test_account@test.com");
        if (accountName.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            result.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, mAccountType);
            result.putString(AccountManager.KEY_AUTHTOKEN,
                    Integer.toString(mTokenCounter.incrementAndGet()));
            result.putParcelable(AccountManagerServiceTestFixtures.KEY_OPTIONS_BUNDLE, options);
        } else if (accountName.equals(
                AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            // Specify data to be returned by the eventual activity.
            Intent eventualActivityResultData = new Intent();
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, accountType);
            // Fill result with Intent.
            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT, eventualActivityResultData);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);

            result.putParcelable(AccountManager.KEY_INTENT, intent);
        } else {
            fillResultWithError(
                    result,
                    AccountManager.ERROR_CODE_INVALID_RESPONSE,
                    AccountManagerServiceTestFixtures.ERROR_MESSAGE);
        }
        return result;
    }

@@ -71,8 +106,38 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
            AccountAuthenticatorResponse response,
            Account account,
            Bundle options) throws NetworkErrorException {
        throw new UnsupportedOperationException(
                "confirmCredentials is not yet supported by the TestAccountAuthenticator");
        if (!mAccountType.equals(account.type)) {
            throw new IllegalArgumentException("Request to the wrong authenticator!");
        }
        Bundle result = new Bundle();

        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            // Specify data to be returned by the eventual activity.
            Intent eventualActivityResultData = new Intent();
            eventualActivityResultData.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);

            // Fill result with Intent.
            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
                    eventualActivityResultData);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);

            result.putParcelable(AccountManager.KEY_INTENT, intent);
        } else {
            // fill with error
            fillResultWithError(
                    result,
                    AccountManager.ERROR_CODE_INVALID_RESPONSE,
                    AccountManagerServiceTestFixtures.ERROR_MESSAGE);
        }
        return result;
    }

    @Override
@@ -81,14 +146,53 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
            Account account,
            String authTokenType,
            Bundle options) throws NetworkErrorException {
        throw new UnsupportedOperationException(
                "getAuthToken is not yet supported by the TestAccountAuthenticator");
        if (!mAccountType.equals(account.type)) {
            throw new IllegalArgumentException("Request to the wrong authenticator!");
        }
        Bundle result = new Bundle();

        long expiryMillis = (options == null)
                ? 0 : options.getLong(AccountManagerServiceTestFixtures.KEY_TOKEN_EXPIRY);
        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            result.putString(
                    AccountManager.KEY_AUTHTOKEN, AccountManagerServiceTestFixtures.AUTH_TOKEN);
            result.putLong(
                    AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY,
                    expiryMillis);
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            // Specify data to be returned by the eventual activity.
            Intent eventualActivityResultData = new Intent();
            eventualActivityResultData.putExtra(
                    AccountManager.KEY_AUTHTOKEN, AccountManagerServiceTestFixtures.AUTH_TOKEN);
            eventualActivityResultData.putExtra(
                    AbstractAccountAuthenticator.KEY_CUSTOM_TOKEN_EXPIRY,
                    expiryMillis);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);

            // Fill result with Intent.
            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
                    eventualActivityResultData);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);

            result.putParcelable(AccountManager.KEY_INTENT, intent);

        } else {
            fillResultWithError(
                    result,
                    AccountManager.ERROR_CODE_INVALID_RESPONSE,
                    AccountManagerServiceTestFixtures.ERROR_MESSAGE);
        }
        return result;
    }

    @Override
    public String getAuthTokenLabel(String authTokenType) {
        throw new UnsupportedOperationException(
                "getAuthTokenLabel is not yet supported by the TestAccountAuthenticator");
        return AccountManagerServiceTestFixtures.AUTH_TOKEN_LABEL;
    }

    @Override
@@ -101,8 +205,31 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
            throw new IllegalArgumentException("Request to the wrong authenticator!");
        }
        Bundle result = new Bundle();

        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
            result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            // Specify data to be returned by the eventual activity.
            Intent eventualActivityResultData = new Intent();
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_NAME, account.name);
            eventualActivityResultData.putExtra(AccountManager.KEY_ACCOUNT_TYPE, account.type);

            // Fill result with Intent.
            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_RESULT,
                    eventualActivityResultData);
            intent.putExtra(AccountManagerServiceTestFixtures.KEY_CALLBACK, response);

            result.putParcelable(AccountManager.KEY_INTENT, intent);
        } else {
            // fill with error
            fillResultWithError(
                    result,
                    AccountManager.ERROR_CODE_INVALID_RESPONSE,
                    AccountManagerServiceTestFixtures.ERROR_MESSAGE);
        }
        return result;
    }

@@ -111,8 +238,17 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
            AccountAuthenticatorResponse response,
            Account account,
            String[] features) throws NetworkErrorException {
        throw new UnsupportedOperationException(
                "hasFeatures is not yet supported by the TestAccountAuthenticator");
        Bundle result = new Bundle();
        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        } else {
            // return null for error
            result = null;
        }

        response.onResult(result);
        return null;
    }

    @Override
@@ -300,6 +436,22 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
        return null;
    }

    @Override
    public Bundle getAccountRemovalAllowed(
            AccountAuthenticatorResponse response, Account account) throws NetworkErrorException {
        Bundle result = new Bundle();
        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        } else if (account.name.equals(
                AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            Intent intent = new Intent(mContext, AccountAuthenticatorDummyActivity.class);
            result.putParcelable(AccountManager.KEY_INTENT, intent);
        } else {
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
        }
        return result;
    }

    private void fillResultWithError(Bundle result, Bundle options) {
        int errorCode = AccountManager.ERROR_CODE_INVALID_RESPONSE;
        String errorMsg = "Default Error Message";