Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 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 | |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame] | 17 | package com.android.contacts.testing; |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 18 | |
| 19 | import android.content.ContentResolver; |
| 20 | import android.content.SharedPreferences; |
| 21 | |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Maps; |
| 23 | |
| 24 | import java.util.HashMap; |
| 25 | |
| 26 | /** |
| 27 | * A mechanism for providing alternative (mock) services to the application |
| 28 | * while running tests. Activities, Services and the Application should check |
| 29 | * with this class to see if a particular service has been overridden. |
| 30 | */ |
| 31 | public class InjectedServices { |
| 32 | |
| 33 | private ContentResolver mContentResolver; |
| 34 | private SharedPreferences mSharedPreferences; |
| 35 | private HashMap<String, Object> mSystemServices; |
| 36 | |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 37 | public void setContentResolver(ContentResolver contentResolver) { |
| 38 | this.mContentResolver = contentResolver; |
| 39 | } |
| 40 | |
| 41 | public ContentResolver getContentResolver() { |
| 42 | return mContentResolver; |
| 43 | } |
| 44 | |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 45 | public void setSharedPreferences(SharedPreferences sharedPreferences) { |
| 46 | this.mSharedPreferences = sharedPreferences; |
| 47 | } |
| 48 | |
| 49 | public SharedPreferences getSharedPreferences() { |
| 50 | return mSharedPreferences; |
| 51 | } |
| 52 | |
Yorke Lee | 2644d94 | 2013-10-28 11:05:43 -0700 | [diff] [blame] | 53 | public void setSystemService(String name, Object service) { |
| 54 | if (mSystemServices == null) { |
| 55 | mSystemServices = Maps.newHashMap(); |
| 56 | } |
| 57 | |
| 58 | mSystemServices.put(name, service); |
| 59 | } |
| 60 | |
| 61 | public Object getSystemService(String name) { |
| 62 | if (mSystemServices != null) { |
| 63 | return mSystemServices.get(name); |
| 64 | } |
| 65 | return null; |
| 66 | } |
| 67 | } |