-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from snuhcs-course/test/image-downloader
Test: Add test for image downloader
- Loading branch information
Showing
2 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
frontend/app/src/test/java/com/example/speechbuddy/data/remote/ProxyImageDownloaderTest.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,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<File>() | ||
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<File>() | ||
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()) } | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
frontend/app/src/test/java/com/example/speechbuddy/data/remote/RealImageDownloader.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,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) } | ||
} | ||
} |