Skip to content

Commit

Permalink
Minor improvements to unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-saia-datadog committed Dec 10, 2024
1 parent be663eb commit bf7e7da
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView

/**
* A collection of view utility functions for resolving absolute
* positions, clipping bounds, and other useful data for
* image views operations.
*/
object ImageViewUtils {
/**
* Resolves the absolute position on the screen of the given [View].
* @param view: the [View].
* @return the [Rect] representing the absolute position of the view.
*/
fun resolveParentRectAbsPosition(view: View): Rect {
val coords = IntArray(2)
// this will always have size >= 2
Expand All @@ -29,6 +39,14 @@ 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.
* @return the clipping [Rect].
*/
fun calculateClipping(parentRect: Rect, childRect: Rect, density: Float): Rect {
val left = if (childRect.left < parentRect.left) {
parentRect.left - childRect.left
Expand Down Expand Up @@ -58,6 +76,12 @@ object ImageViewUtils {
)
}

/**
* Resolves the [Drawable] content [Rect] using the given [ImageView] scale type.
* @param imageView: the [ImageView].
* @param drawable: the [Drawable].
* @return the resolved content [Rect].
*/
fun resolveContentRectWithScaling(
imageView: ImageView,
drawable: Drawable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ internal fun Rect.toWireframeClip(): MobileSegment.WireframeClip {
this.left.toLong(),
this.right.toLong()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.annotation.UiThread
import com.datadog.android.api.InternalLogger
import com.datadog.android.internal.utils.ImageViewUtils
import com.datadog.android.internal.utils.densityNormalized
import com.datadog.android.sessionreplay.SessionReplay
import com.datadog.android.sessionreplay.internal.utils.toWireframeClip
import com.datadog.android.sessionreplay.model.MobileSegment
import com.datadog.android.sessionreplay.recorder.MappingContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,8 @@ internal class DefaultImageWireframeHelperTest {
customResourceIdCacheKey = null,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)
wireframes[0] as MobileSegment.Wireframe.ImageWireframe

assertThat(wireframes[0]).isInstanceOf(MobileSegment.Wireframe.ImageWireframe::class.java)

// Then
val argumentCaptor = argumentCaptor<ResourceResolverCallback>()
Expand Down Expand Up @@ -939,7 +940,8 @@ internal class DefaultImageWireframeHelperTest {
customResourceIdCacheKey = null,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)
wireframes[0] as MobileSegment.Wireframe.ImageWireframe

assertThat(wireframes[0]).isInstanceOf(MobileSegment.Wireframe.ImageWireframe::class.java)

// Then
val argumentCaptor = argumentCaptor<ResourceResolverCallback>()
Expand Down Expand Up @@ -1270,7 +1272,8 @@ internal class DefaultImageWireframeHelperTest {
customResourceIdCacheKey = null,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)
wireframes[0] as MobileSegment.Wireframe.ImageWireframe

assertThat(wireframes[0]).isInstanceOf(MobileSegment.Wireframe.ImageWireframe::class.java)

// Then
val argumentCaptor = argumentCaptor<ResourceResolverCallback>()
Expand Down Expand Up @@ -1318,7 +1321,8 @@ internal class DefaultImageWireframeHelperTest {
customResourceIdCacheKey = null,
asyncJobStatusCallback = mockAsyncJobStatusCallback
)
wireframes[0] as MobileSegment.Wireframe.ImageWireframe

assertThat(wireframes[0]).isInstanceOf(MobileSegment.Wireframe.ImageWireframe::class.java)

// Then
val argumentCaptor = argumentCaptor<ResourceResolverCallback>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.mockito.quality.Strictness
Expand Down Expand Up @@ -372,29 +371,6 @@ internal class DrawableUtilsTest {
assertThat(displayMetricsCaptor.firstValue).isEqualTo(mockDisplayMetrics)
}

@Test
fun `M uses original drawable W createBitmapOfApproxSizeFromDrawable`() {
// Given
whenever(mockDrawable.intrinsicWidth).thenReturn(1)
whenever(mockDrawable.intrinsicHeight).thenReturn(1)

// When
testedDrawableUtils.createBitmapOfApproxSizeFromDrawable(
drawable = mockDrawable,
drawableWidth = mockDrawable.intrinsicWidth,
drawableHeight = mockDrawable.intrinsicHeight,
displayMetrics = mockDisplayMetrics,
config = mockConfig,
bitmapCreationCallback = mockBitmapCreationCallback
)

// Then
verify(mockDrawable).setBounds(any(), any(), any(), any())
verify(mockDrawable).draw(any())
verify(mockSecondDrawable, never()).setBounds(any(), any(), any(), any())
verify(mockSecondDrawable, never()).draw(any())
}

@Test
fun `M return scaled bitmap W createScaledBitmap()`(
@Mock mockScaledBitmap: Bitmap
Expand Down

0 comments on commit bf7e7da

Please sign in to comment.