From 0c1e053b9ad80bc989eab6d53b3cddd0fe9ef6ab Mon Sep 17 00:00:00 2001 From: MyungHyun Ryu Date: Thu, 7 Dec 2023 03:31:05 +0900 Subject: [PATCH] :white_check_mark: Add test for image downloader --- .../data/remote/ProxyImageDownloaderTest.kt | 78 +++++++++++++++++++ .../data/remote/RealImageDownloader.kt | 65 ++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/data/remote/ProxyImageDownloaderTest.kt create mode 100644 frontend/app/src/test/java/com/example/speechbuddy/data/remote/RealImageDownloader.kt diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/ProxyImageDownloaderTest.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/ProxyImageDownloaderTest.kt new file mode 100644 index 00000000..fb2e49be --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/ProxyImageDownloaderTest.kt @@ -0,0 +1,78 @@ +package com.example.speechbuddy.data.remote + +import android.content.Context +import com.example.speechbuddy.domain.models.Symbol +import io.mockk.coVerify +import io.mockk.every +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.File + +class ProxyImageDownloaderTest { + + private val mockRealImageDownloader: RealImageDownloader = mockk(relaxed = true) + private val mockContext: Context = mockk() + private val proxyImageDownloader = ProxyImageDownloader(mockRealImageDownloader, mockContext) + + @get:Rule + val tempFolder = TemporaryFolder() + + @Before + fun setUp() { + mockkStatic(File::class) + } + + @After + fun tearDown() { + unmockkAll() + } + + @Test + fun `checkImage should download image if not present and id is greater than 500`() { + // Arrange + val symbolList = + listOf(Symbol(501, "symbol", "http://example.com/image.png", 1, false, true)) + val filesDir = mockk() + val mockFile: File = mockk() + + every { mockContext.filesDir } returns filesDir + every { filesDir.listFiles() } returns arrayOf(mockFile) + every { mockFile.name } returns "other_file.png" + + // Act + proxyImageDownloader.checkImage(symbolList) + + // Assert + coVerify { + mockRealImageDownloader.downloadImage( + "http://example.com/image.png", + "symbol_501.png" + ) + } + } + + @Test + fun `checkImage should not download image if present`() { + // Arrange + val symbolList = + listOf(Symbol(501, "symbol", "http://example.com/image.png", 1, false, true)) + val filesDir = mockk() + val mockFile: File = mockk() + + every { mockContext.filesDir } returns filesDir + every { filesDir.listFiles() } returns arrayOf(mockFile) + every { mockFile.name } returns "symbol_501.png" + + // Act + proxyImageDownloader.checkImage(symbolList) + + // Assert + coVerify(exactly = 0) { mockRealImageDownloader.downloadImage(any(), any()) } + } +} \ No newline at end of file diff --git a/frontend/app/src/test/java/com/example/speechbuddy/data/remote/RealImageDownloader.kt b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/RealImageDownloader.kt new file mode 100644 index 00000000..d2a3ded0 --- /dev/null +++ b/frontend/app/src/test/java/com/example/speechbuddy/data/remote/RealImageDownloader.kt @@ -0,0 +1,65 @@ +package com.example.speechbuddy.data.remote + +import android.content.Context +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import com.example.speechbuddy.service.BackupService +import io.mockk.coEvery +import io.mockk.coVerify +import io.mockk.mockk +import io.mockk.mockkStatic +import io.mockk.unmockkAll +import okhttp3.ResponseBody +import org.junit.After +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.io.ByteArrayInputStream +import java.io.InputStream + +class RealImageDownloaderTest { + + private val mockBackupService: BackupService = mockk() + private val mockContext: Context = mockk() + private val realImageDownloader = RealImageDownloader(mockBackupService, mockContext) + + @get:Rule + val tempFolder = TemporaryFolder() + + @Before + fun setUp() { + mockkStatic(BitmapFactory::class) + mockkStatic(Bitmap::class) + } + + @After + fun tearDown() { + unmockkAll() + } + + + @Test + fun `downloadImage should download and save image`() { + // Arrange + val url = "http://example.com/image.png" + val filename = "image.png" + val mockResponseBody: ResponseBody = mockk() + val mockBitmap: Bitmap = mockk() + val mockInputStream: InputStream = ByteArrayInputStream(ByteArray(0)) + val filesDir = tempFolder.newFolder() + + coEvery { mockContext.filesDir } returns filesDir + coEvery { mockResponseBody.byteStream() } returns mockInputStream + coEvery { BitmapFactory.decodeStream(mockInputStream) } returns mockBitmap + coEvery { mockBackupService.getImage(url) } returns mockResponseBody + coEvery { mockBitmap.compress(any(), any(), any()) } returns true + + // Act + realImageDownloader.downloadImage(url, filename) + + // Assert + coVerify { mockBackupService.getImage(url) } + coVerify { BitmapFactory.decodeStream(mockInputStream) } + } +} \ No newline at end of file