• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.testutils;
18 
19 import android.accounts.Account;
20 import android.accounts.AccountManager;
21 import android.accounts.AuthenticatorDescription;
22 import android.os.UserHandle;
23 import android.util.ArrayMap;
24 
25 import org.robolectric.annotation.Implementation;
26 import org.robolectric.annotation.Implements;
27 
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 
32 @Implements(AccountManager.class)
33 public class ShadowAccountManager extends org.robolectric.shadows.ShadowAccountManager {
34 
35     private final Map<Integer, List<Account>> mAccountsAsUserMap = new ArrayMap<>();
36     private final Map<Integer, List<AuthenticatorDescription>> mAuthenticatorAsUserMap =
37             new ArrayMap<>();
38 
39     @Implementation
getAccountsAsUser(int userId)40     protected Account[] getAccountsAsUser(int userId) {
41         if (mAccountsAsUserMap.containsKey(userId)) {
42             return mAccountsAsUserMap.get(userId).toArray(new Account[]{});
43         }
44         return getAccounts();
45     }
46 
addAccountAsUser(int userId, Account account)47     public void addAccountAsUser(int userId, Account account) {
48         mAccountsAsUserMap.putIfAbsent(userId, new ArrayList<>());
49         mAccountsAsUserMap.get(userId).add(account);
50     }
51 
52     @Implementation
getAccountsByTypeAsUser(String type, UserHandle userHandle)53     protected Account[] getAccountsByTypeAsUser(String type, UserHandle userHandle) {
54         return getAccountsByType(type);
55     }
56 
57     @Implementation
getAuthenticatorTypesAsUser(int userId)58     protected AuthenticatorDescription[] getAuthenticatorTypesAsUser(int userId) {
59         if (mAuthenticatorAsUserMap.containsKey(userId)) {
60             return mAuthenticatorAsUserMap.get(userId).toArray(new AuthenticatorDescription[]{});
61         }
62         return getAuthenticatorTypes();
63     }
64 
addAuthenticatorAsUser(int userId, AuthenticatorDescription authenticator)65     public void addAuthenticatorAsUser(int userId, AuthenticatorDescription authenticator) {
66         mAuthenticatorAsUserMap.putIfAbsent(userId, new ArrayList<>());
67         mAuthenticatorAsUserMap.get(userId).add(authenticator);
68     }
69 
70     @Override
removeAllAccounts()71     public void removeAllAccounts() {
72         super.removeAllAccounts();
73         mAccountsAsUserMap.clear();
74         mAuthenticatorAsUserMap.clear();
75     }
76 }
77