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