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