Add unit test on ProtectedView and TimelineProtectionState

This commit is contained in:
Benoit Marty
2024-10-03 11:31:01 +02:00
committed by Benoit Marty
parent 4a481d0f84
commit babeaf58fe
2 changed files with 115 additions and 0 deletions

View File

@@ -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<ComponentActivity>()
@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 <R : TestRule> AndroidComposeTestRule<R, ComponentActivity>.setProtectedView(
hideContent: Boolean = false,
onShowClick: () -> Unit = { lambdaError() },
content: @Composable () -> Unit = {},
) {
setContent {
Box {
ProtectedView(
hideContent = hideContent,
onShowClick = onShowClick,
content = content
)
}
}
}

View File

@@ -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()
}
}