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
17package com.android.contacts.common.testing;
18
19import android.content.ContentResolver;
20import android.content.SharedPreferences;
21
22import com.google.common.annotations.VisibleForTesting;
23import com.google.common.collect.Maps;
24
25import 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@NeededForTesting
33public class InjectedServices {
34
35    private ContentResolver mContentResolver;
36    private SharedPreferences mSharedPreferences;
37    private HashMap<String, Object> mSystemServices;
38
39    @NeededForTesting
40    public void setContentResolver(ContentResolver contentResolver) {
41        this.mContentResolver = contentResolver;
42    }
43
44    public ContentResolver getContentResolver() {
45        return mContentResolver;
46    }
47
48    @NeededForTesting
49    public void setSharedPreferences(SharedPreferences sharedPreferences) {
50        this.mSharedPreferences = sharedPreferences;
51    }
52
53    public SharedPreferences getSharedPreferences() {
54        return mSharedPreferences;
55    }
56
57    @NeededForTesting
58    public void setSystemService(String name, Object service) {
59        if (mSystemServices == null) {
60            mSystemServices = Maps.newHashMap();
61        }
62
63        mSystemServices.put(name, service);
64    }
65
66    public Object getSystemService(String name) {
67        if (mSystemServices != null) {
68            return mSystemServices.get(name);
69        }
70        return null;
71    }
72}
73