Skip to content

Commit

Permalink
test: Improve unit test for 'sketch-core' module
Browse files Browse the repository at this point in the history
  • Loading branch information
panpf committed Aug 30, 2024
1 parent d9b07ce commit 0bb7188
Show file tree
Hide file tree
Showing 18 changed files with 298 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ package com.github.panpf.sketch.test.utils
import android.graphics.Bitmap


fun Bitmap.toSizeString(): String = "${width}x${height}"

@Suppress("USELESS_ELVIS")
val Bitmap.configOrNull: Bitmap.Config?
get() = config ?: null

fun Bitmap.toShortInfoString(): String = "Bitmap(${width}x${height},$configOrNull)"


val Bitmap.cornerA: Int
get() = getPixel(0, 0)
val Bitmap.cornerB: Int
Expand All @@ -32,12 +41,4 @@ fun Bitmap.corners(block: Bitmap.() -> List<Int>): List<Int> {
return block(this)
}

fun Bitmap.toSizeString(): String = "${width}x${height}"

fun Bitmap.corners(): List<Int> = listOf(cornerA, cornerB, cornerC, cornerD)

@Suppress("USELESS_ELVIS")
val Bitmap.configOrNull: Bitmap.Config?
get() = config ?: null

fun Bitmap.toShortInfoString(): String = "Bitmap(${width}x${height},$configOrNull)"
fun Bitmap.corners(): List<Int> = listOf(cornerA, cornerB, cornerC, cornerD)
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.panpf.sketch.test.utils

import android.graphics.Bitmap
import androidx.core.graphics.get
import com.github.panpf.sketch.AndroidBitmapImage
import com.github.panpf.sketch.Image
import com.github.panpf.sketch.asSketchImage
Expand All @@ -13,4 +14,13 @@ actual fun createImage(width: Int, height: Int): Image {

actual fun createCacheValue(image: Image, extras: Map<String, Any?>): MemoryCache.Value {
return AndroidBitmapImageValue(image = image as AndroidBitmapImage, extras = extras)
}

actual fun Image.hasAlpha(): Boolean = (this as AndroidBitmapImage).bitmap.hasAlpha()

/**
* Returns the Color at the specified location.
*/
actual fun Image.getPixel(x: Int, y: Int): Int {
return (this as AndroidBitmapImage).bitmap.get(x, y)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,26 @@ import com.github.panpf.sketch.cache.MemoryCache

expect fun createImage(width: Int, height: Int): Image

expect fun createCacheValue(image: Image, extras: Map<String, Any?>): MemoryCache.Value
expect fun createCacheValue(image: Image, extras: Map<String, Any?>): MemoryCache.Value

val Image.cornerA: Int
get() = getPixel(0, 0)
val Image.cornerB: Int
get() = getPixel(width - 1, 0)
val Image.cornerC: Int
get() = getPixel(width - 1, height - 1)
val Image.cornerD: Int
get() = getPixel(0, height - 1)

fun Image.corners(block: Image.() -> List<Int>): List<Int> {
return block(this)
}

fun Image.corners(): List<Int> = listOf(cornerA, cornerB, cornerC, cornerD)

expect fun Image.hasAlpha(): Boolean

/**
* Returns the Color at the specified location.
*/
expect fun Image.getPixel(x: Int, y: Int): Int
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.github.panpf.sketch.SkiaBitmapImage
import com.github.panpf.sketch.asSketchImage
import com.github.panpf.sketch.cache.MemoryCache
import com.github.panpf.sketch.cache.SkiaBitmapImageValue
import com.github.panpf.sketch.util.getPixel
import com.github.panpf.sketch.util.hasAlpha

actual fun createImage(width: Int, height: Int): Image {
return SkiaBitmap().apply {
Expand All @@ -15,4 +17,14 @@ actual fun createImage(width: Int, height: Int): Image {

actual fun createCacheValue(image: Image, extras: Map<String, Any?>): MemoryCache.Value {
return SkiaBitmapImageValue(image = image as SkiaBitmapImage, extras = extras)
}

//actual fun Image.hasAlpha(): Boolean = !(this as SkiaBitmapImage).bitmap.isOpaque
actual fun Image.hasAlpha(): Boolean = (this as SkiaBitmapImage).bitmap.hasAlpha()

/**
* Returns the Color at the specified location.
*/
actual fun Image.getPixel(x: Int, y: Int): Int {
return (this as SkiaBitmapImage).bitmap.getPixel(x, y)
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ internal expect fun blurTransformation(

/**
* Bitmap blur transformation
*
* @see com.github.panpf.sketch.core.common.test.transform.BlurTransformationTest
*/
class BlurTransformation constructor(
/** Blur radius */
Expand Down Expand Up @@ -86,12 +88,27 @@ class BlurTransformation constructor(
}
}

/**
* Create a blur transform record
*
* @see com.github.panpf.sketch.core.common.test.transform.BlurTransformationTest.testBlurTransformed
*/
fun createBlurTransformed(
radius: Int, hasAlphaBitmapBgColor: Int?, maskColor: Int?
) = "BlurTransformed($radius,$hasAlphaBitmapBgColor,$maskColor)"

/**
* Check if the transformed string is a blur transformation
*
* @see com.github.panpf.sketch.core.common.test.transform.BlurTransformationTest.testBlurTransformed
*/
fun isBlurTransformed(transformed: String): Boolean =
transformed.startsWith("BlurTransformed(")

/**
* Get the blur transformation record from the transformed record list
*
* @see com.github.panpf.sketch.core.common.test.transform.BlurTransformationTest.testBlurTransformed
*/
fun List<String>.getBlurTransformed(): String? =
find { isBlurTransformed(it) }
Loading

0 comments on commit 0bb7188

Please sign in to comment.