• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.test;
18 
19 import com.google.android.collect.Maps;
20 import com.google.common.annotations.VisibleForTesting;
21 
22 import android.content.ContentResolver;
23 import android.content.SharedPreferences;
24 
25 import java.util.HashMap;
26 
27 /**
28  * A mechanism for providing alternative (mock) services to the application
29  * while running tests. Activities, Services and the Application should check
30  * with this class to see if a particular service has been overridden.
31  */
32 public class InjectedServices {
33 
34     private ContentResolver mContentResolver;
35     private SharedPreferences mSharedPreferences;
36     private HashMap<String, Object> mSystemServices;
37 
38     @VisibleForTesting
setContentResolver(ContentResolver contentResolver)39     public void setContentResolver(ContentResolver contentResolver) {
40         this.mContentResolver = contentResolver;
41     }
42 
getContentResolver()43     public ContentResolver getContentResolver() {
44         return mContentResolver;
45     }
46 
47     @VisibleForTesting
setSharedPreferences(SharedPreferences sharedPreferences)48     public void setSharedPreferences(SharedPreferences sharedPreferences) {
49         this.mSharedPreferences = sharedPreferences;
50     }
51 
getSharedPreferences()52     public SharedPreferences getSharedPreferences() {
53         return mSharedPreferences;
54     }
55 
56     @VisibleForTesting
setSystemService(String name, Object service)57     public void setSystemService(String name, Object service) {
58         if (mSystemServices == null) {
59             mSystemServices = Maps.newHashMap();
60         }
61 
62         mSystemServices.put(name, service);
63     }
64 
getSystemService(String name)65     public Object getSystemService(String name) {
66         if (mSystemServices != null) {
67             return mSystemServices.get(name);
68         }
69         return null;
70     }
71 }
72