Skip to content

Commit

Permalink
Fixes and updates related with API 35 source code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusc83 committed Sep 20, 2024
1 parent 5a8be07 commit d3892b5
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ package com.datadog.android.sessionreplay.internal.recorder.resources

import android.graphics.Bitmap
import com.datadog.android.sessionreplay.internal.utils.InvocationUtils
import com.datadog.android.sessionreplay.internal.utils.safeConfig

internal class BitmapPoolHelper(
private val invocationUtils: InvocationUtils = InvocationUtils()
) {
internal fun generateKey(bitmap: Bitmap) =
generateKey(bitmap.width, bitmap.height, bitmap.config)
generateKey(bitmap.width, bitmap.height, bitmap.safeConfig())

internal fun generateKey(width: Int, height: Int, config: Bitmap.Config) =
"$width-$height-$config"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 android.graphics.Bitmap.Config

internal fun Bitmap.safeConfig(): Config {
return this.config ?: Config.ARGB_8888
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import androidx.collection.LruCache
internal class CacheUtils<K : Any, V : Any>(
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<K, V>) {
@Suppress("MagicNumber")
val onLowMemorySizeBytes = cache.maxSize() / 2 // 50%
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal class NodeFlattenerTest {

private fun generateTreeFromList(list: List<MobileSegment.Wireframe>): 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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.graphics.Bitmap
import android.os.Build
import com.datadog.android.sessionreplay.forge.ForgeConfigurator
import com.datadog.android.sessionreplay.internal.utils.CacheUtils
import com.datadog.android.sessionreplay.internal.utils.safeConfig
import com.datadog.tools.unit.annotations.TestTargetApi
import com.datadog.tools.unit.extensions.ApiLevelExtension
import fr.xgouchet.elmyr.Forge
Expand Down Expand Up @@ -148,13 +149,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, mockBitmap.safeConfig())

// When
testedCache.put(mockBitmap)

// Then
val actualBitmap = testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockBitmap.config)
val actualBitmap =
testedCache.getBitmapByProperties(mockBitmap.width, mockBitmap.height, mockBitmap.safeConfig())
assertThat(actualBitmap).isEqualTo(mockBitmap)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 android.graphics.Bitmap.Config
import org.assertj.core.api.Assertions.assertThat
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.kotlin.mock
import org.mockito.kotlin.whenever

@Extensions(
ExtendWith(MockitoExtension::class)
)
internal class BitmapExtTest {

@Mock
lateinit var mockBitmap: Bitmap

@Test
fun `M return bitmap config W safeConfig`() {
// Given
val mockConfig: Config = mock()
whenever(mockBitmap.config).thenReturn(mockConfig)

// When
val result = mockBitmap.safeConfig()

// Then
assertThat(result).isEqualTo(mockConfig)
}

@Test
fun `M return default config W safeConfig { internal one is null }`() {
// Given
whenever(mockBitmap.config).thenReturn(null)

// When
val result = mockBitmap.safeConfig()

// Then
assertThat(result).isEqualTo(Config.ARGB_8888)
}
}

0 comments on commit d3892b5

Please sign in to comment.