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