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.annotation.UserIdInt;
21 import android.content.ContentResolver;
22 import android.content.SyncAdapterType;
23 import android.text.TextUtils;
24 
25 import org.robolectric.annotation.Implementation;
26 import org.robolectric.annotation.Implements;
27 
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 @Implements(ContentResolver.class)
32 public class ShadowContentResolver extends org.robolectric.shadows.ShadowContentResolver {
33 
34     private static SyncAdapterType[] sSyncAdapterTypes = new SyncAdapterType[0];
35     private static Map<String, Integer> sSyncable = new HashMap<>();
36     private static Map<String, Boolean> sSyncAutomatically = new HashMap<>();
37     private static Map<Integer, Boolean> sMasterSyncAutomatically = new HashMap<>();
38 
39     @Implementation
getSyncAdapterTypesAsUser(int userId)40     protected static SyncAdapterType[] getSyncAdapterTypesAsUser(int userId) {
41         return sSyncAdapterTypes;
42     }
43 
44     @Implementation
getIsSyncableAsUser(Account account, String authority, int userId)45     protected static int getIsSyncableAsUser(Account account, String authority, int userId) {
46         return sSyncable.getOrDefault(authority, 1);
47     }
48 
49     @Implementation
getSyncAutomaticallyAsUser(Account account, String authority, int userId)50     protected static boolean getSyncAutomaticallyAsUser(Account account, String authority,
51             int userId) {
52         return sSyncAutomatically.getOrDefault(authority, true);
53     }
54 
55     @Implementation
setSyncAutomaticallyAsUser(Account account, String authority, boolean sync, int userId)56     protected static void setSyncAutomaticallyAsUser(Account account, String authority,
57             boolean sync, int userId) {
58         if (TextUtils.isEmpty(authority)) {
59             throw new IllegalArgumentException("Authority must be non-empty");
60         }
61     }
62 
63     @Implementation
getMasterSyncAutomaticallyAsUser(int userId)64     protected static boolean getMasterSyncAutomaticallyAsUser(int userId) {
65         return sMasterSyncAutomatically.getOrDefault(userId, true);
66     }
67 
68     @Implementation
setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId)69     protected static void setMasterSyncAutomaticallyAsUser(boolean sync, @UserIdInt int userId) {
70         sMasterSyncAutomatically.put(userId, sync);
71     }
72 
setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes)73     public static void setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes) {
74         sSyncAdapterTypes = syncAdapterTypes;
75     }
76 
setSyncable(String authority, int syncable)77     public static void setSyncable(String authority, int syncable) {
78         sSyncable.put(authority, syncable);
79     }
80 
setSyncAutomatically(String authority, boolean syncAutomatically)81     public static void setSyncAutomatically(String authority, boolean syncAutomatically) {
82         sSyncAutomatically.put(authority, syncAutomatically);
83     }
84 
setMasterSyncAutomatically(int userId, boolean syncAutomatically)85     public static void setMasterSyncAutomatically(int userId, boolean syncAutomatically) {
86         sMasterSyncAutomatically.put(userId, syncAutomatically);
87     }
88 
reset()89     public static void reset() {
90         sSyncable.clear();
91         sSyncAutomatically.clear();
92         sMasterSyncAutomatically.clear();
93         sSyncAdapterTypes = new SyncAdapterType[0];
94     }
95 }
96