summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/messaging/datamodel/ParticipantRefreshTest.java
blob: cd1d6c72a195d1bd772653c2fd6cbe51863e63e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.messaging.datamodel;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.TextUtils;

import com.android.messaging.BugleTestCase;
import com.android.messaging.FakeContentProvider;
import com.android.messaging.FakeContext;
import com.android.messaging.FakeFactory;
import com.android.messaging.datamodel.DatabaseHelper.ParticipantColumns;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.datamodel.data.ParticipantData.ParticipantsQuery;
import com.android.messaging.util.ContactUtil;

import org.junit.Assert;

/**
 * Utility class for testing ParticipantRefresh class for different scenarios.
 */
@SmallTest
public class ParticipantRefreshTest extends BugleTestCase {
    private FakeContext mContext;
    FakeFactory mFakeFactory;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        mContext = new FakeContext(getTestContext());

        final ContentProvider provider = new MessagingContentProvider();
        provider.attachInfo(mContext, null);
        mContext.addContentProvider(MessagingContentProvider.AUTHORITY, provider);

        final FakeDataModel fakeDataModel = new FakeDataModel(mContext);
        mFakeFactory = FakeFactory.registerWithFakeContext(getTestContext(), mContext)
                .withDataModel(fakeDataModel);
    }

    /**
     * Add some phonelookup result into take PhoneLookup content provider. This will be
     * used for doing phone lookup during participant refresh.
     */
    private void addPhoneLookup(final String phone, final Object[][] lookupResult) {
        final Uri uri = ContactUtil.lookupPhone(mContext, phone).getUri();
        final FakeContentProvider phoneLookup = new FakeContentProvider(mContext,
                uri, false);
        phoneLookup.addOverrideData(uri, null, null, ContactUtil.PhoneLookupQuery.PROJECTION,
                lookupResult);
        mFakeFactory.withProvider(uri, phoneLookup);
    }

    /**
     * Add some participant to test database.
     */
    private void addParticipant(final String normalizedDestination, final long contactId,
            final String name, final String photoUrl) {
        final DatabaseWrapper db = DataModel.get().getDatabase();
        final ContentValues values = new ContentValues();

        values.put(ParticipantColumns.NORMALIZED_DESTINATION, normalizedDestination);
        values.put(ParticipantColumns.CONTACT_ID, contactId);
        values.put(ParticipantColumns.FULL_NAME, name);
        values.put(ParticipantColumns.PROFILE_PHOTO_URI, photoUrl);

        db.beginTransaction();
        try {
            db.insert(DatabaseHelper.PARTICIPANTS_TABLE, null, values);
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    /**
     * Verify that participant in the database has expected contacdtId, name and photoUrl fields.
     */
    private void verifyParticipant(final String normalizedDestination, final long contactId,
            final String name, final String photoUrl) {
        final DatabaseWrapper db = DataModel.get().getDatabase();
        db.beginTransaction();
        try {
            final String selection = ParticipantColumns.NORMALIZED_DESTINATION + "=?";
            final String[] selectionArgs = new String[] { normalizedDestination };

            final Cursor cursor = db.query(DatabaseHelper.PARTICIPANTS_TABLE,
                    ParticipantsQuery.PROJECTION, selection, selectionArgs, null, null, null);

            if (cursor == null || cursor.getCount() != 1) {
                Assert.fail("Should have participants for:" + normalizedDestination);
                return;
            }

            cursor.moveToFirst();
            final int currentContactId = cursor.getInt(ParticipantsQuery.INDEX_CONTACT_ID);
            final String currentName = cursor.getString(ParticipantsQuery.INDEX_FULL_NAME);
            final String currentPhotoUrl =
                    cursor.getString(ParticipantsQuery.INDEX_PROFILE_PHOTO_URI);
            if (currentContactId != contactId) {
                Assert.fail("Contact Id doesn't match. normalizedNumber=" + normalizedDestination +
                        " expected=" + contactId + " actual=" + currentContactId);
                return;
            }

            if (!TextUtils.equals(currentName, name)) {
                Assert.fail("Name doesn't match. normalizedNumber=" + normalizedDestination +
                        " expected=" + name + " actual=" + currentName);
                return;
            }

            if (!TextUtils.equals(currentPhotoUrl, photoUrl)) {
                Assert.fail("Contact Id doesn't match. normalizedNumber=" + normalizedDestination +
                        " expected=" + photoUrl + " actual=" + currentPhotoUrl);
                return;
            }

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }

    /**
     * Verify that incremental refresh will resolve previously not resolved participants.
     */
    public void testIncrementalRefreshNotResolvedSingleMatch() {
        addParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_RESOLVED,
                null, null);
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_INCREMENTAL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that incremental refresh will resolve previously not resolved participants.
     */
    public void testIncrementalRefreshNotResolvedMultiMatch() {
        addParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_RESOLVED,
                null, null);
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null },
                { 2L, "Joe", "content://photo/joe", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_INCREMENTAL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that incremental refresh will not touch already-resolved participants.
     */
    public void testIncrementalRefreshResolvedSingleMatch() {
        addParticipant("650-123-1233", 1, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_INCREMENTAL);
        verifyParticipant("650-123-1233", 1, "Joh", "content://photo/joh");
    }

    /**
     * Verify that full refresh will correct already-resolved participants if needed
     */
    public void testFullRefreshResolvedSingleMatch() {
        addParticipant("650-123-1233", 1, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that incremental refresh will not touch participant that is marked as not found.
     */
    public void testIncrementalRefreshNotFound() {
        addParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_FOUND,
                null, null);
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_INCREMENTAL);
        verifyParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_FOUND,
                null, null);
    }

    /**
     * Verify that full refresh will resolve participant that is marked as not found.
     */
    public void testFullRefreshNotFound() {
        addParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_FOUND,
                null, null);
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that refresh take consideration of current contact_id when having multiple matches.
     */
    public void testFullRefreshResolvedMultiMatch1() {
        addParticipant("650-123-1233", 1, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null },
                { 2L, "Joe", "content://photo/joe", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that refresh take consideration of current contact_id when having multiple matches.
     */
    public void testFullRefreshResolvedMultiMatch2() {
        addParticipant("650-123-1233", 2, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null },
                { 2L, "Joe", "content://photo/joe", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", 2, "Joe", "content://photo/joe");
    }

    /**
     * Verify that refresh take first contact in case current contact_id no longer matches.
     */
    public void testFullRefreshResolvedMultiMatch3() {
        addParticipant("650-123-1233", 3, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {
                { 1L, "John", "content://photo/john", "650-123-1233", null, null, null },
                { 2L, "Joe", "content://photo/joe", "650-123-1233", null, null, null }
        });

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", 1, "John", "content://photo/john");
    }

    /**
     * Verify that refresh take first contact in case current contact_id no longer matches.
     */
    public void testFullRefreshResolvedBeforeButNotFoundNow() {
        addParticipant("650-123-1233", 3, "Joh", "content://photo/joh");
        addPhoneLookup("650-123-1233", new Object[][] {});

        ParticipantRefresh.refreshParticipants(ParticipantRefresh.REFRESH_MODE_FULL);
        verifyParticipant("650-123-1233", ParticipantData.PARTICIPANT_CONTACT_ID_NOT_FOUND,
                null, null);
    }
}