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.tests.mocks; 18 19 import com.android.contacts.model.AccountTypeManager; 20 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.ContextWrapper; 24 import android.content.Intent; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ProviderInfo; 27 import android.provider.ContactsContract; 28 import android.provider.Settings; 29 import android.test.mock.MockContentResolver; 30 31 /** 32 * A mock context for contacts unit tests. Forwards everything to 33 * a supplied context, except content resolver operations, which are sent 34 * to mock content providers. 35 */ 36 public class ContactsMockContext extends ContextWrapper { 37 private ContactsMockPackageManager mPackageManager; 38 private MockContentResolver mContentResolver; 39 private MockContentProvider mContactsProvider; 40 private MockContentProvider mSettingsProvider; 41 private MockAccountTypeManager mMockAccountTypeManager; 42 private Intent mIntentForStartActivity; 43 44 public ContactsMockContext(Context base) { 45 super(base); 46 mPackageManager = new ContactsMockPackageManager(); 47 mContentResolver = new MockContentResolver(); 48 mContactsProvider = new MockContentProvider(); 49 mContentResolver.addProvider(ContactsContract.AUTHORITY, mContactsProvider); 50 mContactsProvider.attachInfo(this, new ProviderInfo()); 51 mSettingsProvider = new MockContentProvider(); 52 mSettingsProvider.attachInfo(this, new ProviderInfo()); 53 mContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider); 54 } 55 56 public void setMockAccountTypeManager(MockAccountTypeManager mockAccountTypeManager) { 57 mMockAccountTypeManager = mockAccountTypeManager; 58 } 59 60 @Override 61 public ContentResolver getContentResolver() { 62 return mContentResolver; 63 } 64 65 public MockContentProvider getContactsProvider() { 66 return mContactsProvider; 67 } 68 69 public MockContentProvider getSettingsProvider() { 70 return mSettingsProvider; 71 } 72 73 @Override 74 public PackageManager getPackageManager() { 75 return mPackageManager; 76 } 77 78 @Override 79 public Context getApplicationContext() { 80 return this; 81 } 82 83 /** 84 * Instead of actually sending Intent, this method just remembers what Intent was supplied last. 85 * You can check the content via {@link #getIntentForStartActivity()} for verification. 86 */ 87 @Override 88 public void startActivity(Intent intent) { 89 mIntentForStartActivity = intent; 90 } 91 92 public Intent getIntentForStartActivity() { 93 return mIntentForStartActivity; 94 } 95 96 public void verify() { 97 mContactsProvider.verify(); 98 mSettingsProvider.verify(); 99 } 100 101 @Override 102 public Object getSystemService(String name) { 103 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) { 104 return mMockAccountTypeManager; 105 } 106 return super.getSystemService(name); 107 } 108 } 109