1 /*
2  * Copyright (C) 2016 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 
17 package com.android.settings.testutils.shadow;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AuthenticatorDescription;
22 import android.annotation.NonNull;
23 
24 import org.robolectric.annotation.Implementation;
25 import org.robolectric.annotation.Implements;
26 
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 
32 @Implements(AccountManager.class)
33 public class ShadowAccountManager {
34 
35     private static final Map<String, AuthenticatorDescription> sAuthenticators = new HashMap<>();
36     private static final Map<Integer, List<Account>> sAccountsByUserId = new HashMap<>();
37 
38     @Implementation
getAuthenticatorTypesAsUser(int userId)39     protected AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
40         return sAuthenticators.values().toArray(new AuthenticatorDescription[sAuthenticators.size()]);
41     }
42 
addAuthenticator(AuthenticatorDescription authenticator)43     public static void addAuthenticator(AuthenticatorDescription authenticator) {
44         sAuthenticators.put(authenticator.type, authenticator);
45     }
46 
reset()47     public static void reset() {
48         sAuthenticators.clear();
49         sAccountsByUserId.clear();
50     }
51 
52     @Implementation @NonNull
getAccountsAsUser(int userId)53     protected Account[] getAccountsAsUser(int userId) {
54         if (sAccountsByUserId.containsKey(userId)) {
55             return sAccountsByUserId.get(userId).toArray(new Account[0]);
56         } else {
57             return new Account[0];
58         }
59     }
60 
addAccountForUser(int userId, Account account)61     public static void addAccountForUser(int userId, Account account) {
62         if (!sAccountsByUserId.containsKey(userId)) {
63             sAccountsByUserId.put(userId, new ArrayList<>());
64         }
65         sAccountsByUserId.get(userId).add(account);
66     }
67 }
68