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 static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
20 
21 import android.accounts.Account;
22 import android.content.ContentResolver;
23 import android.content.SyncAdapterType;
24 import android.database.Cursor;
25 import android.database.MatrixCursor;
26 import android.net.Uri;
27 import android.provider.SearchIndexablesContract;
28 import android.text.TextUtils;
29 
30 import org.robolectric.annotation.Implementation;
31 import org.robolectric.annotation.Implements;
32 
33 import java.util.HashMap;
34 import java.util.Map;
35 
36 @Implements(ContentResolver.class)
37 public class ShadowContentResolver {
38 
39     private static SyncAdapterType[] sSyncAdapterTypes = new SyncAdapterType[0];
40     private static Map<String, Integer> sSyncable = new HashMap<>();
41     private static Map<String, Boolean> sSyncAutomatically = new HashMap<>();
42     private static Map<Integer, Boolean> sMasterSyncAutomatically = new HashMap<>();
43 
44     @Implementation
getSyncAdapterTypesAsUser(int userId)45     protected static SyncAdapterType[] getSyncAdapterTypesAsUser(int userId) {
46         return sSyncAdapterTypes;
47     }
48 
49     @Implementation
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)50     protected final Cursor query(Uri uri, String[] projection, String selection,
51             String[] selectionArgs, String sortOrder) {
52         MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
53         MatrixCursor.RowBuilder builder = cursor.newRow()
54                 .add(SearchIndexablesContract.NonIndexableKey.COLUMN_KEY_VALUE, "");
55         return cursor;
56     }
57 
58     @Implementation
getIsSyncableAsUser(Account account, String authority, int userId)59     protected static int getIsSyncableAsUser(Account account, String authority, int userId) {
60         return sSyncable.containsKey(authority) ? sSyncable.get(authority) : 1;
61     }
62 
63     @Implementation
getSyncAutomaticallyAsUser(Account account, String authority, int userId)64     protected static boolean getSyncAutomaticallyAsUser(Account account, String authority,
65             int userId) {
66         return sSyncAutomatically.containsKey(authority) ? sSyncAutomatically.get(authority) : true;
67     }
68 
69     @Implementation
setSyncAutomaticallyAsUser(Account account, String authority, boolean sync, int userId)70     protected static void setSyncAutomaticallyAsUser(Account account, String authority,
71             boolean sync, int userId) {
72         if (TextUtils.isEmpty(authority)) {
73             throw new IllegalArgumentException("Authority must be non-empty");
74         }
75     }
76 
77     @Implementation
getMasterSyncAutomaticallyAsUser(int userId)78     protected static boolean getMasterSyncAutomaticallyAsUser(int userId) {
79         return sMasterSyncAutomatically.containsKey(userId)
80                 ? sMasterSyncAutomatically.get(userId) : true;
81     }
82 
setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes)83     public static void setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes) {
84         sSyncAdapterTypes = syncAdapterTypes;
85     }
86 
setSyncable(String authority, int syncable)87     public static void setSyncable(String authority, int syncable) {
88         sSyncable.put(authority, syncable);
89     }
90 
setSyncAutomatically(String authority, boolean syncAutomatically)91     public static void setSyncAutomatically(String authority, boolean syncAutomatically) {
92         sSyncAutomatically.put(authority, syncAutomatically);
93     }
94 
setMasterSyncAutomatically(int userId, boolean syncAutomatically)95     public static void setMasterSyncAutomatically(int userId, boolean syncAutomatically) {
96         sMasterSyncAutomatically.put(userId, syncAutomatically);
97     }
98 
reset()99     public static void reset() {
100         sSyncable.clear();
101         sSyncAutomatically.clear();
102         sMasterSyncAutomatically.clear();
103         sSyncAdapterTypes = new SyncAdapterType[0];
104     }
105 }
106