TextDrawable: handle spaces at start/end of displayNames

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-09-23 12:10:33 +02:00
parent 222f9758d5
commit 43bf5b982a
No known key found for this signature in database
GPG key ID: 2585783189A62105
2 changed files with 38 additions and 13 deletions

View file

@ -23,7 +23,6 @@
package com.owncloud.android.ui;
import android.accounts.Account;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
@ -133,12 +132,12 @@ public class TextDrawable extends Drawable {
}
@VisibleForTesting
public static String extractCharsFromDisplayName(@NonNull String displayName) {
if (displayName.isEmpty()) {
public static String extractCharsFromDisplayName(@NonNull final String displayName) {
final String trimmed = displayName.trim();
if (trimmed.isEmpty()) {
return "";
}
String[] nameParts = displayName.split("\\s+");
String[] nameParts = trimmed.split("\\s+");
StringBuilder firstTwoLetters = new StringBuilder();
for (int i = 0; i < Math.min(2, nameParts.length); i++) {

View file

@ -23,17 +23,43 @@ package com.owncloud.android.ui
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
@RunWith(Parameterized::class)
class TextDrawableTest {
@Parameterized.Parameter(0)
lateinit var expected: String
@Parameterized.Parameter(1)
lateinit var input: String
@Test
fun twoDigitAvatars() {
assertEquals("A", TextDrawable.extractCharsFromDisplayName("Admin"))
assertEquals("TS", TextDrawable.extractCharsFromDisplayName("Test Server Admin"))
assertEquals("", TextDrawable.extractCharsFromDisplayName(""))
assertEquals("CP", TextDrawable.extractCharsFromDisplayName("Cormier Paulette"))
assertEquals("WB", TextDrawable.extractCharsFromDisplayName("winston brent"))
assertEquals("BJ", TextDrawable.extractCharsFromDisplayName("Baker James Lorena"))
assertEquals("BJ", TextDrawable.extractCharsFromDisplayName("Baker James Lorena"))
assertEquals("E", TextDrawable.extractCharsFromDisplayName("email@nextcloud.localhost"))
assertEquals(
"Avatar chars from displayname not correct",
expected,
TextDrawable.extractCharsFromDisplayName(input)
)
}
companion object {
@Parameterized.Parameters(name = "{1}")
@JvmStatic
fun data(): Iterable<Array<String>> = listOf(
arrayOf("A", "Admin"),
arrayOf("TS", "Test Server Admin"),
arrayOf("", ""),
arrayOf("CP", "Cormier Paulette"),
arrayOf("WB", "winston brent"),
arrayOf("BJ", "Baker James Lorena"),
arrayOf("BJ", "Baker James Lorena"),
arrayOf("E", "email@nextcloud.localhost"),
arrayOf("SA", " Spaces At Start"),
arrayOf("SA", "Spaces At End "),
arrayOf("SA", " Spaces At Start And End "),
arrayOf("", " ")
)
}
}