1 /*
2  * Copyright (C) 2010 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.contacts.common.testing;
18 
19 import android.content.ContentResolver;
20 import android.content.SharedPreferences;
21 import android.util.ArrayMap;
22 import java.util.Map;
23 
24 /**
25  * A mechanism for providing alternative (mock) services to the application while running tests.
26  * Activities, Services and the Application should check with this class to see if a particular
27  * service has been overridden.
28  */
29 public class InjectedServices {
30 
31   private ContentResolver mContentResolver;
32   private SharedPreferences mSharedPreferences;
33   private Map<String, Object> mSystemServices;
34 
getContentResolver()35   public ContentResolver getContentResolver() {
36     return mContentResolver;
37   }
38 
setContentResolver(ContentResolver contentResolver)39   public void setContentResolver(ContentResolver contentResolver) {
40     this.mContentResolver = contentResolver;
41   }
42 
getSharedPreferences()43   public SharedPreferences getSharedPreferences() {
44     return mSharedPreferences;
45   }
46 
setSharedPreferences(SharedPreferences sharedPreferences)47   public void setSharedPreferences(SharedPreferences sharedPreferences) {
48     this.mSharedPreferences = sharedPreferences;
49   }
50 
setSystemService(String name, Object service)51   public void setSystemService(String name, Object service) {
52     if (mSystemServices == null) {
53       mSystemServices = new ArrayMap<>();
54     }
55 
56     mSystemServices.put(name, service);
57   }
58 
getSystemService(String name)59   public Object getSystemService(String name) {
60     if (mSystemServices != null) {
61       return mSystemServices.get(name);
62     }
63     return null;
64   }
65 }
66