Skip to content

Commit

Permalink
Merge pull request #2042 from DataDog/xgouchet/flaky_tests
Browse files Browse the repository at this point in the history
Fix SR flaky test
  • Loading branch information
xgouchet authored May 21, 2024
2 parents c03bdaf + 9887c26 commit 9117094
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 5 deletions.
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
}
}

0 comments on commit 9117094

Please sign in to comment.