diff --git a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/ProtectedViewTest.kt b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/ProtectedViewTest.kt new file mode 100644 index 0000000000..a1e876f328 --- /dev/null +++ b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/ProtectedViewTest.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.features.messages.impl.timeline.protection + +import androidx.activity.ComponentActivity +import androidx.compose.foundation.layout.Box +import androidx.compose.runtime.Composable +import androidx.compose.ui.test.junit4.AndroidComposeTestRule +import androidx.compose.ui.test.junit4.createAndroidComposeRule +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.test.ext.junit.runners.AndroidJUnit4 +import io.element.android.libraries.designsystem.theme.components.Text +import io.element.android.tests.testutils.ensureCalledOnce +import io.element.android.tests.testutils.lambda.lambdaError +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TestRule +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class ProtectedViewTest { + @get:Rule val rule = createAndroidComposeRule() + + @Test + fun `when hideContent is false, the content is rendered`() { + rule.setProtectedView( + hideContent = false, + content = { + Text("Hello") + } + ) + rule.onNodeWithText("Hello").assertExists() + } + + @Test + fun `when hideContent is true, the content is not rendered, and user can reveal it`() { + ensureCalledOnce { + rule.setProtectedView( + hideContent = true, + onShowClick = it, + content = { + Text("Hello") + } + ) + rule.onNodeWithText("Hello").assertDoesNotExist() + rule.onNodeWithText("Show").performClick() + } + } +} + +private fun AndroidComposeTestRule.setProtectedView( + hideContent: Boolean = false, + onShowClick: () -> Unit = { lambdaError() }, + content: @Composable () -> Unit = {}, +) { + setContent { + Box { + ProtectedView( + hideContent = hideContent, + onShowClick = onShowClick, + content = content + ) + } + } +} diff --git a/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/TimelineProtectionStateTest.kt b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/TimelineProtectionStateTest.kt new file mode 100644 index 0000000000..0a05636df1 --- /dev/null +++ b/features/messages/impl/src/test/kotlin/io/element/android/features/messages/impl/timeline/protection/TimelineProtectionStateTest.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2024 New Vector Ltd. + * + * SPDX-License-Identifier: AGPL-3.0-only + * Please see LICENSE in the repository root for full details. + */ + +package io.element.android.features.messages.impl.timeline.protection + +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.matrix.test.AN_EVENT_ID +import io.element.android.libraries.matrix.test.AN_EVENT_ID_2 +import kotlinx.collections.immutable.persistentSetOf +import org.junit.Test + +class TimelineProtectionStateTest { + @Test + fun `when protectionState is RenderAll, hideContent always return null`() { + val sut = aTimelineProtectionState( + protectionState = ProtectionState.RenderAll + ) + assertThat(sut.hideContent(null)).isFalse() + assertThat(sut.hideContent(AN_EVENT_ID)).isFalse() + } + + @Test + fun `when protectionState is RenderOnly with empty set, hideContent always return true`() { + val sut = aTimelineProtectionState( + protectionState = ProtectionState.RenderOnly(persistentSetOf()) + ) + assertThat(sut.hideContent(null)).isTrue() + assertThat(sut.hideContent(AN_EVENT_ID)).isTrue() + } + + @Test + fun `when protectionState is RenderOnly with an Event, hideContent always return true`() { + val sut = aTimelineProtectionState( + protectionState = ProtectionState.RenderOnly(persistentSetOf(AN_EVENT_ID)) + ) + assertThat(sut.hideContent(null)).isTrue() + assertThat(sut.hideContent(AN_EVENT_ID)).isFalse() + assertThat(sut.hideContent(AN_EVENT_ID_2)).isTrue() + } +}