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

Commit 0eabf022 authored by Fred Quintana's avatar Fred Quintana
Browse files

change the IsolatedContext to have a test version of the AccountManager that...

change the IsolatedContext to have a test version of the AccountManager that has no IBinder to the AccountManagerService.
parent d004ee1b
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -67,6 +67,15 @@ public class AccountManager {
        mMainHandler = new Handler(mContext.getMainLooper());
    }

    /**
     * @hide used for testing only
     */
    public AccountManager(Context context, IAccountManager service, Handler handler) {
        mContext = context;
        mService = service;
        mMainHandler = handler;
    }

    public static AccountManager get(Context context) {
        return (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    }
+20 −1
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@ package android.test;

import com.google.android.collect.Lists;

import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdatedListener;
import android.content.ContextWrapper;
import android.content.ContentResolver;
import android.content.Intent;
@@ -11,6 +13,8 @@ import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import java.util.List;

@@ -21,6 +25,7 @@ import java.util.List;
public class IsolatedContext extends ContextWrapper {

    private ContentResolver mResolver;
    private final MockAccountManager mMockAccountManager;

    private List<Intent> mBroadcastIntents = Lists.newArrayList();

@@ -28,6 +33,7 @@ public class IsolatedContext extends ContextWrapper {
            ContentResolver resolver, Context targetContext) {
        super(targetContext);
        mResolver = resolver;
        mMockAccountManager = new MockAccountManager();
    }

    /** Returns the list of intents that were broadcast since the last call to this method. */
@@ -78,8 +84,21 @@ public class IsolatedContext extends ContextWrapper {

    @Override
    public Object getSystemService(String name) {
        // No services exist in this context.
        if (Context.ACCOUNT_SERVICE.equals(name)) {
            return mMockAccountManager;
        }
        // No other services exist in this context.
        return null;
    }

    private class MockAccountManager extends AccountManager {
        public MockAccountManager() {
            super(IsolatedContext.this, null /* IAccountManager */, null /* handler */);
        }

        public void addOnAccountsUpdatedListener(OnAccountsUpdatedListener listener,
                Handler handler, boolean updateImmediately) {
            // do nothing
        }
    }
}