|
| 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.features.messages.impl.crypto.identity |
| 9 | + |
| 10 | +import androidx.activity.ComponentActivity |
| 11 | +import androidx.compose.ui.test.junit4.AndroidComposeTestRule |
| 12 | +import androidx.compose.ui.test.junit4.createAndroidComposeRule |
| 13 | +import androidx.compose.ui.test.onNodeWithText |
| 14 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 15 | +import io.element.android.libraries.designsystem.components.avatar.anAvatarData |
| 16 | +import io.element.android.libraries.matrix.api.core.UserId |
| 17 | +import io.element.android.libraries.matrix.api.encryption.identity.IdentityState |
| 18 | +import io.element.android.libraries.ui.strings.CommonStrings |
| 19 | +import io.element.android.tests.testutils.EventsRecorder |
| 20 | +import io.element.android.tests.testutils.clickOn |
| 21 | +import org.junit.Rule |
| 22 | +import org.junit.Test |
| 23 | +import org.junit.rules.TestRule |
| 24 | +import org.junit.runner.RunWith |
| 25 | + |
| 26 | +@RunWith(AndroidJUnit4::class) |
| 27 | +class IdentityChangeStateViewTest { |
| 28 | + @get:Rule val rule = createAndroidComposeRule<ComponentActivity>() |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `show and resolve pin violation`() { |
| 32 | + val eventsRecorder = EventsRecorder<IdentityChangeEvent>() |
| 33 | + rule.setIdentityChangeStateView( |
| 34 | + state = anIdentityChangeState( |
| 35 | + listOf( |
| 36 | + RoomMemberIdentityStateChange( |
| 37 | + identityRoomMember = IdentityRoomMember(UserId("@alice:localhost"), "Alice", anAvatarData()), |
| 38 | + identityState = IdentityState.PinViolation |
| 39 | + ) |
| 40 | + ), |
| 41 | + eventsRecorder |
| 42 | + ), |
| 43 | + ) |
| 44 | + |
| 45 | + rule.onNodeWithText("identity appears to have changed", substring = true).assertExists("should display pin violation warning") |
| 46 | + rule.onNodeWithText("@alice:localhost", substring = true).assertExists("should display user mxid") |
| 47 | + rule.onNodeWithText("Alice", substring = true).assertExists("should display user displayname") |
| 48 | + |
| 49 | + rule.clickOn(res = CommonStrings.action_ok) |
| 50 | + eventsRecorder.assertSingle(IdentityChangeEvent.PinViolation(UserId("@alice:localhost"))) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `show and resolve verification violation`() { |
| 55 | + val eventsRecorder = EventsRecorder<IdentityChangeEvent>() |
| 56 | + rule.setIdentityChangeStateView( |
| 57 | + state = anIdentityChangeState( |
| 58 | + listOf( |
| 59 | + RoomMemberIdentityStateChange( |
| 60 | + identityRoomMember = IdentityRoomMember(UserId("@alice:localhost"), "Alice", anAvatarData()), |
| 61 | + identityState = IdentityState.VerificationViolation |
| 62 | + ) |
| 63 | + ), |
| 64 | + eventsRecorder |
| 65 | + ), |
| 66 | + ) |
| 67 | + |
| 68 | + rule.onNodeWithText("verified identity has changed", substring = true).assertExists("should display verification violation warning") |
| 69 | + rule.onNodeWithText("@alice:localhost", substring = true).assertExists("should display user mxid") |
| 70 | + rule.onNodeWithText("Alice", substring = true).assertExists("should display user displayname") |
| 71 | + |
| 72 | + rule.clickOn(res = CommonStrings.crypto_identity_change_withdraw_verification_action) |
| 73 | + eventsRecorder.assertSingle(IdentityChangeEvent.VerificationViolation(UserId("@alice:localhost"))) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `Should not show any banner if no violations`() { |
| 78 | + rule.setIdentityChangeStateView( |
| 79 | + state = anIdentityChangeState( |
| 80 | + listOf( |
| 81 | + RoomMemberIdentityStateChange( |
| 82 | + identityRoomMember = IdentityRoomMember(UserId("@alice:localhost"), "Alice", anAvatarData()), |
| 83 | + identityState = IdentityState.Verified |
| 84 | + ), |
| 85 | + RoomMemberIdentityStateChange( |
| 86 | + identityRoomMember = IdentityRoomMember(UserId("@bob:localhost"), "Bob", anAvatarData()), |
| 87 | + identityState = IdentityState.Pinned |
| 88 | + ) |
| 89 | + ), |
| 90 | + ), |
| 91 | + ) |
| 92 | + |
| 93 | + rule.onNodeWithText("identity appears to have changed", substring = true).assertDoesNotExist() |
| 94 | + rule.onNodeWithText("verified identity has changed", substring = true).assertDoesNotExist() |
| 95 | + } |
| 96 | + |
| 97 | + private fun <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setIdentityChangeStateView( |
| 98 | + state: IdentityChangeState, |
| 99 | + ) { |
| 100 | + setContent { |
| 101 | + IdentityChangeStateView( |
| 102 | + state = state, |
| 103 | + onLinkClick = {}, |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments