|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +package io.element.android.libraries.matrix.ui.model |
| 9 | + |
| 10 | +import com.google.common.truth.Truth.assertThat |
| 11 | +import io.element.android.libraries.designsystem.components.avatar.AvatarData |
| 12 | +import io.element.android.libraries.designsystem.components.avatar.AvatarSize |
| 13 | +import io.element.android.libraries.matrix.api.user.MatrixUser |
| 14 | +import io.element.android.libraries.matrix.test.A_USER_ID |
| 15 | +import org.junit.Test |
| 16 | + |
| 17 | +class MatrixUserExtensionsTest { |
| 18 | + @Test |
| 19 | + fun `getAvatarData should return the expected value`() { |
| 20 | + val matrixUser = MatrixUser( |
| 21 | + userId = A_USER_ID, |
| 22 | + displayName = "displayName", |
| 23 | + avatarUrl = "avatarUrl", |
| 24 | + ) |
| 25 | + val expected = AvatarData( |
| 26 | + id = A_USER_ID.value, |
| 27 | + name = "displayName", |
| 28 | + url = "avatarUrl", |
| 29 | + size = AvatarSize.UserHeader, |
| 30 | + ) |
| 31 | + assertThat(matrixUser.getAvatarData(AvatarSize.UserHeader)).isEqualTo(expected) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `getBestName should return the display name is available`() { |
| 36 | + val matrixUser = MatrixUser( |
| 37 | + userId = A_USER_ID, |
| 38 | + displayName = "displayName", |
| 39 | + ) |
| 40 | + assertThat(matrixUser.getBestName()).isEqualTo("displayName") |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `getBestName should return the id when name is not available`() { |
| 45 | + val matrixUser = MatrixUser( |
| 46 | + userId = A_USER_ID, |
| 47 | + displayName = null, |
| 48 | + ) |
| 49 | + assertThat(matrixUser.getBestName()).isEqualTo(A_USER_ID.value) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `getBestName should return the id when name is empty`() { |
| 54 | + val matrixUser = MatrixUser( |
| 55 | + userId = A_USER_ID, |
| 56 | + displayName = "", |
| 57 | + ) |
| 58 | + assertThat(matrixUser.getBestName()).isEqualTo(A_USER_ID.value) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `getFullName should return the display name is available and the userId`() { |
| 63 | + val matrixUser = MatrixUser( |
| 64 | + userId = A_USER_ID, |
| 65 | + displayName = "displayName", |
| 66 | + ) |
| 67 | + assertThat(matrixUser.getFullName()).isEqualTo("displayName (@alice:server.org)") |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `getBestName should return only the id when name is not available`() { |
| 72 | + val matrixUser = MatrixUser( |
| 73 | + userId = A_USER_ID, |
| 74 | + displayName = null, |
| 75 | + ) |
| 76 | + assertThat(matrixUser.getFullName()).isEqualTo(A_USER_ID.value) |
| 77 | + } |
| 78 | +} |
0 commit comments