Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUMM-3774 prevent crash in Canvas Wrapper #1954

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,18 @@ import com.datadog.android.api.InternalLogger
internal class CanvasWrapper(
private val logger: InternalLogger = InternalLogger.UNBOUND
) {

internal fun createCanvas(bitmap: Bitmap): Canvas? {
@Suppress("SwallowedException", "TooGenericExceptionCaught")
if (bitmap.isRecycled || !bitmap.isMutable) {
logger.log(
level = InternalLogger.Level.ERROR,
target = InternalLogger.Target.MAINTAINER,
{ INVALID_BITMAP }
)
return null
}

@Suppress("TooGenericExceptionCaught")
return try {
Canvas(bitmap)
} catch (e: IllegalStateException) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: the comment on the line below should be updated to should never happen since we are passing a mutable bitmap

Expand All @@ -38,6 +48,7 @@ internal class CanvasWrapper(
}

private companion object {
private const val INVALID_BITMAP = "Cannot create canvas: bitmap is either already recycled or immutable"
private const val FAILED_TO_CREATE_CANVAS = "Failed to create canvas"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.recorder.wrappers

import android.graphics.Bitmap
import com.datadog.android.api.InternalLogger
import com.datadog.android.sessionreplay.forge.ForgeConfigurator
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.doReturn
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 CanvasWrapperTest {

lateinit var testedWrapper: CanvasWrapper

@Mock
lateinit var mockLogger: InternalLogger

@Mock
lateinit var mockBitmap: Bitmap

@BeforeEach
fun `set up`() {
testedWrapper = CanvasWrapper(mockLogger)
}

@Test
fun `M return null W createCanvas() {recycled bitmap}`() {
// Given
whenever(mockBitmap.isRecycled) doReturn true

// When
val canvas = testedWrapper.createCanvas(mockBitmap)

// Then
assertThat(canvas).isNull()
}

@Test
fun `M return null W createCanvas() {immutable bitmap}`() {
// Given
whenever(mockBitmap.isRecycled) doReturn false
whenever(mockBitmap.isMutable) doReturn false

// When
val canvas = testedWrapper.createCanvas(mockBitmap)

// Then
assertThat(canvas).isNull()
}
}