From 39c3e590dbaf61150345164d01bb34c8c6687096 Mon Sep 17 00:00:00 2001 From: luyi Date: Mon, 4 Nov 2024 17:15:38 +0100 Subject: [PATCH] RUM-3460: Fix padding and resizing issue for image view mapper --- .../recorder/mapper/ImageViewMapper.kt | 7 ++++- .../resources/DefaultImageWireframeHelper.kt | 2 -- .../internal/utils/ImageViewUtils.kt | 13 +++++---- .../internal/utils/ImageViewUtilsTest.kt | 16 ++++++++--- .../sr_image_buttons_allow_payload.json | 18 ------------- .../sr_image_buttons_mask_payload.json | 27 +++++++------------ ...image_buttons_mask_user_input_payload.json | 18 ------------- .../sr_images_allow_payload.json | 12 --------- .../sr_images_mask_payload.json | 12 --------- .../sr_images_mask_user_input_payload.json | 12 --------- .../res/layout/fragment_image_components.xml | 3 ++- 11 files changed, 37 insertions(+), 103 deletions(-) diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/mapper/ImageViewMapper.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/mapper/ImageViewMapper.kt index 82c4a82d0c..5c00c371c8 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/mapper/ImageViewMapper.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/mapper/ImageViewMapper.kt @@ -52,7 +52,12 @@ internal class ImageViewMapper( val resources = view.resources val density = resources.displayMetrics.density - val clipping = imageViewUtils.calculateClipping(parentRect, contentRect, density) + + val clipping = if (view.cropToPadding) { + imageViewUtils.calculateClipping(parentRect, contentRect, density) + } else { + null + } val contentXPosInDp = contentRect.left.densityNormalized(density).toLong() val contentYPosInDp = contentRect.top.densityNormalized(density).toLong() val contentWidthPx = contentRect.width() diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/DefaultImageWireframeHelper.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/DefaultImageWireframeHelper.kt index 45815610a7..431ea35380 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/DefaultImageWireframeHelper.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/DefaultImageWireframeHelper.kt @@ -7,7 +7,6 @@ package com.datadog.android.sessionreplay.internal.recorder.resources import android.graphics.drawable.Drawable -import android.graphics.drawable.GradientDrawable import android.graphics.drawable.InsetDrawable import android.graphics.drawable.LayerDrawable import android.view.View @@ -238,7 +237,6 @@ internal class DefaultImageWireframeHelper( } } - is GradientDrawable -> DrawableProperties(drawable, view.width, view.height) else -> DrawableProperties(drawable, width, height) } } diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt index df6d28a678..d95ab4886e 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtils.kt @@ -19,12 +19,15 @@ internal object ImageViewUtils { // this will always have size >= 2 @Suppress("UnsafeThirdPartyFunctionCall") view.getLocationOnScreen(coords) - + val leftPadding = view.paddingLeft + val rightPadding = view.paddingRight + val topPadding = view.paddingTop + val bottomPadding = view.paddingBottom return Rect( - coords[0], - coords[1], - coords[0] + view.width, - coords[1] + view.height + coords[0] + leftPadding, + coords[1] + topPadding, + coords[0] + view.width - rightPadding, + coords[1] + view.height - bottomPadding ) } diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt index 70350a616d..e21d45ef89 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/ImageViewUtilsTest.kt @@ -212,6 +212,10 @@ internal class ImageViewUtilsTest { val fakeGlobalY = forge.aPositiveInt() val fakeWidth = forge.aPositiveInt() val fakeHeight = forge.aPositiveInt() + val fakeTopPadding = forge.aPositiveInt() + val fakeBottomPadding = forge.aPositiveInt() + val fakeLeftPadding = forge.aPositiveInt() + val fakeRightPadding = forge.aPositiveInt() val mockView: View = mock { whenever(it.getLocationOnScreen(any())).thenAnswer { val coords = it.arguments[0] as IntArray @@ -221,16 +225,20 @@ internal class ImageViewUtilsTest { } whenever(it.width).thenReturn(fakeWidth) whenever(it.height).thenReturn(fakeHeight) + whenever(it.paddingTop).thenReturn(fakeTopPadding) + whenever(it.paddingLeft).thenReturn(fakeLeftPadding) + whenever(it.paddingRight).thenReturn(fakeRightPadding) + whenever(it.paddingBottom).thenReturn(fakeBottomPadding) } // When val result = testedImageViewUtils.resolveParentRectAbsPosition(mockView) // Then - assertThat(result.left).isEqualTo(fakeGlobalX) - assertThat(result.top).isEqualTo(fakeGlobalY) - assertThat(result.right).isEqualTo(fakeGlobalX + fakeWidth) - assertThat(result.bottom).isEqualTo(fakeGlobalY + fakeHeight) + assertThat(result.left).isEqualTo(fakeGlobalX + fakeLeftPadding) + assertThat(result.top).isEqualTo(fakeGlobalY + fakeTopPadding) + assertThat(result.right).isEqualTo(fakeGlobalX + fakeWidth - fakeRightPadding) + assertThat(result.bottom).isEqualTo(fakeGlobalY + fakeHeight - fakeBottomPadding) } @Test diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_allow_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_allow_payload.json index 96b2361f56..2c27722bb4 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_allow_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_allow_payload.json @@ -20,12 +20,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, @@ -33,12 +27,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, @@ -46,12 +34,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false } diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_payload.json index 0e80959c89..abb29c3d27 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_payload.json @@ -17,32 +17,23 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, + "type": "shape" + }, + { "type": "placeholder", "label": "Image" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, + "type": "shape" + }, + { "type": "placeholder", "label": "Image" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, + "type": "shape" + }, + { "type": "placeholder", "label": "Image" } diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_user_input_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_user_input_payload.json index 96b2361f56..2c27722bb4 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_user_input_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_image_buttons_mask_user_input_payload.json @@ -20,12 +20,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, @@ -33,12 +27,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, @@ -46,12 +34,6 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false } diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_allow_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_allow_payload.json index a0abf13e6f..38508847fe 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_allow_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_allow_payload.json @@ -17,22 +17,10 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false } diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_payload.json index 20942c44ef..3bf75a9570 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_payload.json @@ -17,22 +17,10 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "placeholder", "label": "Image" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "placeholder", "label": "Image" } diff --git a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_user_input_payload.json b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_user_input_payload.json index a0abf13e6f..38508847fe 100644 --- a/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_user_input_payload.json +++ b/instrumented/integration/src/androidTest/assets/session_replay_payloads/sr_images_mask_user_input_payload.json @@ -17,22 +17,10 @@ "type": "shape" }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false }, { - "clip": { - "top": 2, - "bottom": 3, - "left": 0, - "right": 0 - }, "type": "image", "isEmpty": false } diff --git a/sample/kotlin/src/main/res/layout/fragment_image_components.xml b/sample/kotlin/src/main/res/layout/fragment_image_components.xml index 93e42aea23..7f7d33a755 100644 --- a/sample/kotlin/src/main/res/layout/fragment_image_components.xml +++ b/sample/kotlin/src/main/res/layout/fragment_image_components.xml @@ -339,7 +339,8 @@