From d0c8cc3b57a26bfc9c722075bdf041ab7c4a649c Mon Sep 17 00:00:00 2001 From: Philip Date: Mon, 8 Jan 2018 15:25:46 +0000 Subject: [PATCH] Don't load recipients with no email (fixes #2973) (#2985) Don't load recipients with no email --- .../k9/activity/compose/RecipientLoader.java | 5 +- .../activity/compose/RecipientLoaderTest.java | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java index 21d02c2c7..c679a8bc7 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/compose/RecipientLoader.java @@ -343,8 +343,8 @@ public class RecipientLoader extends AsyncTaskLoader> { String lookupKey = cursor.getString(INDEX_LOOKUP_KEY); // already exists? just skip then - if (recipientMap.containsKey(email)) { - // TODO merge? do something else? what do we do? + if (email == null || recipientMap.containsKey(email)) { + // TODO We should probably merging contacts with the same email address continue; } @@ -373,7 +373,6 @@ public class RecipientLoader extends AsyncTaskLoader> { break; } } - Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey); if (recipient.isValidEmailAddress()) { diff --git a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java index 86c61c566..a4aed64b7 100644 --- a/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java +++ b/k9mail/src/test/java/com/fsck/k9/activity/compose/RecipientLoaderTest.java @@ -7,6 +7,8 @@ import android.content.ContentResolver; import android.content.Context; import android.database.MatrixCursor; import android.net.Uri; +import android.provider.ContactsContract; +import android.provider.ContactsContract.CommonDataKinds.Email; import com.fsck.k9.K9RobolectricTestRunner; import com.fsck.k9.mail.Address; @@ -15,7 +17,9 @@ import com.fsck.k9.view.RecipientSelectView.RecipientCryptoStatus; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import static android.provider.ContactsContract.CommonDataKinds.Email.TYPE_HOME; import static org.junit.Assert.*; import static org.mockito.AdditionalMatchers.aryEq; import static org.mockito.Matchers.any; @@ -28,10 +32,22 @@ import static org.mockito.Mockito.when; @RunWith(K9RobolectricTestRunner.class) public class RecipientLoaderTest { static final String CRYPTO_PROVIDER = "cryptoProvider"; + static final String[] PROJECTION = { + ContactsContract.CommonDataKinds.Email._ID, + ContactsContract.Contacts.DISPLAY_NAME_PRIMARY, + ContactsContract.Contacts.LOOKUP_KEY, + ContactsContract.CommonDataKinds.Email.ADDRESS, + ContactsContract.CommonDataKinds.Email.TYPE, + ContactsContract.CommonDataKinds.Email.LABEL, + ContactsContract.CommonDataKinds.Email.CONTACT_ID, + ContactsContract.Contacts.PHOTO_THUMBNAIL_URI + }; static final String[] PROJECTION_CRYPTO_ADDRESSES = { "address", "uid_address" }; static final String[] PROJECTION_CRYPTO_STATUS = { "address", "uid_key_status", "autocrypt_key_status" }; static final Address CONTACT_ADDRESS_1 = Address.parse("Contact Name ")[0]; static final Address CONTACT_ADDRESS_2 = Address.parse("Other Contact Name ")[0]; + static final String[] CONTACT_1 = new String[] {"0", "Bob", "bob", "bob@host.com", ""+TYPE_HOME, null, "1", null}; + static final String[] CONTACT_NO_EMAIL = new String[] {"0", "Bob", "bob", null, ""+TYPE_HOME, null, "1", null}; static final String QUERYSTRING = "querystring"; @@ -44,6 +60,8 @@ public class RecipientLoaderTest { context = mock(Context.class); contentResolver = mock(ContentResolver.class); + when(context.getApplicationContext()).thenReturn(RuntimeEnvironment.application); + when(context.getContentResolver()).thenReturn(contentResolver); } @@ -141,4 +159,38 @@ public class RecipientLoaderTest { any(String.class))).thenReturn(cursorCryptoStatus); } + private void setupContactProvider(String queriedAddress, String[]... contacts) { + MatrixCursor cursor = new MatrixCursor(PROJECTION); + for (String[] contact : contacts) { + cursor.addRow(contact); + } + when(contentResolver + .query(eq(Email.CONTENT_URI), + aryEq(PROJECTION), + any(String.class), + aryEq(new String[] { queriedAddress, queriedAddress }), + any(String.class))).thenReturn(cursor); + } + + @Test + public void queryContactProvider() throws Exception { + RecipientLoader recipientLoader = new RecipientLoader(context, CRYPTO_PROVIDER, QUERYSTRING); + setupContactProvider("%" + QUERYSTRING + "%", CONTACT_1); + + List recipients = recipientLoader.loadInBackground(); + + assertEquals(1, recipients.size()); + assertEquals("bob@host.com", recipients.get(0).address.getAddress()); + assertEquals(RecipientCryptoStatus.UNAVAILABLE, recipients.get(0).getCryptoStatus()); + } + + @Test + public void queryContactProvider_ignoresRecipientWithNoEmail() throws Exception { + RecipientLoader recipientLoader = new RecipientLoader(context, CRYPTO_PROVIDER, QUERYSTRING); + setupContactProvider("%" + QUERYSTRING + "%", CONTACT_NO_EMAIL); + + List recipients = recipientLoader.loadInBackground(); + + assertEquals(0, recipients.size()); + } } \ No newline at end of file