-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2296 from DataDog/yl/compose/semantics-button-role
RUM-6194: Add Semantics Mapper for Button role
- Loading branch information
Showing
8 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...dog/android/sessionreplay/compose/internal/mappers/semantics/ButtonSemanticsNodeMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.compose.internal.mappers.semantics | ||
|
||
import androidx.compose.ui.semantics.SemanticsNode | ||
import androidx.compose.ui.unit.Density | ||
import com.datadog.android.sessionreplay.compose.internal.data.ComposeWireframe | ||
import com.datadog.android.sessionreplay.compose.internal.data.UiContext | ||
import com.datadog.android.sessionreplay.compose.internal.utils.SemanticsUtils | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.android.sessionreplay.utils.ColorStringFormatter | ||
import com.datadog.android.sessionreplay.utils.GlobalBounds | ||
|
||
internal class ButtonSemanticsNodeMapper( | ||
colorStringFormatter: ColorStringFormatter, | ||
private val semanticsUtils: SemanticsUtils = SemanticsUtils() | ||
) : AbstractSemanticsNodeMapper(colorStringFormatter) { | ||
|
||
override fun map(semanticsNode: SemanticsNode, parentContext: UiContext): ComposeWireframe { | ||
val density = semanticsNode.layoutInfo.density | ||
val bounds = resolveBound(semanticsNode) | ||
val buttonStyle = resolveSemanticsButtonStyle(semanticsNode, bounds, density) | ||
return ComposeWireframe( | ||
MobileSegment.Wireframe.ShapeWireframe( | ||
id = semanticsNode.id.toLong(), | ||
x = bounds.x, | ||
y = bounds.y, | ||
width = bounds.width, | ||
height = bounds.height, | ||
shapeStyle = buttonStyle | ||
), | ||
uiContext = parentContext.copy( | ||
parentContentColor = buttonStyle.backgroundColor ?: parentContext.parentContentColor | ||
) | ||
) | ||
} | ||
|
||
private fun resolveSemanticsButtonStyle( | ||
semanticsNode: SemanticsNode, | ||
globalBounds: GlobalBounds, | ||
density: Density | ||
): MobileSegment.ShapeStyle { | ||
val color = semanticsUtils.resolveSemanticsModifierColor(semanticsNode) | ||
val cornerRadius = semanticsUtils.resolveSemanticsModifierCornerRadius(semanticsNode, globalBounds, density) | ||
return MobileSegment.ShapeStyle( | ||
backgroundColor = color?.let { convertColor(it) }, | ||
cornerRadius = cornerRadius | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...android/sessionreplay/compose/internal/mappers/semantics/ButtonSemanticsNodeMapperTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.android.sessionreplay.compose.internal.mappers.semantics | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.semantics.SemanticsNode | ||
import com.datadog.android.sessionreplay.compose.internal.data.UiContext | ||
import com.datadog.android.sessionreplay.compose.internal.utils.SemanticsUtils | ||
import com.datadog.android.sessionreplay.compose.test.elmyr.SessionReplayComposeForgeConfigurator | ||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import fr.xgouchet.elmyr.Forge | ||
import fr.xgouchet.elmyr.annotation.FloatForgery | ||
import fr.xgouchet.elmyr.annotation.Forgery | ||
import fr.xgouchet.elmyr.annotation.LongForgery | ||
import fr.xgouchet.elmyr.annotation.StringForgery | ||
import fr.xgouchet.elmyr.junit5.ForgeConfiguration | ||
import fr.xgouchet.elmyr.junit5.ForgeExtension | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.junit.jupiter.api.extension.Extensions | ||
import org.mockito.Mock | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
import org.mockito.junit.jupiter.MockitoSettings | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.doReturn | ||
import org.mockito.kotlin.eq | ||
import org.mockito.kotlin.whenever | ||
import org.mockito.quality.Strictness | ||
|
||
@Extensions( | ||
ExtendWith(MockitoExtension::class), | ||
ExtendWith(ForgeExtension::class) | ||
) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
@ForgeConfiguration(SessionReplayComposeForgeConfigurator::class) | ||
internal class ButtonSemanticsNodeMapperTest : AbstractCompositionGroupMapperTest() { | ||
|
||
private lateinit var testedButtonSemanticsNodeMapper: ButtonSemanticsNodeMapper | ||
|
||
@Mock | ||
private lateinit var mockSemanticsNode: SemanticsNode | ||
|
||
@Mock | ||
private lateinit var mockSemanticsUtils: SemanticsUtils | ||
|
||
@LongForgery(min = 0L, max = 0xffffff) | ||
var fakeBackgroundColor: Long = 0L | ||
|
||
@FloatForgery | ||
var fakeCornerRadius: Float = 0f | ||
|
||
@StringForgery(regex = "#[0-9A-F]{8}") | ||
lateinit var fakeBackgroundColorHexString: String | ||
|
||
@Forgery | ||
lateinit var fakeUiContext: UiContext | ||
|
||
@BeforeEach | ||
override fun `set up`(forge: Forge) { | ||
super.`set up`(forge) | ||
mockColorStringFormatter(fakeBackgroundColor, fakeBackgroundColorHexString) | ||
|
||
testedButtonSemanticsNodeMapper = ButtonSemanticsNodeMapper( | ||
colorStringFormatter = mockColorStringFormatter, | ||
semanticsUtils = mockSemanticsUtils | ||
) | ||
} | ||
|
||
private fun mockSemanticsNode(): SemanticsNode { | ||
return mockSemanticsNodeWithBound { | ||
whenever(mockSemanticsNode.layoutInfo).doReturn(mockLayoutInfo) | ||
} | ||
} | ||
|
||
@Test | ||
fun `M return the correct wireframe W map`() { | ||
// Given | ||
val mockSemanticsNode = mockSemanticsNode() | ||
whenever(mockSemanticsUtils.resolveSemanticsModifierColor(mockSemanticsNode)).thenReturn( | ||
Color(fakeBackgroundColor).value.toLong() | ||
) | ||
whenever( | ||
mockSemanticsUtils.resolveSemanticsModifierCornerRadius( | ||
eq(mockSemanticsNode), | ||
any(), | ||
eq(mockDensity) | ||
) | ||
).thenReturn(fakeCornerRadius) | ||
|
||
// When | ||
val actual = testedButtonSemanticsNodeMapper.map( | ||
mockSemanticsNode, | ||
fakeUiContext | ||
) | ||
|
||
// Then | ||
val expected = MobileSegment.Wireframe.ShapeWireframe( | ||
id = fakeSemanticsId.toLong(), | ||
x = (fakeBounds.left / fakeDensity).toLong(), | ||
y = (fakeBounds.top / fakeDensity).toLong(), | ||
width = (fakeBounds.size.width / fakeDensity).toLong(), | ||
height = (fakeBounds.size.height / fakeDensity).toLong(), | ||
shapeStyle = MobileSegment.ShapeStyle( | ||
backgroundColor = fakeBackgroundColorHexString, | ||
cornerRadius = fakeCornerRadius | ||
) | ||
) | ||
assertThat(actual.wireframe).isEqualTo(expected) | ||
} | ||
} |