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