Don't load recipients with no email (fixes #2973) (#2985)

Don't load recipients with no email
This commit is contained in:
Philip 2018-01-08 15:25:46 +00:00 committed by Vincent Breitmoser
parent 111f7fc194
commit d0c8cc3b57
2 changed files with 54 additions and 3 deletions

View file

@ -343,8 +343,8 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
String lookupKey = cursor.getString(INDEX_LOOKUP_KEY); String lookupKey = cursor.getString(INDEX_LOOKUP_KEY);
// already exists? just skip then // already exists? just skip then
if (recipientMap.containsKey(email)) { if (email == null || recipientMap.containsKey(email)) {
// TODO merge? do something else? what do we do? // TODO We should probably merging contacts with the same email address
continue; continue;
} }
@ -373,7 +373,6 @@ public class RecipientLoader extends AsyncTaskLoader<List<Recipient>> {
break; break;
} }
} }
Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey); Recipient recipient = new Recipient(name, email, addressLabel, contactId, lookupKey);
if (recipient.isValidEmailAddress()) { if (recipient.isValidEmailAddress()) {

View file

@ -7,6 +7,8 @@ import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import com.fsck.k9.K9RobolectricTestRunner; import com.fsck.k9.K9RobolectricTestRunner;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
@ -15,7 +17,9 @@ import com.fsck.k9.view.RecipientSelectView.RecipientCryptoStatus;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.junit.Assert.*;
import static org.mockito.AdditionalMatchers.aryEq; import static org.mockito.AdditionalMatchers.aryEq;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@ -28,10 +32,22 @@ import static org.mockito.Mockito.when;
@RunWith(K9RobolectricTestRunner.class) @RunWith(K9RobolectricTestRunner.class)
public class RecipientLoaderTest { public class RecipientLoaderTest {
static final String CRYPTO_PROVIDER = "cryptoProvider"; 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_ADDRESSES = { "address", "uid_address" };
static final String[] PROJECTION_CRYPTO_STATUS = { "address", "uid_key_status", "autocrypt_key_status" }; static final String[] PROJECTION_CRYPTO_STATUS = { "address", "uid_key_status", "autocrypt_key_status" };
static final Address CONTACT_ADDRESS_1 = Address.parse("Contact Name <address@example.org>")[0]; static final Address CONTACT_ADDRESS_1 = Address.parse("Contact Name <address@example.org>")[0];
static final Address CONTACT_ADDRESS_2 = Address.parse("Other Contact Name <address_two@example.org>")[0]; static final Address CONTACT_ADDRESS_2 = Address.parse("Other Contact Name <address_two@example.org>")[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"; static final String QUERYSTRING = "querystring";
@ -44,6 +60,8 @@ public class RecipientLoaderTest {
context = mock(Context.class); context = mock(Context.class);
contentResolver = mock(ContentResolver.class); contentResolver = mock(ContentResolver.class);
when(context.getApplicationContext()).thenReturn(RuntimeEnvironment.application);
when(context.getContentResolver()).thenReturn(contentResolver); when(context.getContentResolver()).thenReturn(contentResolver);
} }
@ -141,4 +159,38 @@ public class RecipientLoaderTest {
any(String.class))).thenReturn(cursorCryptoStatus); 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<Recipient> 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<Recipient> recipients = recipientLoader.loadInBackground();
assertEquals(0, recipients.size());
}
} }