Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SR flaky test #2042

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.datadog.android.sessionreplay.internal.recorder.mapper
import android.content.res.ColorStateList
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.os.Build
import android.widget.SeekBar
import com.datadog.android.sessionreplay.SessionReplayPrivacy
import com.datadog.android.sessionreplay.forge.ForgeConfigurator
Expand All @@ -12,6 +13,8 @@ import com.datadog.android.sessionreplay.model.MobileSegment
import com.datadog.android.sessionreplay.recorder.mapper.AbstractWireframeMapperTest
import com.datadog.android.sessionreplay.utils.OPAQUE_ALPHA_VALUE
import com.datadog.android.sessionreplay.utils.PARTIALLY_OPAQUE_ALPHA_VALUE
import com.datadog.tools.unit.annotations.TestTargetApi
import com.datadog.tools.unit.extensions.ApiLevelExtension
import fr.xgouchet.elmyr.annotation.FloatForgery
import fr.xgouchet.elmyr.annotation.IntForgery
import fr.xgouchet.elmyr.annotation.LongForgery
Expand All @@ -35,7 +38,8 @@ import kotlin.math.max

@Extensions(
ExtendWith(MockitoExtension::class),
ExtendWith(ForgeExtension::class)
ExtendWith(ForgeExtension::class),
ExtendWith(ApiLevelExtension::class)
)
@MockitoSettings(strictness = Strictness.LENIENT)
@ForgeConfiguration(value = ForgeConfigurator::class)
Expand Down Expand Up @@ -109,11 +113,146 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
)
}

// region Android O+ (allows setting a min progress value)

@Test
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return partial wireframes W map {invalid thumb id, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.THUMB_KEY_NAME, null)

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(2)
assertThatBoundsAreCloseEnough(wireframes[0], expectedNonActiveTrackWireframe)
assertThatBoundsAreCloseEnough(wireframes[1], expectedActiveTrackWireframe)
}

@Test
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return partial wireframes W map {invalid active track id, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.ACTIVE_TRACK_KEY_NAME, null)

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(2)
assertThatBoundsAreCloseEnough(wireframes[0], expectedNonActiveTrackWireframe)
assertThatBoundsAreCloseEnough(wireframes[1], expectedThumbWireframe)
}

@Test
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return partial wireframes W map {invalid non active track id, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.NON_ACTIVE_TRACK_KEY_NAME, null)

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(2)
assertThatBoundsAreCloseEnough(wireframes[0], expectedActiveTrackWireframe)
assertThatBoundsAreCloseEnough(wireframes[1], expectedThumbWireframe)
}

@Test
fun `M return partial wireframes W map { invalid thumb id`() {
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return wireframes W map {privacy=ALLOW, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(3)
assertThatBoundsAreCloseEnough(wireframes[0], expectedNonActiveTrackWireframe)
assertThatBoundsAreCloseEnough(wireframes[1], expectedActiveTrackWireframe)
assertThatBoundsAreCloseEnough(wireframes[2], expectedThumbWireframe)
}

@Test
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return wireframes W map {privacy=MASK, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.MASK)
prepareMockSeekBar()

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(1)
assertThatBoundsAreCloseEnough(wireframes[0], expectedNonActiveTrackWireframe)
}

@Test
@TestTargetApi(Build.VERSION_CODES.O)
fun `M return wireframes W map {privacy=MASK_USER_INPUT, Android O+}`() {
// Given
withPrivacy(SessionReplayPrivacy.MASK_USER_INPUT)
prepareMockSeekBar()

// When
val wireframes = testedWireframeMapper.map(
mockMappedView,
fakeMappingContext,
mockAsyncJobStatusCallback,
mockInternalLogger
)

// Then
assertThat(wireframes).hasSize(1)
assertThat(wireframes[0]).isEqualTo(expectedNonActiveTrackWireframe)
}

// endregion

// region Android O+ (allows setting a min progress value)

@Test
fun `M return partial wireframes W map { invalid thumb id }`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.THUMB_KEY_NAME, null)

// When
Expand All @@ -131,8 +270,9 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
}

@Test
fun `M return partial wireframes W map { invalid track id`() {
fun `M return partial wireframes W map { invalid active track id }`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.ACTIVE_TRACK_KEY_NAME, null)
Expand All @@ -152,8 +292,9 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
}

@Test
fun `M return partial wireframes W map { invalid non active track id`() {
fun `M return partial wireframes W map { invalid non active track id }`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()
mockChildUniqueIdentifier(SeekBarWireframeMapper.NON_ACTIVE_TRACK_KEY_NAME, null)
Expand All @@ -175,6 +316,7 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
@Test
fun `M return wireframes W map {privacy=ALLOW}`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.ALLOW)
prepareMockSeekBar()

Expand All @@ -196,6 +338,7 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
@Test
fun `M return wireframes W map {privacy=MASK}`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.MASK)
prepareMockSeekBar()

Expand All @@ -215,6 +358,7 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
@Test
fun `M return wireframes W map {privacy=MASK_USER_INPUT}`() {
// Given
fakeMinValue = 0
withPrivacy(SessionReplayPrivacy.MASK_USER_INPUT)
prepareMockSeekBar()

Expand All @@ -231,6 +375,8 @@ internal class SeekBarWireframeMapperTest : AbstractWireframeMapperTest<SeekBar,
assertThat(wireframes[0]).isEqualTo(expectedNonActiveTrackWireframe)
}

// endregion

// region Internal

private fun prepareMockSeekBar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ internal abstract class AbstractWireframeMapperTest<V : View, WM : WireframeMapp
val actualCornerRadius = actual.shapeStyle?.cornerRadius?.toLong()
val expectedCornerRadius = expected.shapeStyle?.cornerRadius?.toLong()
if (actualCornerRadius != null && expectedCornerRadius != null) {
assertThat(actualCornerRadius).isCloseTo(expectedCornerRadius, withinPercentage(5))
assertThat(actualCornerRadius).isCloseTo(expectedCornerRadius, withinPercentage(BOUNDS_THRESHOLD_PERCENT))
}
}

// endregion

companion object {
const val BOUNDS_THRESHOLD_PERCENT = 12.5
const val BOUNDS_THRESHOLD_OFFSET = 5L
}
}