blob: 3b90676af8e61d87d7ef79a7a9a6646a418276ff [file] [log] [blame]
Yorke Lee2644d942013-10-28 11:05:43 -07001/*
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
Yorke Lee16bf4322014-05-28 12:18:24 -070017package com.android.contacts.common.testing;
Yorke Lee2644d942013-10-28 11:05:43 -070018
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 */
32public class InjectedServices {
33
34 private ContentResolver mContentResolver;
35 private SharedPreferences mSharedPreferences;
36 private HashMap<String, Object> mSystemServices;
37
Yorke Lee2644d942013-10-28 11:05:43 -070038 public void setContentResolver(ContentResolver contentResolver) {
39 this.mContentResolver = contentResolver;
40 }
41
42 public ContentResolver getContentResolver() {
43 return mContentResolver;
44 }
45
Yorke Lee2644d942013-10-28 11:05:43 -070046 public void setSharedPreferences(SharedPreferences sharedPreferences) {
47 this.mSharedPreferences = sharedPreferences;
48 }
49
50 public SharedPreferences getSharedPreferences() {
51 return mSharedPreferences;
52 }
53
Yorke Lee2644d942013-10-28 11:05:43 -070054 public void setSystemService(String name, Object service) {
55 if (mSystemServices == null) {
56 mSystemServices = Maps.newHashMap();
57 }
58
59 mSystemServices.put(name, service);
60 }
61
62 public Object getSystemService(String name) {
63 if (mSystemServices != null) {
64 return mSystemServices.get(name);
65 }
66 return null;
67 }
68}