Skip to content

Commit

Permalink
Merge pull request #1037 from DataDog/mconstantin/rumm-2494/wireframe…
Browse files Browse the repository at this point in the history
…-mapper-use-reference-address-as-view-id

RUMM-2494 Use View hashcode as unique identifier for mapped Wireframe
  • Loading branch information
mariusc83 authored Sep 12, 2022
2 parents e9cae1d + f860fd5 commit 7a7508e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ internal abstract class BaseWireframeMapper<T : View, S : MobileSegment.Wirefram
WireframeMapper<T, S> {

protected fun resolveViewId(view: View): Long {
return if (view.id != View.NO_ID) view.id.toLong() else view.hashCode().toLong()
// we will use the System.identityHashcode in here which always returns the default
// hashcode value whether or not a child class overrides this.
return System.identityHashCode(view).toLong()
}

protected fun colorAndAlphaAsStringHexa(color: Int, alphaAsHexa: Long): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ internal fun Forge.aMockViewWithChildren(
level,
maxLevel
)
whenever(mockViewGroup.id).thenReturn(currentLevel * 10 + i)
whenever(mockViewGroup.getChildAt(i)).thenReturn(mockChildGroup)
}
return mockViewGroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal abstract class BaseWireframeMapperTest {
val x = coordinates[0].densityNormalized(fakePixelDensity).toLong()
val y = coordinates[1].densityNormalized(fakePixelDensity).toLong()
return MobileSegment.Wireframe.ShapeWireframe(
id.toLong(),
System.identityHashCode(this).toLong(),
x = x,
y = y,
width = width.toLong().densityNormalized(fakePixelDensity),
Expand All @@ -44,7 +44,7 @@ internal abstract class BaseWireframeMapperTest {
val x = coordinates[0].densityNormalized(fakePixelDensity).toLong()
val y = coordinates[1].densityNormalized(fakePixelDensity).toLong()
return MobileSegment.Wireframe.TextWireframe(
id.toLong(),
System.identityHashCode(this).toLong(),
x = x,
y = y,
text = resolveTextValue(this),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,10 @@ internal class ViewWireframeMapperTest : BaseWireframeMapperTest() {
}

@Test
fun `M default to View hashcode W map() { View and id = NO_ID}`(forge: Forge) {
// Given
val mockView: View = forge.aMockView<View>().apply {
whenever(id).thenReturn(View.NO_ID)
// we cannot mock the hashcode method as it is final
}

// When
val shapeWireframe = testedWireframeMapper.map(mockView, fakePixelDensity)

// Then
val expectedWireframe = mockView.toShapeWireframe().copy(id = mockView.hashCode().toLong())
assertThat(shapeWireframe).isEqualTo(expectedWireframe)
}

@Test
fun `M use the View hashcode for Wireframe id W produce() { id is not valid }`(forge: Forge) {
fun `M use the View hashcode for Wireframe id W produce()`(forge: Forge) {
// Given
val mockViews = forge.aList(size = forge.anInt(min = 10, max = 20)) {
val mockRoot = aMockView<View>()
whenever(mockRoot.id).thenReturn(View.NO_ID)
mockRoot
aMockView<View>()
}

// When
Expand Down

0 comments on commit 7a7508e

Please sign in to comment.