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

Commit 8c505dba authored by Hongming Jin's avatar Hongming Jin
Browse files

Test: AccountManagerService APCT test.

APCT tests for AccountManagerService for getAccountsByFeatures

Bug: 31346530
Change-Id: I996d667fb72478f635a8afa5b53fb8b4d350b22a
parent f32d909d
Loading
Loading
Loading
Loading
+152 −59
Original line number Original line Diff line number Diff line
@@ -380,8 +380,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                null); // optionsIn
                null); // optionsIn
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -398,8 +397,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                    null); // optionsIn
                    null); // optionsIn
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -678,8 +676,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                null); // optionsIn
                null); // optionsIn
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -695,8 +692,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                null); // optionsIn
                null); // optionsIn
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -863,8 +859,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -880,8 +875,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1156,8 +1150,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
                AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1171,8 +1164,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
                AccountManagerServiceTestFixtures.ACCOUNT_STATUS_TOKEN);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1186,8 +1178,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                null);
                null);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1237,8 +1228,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                "testPackage"); // opPackageName
                "testPackage"); // opPackageName
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1253,8 +1243,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                "testPackage"); // opPackageName
                "testPackage"); // opPackageName
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1269,10 +1258,8 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                    "testPackage"); // opPackageName
                    "testPackage"); // opPackageName
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }

    }
    }


    @SmallTest
    @SmallTest
@@ -1290,8 +1277,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                "testPackage"); // opPackageName
                "testPackage"); // opPackageName
            fail("SecurityException expected. But no exception was thrown.");
            fail("SecurityException expected. But no exception was thrown.");
        } catch (SecurityException e) {
        } catch (SecurityException e) {
        } catch(Exception e){
            // SecurityException is expected.
            fail(String.format("Expect SecurityException, but got %s.", e));
        }
        }
    }
    }


@@ -1339,8 +1325,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1355,8 +1340,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1373,8 +1357,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("SecurityException expected. But no exception was thrown.");
            fail("SecurityException expected. But no exception was thrown.");
        } catch (SecurityException e) {
        } catch (SecurityException e) {
        } catch(Exception e){
            // SecurityException is expected.
            fail(String.format("Expect SecurityException, but got %s.", e));
        }
        }
    }
    }


@@ -1504,8 +1487,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                "authTokenType");
                "authTokenType");
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1519,8 +1501,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                null); // authTokenType
                null); // authTokenType
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1537,8 +1518,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                    createGetAuthTokenOptions());
                    createGetAuthTokenOptions());
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1595,8 +1575,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                    createGetAuthTokenOptions());
                    createGetAuthTokenOptions());
            fail("SecurityException expected. But no exception was thrown.");
            fail("SecurityException expected. But no exception was thrown.");
        } catch (SecurityException e) {
        } catch (SecurityException e) {
        } catch(Exception e){
            // SecurityException is expected.
            fail(String.format("Expect SecurityException, but got %s.", e));
        }
        }
    }
    }


@@ -1767,8 +1746,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -1786,8 +1764,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2036,8 +2013,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2053,8 +2029,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                UserHandle.USER_SYSTEM);
                UserHandle.USER_SYSTEM);
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2170,8 +2145,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                new Bundle()); // options
                new Bundle()); // options
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2187,8 +2161,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                new Bundle()); // options
                new Bundle()); // options
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2305,8 +2278,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                false); // expectActivityLaunch
                false); // expectActivityLaunch
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2320,8 +2292,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                false); // expectActivityLaunch
                false); // expectActivityLaunch
            fail("IllegalArgumentException expected. But no exception was thrown.");
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
        } catch (IllegalArgumentException e) {
        } catch(Exception e){
            // IllegalArgumentException is expected.
            fail(String.format("Expect IllegalArgumentException, but got %s.", e));
        }
        }
    }
    }


@@ -2337,8 +2308,7 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                false); // expectActivityLaunch
                false); // expectActivityLaunch
            fail("SecurityException expected. But no exception was thrown.");
            fail("SecurityException expected. But no exception was thrown.");
        } catch (SecurityException e) {
        } catch (SecurityException e) {
        } catch(Exception e){
            // SecurityException is expected.
            fail(String.format("Expect SecurityException, but got %s.", e));
        }
        }
    }
    }


