-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a goldens based test utility for compose
- Loading branch information
Showing
4 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
Binary file added
BIN
+6.2 KB
integration/compose/src/androidTest/assets/glideImage_startsAnimatedDrawable.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
integration/compose/src/androidTest/java/com/bumptech/glide/test/goldens.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.bumptech.glide.test | ||
|
||
import android.content.Context | ||
import android.graphics.Bitmap | ||
import android.graphics.BitmapFactory | ||
import android.os.Environment | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toPixelMap | ||
import androidx.test.core.app.ApplicationProvider | ||
import java.io.BufferedOutputStream | ||
import java.io.File | ||
import java.io.FileNotFoundException | ||
import java.io.FileOutputStream | ||
import java.io.IOException | ||
import java.io.OutputStream | ||
import java.lang.IllegalStateException | ||
|
||
const val GENERATED_FILES_DIR = "compose_goldens" | ||
const val EXTENSION = "png" | ||
|
||
fun ImageBitmap.compareToGolden(testName: String) { | ||
val bitmap = toBitmap() | ||
val existingGolden = readExistingGolden(testName) | ||
val reasonForWrite = if (existingGolden == null) { | ||
"Missing golden" | ||
} else if (!existingGolden.sameAs(bitmap)) { | ||
"Different golden" | ||
} else { | ||
null | ||
} | ||
if (reasonForWrite != null) { | ||
val filePath = writeBitmap(bitmap, testName) | ||
throw IllegalStateException("$reasonForWrite for $testName, wrote a new one. cd to androidTest/assets and run: adb pull $filePath") | ||
} | ||
} | ||
|
||
private fun ImageBitmap.toBitmap(): Bitmap { | ||
val pixels = toPixelMap() | ||
val bitmap = Bitmap.createBitmap(pixels.width, pixels.height, Bitmap.Config.ARGB_8888) | ||
bitmap.setPixels( | ||
pixels.buffer, | ||
pixels.bufferOffset, | ||
pixels.stride, | ||
0, | ||
0, | ||
pixels.width, | ||
pixels.height | ||
) | ||
return bitmap | ||
} | ||
|
||
private fun readExistingGolden(testName: String): Bitmap? { | ||
return try { | ||
ApplicationProvider.getApplicationContext<Context>() | ||
.assets | ||
.open(testFileName(testName)).use { | ||
val options = BitmapFactory.Options() | ||
options.inScaled = false | ||
BitmapFactory.decodeStream(it, null, options) | ||
} | ||
} catch (e: FileNotFoundException) { | ||
null | ||
} | ||
} | ||
|
||
private fun testFileName(testName: String) = "$testName.$EXTENSION" | ||
|
||
private fun getTestFilesDir(): File { | ||
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) | ||
return File(dir, GENERATED_FILES_DIR) | ||
} | ||
|
||
private fun writeBitmap(bitmap: Bitmap, testName: String): String { | ||
val testFilesDir = getTestFilesDir() | ||
require(!(!testFilesDir.exists() && !testFilesDir.mkdirs())) { "Failed to make directory: $testFilesDir" } | ||
val file = File(testFilesDir, testFileName(testName)) | ||
check(!(file.exists() && !file.delete())) { "Failed to remove existing file: $file" } | ||
var os: OutputStream? = null | ||
try { | ||
os = BufferedOutputStream(FileOutputStream(file)) | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os) | ||
os.close() | ||
} catch (e: IOException) { | ||
throw RuntimeException(e) | ||
} finally { | ||
if (os != null) { | ||
try { | ||
os.close() | ||
} catch (e: IOException) { | ||
// Ignored. | ||
} | ||
} | ||
} | ||
return file.absolutePath | ||
} |