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 23, 2024
1 parent 5a8be07 commit cec0387
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<DataStoreReadCallback<ByteArray>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<R> safeCall(call: () -> R): R? =
internal fun <R> safeCall(call: () -> R): R? =
invocationUtils.safeCallWithErrorLogging(
call = { call() },
failureMessage = BITMAP_OPERATION_FAILED
Expand Down
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 @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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}")
}
}

0 comments on commit cec0387

Please sign in to comment.