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

Commit 79b289b7 authored by Fan Wu's avatar Fan Wu
Browse files

Updaing ShadowAccountManager to extending from official shadow library

Bug: 319052511

Test: atest SettingsRoboTests
Change-Id: I53ec4261351f49305bb90ff10a6c00549a31e428
parent a01eee9b
Loading
Loading
Loading
Loading
+16 −9
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

import android.accounts.AccountManager;
import android.accounts.AuthenticatorDescription;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
@@ -35,6 +36,7 @@ import androidx.fragment.app.FragmentActivity;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;

import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
@@ -44,33 +46,38 @@ import com.android.settings.testutils.shadow.ShadowRestrictedLockUtilsInternal;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class,
        ShadowRestrictedLockUtilsInternal.class})
public class ChooseAccountPreferenceControllerTest {
    @Rule
    public final MockitoRule mMockitoRule = MockitoJUnit.rule();

    private Context mContext;
    private ChooseAccountPreferenceController mController;
    private Activity mActivity;
    private PreferenceManager mPreferenceManager;
    private PreferenceScreen mPreferenceScreen;
    private ShadowAccountManager mAccountManager;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mContext = ApplicationProvider.getApplicationContext();
        mController = spy(new ChooseAccountPreferenceController(mContext, "controller_key"));
        mActivity = Robolectric.setupActivity(FragmentActivity.class);
        mPreferenceManager = new PreferenceManager(mContext);
        mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
        mAccountManager = (ShadowAccountManager) Shadows.shadowOf(AccountManager.get(mContext));
    }

    @After
@@ -108,7 +115,7 @@ public class ChooseAccountPreferenceControllerTest {
        final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
                "com.android.settings",
                R.string.header_add_an_account, 0, 0, 0, false);
        ShadowAccountManager.addAuthenticator(authDesc);
        mAccountManager.addAuthenticator(authDesc);

        final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
                "com.acct1" /* accountType */, false /* userVisible */,
@@ -133,7 +140,7 @@ public class ChooseAccountPreferenceControllerTest {
        final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
                "com.android.settings",
                R.string.header_add_an_account, 0, 0, 0, false);
        ShadowAccountManager.addAuthenticator(authDesc);
        mAccountManager.addAuthenticator(authDesc);

        final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
                "com.acct1" /* accountType */, false /* userVisible */,
@@ -158,7 +165,7 @@ public class ChooseAccountPreferenceControllerTest {
        final AuthenticatorDescription authDesc = new AuthenticatorDescription("com.acct1",
                "com.android.settings",
                R.string.header_add_an_account, 0, 0, 0, false);
        ShadowAccountManager.addAuthenticator(authDesc);
        mAccountManager.addAuthenticator(authDesc);

        final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
                "com.acct1" /* accountType */, false /* userVisible */,
@@ -184,8 +191,8 @@ public class ChooseAccountPreferenceControllerTest {
        final AuthenticatorDescription authDesc2 = new AuthenticatorDescription("com.acct2",
                "com.android.settings",
                R.string.header_add_an_account, 0, 0, 0, false);
        ShadowAccountManager.addAuthenticator(authDesc);
        ShadowAccountManager.addAuthenticator(authDesc2);
        mAccountManager.addAuthenticator(authDesc);
        mAccountManager.addAuthenticator(authDesc2);

        final SyncAdapterType[] syncAdapters = {new SyncAdapterType("authority" /* authority */,
                "com.acct1" /* accountType */, false /* userVisible */,
+3 −2
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ import java.util.List;
import java.util.Map;

@Implements(AccountManager.class)
public class ShadowAccountManager {
public class ShadowAccountManager extends org.robolectric.shadows.ShadowAccountManager {

    private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
    private static final Map<Integer, List<Account>> sAccountsByUserId = new HashMap<>();
@@ -41,7 +41,8 @@ public class ShadowAccountManager {
        return sAuthenticators.values().toArray(new AuthenticatorDescription[sAuthenticators.size()]);
    }

    public static void addAuthenticator(AuthenticatorDescription authenticator) {
    @Override
    public void addAuthenticator(AuthenticatorDescription authenticator) {
        sAuthenticators.put(authenticator.type, authenticator);
    }