From cec038770b2002b17048d6621b9ae4b8da61b41d Mon Sep 17 00:00:00 2001 From: Marius Constantin Date: Fri, 20 Sep 2024 11:53:15 +0200 Subject: [PATCH] Fixes and updates related with API 35 source code. --- .../file/datastore/DataStoreFileReaderTest.kt | 4 +- .../recorder/resources/BitmapPoolHelper.kt | 11 ++- .../internal/utils/CacheUtils.kt | 4 + .../internal/processor/NodeFlattenerTest.kt | 4 +- .../recorder/resources/BitmapPoolTest.kt | 5 +- .../internal/utils/BitmapPoolHelperTest.kt | 80 +++++++++++++++++++ 6 files changed, 99 insertions(+), 9 deletions(-) create mode 100644 features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/BitmapPoolHelperTest.kt diff --git a/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/internal/persistence/file/datastore/DataStoreFileReaderTest.kt b/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/internal/persistence/file/datastore/DataStoreFileReaderTest.kt index aac558bf7e..3e7b9c71f9 100644 --- a/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/internal/persistence/file/datastore/DataStoreFileReaderTest.kt +++ b/dd-sdk-android-core/src/test/kotlin/com/datadog/android/core/internal/persistence/file/datastore/DataStoreFileReaderTest.kt @@ -142,7 +142,7 @@ internal class DataStoreFileReaderTest { @Test fun `M log error W read() { invalid number of blocks }`() { // Given - blocksReturned.removeLast() + blocksReturned.removeAt(blocksReturned.lastIndex) val foundBlocks = blocksReturned.size val expectedBlocks = TLVBlockType.values().size @@ -213,7 +213,7 @@ internal class DataStoreFileReaderTest { @Test fun `M return onFailure W read() { invalid number of blocks }`() { // Given - blocksReturned.removeLast() + blocksReturned.removeAt(blocksReturned.lastIndex) val expectedMessage = INVALID_NUMBER_OF_BLOCKS_ERROR.format(Locale.US, blocksReturned.size, TLVBlockType.values().size) val mockCallback = mock>() diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolHelper.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolHelper.kt index 63d0b91100..c03d66ea3f 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolHelper.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolHelper.kt @@ -15,10 +15,15 @@ internal class BitmapPoolHelper( internal fun generateKey(bitmap: Bitmap) = generateKey(bitmap.width, bitmap.height, bitmap.config) - internal fun generateKey(width: Int, height: Int, config: Bitmap.Config) = - "$width-$height-$config" + internal fun generateKey(width: Int, height: Int, config: Bitmap.Config?): String { + return if (config != null) { + "$width-$height-$config" + } else { + "$width-$height-null" + } + } - internal fun safeCall(call: () -> R): R? = + internal fun safeCall(call: () -> R): R? = invocationUtils.safeCallWithErrorLogging( call = { call() }, failureMessage = BITMAP_OPERATION_FAILED diff --git a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/CacheUtils.kt b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/CacheUtils.kt index 0fdff54672..03945c3624 100644 --- a/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/CacheUtils.kt +++ b/features/dd-sdk-android-session-replay/src/main/kotlin/com/datadog/android/sessionreplay/internal/utils/CacheUtils.kt @@ -12,6 +12,10 @@ import androidx.collection.LruCache internal class CacheUtils( private val invocationUtils: InvocationUtils = InvocationUtils() ) { + + // some of this memory level are not being triggered after API 34. We still need to keep them for now + // for lower versions + @Suppress("DEPRECATION") internal fun handleTrimMemory(level: Int, cache: LruCache) { @Suppress("MagicNumber") val onLowMemorySizeBytes = cache.maxSize() / 2 // 50% diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/processor/NodeFlattenerTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/processor/NodeFlattenerTest.kt index e786c2ef1a..9c311cc15e 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/processor/NodeFlattenerTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/processor/NodeFlattenerTest.kt @@ -141,7 +141,7 @@ internal class NodeFlattenerTest { private fun generateTreeFromList(list: List): Node { val mutableList = list.toMutableList() - val root = mutableList.removeFirst().toNode() + val root = mutableList.removeAt(0).toNode() val middle = mutableList.size / 2 // add left // we need to create a new list as Kotlin .subList re - uses the old list @@ -155,7 +155,7 @@ internal class NodeFlattenerTest { if (leafs.isEmpty()) { return } - val leafToAdd = leafs.removeFirst().toNode() + val leafToAdd = leafs.removeAt(0).toNode() parent.addChild(leafToAdd) val middle = leafs.size / 2 // add left diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolTest.kt index 8e574d1428..d1b50b4ec5 100644 --- a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolTest.kt +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/recorder/resources/BitmapPoolTest.kt @@ -148,13 +148,14 @@ internal class BitmapPoolTest { fun `M mark bitmap as free W put() { if bitmap already in the pool }`() { // Given testedCache.put(mockBitmap) - testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockBitmap.config) + testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockConfig) // When testedCache.put(mockBitmap) // Then - val actualBitmap = testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockBitmap.config) + val actualBitmap = + testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockConfig) assertThat(actualBitmap).isEqualTo(mockBitmap) } diff --git a/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/BitmapPoolHelperTest.kt b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/BitmapPoolHelperTest.kt new file mode 100644 index 0000000000..289defb09e --- /dev/null +++ b/features/dd-sdk-android-session-replay/src/test/kotlin/com/datadog/android/sessionreplay/internal/utils/BitmapPoolHelperTest.kt @@ -0,0 +1,80 @@ +/* + * 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.internal.utils + +import android.graphics.Bitmap +import com.datadog.android.sessionreplay.forge.ForgeConfigurator +import com.datadog.android.sessionreplay.internal.recorder.resources.BitmapPoolHelper +import fr.xgouchet.elmyr.Forge +import fr.xgouchet.elmyr.annotation.IntForgery +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.whenever +import org.mockito.quality.Strictness + +@Extensions( + ExtendWith(MockitoExtension::class), + ExtendWith(ForgeExtension::class) +) +@MockitoSettings(strictness = Strictness.LENIENT) +@ForgeConfiguration(ForgeConfigurator::class) +internal class BitmapPoolHelperTest { + + lateinit var testedHelper: BitmapPoolHelper + + @Mock + lateinit var mockBitmap: Bitmap + + @IntForgery + var fakeWidth: Int = 0 + + @IntForgery + var fakeHeight: Int = 0 + private lateinit var fakeConfig: Bitmap.Config + + @BeforeEach + fun `set up`(forge: Forge) { + fakeConfig = forge.aValueFrom(Bitmap.Config::class.java) + testedHelper = BitmapPoolHelper() + whenever(mockBitmap.width).thenReturn(fakeWidth) + whenever(mockBitmap.height).thenReturn(fakeHeight) + whenever(mockBitmap.config).thenReturn(fakeConfig) + } + + @Test + fun `M generate bitmap key W generateKey`( + forge: Forge + ) { + // When + val key = testedHelper.generateKey(mockBitmap) + + // Then + assertThat(key).isEqualTo("$fakeWidth-$fakeHeight-$fakeConfig") + } + + @Test + fun `M generate bitmap key W generateKey { config is null }`( + forge: Forge + ) { + // Given + whenever(mockBitmap.config).thenReturn(null) + + // When + val key = testedHelper.generateKey(mockBitmap) + + // Then + assertThat(key).isEqualTo("$fakeWidth-$fakeHeight-${null}") + } +}