Skip to content

Commit

Permalink
Merge pull request #105 from snuhcs-course/test/image-downloader
Browse files Browse the repository at this point in the history
Test: Add test for image downloader
  • Loading branch information
JH747 authored Dec 9, 2023
2 parents 1451dfa + 0c1e053 commit 06136fd
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
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()) }
}
}
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) }
}
}

0 comments on commit 06136fd

Please sign in to comment.