Home | History | Annotate | Download | only in interactions
      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.interactions;
     18 
     19 import android.content.ContentUris;
     20 import android.net.Uri;
     21 import android.provider.ContactsContract.Contacts;
     22 import android.provider.ContactsContract.Contacts.Entity;
     23 import android.test.ActivityInstrumentationTestCase2;
     24 import android.test.suitebuilder.annotation.MediumTest;
     25 
     26 import com.android.contacts.ContactsApplication;
     27 import com.android.contacts.R;
     28 import com.android.contacts.model.AccountTypeManager;
     29 import com.android.contacts.model.account.AccountType;
     30 import com.android.contacts.model.account.BaseAccountType;
     31 import com.android.contacts.test.FragmentTestActivity;
     32 import com.android.contacts.test.IntegrationTestUtils;
     33 import com.android.contacts.test.mocks.ContactsMockContext;
     34 import com.android.contacts.test.mocks.MockAccountTypeManager;
     35 import com.android.contacts.test.mocks.MockContentProvider;
     36 import com.android.contacts.test.mocks.MockContentProvider.Query;
     37 import com.android.contacts.testing.InjectedServices;
     38 
     39 /**
     40  * Tests for {@link ContactDeletionInteraction}.
     41  *
     42  * Running all tests:
     43  *
     44  *   runtest contacts
     45  * or
     46  *   adb shell am instrument \
     47  *     -w com.android.contacts.tests/android.test.InstrumentationTestRunner
     48  */
     49 @MediumTest
     50 public class ContactDeletionInteractionTest
     51         extends ActivityInstrumentationTestCase2<FragmentTestActivity> {
     52     private static final Uri CONTACT_URI = ContentUris.withAppendedId(Contacts.CONTENT_URI, 13);
     53     private static final Uri ENTITY_URI = Uri.withAppendedPath(
     54             CONTACT_URI, Entity.CONTENT_DIRECTORY);
     55 
     56     public static final String WRITABLE_ACCOUNT_TYPE = "writable";
     57     public static final String READONLY_ACCOUNT_TYPE = "readonly";
     58 
     59     private ContactsMockContext mContext;
     60     private MockContentProvider mContactsProvider;
     61     private ContactDeletionInteraction mFragment;
     62     private IntegrationTestUtils mUtils;
     63 
     64     public ContactDeletionInteractionTest() {
     65         super(FragmentTestActivity.class);
     66     }
     67 
     68     @Override
     69     protected void setUp() throws Exception {
     70         super.setUp();
     71         // This test requires that the screen be turned on.
     72         mUtils = new IntegrationTestUtils(getInstrumentation());
     73         mUtils.acquireScreenWakeLock(getInstrumentation().getTargetContext());
     74 
     75         mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
     76         InjectedServices services = new InjectedServices();
     77         services.setContentResolver(mContext.getContentResolver());
     78 
     79         AccountType readOnlyAccountType = new BaseAccountType() {
     80             @Override
     81             public boolean areContactsWritable() {
     82                 return false;
     83             }
     84         };
     85         readOnlyAccountType.accountType = READONLY_ACCOUNT_TYPE;
     86 
     87         AccountType writableAccountType = new BaseAccountType() {
     88             @Override
     89             public boolean areContactsWritable() {
     90                 return true;
     91             }
     92         };
     93         writableAccountType.accountType = WRITABLE_ACCOUNT_TYPE;
     94         ContactsApplication.injectServices(services);
     95 
     96         final MockAccountTypeManager mockManager = new MockAccountTypeManager(
     97                 new AccountType[] { writableAccountType, readOnlyAccountType }, null);
     98         AccountTypeManager.setInstanceForTest(mockManager);
     99         mContactsProvider = mContext.getContactsProvider();
    100     }
    101 
    102     @Override
    103     protected void tearDown() throws Exception {
    104         ContactsApplication.injectServices(null);
    105         mUtils.releaseScreenWakeLock();
    106         super.tearDown();
    107     }
    108 
    109     public void testSingleWritableRawContact() {
    110         expectQuery().returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt");
    111         assertWithMessageId(R.string.deleteConfirmation);
    112     }
    113 
    114     public void testReadOnlyRawContacts() {
    115         expectQuery().returnRow(1, READONLY_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt");
    116         assertWithMessageId(R.string.readOnlyContactWarning);
    117     }
    118 
    119     public void testMixOfWritableAndReadOnlyRawContacts() {
    120         expectQuery()
    121                 .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt")
    122                 .returnRow(2, READONLY_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt");
    123         assertWithMessageId(R.string.readOnlyContactDeleteConfirmation);
    124     }
    125 
    126     public void testMultipleWritableRawContacts() {
    127         expectQuery()
    128                 .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt")
    129                 .returnRow(2, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt");
    130         assertWithMessageId(R.string.multipleContactDeleteConfirmation);
    131     }
    132 
    133     private Query expectQuery() {
    134         return mContactsProvider.expectQuery(ENTITY_URI).withProjection(
    135                 Entity.RAW_CONTACT_ID, Entity.ACCOUNT_TYPE, Entity.DATA_SET, Entity.CONTACT_ID,
    136                 Entity.LOOKUP_KEY, Entity.DISPLAY_NAME, Entity.DISPLAY_NAME_ALTERNATIVE);
    137     }
    138 
    139     private void assertWithMessageId(int messageId) {
    140         final FragmentTestActivity activity = getActivity();
    141 
    142         final TestLoaderManager mockLoaderManager = new TestLoaderManager();
    143         getInstrumentation().runOnMainSync(new Runnable() {
    144             @Override
    145             public void run() {
    146                 mFragment = ContactDeletionInteraction.startWithTestLoaderManager(
    147                         activity, CONTACT_URI, false, mockLoaderManager);
    148             }
    149         });
    150 
    151         getInstrumentation().waitForIdleSync();
    152 
    153         mockLoaderManager.waitForLoaders(R.id.dialog_delete_contact_loader_id);
    154 
    155         getInstrumentation().waitForIdleSync();
    156 
    157         mContext.verify();
    158 
    159         assertEquals(messageId, mFragment.mMessageId);
    160     }
    161 }
    162