Skip to content

Commit

Permalink
RUM-6195: Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Dec 18, 2024
1 parent 1f3e4af commit 11155d6
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import android.widget.ImageView
object ImageViewUtils {
/**
* Resolves the absolute position on the screen of the given [View].
* @param view: the [View].
* @param view the [View].
* @return the [Rect] representing the absolute position of the view.
*/
fun resolveParentRectAbsPosition(view: View): Rect {
Expand All @@ -42,9 +42,9 @@ object ImageViewUtils {
/**
* Calculates the clipping [Rect] of the given child [Rect] using its parent [Rect] and
* the screen density.
* @param parentRect: the parent [Rect].
* @param childRect: the child [Rect].
* @param density: the screen density.
* @param parentRect the parent [Rect].
* @param childRect the child [Rect].
* @param density the screen density.
* @return the clipping [Rect].
*/
fun calculateClipping(parentRect: Rect, childRect: Rect, density: Float): Rect {
Expand Down Expand Up @@ -78,9 +78,9 @@ object ImageViewUtils {

/**
* Resolves the [Drawable] content [Rect] using the given [ImageView] scale type.
* @param imageView: the [ImageView].
* @param drawable: the [Drawable].
* @param customScaleType: optional custom [ImageView.ScaleType].
* @param imageView the [ImageView].
* @param drawable the [Drawable].
* @param customScaleType optional custom [ImageView.ScaleType].
* @return the resolved content [Rect].
*/
fun resolveContentRectWithScaling(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal class CheckboxSemanticsNodeMapper(
)
)
} else {
createCheckboxWireframe(
createCheckboxWireframes(
parentContext = parentContext,
asyncJobStatusCallback = asyncJobStatusCallback,
semanticsNode = semanticsNode,
Expand Down Expand Up @@ -78,7 +78,7 @@ internal class CheckboxSemanticsNodeMapper(
)
}

private fun createCheckboxWireframe(
private fun createCheckboxWireframes(
parentContext: UiContext,
asyncJobStatusCallback: AsyncJobStatusCallback,
semanticsNode: SemanticsNode,
Expand Down Expand Up @@ -140,7 +140,7 @@ internal class CheckboxSemanticsNodeMapper(
}

// if we failed to create a wireframe from the path
return createManualCheckedWireframe(
return createManualCheckedWireframes(
semanticsNode = semanticsNode,
globalBounds = globalBounds,
backgroundColor = fillColorRgba,
Expand All @@ -162,15 +162,15 @@ internal class CheckboxSemanticsNodeMapper(
} ?: DEFAULT_COLOR_BLACK
}

private fun createManualCheckedWireframe(
private fun createManualCheckedWireframes(
semanticsNode: SemanticsNode,
globalBounds: GlobalBounds,
backgroundColor: String,
borderColor: String
): List<MobileSegment.Wireframe> {
val strokeColor = getFallbackCheckmarkColor(backgroundColor)

val wireframesList = mutableListOf<MobileSegment.Wireframe>()
val wireframes = mutableListOf<MobileSegment.Wireframe>()

val background = createUncheckedState(
semanticsNode = semanticsNode,
Expand All @@ -180,7 +180,7 @@ internal class CheckboxSemanticsNodeMapper(
currentIndex = 0
)

wireframesList.add(background)
wireframes.add(background)

val checkmarkWidth = globalBounds.width * CHECKMARK_SIZE_FACTOR
val checkmarkHeight = globalBounds.height * CHECKMARK_SIZE_FACTOR
Expand All @@ -203,8 +203,8 @@ internal class CheckboxSemanticsNodeMapper(
)
)

wireframesList.add(foreground)
return wireframesList
wireframes.add(foreground)
return wireframes
}

private fun createUncheckedState(
Expand All @@ -213,24 +213,22 @@ internal class CheckboxSemanticsNodeMapper(
backgroundColor: String,
borderColor: String,
currentIndex: Int
): MobileSegment.Wireframe {
return MobileSegment.Wireframe.ShapeWireframe(
id = resolveId(semanticsNode, currentIndex),
x = globalBounds.x,
y = globalBounds.y,
width = CHECKBOX_SIZE_DP.toLong(),
height = CHECKBOX_SIZE_DP.toLong(),
shapeStyle = MobileSegment.ShapeStyle(
backgroundColor = backgroundColor,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
),
border = MobileSegment.ShapeBorder(
color = borderColor,
width = BOX_BORDER_WIDTH_DP
)
) = MobileSegment.Wireframe.ShapeWireframe(
id = resolveId(semanticsNode, currentIndex),
x = globalBounds.x,
y = globalBounds.y,
width = CHECKBOX_SIZE_DP.toLong(),
height = CHECKBOX_SIZE_DP.toLong(),
shapeStyle = MobileSegment.ShapeStyle(
backgroundColor = backgroundColor,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
),
border = MobileSegment.ShapeBorder(
color = borderColor,
width = BOX_BORDER_WIDTH_DP
)
}
)

private fun isCheckboxChecked(semanticsNode: SemanticsNode): Boolean =
semanticsNode.config.getOrNull(SemanticsProperties.ToggleableState) == ToggleableState.On
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,24 @@ internal class ColorUtils(
}
}

internal fun convertRgbaToArgb(rgbaString: String): String {
if (rgbaString.length < 2) return rgbaString
internal fun convertRgbaToArgb(input: String): String {
if (input.length < RGBA_LENGTH) return input

// for takeLast: n > 0
@Suppress("UnsafeThirdPartyFunctionCall")
val alphaValue = rgbaString.takeLast(2)
val alphaValue = input.takeLast(2)

// for substring: length is necessarily > 1 at this point
// for dropLast: n > 0
@Suppress("UnsafeThirdPartyFunctionCall")
val rgbColor = rgbaString
val rgbColor = input
.substring(1)
.dropLast(2)
return "#$alphaValue$rgbColor"
}

internal companion object {
internal const val COLOR_PARSE_ERROR = "Failed to parse color: %s"
internal const val RGBA_LENGTH = 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ internal class SemanticsUtils(private val reflectionUtils: ReflectionUtils = Ref
}
}

val result = (color?.value as? Color)
?.value
val result = (color?.value as? Color)?.value

return result?.toLong()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val actualWireframe = semanticsWireframe.wireframes[0] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val actualWireframe = semanticsWireframe.wireframes[0] as MobileSegment.Wireframe.ShapeWireframe

val expectedShapeBorder = MobileSegment.ShapeBorder(
color = fakeBorderColorHexString,
Expand All @@ -174,9 +175,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(actualWireframe?.border).isEqualTo(expectedShapeBorder)
assertThat(actualWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(actualWireframe.border).isEqualTo(expectedShapeBorder)
assertThat(actualWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand All @@ -195,7 +195,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val actualWireframe = semanticsWireframe.wireframes[0] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val actualWireframe = semanticsWireframe.wireframes[0] as MobileSegment.Wireframe.ShapeWireframe

val expectedShapeBorder = MobileSegment.ShapeBorder(
color = DEFAULT_COLOR_BLACK,
Expand All @@ -208,9 +209,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(actualWireframe?.border).isEqualTo(expectedShapeBorder)
assertThat(actualWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(actualWireframe.border).isEqualTo(expectedShapeBorder)
assertThat(actualWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand All @@ -229,7 +229,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val backgroundWireframe = semanticsWireframe.wireframes[0] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val backgroundWireframe = semanticsWireframe.wireframes[0] as MobileSegment.Wireframe.ShapeWireframe

val expectedBgShapeBorder = MobileSegment.ShapeBorder(
color = fakeBorderColorHexString,
Expand All @@ -242,9 +243,8 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(backgroundWireframe?.border).isEqualTo(expectedBgShapeBorder)
assertThat(backgroundWireframe?.shapeStyle).isEqualTo(expectedBgShapeStyle)
assertThat(backgroundWireframe.border).isEqualTo(expectedBgShapeBorder)
assertThat(backgroundWireframe.shapeStyle).isEqualTo(expectedBgShapeStyle)
}

@Test
Expand All @@ -266,15 +266,15 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val foregroundWireframe = semanticsWireframe.wireframes[1] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val foregroundWireframe = semanticsWireframe.wireframes[1] as MobileSegment.Wireframe.ShapeWireframe
val expectedShapeStyle = MobileSegment.ShapeStyle(
backgroundColor = DEFAULT_COLOR_WHITE,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(foregroundWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(foregroundWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand All @@ -295,17 +295,18 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
parentContext = mockUiContext,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

// Then
assertThat(semanticsWireframe.wireframes).hasSize(2)

val foregroundWireframe = semanticsWireframe.wireframes[1] as? MobileSegment.Wireframe.ShapeWireframe
val foregroundWireframe = semanticsWireframe.wireframes[1] as MobileSegment.Wireframe.ShapeWireframe
val expectedShapeStyle = MobileSegment.ShapeStyle(
backgroundColor = DEFAULT_COLOR_BLACK,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(foregroundWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(foregroundWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand All @@ -321,15 +322,15 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val foregroundWireframe = semanticsWireframe.wireframes[1] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val foregroundWireframe = semanticsWireframe.wireframes[1] as MobileSegment.Wireframe.ShapeWireframe
val expectedShapeStyle = MobileSegment.ShapeStyle(
backgroundColor = DEFAULT_COLOR_BLACK,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(foregroundWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(foregroundWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand All @@ -345,15 +346,15 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

val foregroundWireframe = semanticsWireframe.wireframes[1] as? MobileSegment.Wireframe.ShapeWireframe
// Then
val foregroundWireframe = semanticsWireframe.wireframes[1] as MobileSegment.Wireframe.ShapeWireframe
val expectedShapeStyle = MobileSegment.ShapeStyle(
backgroundColor = DEFAULT_COLOR_BLACK,
opacity = 1f,
cornerRadius = CHECKBOX_CORNER_RADIUS
)

// Then
assertThat(foregroundWireframe?.shapeStyle).isEqualTo(expectedShapeStyle)
assertThat(foregroundWireframe.shapeStyle).isEqualTo(expectedShapeStyle)
}

@Test
Expand Down Expand Up @@ -400,6 +401,7 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

// Then
val expectedShapeStyle = MobileSegment.ShapeStyle(
backgroundColor = fakeFillColorHexString,
opacity = 1f,
Expand All @@ -411,7 +413,6 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
width = BOX_BORDER_WIDTH_DP
)

// Then
verify(mockUiContext.imageWireframeHelper).createImageWireframeByPath(
id = any(),
globalBounds = eq(fakeGlobalBounds),
Expand Down Expand Up @@ -492,11 +493,12 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
parentContext = mockUiContext,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)

// Then
assertThat(wireframes.wireframes).hasSize(1)
val actualWireframe = wireframes.wireframes[0] as? MobileSegment.Wireframe.ShapeWireframe
val actualWireframe = wireframes.wireframes[0] as MobileSegment.Wireframe.ShapeWireframe
assertThat(actualWireframe).isNotNull

// Then
verify(mockUiContext.imageWireframeHelper, never()).createImageWireframeByBitmap(
id = any(),
globalBounds = any(),
Expand All @@ -509,7 +511,7 @@ internal class CheckboxSemanticsNodeMapperTest : AbstractSemanticsNodeMapperTest
shapeStyle = anyOrNull(),
border = anyOrNull()
)
assertThat(actualWireframe?.shapeStyle?.backgroundColor).isEqualTo(DEFAULT_COLOR_WHITE)
assertThat(actualWireframe?.border?.color).isEqualTo(DEFAULT_COLOR_BLACK)
assertThat(actualWireframe.shapeStyle?.backgroundColor).isEqualTo(DEFAULT_COLOR_WHITE)
assertThat(actualWireframe.border?.color).isEqualTo(DEFAULT_COLOR_BLACK)
}
}
Loading

0 comments on commit 11155d6

Please sign in to comment.