Paul Duffin | 0aa3b20 | 2017-12-22 16:13:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package android.test.mock; |
| 17 | |
| 18 | import android.accounts.Account; |
| 19 | import android.accounts.AccountManager; |
| 20 | import android.accounts.AccountManagerCallback; |
| 21 | import android.accounts.AccountManagerFuture; |
| 22 | import android.accounts.AuthenticatorException; |
| 23 | import android.accounts.OnAccountsUpdateListener; |
| 24 | import android.accounts.OperationCanceledException; |
| 25 | import android.content.Context; |
| 26 | import android.os.Handler; |
| 27 | import java.io.IOException; |
| 28 | import java.util.concurrent.TimeUnit; |
| 29 | |
| 30 | /** |
| 31 | * A mock {@link android.accounts.AccountManager} class. |
| 32 | * |
| 33 | * <p>Provided for use by {@code android.test.IsolatedContext}. |
| 34 | * |
| 35 | * @deprecated Use a mocking framework like <a href="https://github.com/mockito/mockito">Mockito</a>. |
| 36 | * New tests should be written using the |
| 37 | * <a href="{@docRoot} |
| 38 | * tools/testing-support-library/index.html">Android Testing Support Library</a>. |
| 39 | */ |
| 40 | @Deprecated |
| 41 | public class MockAccountManager { |
| 42 | |
| 43 | /** |
| 44 | * Create a new mock {@link AccountManager} instance. |
| 45 | * |
| 46 | * @param context the {@link Context} to which the returned object belongs. |
| 47 | * @return the new instance. |
| 48 | */ |
| 49 | public static AccountManager newMockAccountManager(Context context) { |
| 50 | return new MockAccountManagerImpl(context); |
| 51 | } |
| 52 | |
| 53 | private MockAccountManager() { |
| 54 | } |
| 55 | |
| 56 | private static class MockAccountManagerImpl extends AccountManager { |
| 57 | |
| 58 | MockAccountManagerImpl(Context context) { |
| 59 | super(context, null /* IAccountManager */, null /* handler */); |
| 60 | } |
| 61 | |
| 62 | public void addOnAccountsUpdatedListener(OnAccountsUpdateListener listener, |
| 63 | Handler handler, boolean updateImmediately) { |
| 64 | // do nothing |
| 65 | } |
| 66 | |
| 67 | public Account[] getAccounts() { |
| 68 | return new Account[] {}; |
| 69 | } |
| 70 | |
| 71 | public AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( |
| 72 | final String type, final String[] features, |
| 73 | AccountManagerCallback<Account[]> callback, Handler handler) { |
| 74 | return new MockAccountManagerFuture<Account[]>(new Account[0]); |
| 75 | } |
| 76 | |
| 77 | public String blockingGetAuthToken(Account account, String authTokenType, |
| 78 | boolean notifyAuthFailure) |
| 79 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 80 | return null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * A very simple AccountManagerFuture class |
| 86 | * that returns what ever was passed in |
| 87 | */ |
| 88 | private static class MockAccountManagerFuture<T> |
| 89 | implements AccountManagerFuture<T> { |
| 90 | |
| 91 | T mResult; |
| 92 | |
| 93 | MockAccountManagerFuture(T result) { |
| 94 | mResult = result; |
| 95 | } |
| 96 | |
| 97 | public boolean cancel(boolean mayInterruptIfRunning) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | public boolean isCancelled() { |
| 102 | return false; |
| 103 | } |
| 104 | |
| 105 | public boolean isDone() { |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | public T getResult() |
| 110 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 111 | return mResult; |
| 112 | } |
| 113 | |
| 114 | public T getResult(long timeout, TimeUnit unit) |
| 115 | throws OperationCanceledException, IOException, AuthenticatorException { |
| 116 | return getResult(); |
| 117 | } |
| 118 | } |
| 119 | } |