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
17package com.android.contacts.interactions;
18
19import android.content.ContentUris;
20import android.net.Uri;
21import android.provider.ContactsContract.Contacts;
22import android.provider.ContactsContract.Contacts.Entity;
23import android.test.ActivityInstrumentationTestCase2;
24import android.test.suitebuilder.annotation.SmallTest;
25
26import com.android.contacts.ContactsApplication;
27import com.android.contacts.R;
28import com.android.contacts.common.test.FragmentTestActivity;
29import com.android.contacts.common.test.IntegrationTestUtils;
30import com.android.contacts.common.test.mocks.ContactsMockContext;
31import com.android.contacts.common.test.mocks.MockContentProvider;
32import com.android.contacts.common.test.mocks.MockContentProvider.Query;
33import com.android.contacts.common.model.AccountTypeManager;
34import com.android.contacts.common.model.account.AccountType;
35import com.android.contacts.common.model.account.BaseAccountType;
36import com.android.contacts.common.testing.InjectedServices;
37import com.android.contacts.common.test.mocks.MockAccountTypeManager;
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@SmallTest
50public 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");
111        assertWithMessageId(R.string.deleteConfirmation);
112    }
113
114    public void testReadOnlyRawContacts() {
115        expectQuery().returnRow(1, READONLY_ACCOUNT_TYPE, null, 13, "foo");
116        assertWithMessageId(R.string.readOnlyContactWarning);
117    }
118
119    public void testMixOfWritableAndReadOnlyRawContacts() {
120        expectQuery()
121                .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo")
122                .returnRow(2, READONLY_ACCOUNT_TYPE, null, 13, "foo");
123        assertWithMessageId(R.string.readOnlyContactDeleteConfirmation);
124    }
125
126    public void testMultipleWritableRawContacts() {
127        expectQuery()
128                .returnRow(1, WRITABLE_ACCOUNT_TYPE, null, 13, "foo")
129                .returnRow(2, WRITABLE_ACCOUNT_TYPE, null, 13, "foo");
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);
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