@@ -2364,6 +2334,129 @@ public class AccountManagerServiceTest extends AndroidTestCase {
                result.getString(AccountManager.KEY_ACCOUNT_TYPE));
                result.getString(AccountManager.KEY_ACCOUNT_TYPE));
    }
    }


    @SmallTest
    public void testGetAccountsByFeaturesWithNullResponse() throws Exception {
        unlockSystemUser();
        try {
            mAms.getAccountsByFeatures(
                null, // response
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1,
                AccountManagerServiceTestFixtures.ACCOUNT_FEATURES,
                "testpackage"); // opPackageName
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
            // IllegalArgumentException is expected.
        }
    }

    @SmallTest
    public void testGetAccountsByFeaturesWithNullAccountType() throws Exception {
        unlockSystemUser();
        try {
            mAms.getAccountsByFeatures(
                mMockAccountManagerResponse, // response
                null, // accountType
                AccountManagerServiceTestFixtures.ACCOUNT_FEATURES,
                "testpackage"); // opPackageName
            fail("IllegalArgumentException expected. But no exception was thrown.");
        } catch (IllegalArgumentException e) {
            // IllegalArgumentException is expected.
        }
    }

    @SmallTest
    public void testGetAccountsByFeaturesAccountNotVisible() throws Exception {
        unlockSystemUser();

        when(mMockContext.checkCallingOrSelfPermission(anyString())).thenReturn(
                PackageManager.PERMISSION_DENIED);
        when(mMockPackageManager.checkSignatures(anyInt(), anyInt()))
                    .thenReturn(PackageManager.SIGNATURE_NO_MATCH);

        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        mAms.getAccountsByFeatures(
                response, // response
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType
                AccountManagerServiceTestFixtures.ACCOUNT_FEATURES,
                "testpackage"); // opPackageName
        waitForLatch(latch);

        verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture());
        Bundle result = mBundleCaptor.getValue();
        Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS);
        assertTrue(accounts.length == 0);
    }

    @SmallTest
    public void testGetAccountsByFeaturesNullFeatureReturnsAllAccounts() throws Exception {
        unlockSystemUser();

        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null);
        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, "p12", null);

        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        mAms.getAccountsByFeatures(
                response, // response
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType
                null, // features
                "testpackage"); // opPackageName
        waitForLatch(latch);

        verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture());
        Bundle result = mBundleCaptor.getValue();
        Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS);
        Arrays.sort(accounts, new AccountSorter());
        assertEquals(2, accounts.length);
        assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, accounts[0]);
        assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, accounts[1]);
    }

    @SmallTest
    public void testGetAccountsByFeaturesReturnsAccountsWithFeaturesOnly() throws Exception {
        unlockSystemUser();

        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null);
        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_INTERVENE, "p12", null);

        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        mAms.getAccountsByFeatures(
                response, // response
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType
                AccountManagerServiceTestFixtures.ACCOUNT_FEATURES,
                "testpackage"); // opPackageName
        waitForLatch(latch);

        verify(mMockAccountManagerResponse).onResult(mBundleCaptor.capture());
        Bundle result = mBundleCaptor.getValue();
        Account[] accounts = (Account[]) result.getParcelableArray(AccountManager.KEY_ACCOUNTS);
        assertEquals(1, accounts.length);
        assertEquals(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, accounts[0]);
    }

    @SmallTest
    public void testGetAccountsByFeaturesError() throws Exception {
        unlockSystemUser();

        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_SUCCESS, "p11", null);
        mAms.addAccountExplicitly(AccountManagerServiceTestFixtures.ACCOUNT_ERROR, "p12", null);

        final CountDownLatch latch = new CountDownLatch(1);
        Response response = new Response(latch, mMockAccountManagerResponse);
        mAms.getAccountsByFeatures(
                response, // response
                AccountManagerServiceTestFixtures.ACCOUNT_TYPE_1, // accountType
                AccountManagerServiceTestFixtures.ACCOUNT_FEATURES,
                "testpackage"); // opPackageName
        waitForLatch(latch);

        verify(mMockAccountManagerResponse).onError(
                eq(AccountManager.ERROR_CODE_INVALID_RESPONSE), anyString());
        verify(mMockAccountManagerResponse, never()).onResult(any(Bundle.class));
    }

    private void waitForLatch(CountDownLatch latch) {
    private void waitForLatch(CountDownLatch latch) {
        try {
        try {
            latch.await(LATCH_TIMEOUT_MS, TimeUnit.MILLISECONDS);
            latch.await(LATCH_TIMEOUT_MS, TimeUnit.MILLISECONDS);
+4 −1
Original line number Original line Diff line number Diff line
@@ -240,8 +240,11 @@ public class TestAccountType1Authenticator extends AbstractAccountAuthenticator
            String[] features) throws NetworkErrorException {
            String[] features) throws NetworkErrorException {
        Bundle result = new Bundle();
        Bundle result = new Bundle();
        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
        if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_SUCCESS)) {
            // fill bundle with a success result.
            // fill bundle with true.
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, true);
        } else if (account.name.equals(AccountManagerServiceTestFixtures.ACCOUNT_NAME_INTERVENE)) {
            // fill bundle with false.
            result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
        } else {
        } else {
            // return null for error
            // return null for error
            result = null;
            result = null;