Add support for setting an optional last name in profiles.

This commit is contained in:
Alex Hart
2019-12-20 16:12:22 -04:00
committed by Greyson Parrelli
parent f2b9bf0b8c
commit 3907ec8b51
57 changed files with 1641 additions and 1847 deletions

View File

@@ -0,0 +1,164 @@
package org.thoughtcrime.securesms.profiles;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
public final class ProfileNameTest {
@Test
public void givenEmpty_thenIExpectSaneDefaults() {
// GIVEN
ProfileName profileName = ProfileName.EMPTY;
// THEN
assertNotNull("ProfileName should be non-null", profileName);
assertFalse("ProfileName should not be CJKV", profileName.isProfileNameCJKV());
assertEquals("ProfileName should have empty given name", "", profileName.getGivenName());
assertEquals("ProfileName should have empty family name", "", profileName.getFamilyName());
}
@Test
public void givenNullProfileName_whenIFromDataString_thenIExpectSaneDefaults() {
// GIVEN
ProfileName profileName = ProfileName.fromSerialized(null);
// THEN
assertSame(ProfileName.EMPTY, profileName);
}
@Test
public void givenProfileNameWithGivenNameOnly_whenIFromDataString_thenIExpectValidProfileName() {
// GIVEN
String profileName = "Given";
// WHEN
ProfileName name = ProfileName.fromSerialized(profileName);
// THEN
assertNotNull("ProfileName should be non-null", name);
assertFalse("ProfileName should not be CJKV", name.isProfileNameCJKV());
assertEquals("ProfileName should have expected given name", profileName, name.getGivenName());
assertEquals("ProfileName should have empty family name", "", name.getFamilyName());
}
@Test
public void givenProfileNameWithEnglishGivenNameAndEnglishFamilyName_whenIFromDataString_thenIExpectValidProfileName() {
// GIVEN
String profileName = "Given\0Family";
// WHEN
ProfileName name = ProfileName.fromSerialized(profileName);
// THEN
assertNotNull("ProfileName should be non-null", name);
assertFalse("ProfileName should not be CJKV", name.isProfileNameCJKV());
assertEquals("ProfileName should have expected given name", "Given", name.getGivenName());
assertEquals("ProfileName should have expected family name", "Family", name.getFamilyName());
}
@Test
public void givenProfileNameWithEnglishGivenNameAndCJKVFamilyName_whenIFromDataString_thenIExpectNonCJKVProfileName() {
// GIVEN
String profileName = "Given\0码";
// WHEN
ProfileName name = ProfileName.fromSerialized(profileName);
// THEN
assertNotNull("ProfileName should be non-null", name);
assertFalse("ProfileName should not be CJKV", name.isProfileNameCJKV());
assertEquals("ProfileName should have expected given name", "Given", name.getGivenName());
assertEquals("ProfileName should have expected family name", "", name.getFamilyName());
}
@Test
public void givenProfileNameWithCJKVGivenNameAndCJKVFamilyName_whenIFromDataString_thenIExpectNonCJKVProfileName() {
// GIVEN
String profileName = "\0码";
// WHEN
ProfileName name = ProfileName.fromSerialized(profileName);
// THEN
assertNotNull("ProfileName should be non-null", name);
assertTrue("ProfileName should be CJKV", name.isProfileNameCJKV());
assertEquals("ProfileName should have expected given name", "", name.getGivenName());
assertEquals("ProfileName should have expected family name", "", name.getFamilyName());
}
@Test
public void givenProfileNameWithCJKVGivenNameAndEnglishFamilyName_whenIFromDataString_thenIExpectNonCJKVProfileName() {
// GIVEN
String profileName = "\0Family";
// WHEN
ProfileName name = ProfileName.fromSerialized(profileName);
// THEN
assertNotNull("ProfileName should be non-null", name);
assertFalse("ProfileName should not be CJKV", name.isProfileNameCJKV());
assertEquals("ProfileName should have expected given name", "", name.getGivenName());
assertEquals("ProfileName should have expected family name", "Family", name.getFamilyName());
}
@Test
public void givenProfileNameWithEmptyInputs_whenIToDataString_thenIExpectAnEmptyString() {
// GIVEN
ProfileName name = ProfileName.fromParts("", "");
// WHEN
String data = name.serialize();
// THEN
assertEquals("Blank String should be returned (For back compat)", "", data);
}
@Test
public void givenProfileNameWithEmptyGivenName_whenIToDataString_thenIExpectAnEmptyString() {
// GIVEN
ProfileName name = ProfileName.fromParts("", "Family");
// WHEN
String data = name.serialize();
// THEN
assertEquals("Blank String should be returned (For back compat)", "", data);
}
@Test
public void givenProfileNameWithGivenName_whenIToDataString_thenIExpectValidProfileName() {
// GIVEN
ProfileName name = ProfileName.fromParts("Given", "");
// WHEN
String data = name.serialize();
// THEN
assertEquals(data, "Given\0");
}
@Test
public void givenProfileNameWithGivenNameAndFamilyName_whenIToDataString_thenIExpectValidProfileName() {
// GIVEN
ProfileName name = ProfileName.fromParts("Given", "Family");
// WHEN
String data = name.serialize();
// THEN
assertEquals(data, "Given\0Family");
}
@Test
public void fromParts_with_long_name_parts() {
ProfileName name = ProfileName.fromParts("GivenSomeVeryLongNameSomeVeryLongName", "FamilySomeVeryLongNameSomeVeryLongName");
assertEquals("GivenSomeVeryLongNameSomeV", name.getGivenName());
assertEquals("FamilySomeVeryLongNameSome", name.getFamilyName());
}
}

