Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 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 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 19 | import android.content.ContentUris; |
| 20 | import android.net.Uri; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 21 | import android.provider.ContactsContract.Contacts; |
| 22 | import android.provider.ContactsContract.Contacts.Entity; |
| 23 | import android.test.ActivityInstrumentationTestCase2; |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 24 | import android.test.suitebuilder.annotation.MediumTest; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 25 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 26 | import com.android.contacts.ContactsApplication; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 27 | import com.android.contacts.R; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 28 | import com.android.contacts.common.model.AccountTypeManager; |
Chiao Cheng | 428f008 | 2012-11-13 18:38:56 -0800 | [diff] [blame] | 29 | import com.android.contacts.common.model.account.AccountType; |
| 30 | import com.android.contacts.common.model.account.BaseAccountType; |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 31 | import com.android.contacts.common.test.FragmentTestActivity; |
| 32 | import com.android.contacts.common.test.IntegrationTestUtils; |
| 33 | import com.android.contacts.common.test.mocks.ContactsMockContext; |
Yorke Lee | cd321f6 | 2013-10-28 15:20:15 -0700 | [diff] [blame] | 34 | import com.android.contacts.common.test.mocks.MockAccountTypeManager; |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 35 | import com.android.contacts.common.test.mocks.MockContentProvider; |
| 36 | import com.android.contacts.common.test.mocks.MockContentProvider.Query; |
| 37 | import com.android.contacts.common.testing.InjectedServices; |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 38 | |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 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 | */ |
Marcus Hagerott | 38212ae | 2016-09-13 09:18:09 -0700 | [diff] [blame] | 49 | @MediumTest |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 50 | public class ContactDeletionInteractionTest |
| 51 | extends ActivityInstrumentationTestCase2<FragmentTestActivity> { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 56 | public static final String WRITABLE_ACCOUNT_TYPE = "writable"; |
| 57 | public static final String READONLY_ACCOUNT_TYPE = "readonly"; |
| 58 | |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 59 | private ContactsMockContext mContext; |
| 60 | private MockContentProvider mContactsProvider; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 61 | private ContactDeletionInteraction mFragment; |
Hugo Hudson | 149fb7f | 2011-07-21 14:57:53 +0100 | [diff] [blame] | 62 | private IntegrationTestUtils mUtils; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 63 | |
| 64 | public ContactDeletionInteractionTest() { |
| 65 | super(FragmentTestActivity.class); |
| 66 | } |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 67 | |
| 68 | @Override |
| 69 | protected void setUp() throws Exception { |
| 70 | super.setUp(); |
Hugo Hudson | 149fb7f | 2011-07-21 14:57:53 +0100 | [diff] [blame] | 71 | // This test requires that the screen be turned on. |
| 72 | mUtils = new IntegrationTestUtils(getInstrumentation()); |
| 73 | mUtils.acquireScreenWakeLock(getInstrumentation().getTargetContext()); |
| 74 | |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 75 | mContext = new ContactsMockContext(getInstrumentation().getTargetContext()); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 76 | InjectedServices services = new InjectedServices(); |
| 77 | services.setContentResolver(mContext.getContentResolver()); |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 78 | |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 79 | AccountType readOnlyAccountType = new BaseAccountType() { |
| 80 | @Override |
| 81 | public boolean areContactsWritable() { |
| 82 | return false; |
| 83 | } |
| 84 | }; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 85 | readOnlyAccountType.accountType = READONLY_ACCOUNT_TYPE; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 86 | |
Daniel Lehmann | 96e87fd | 2011-09-20 21:31:37 -0700 | [diff] [blame] | 87 | AccountType writableAccountType = new BaseAccountType() { |
| 88 | @Override |
| 89 | public boolean areContactsWritable() { |
| 90 | return true; |
| 91 | } |
| 92 | }; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 93 | writableAccountType.accountType = WRITABLE_ACCOUNT_TYPE; |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 94 | ContactsApplication.injectServices(services); |
Chiao Cheng | dabac3f | 2012-11-01 17:03:07 -0700 | [diff] [blame] | 95 | |
| 96 | final MockAccountTypeManager mockManager = new MockAccountTypeManager( |
| 97 | new AccountType[] { writableAccountType, readOnlyAccountType }, null); |
| 98 | AccountTypeManager.setInstanceForTest(mockManager); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 99 | mContactsProvider = mContext.getContactsProvider(); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Override |
| 103 | protected void tearDown() throws Exception { |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 104 | ContactsApplication.injectServices(null); |
Hugo Hudson | 149fb7f | 2011-07-21 14:57:53 +0100 | [diff] [blame] | 105 | mUtils.releaseScreenWakeLock(); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 106 | super.tearDown(); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public void testSingleWritableRawContact() { |
James Laskey | 202ad07 | 2016-10-21 09:40:56 -0700 | [diff] [blame] | 110 | expectQuery().returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt"); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 111 | assertWithMessageId(R.string.deleteConfirmation); |
| 112 | } |
| 113 | |
| 114 | public void testReadOnlyRawContacts() { |
James Laskey | 202ad07 | 2016-10-21 09:40:56 -0700 | [diff] [blame] | 115 | expectQuery().returnRow(1, READONLY_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt"); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 116 | assertWithMessageId(R.string.readOnlyContactWarning); |
| 117 | } |
| 118 | |
| 119 | public void testMixOfWritableAndReadOnlyRawContacts() { |
| 120 | expectQuery() |
James Laskey | 202ad07 | 2016-10-21 09:40:56 -0700 | [diff] [blame] | 121 | .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt") |
| 122 | .returnRow(2, READONLY_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt"); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 123 | assertWithMessageId(R.string.readOnlyContactDeleteConfirmation); |
| 124 | } |
| 125 | |
| 126 | public void testMultipleWritableRawContacts() { |
| 127 | expectQuery() |
James Laskey | 202ad07 | 2016-10-21 09:40:56 -0700 | [diff] [blame] | 128 | .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt") |
| 129 | .returnRow(2, WRITABLE_ACCOUNT_TYPE, null, 13, "foo", "baz", "bazAlt"); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 130 | assertWithMessageId(R.string.multipleContactDeleteConfirmation); |
| 131 | } |
| 132 | |
| 133 | private Query expectQuery() { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 134 | return mContactsProvider.expectQuery(ENTITY_URI).withProjection( |
Dave Santoro | 2b3f3c5 | 2011-07-26 17:35:42 -0700 | [diff] [blame] | 135 | Entity.RAW_CONTACT_ID, Entity.ACCOUNT_TYPE, Entity.DATA_SET, Entity.CONTACT_ID, |
James Laskey | 202ad07 | 2016-10-21 09:40:56 -0700 | [diff] [blame] | 136 | Entity.LOOKUP_KEY, Entity.DISPLAY_NAME, Entity.DISPLAY_NAME_ALTERNATIVE); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | private void assertWithMessageId(int messageId) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 140 | final FragmentTestActivity activity = getActivity(); |
| 141 | |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 142 | final TestLoaderManager mockLoaderManager = new TestLoaderManager(); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 143 | getInstrumentation().runOnMainSync(new Runnable() { |
| 144 | @Override |
| 145 | public void run() { |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 146 | mFragment = ContactDeletionInteraction.startWithTestLoaderManager( |
| 147 | activity, CONTACT_URI, false, mockLoaderManager); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 148 | } |
| 149 | }); |
| 150 | |
| 151 | getInstrumentation().waitForIdleSync(); |
| 152 | |
Flavio Lerda | 34ce5e9 | 2011-06-06 15:25:17 +0100 | [diff] [blame] | 153 | mockLoaderManager.waitForLoaders(R.id.dialog_delete_contact_loader_id); |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 154 | |
| 155 | getInstrumentation().waitForIdleSync(); |
| 156 | |
| 157 | mContext.verify(); |
| 158 | |
| 159 | assertEquals(messageId, mFragment.mMessageId); |
Dmitri Plotnikov | 9692f26 | 2010-06-29 18:53:11 -0700 | [diff] [blame] | 160 | } |
| 161 | } |