Home | History | Annotate | Download | only in mocks
      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         this(base, ContactsContract.AUTHORITY);
     43     }
     44 
     45     public ContactsMockContext(Context base, String authority) {
     46         super(base);
     47         mPackageManager = new ContactsMockPackageManager();
     48         mContentResolver = new MockContentResolver();
     49         mContactsProvider = new MockContentProvider();
     50         mContentResolver.addProvider(authority, mContactsProvider);
     51         mSettingsProvider = new MockContentProvider();
     52         mContentResolver.addProvider(Settings.AUTHORITY, mSettingsProvider);
     53     }
     54 
     55     @Override
     56     public ContentResolver getContentResolver() {
     57         return mContentResolver;
     58     }
     59 
     60     public MockContentProvider getContactsProvider() {
     61         return mContactsProvider;
     62     }
     63 
     64     public MockContentProvider getSettingsProvider() {
     65         return mSettingsProvider;
     66     }
     67 
     68     @Override
     69     public PackageManager getPackageManager() {
     70         return mPackageManager;
     71     }
     72 
     73     @Override
     74     public Context getApplicationContext() {
     75         return this;
     76     }
     77 
     78     /**
     79      * Instead of actually sending Intent, this method just remembers what Intent was supplied last.
     80      * You can check the content via {@link #getIntentForStartActivity()} for verification.
     81      */
     82     @Override
     83     public void startActivity(Intent intent) {
     84         mIntentForStartActivity = intent;
     85     }
     86 
     87     public Intent getIntentForStartActivity() {
     88         return mIntentForStartActivity;
     89     }
     90 
     91     public void verify() {
     92         mContactsProvider.verify();
     93         mSettingsProvider.verify();
     94     }
     95 
     96 }
     97