View File

@@ -8,6 +8,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.thoughtcrime.securesms.profiles.ProfileName;
import org.whispersystems.libsignal.util.guava.Optional;
import static android.provider.ContactsContract.Intents.Insert.NAME;
@@ -24,7 +25,7 @@ public final class RecipientExporterTest {
@Test
public void asAddContactIntent_with_phone_number() {
Recipient recipient = givenPhoneRecipient("Alice", "+1555123456");
Recipient recipient = givenPhoneRecipient(ProfileName.fromParts("Alice", null), "+1555123456");
Intent intent = RecipientExporter.export(recipient).asAddContactIntent();
@@ -37,7 +38,7 @@ public final class RecipientExporterTest {
@Test
public void asAddContactIntent_with_email() {
Recipient recipient = givenEmailRecipient("Bob", "bob@signal.org");
Recipient recipient = givenEmailRecipient(ProfileName.fromParts("Bob", null), "bob@signal.org");
Intent intent = RecipientExporter.export(recipient).asAddContactIntent();
@@ -48,7 +49,7 @@ public final class RecipientExporterTest {
assertNull(intent.getStringExtra(PHONE));
}
private Recipient givenPhoneRecipient(String profileName, String phone) {
private Recipient givenPhoneRecipient(ProfileName profileName, String phone) {
Recipient recipient = mock(Recipient.class);
when(recipient.getProfileName()).thenReturn(profileName);
@@ -59,7 +60,7 @@ public final class RecipientExporterTest {
return recipient;
}
private Recipient givenEmailRecipient(String profileName, String email) {
private Recipient givenEmailRecipient(ProfileName profileName, String email) {
Recipient recipient = mock(Recipient.class);
when(recipient.getProfileName()).thenReturn(profileName);

View File

@@ -0,0 +1,65 @@
package org.thoughtcrime.securesms.util.cjkv;
import android.app.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.*;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE, application = Application.class)
public class CJKVUtilTest {
private static final String CJKV_CHARS = "统码";
private static final String NON_CJKV_CHAR = "a";
private static final String MIXED_CHARS = CJKV_CHARS + NON_CJKV_CHAR;
@Test
public void givenAllCJKVChars_whenIsCJKV_thenIExpectTrue() {
// WHEN
boolean result = CJKVUtil.isCJKV(CJKV_CHARS);
//THEN
assertTrue(result);
}
@Test
public void givenNoCJKVChars_whenIsCJKV_thenIExpectFalse() {
// WHEN
boolean result = CJKVUtil.isCJKV(NON_CJKV_CHAR);
// THEN
assertFalse(result);
}
@Test
public void givenOneNonCJKVChar_whenIsCJKV_thenIExpectFalse() {
// WHEN
boolean result = CJKVUtil.isCJKV(MIXED_CHARS);
// THEN
assertFalse(result);
}
@Test
public void givenAnEmptyString_whenIsCJKV_thenIExpectTrue() {
// WHEN
boolean result = CJKVUtil.isCJKV("");
// THEN
assertTrue(result);
}
@Test
public void givenNull_whenIsCJKV_thenIExpectTrue() {
// WHEN
boolean result = CJKVUtil.isCJKV(null);
// THEN
assertTrue(result);
}
}