Merge pull request #2221 from philipwhiuk/testAndFixCPLforSurrogatePairs
Test and fix CPL for surrogate pairs
This commit is contained in:
commit
2ac9de8c4e
2 changed files with 113 additions and 14 deletions
|
@ -22,6 +22,7 @@ import android.graphics.drawable.BitmapDrawable;
|
|||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.support.v4.util.LruCache;
|
||||
import android.text.TextUtils;
|
||||
import android.widget.ImageView;
|
||||
|
@ -74,6 +75,21 @@ public class ContactPictureLoader {
|
|||
0xffCC0000
|
||||
};
|
||||
|
||||
@VisibleForTesting
|
||||
protected static String calcUnknownContactLetter(Address address) {
|
||||
String letter = null;
|
||||
String personal = address.getPersonal();
|
||||
String str = (personal != null) ? personal : address.getAddress();
|
||||
|
||||
Matcher m = EXTRACT_LETTER_PATTERN.matcher(str);
|
||||
if (m.find()) {
|
||||
letter = m.group(0).toUpperCase(Locale.US);
|
||||
}
|
||||
|
||||
return (TextUtils.isEmpty(letter)) ?
|
||||
FALLBACK_CONTACT_LETTER : letter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
|
@ -160,20 +176,6 @@ public class ContactPictureLoader {
|
|||
return CONTACT_DUMMY_COLORS_ARGB[colorIndex];
|
||||
}
|
||||
|
||||
private String calcUnknownContactLetter(Address address) {
|
||||
String letter = null;
|
||||
String personal = address.getPersonal();
|
||||
String str = (personal != null) ? personal : address.getAddress();
|
||||
|
||||
Matcher m = EXTRACT_LETTER_PATTERN.matcher(str);
|
||||
if (m.find()) {
|
||||
letter = m.group(0).toUpperCase(Locale.US);
|
||||
}
|
||||
|
||||
return (TextUtils.isEmpty(letter)) ?
|
||||
FALLBACK_CONTACT_LETTER : letter.substring(0, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates a bitmap with a color and a capital letter for contacts without picture.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
package com.fsck.k9.activity.misc;
|
||||
|
||||
|
||||
import com.fsck.k9.K9RobolectricTestRunner;
|
||||
import com.fsck.k9.mail.Address;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(K9RobolectricTestRunner.class)
|
||||
public class ContactPictureLoaderTest {
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withNoNameUsesAddress() {
|
||||
Address address = new Address("<c@d.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("C", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withAsciiName() {
|
||||
Address address = new Address("abcd <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("A", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withLstroke() {
|
||||
Address address = new Address("Łatynka <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("Ł", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withChinese() {
|
||||
Address address = new Address("千里之行﹐始于足下 <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("千", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withCombinedGlyphs() {
|
||||
Address address = new Address("\u0061\u0300 <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("\u0041\u0300", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_withSurrogatePair() {
|
||||
Address address = new Address("\uD800\uDFB5 <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("\uD800\uDFB5", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_ignoresSpace() {
|
||||
Address address = new Address(" abcd <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("A", result);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_ignoresUsePunctuation() {
|
||||
Address address = new Address("-a <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("A", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calcUnknownContactLetter_ignoresMatchEmoji() {
|
||||
Address address = new Address("\uD83D\uDE00 <a@b.com>");
|
||||
|
||||
String result = ContactPictureLoader.calcUnknownContactLetter(address);
|
||||
|
||||
assertEquals("?", result);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue