blob: a89ddc96de1ddace4f99cc21f1a4e97e65844fcc [file] [log] [blame]
Dmitri Plotnikov9692f262010-06-29 18:53:11 -07001/*
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
17package com.android.contacts.interactions;
18
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070019import android.content.ContentUris;
20import android.net.Uri;
21import android.os.AsyncTask;
22import android.provider.ContactsContract.Contacts;
23import android.provider.ContactsContract.Contacts.Entity;
24import android.test.ActivityInstrumentationTestCase2;
25import android.test.suitebuilder.annotation.SmallTest;
26
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080027import com.android.contacts.ContactsApplication;
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070028import com.android.contacts.R;
Chiao Chengdabac3f2012-11-01 17:03:07 -070029import com.android.contacts.common.test.FragmentTestActivity;
30import com.android.contacts.common.test.IntegrationTestUtils;
31import com.android.contacts.common.test.mocks.ContactsMockContext;
32import com.android.contacts.common.test.mocks.MockContentProvider;
33import com.android.contacts.common.test.mocks.MockContentProvider.Query;
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080034import com.android.contacts.model.AccountTypeManager;
Chiao Cheng428f0082012-11-13 18:38:56 -080035import com.android.contacts.common.model.account.AccountType;
36import com.android.contacts.common.model.account.BaseAccountType;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080037import com.android.contacts.test.InjectedServices;
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080038import com.android.contacts.tests.mocks.MockAccountTypeManager;
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070039
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070040/**
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 Miyakawa04549602011-11-23 13:25:10 -080050@SmallTest
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080051public class ContactDeletionInteractionTest
52 extends ActivityInstrumentationTestCase2<FragmentTestActivity> {
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070053
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080054 static {
55 // AsyncTask class needs to be initialized on the main thread.
56 AsyncTask.init();
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070057 }
58
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080059 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 Plotnikov1173ae22011-01-09 14:00:39 -080063 public static final String WRITABLE_ACCOUNT_TYPE = "writable";
64 public static final String READONLY_ACCOUNT_TYPE = "readonly";
65
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070066 private ContactsMockContext mContext;
67 private MockContentProvider mContactsProvider;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080068 private ContactDeletionInteraction mFragment;
Hugo Hudson149fb7f2011-07-21 14:57:53 +010069 private IntegrationTestUtils mUtils;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080070
71 public ContactDeletionInteractionTest() {
72 super(FragmentTestActivity.class);
73 }
Dmitri Plotnikov9692f262010-06-29 18:53:11 -070074
75 @Override
76 protected void setUp() throws Exception {
77 super.setUp();
Hugo Hudson149fb7f2011-07-21 14:57:53 +010078 // This test requires that the screen be turned on.
79 mUtils = new IntegrationTestUtils(getInstrumentation());
80 mUtils.acquireScreenWakeLock(getInstrumentation().getTargetContext());
81
Chiao Chengdabac3f2012-11-01 17:03:07 -070082 mContext = new ContactsMockContext(getInstrumentation().getTargetContext());
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080083 InjectedServices services = new InjectedServices();
84 services.setContentResolver(mContext.getContentResolver());
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080085
Daniel Lehmann96e87fd2011-09-20 21:31:37 -070086 AccountType readOnlyAccountType = new BaseAccountType() {
87 @Override
88 public boolean areContactsWritable() {
89 return false;
90 }
91 };
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080092 readOnlyAccountType.accountType = READONLY_ACCOUNT_TYPE;
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080093
Daniel Lehmann96e87fd2011-09-20 21:31:37 -070094 AccountType writableAccountType = new BaseAccountType() {
95 @Override
96 public boolean areContactsWritable() {
97 return true;
98 }
99 };
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -0800100 writableAccountType.accountType = WRITABLE_ACCOUNT_TYPE;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800101 ContactsApplication.injectServices(services);
Chiao Chengdabac3f2012-11-01 17:03:07 -0700102
103 final MockAccountTypeManager mockManager = new MockAccountTypeManager(
104 new AccountType[] { writableAccountType, readOnlyAccountType }, null);
105 AccountTypeManager.setInstanceForTest(mockManager);
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700106 mContactsProvider = mContext.getContactsProvider();
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800107 }
108
109 @Override
110 protected void tearDown() throws Exception {
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800111 ContactsApplication.injectServices(null);
Hugo Hudson149fb7f2011-07-21 14:57:53 +0100112 mUtils.releaseScreenWakeLock();
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800113 super.tearDown();
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700114 }
115
116 public void testSingleWritableRawContact() {
Flavio Lerda05562502011-07-28 14:25:07 +0100117 expectQuery().returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo");
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700118 assertWithMessageId(R.string.deleteConfirmation);
119 }
120
121 public void testReadOnlyRawContacts() {
Flavio Lerda05562502011-07-28 14:25:07 +0100122 expectQuery().returnRow(1, READONLY_ACCOUNT_TYPE, null, 13, "foo");
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700123 assertWithMessageId(R.string.readOnlyContactWarning);
124 }
125
126 public void testMixOfWritableAndReadOnlyRawContacts() {
127 expectQuery()
Flavio Lerda05562502011-07-28 14:25:07 +0100128 .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo")
129 .returnRow(2, READONLY_ACCOUNT_TYPE, null, 13, "foo");
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700130 assertWithMessageId(R.string.readOnlyContactDeleteConfirmation);
131 }
132
133 public void testMultipleWritableRawContacts() {
134 expectQuery()
Flavio Lerda05562502011-07-28 14:25:07 +0100135 .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo")
136 .returnRow(2, WRITABLE_ACCOUNT_TYPE, null, 13, "foo");
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700137 assertWithMessageId(R.string.multipleContactDeleteConfirmation);
138 }
139
140 private Query expectQuery() {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800141 return mContactsProvider.expectQuery(ENTITY_URI).withProjection(
Dave Santoro2b3f3c52011-07-26 17:35:42 -0700142 Entity.RAW_CONTACT_ID, Entity.ACCOUNT_TYPE, Entity.DATA_SET, Entity.CONTACT_ID,
143 Entity.LOOKUP_KEY);
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700144 }
145
146 private void assertWithMessageId(int messageId) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800147 final FragmentTestActivity activity = getActivity();
148
Flavio Lerda34ce5e92011-06-06 15:25:17 +0100149 final TestLoaderManager mockLoaderManager = new TestLoaderManager();
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800150 getInstrumentation().runOnMainSync(new Runnable() {
151 @Override
152 public void run() {
Flavio Lerda34ce5e92011-06-06 15:25:17 +0100153 mFragment = ContactDeletionInteraction.startWithTestLoaderManager(
154 activity, CONTACT_URI, false, mockLoaderManager);
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800155 }
156 });
157
158 getInstrumentation().waitForIdleSync();
159
Flavio Lerda34ce5e92011-06-06 15:25:17 +0100160 mockLoaderManager.waitForLoaders(R.id.dialog_delete_contact_loader_id);
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -0800161
162 getInstrumentation().waitForIdleSync();
163
164 mContext.verify();
165
166 assertEquals(messageId, mFragment.mMessageId);
Dmitri Plotnikov9692f262010-06-29 18:53:11 -0700167 }
168}