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