Skip to content

Commit

Permalink
added test for when exception happens during compression
Browse files Browse the repository at this point in the history
  • Loading branch information
davotoula committed Oct 9, 2024
1 parent 6cd50a1 commit 76a66e7
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
package com.vitorpamplona.amethyst.ui.components

import android.content.Context
import android.net.Uri
import android.os.Looper
import com.abedelazizshe.lightcompressorlibrary.VideoCompressor
Expand All @@ -30,7 +31,6 @@ import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkClass
import io.mockk.mockkObject
import io.mockk.mockkStatic
import io.mockk.unmockkAll
Expand Down Expand Up @@ -146,7 +146,7 @@ class MediaCompressorTest {
MediaCompressor().compress(
uri,
contentType,
applicationContext = mockkClass(android.content.Context::class, relaxed = true),
applicationContext = mockk<Context>(relaxed = true),
onReady = { _, _, _ -> },
onError = { },
mediaQuality = mediaQuality,
Expand All @@ -155,4 +155,30 @@ class MediaCompressorTest {
// Verify
coVerify(exactly = 1) { Compressor.compress(any(), any(), any(), any()) }
}

@Test
fun `Image compression should return back same uri on exception`() =
runTest {
// setup
val mockContext = mockk<Context>(relaxed = true)
val mockUri = mockk<Uri>()
val mockOnReady = mockk<(Uri, String?, Long?) -> Unit>(relaxed = true)

mockkObject(MediaCompressorFileUtils)
every { MediaCompressorFileUtils.from(any(), any()) } returns File("test")
coEvery { Compressor.compress(any(), any<File>(), any(), any()) } throws Exception("Compression error")

// Execute
MediaCompressor().compress(
uri = mockUri,
contentType = "image/jpeg",
applicationContext = mockContext,
onReady = mockOnReady,
onError = { },
mediaQuality = CompressorQuality.MEDIUM,
)

// Verify: onReady should be called with original uri, content type, and null size
verify { mockOnReady.invoke(mockUri, "image/jpeg", null) }
}
}

0 comments on commit 76a66e7

Please sign in to comment.