• 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 import org.robolectric.annotation.Resetter;
30 
31 import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
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     public static SyncAdapterType[] getSyncAdapterTypesAsUser(int userId) {
46         return sSyncAdapterTypes;
47     }
48 
49     @Implementation
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)50     public 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     public 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     public static boolean getSyncAutomaticallyAsUser(Account account, String authority,
65             int userId) {
66         return sSyncAutomatically.containsKey(authority) ? sSyncAutomatically.get(authority) : true;
67     }
68 
69     @Implementation
getMasterSyncAutomaticallyAsUser(int userId)70     public static boolean getMasterSyncAutomaticallyAsUser(int userId) {
71         return sMasterSyncAutomatically.containsKey(userId)
72                 ? sMasterSyncAutomatically.get(userId) : true;
73     }
74 
setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes)75     public static void setSyncAdapterTypes(SyncAdapterType[] syncAdapterTypes) {
76         sSyncAdapterTypes = syncAdapterTypes;
77     }
78 
setSyncable(String authority, int syncable)79     public static void setSyncable(String authority, int syncable) {
80         sSyncable.put(authority, syncable);
81     }
82 
setSyncAutomatically(String authority, boolean syncAutomatically)83     public static void setSyncAutomatically(String authority, boolean syncAutomatically) {
84         sSyncAutomatically.put(authority, syncAutomatically);
85     }
86 
setMasterSyncAutomatically(int userId, boolean syncAutomatically)87     public static void setMasterSyncAutomatically(int userId, boolean syncAutomatically) {
88         sMasterSyncAutomatically.put(userId, syncAutomatically);
89     }
90 
reset()91     public static void reset() {
92         sSyncable.clear();
93         sSyncAutomatically.clear();
94         sMasterSyncAutomatically.clear();
95         sSyncAdapterTypes = new SyncAdapterType[0];
96     }
97 }
98