mirror of
https://github.com/element-hq/element-android
synced 2024-11-24 10:25:35 +03:00
Adding a FakeFile class for unit tests
This commit is contained in:
parent
0170171caa
commit
562780a169
2 changed files with 67 additions and 19 deletions
|
@ -23,6 +23,7 @@ import com.airbnb.mvrx.test.MvRxTestRule
|
||||||
import im.vector.app.core.intent.getMimeTypeFromUri
|
import im.vector.app.core.intent.getMimeTypeFromUri
|
||||||
import im.vector.app.core.utils.saveMedia
|
import im.vector.app.core.utils.saveMedia
|
||||||
import im.vector.app.features.notifications.NotificationUtils
|
import im.vector.app.features.notifications.NotificationUtils
|
||||||
|
import im.vector.app.test.fakes.FakeFile
|
||||||
import im.vector.app.test.fakes.FakeSession
|
import im.vector.app.test.fakes.FakeSession
|
||||||
import io.mockk.MockKAnnotations
|
import io.mockk.MockKAnnotations
|
||||||
import io.mockk.coEvery
|
import io.mockk.coEvery
|
||||||
|
@ -42,7 +43,6 @@ import org.junit.After
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class DownloadMediaUseCaseTest {
|
class DownloadMediaUseCaseTest {
|
||||||
|
|
||||||
|
@ -57,6 +57,8 @@ class DownloadMediaUseCaseTest {
|
||||||
@MockK
|
@MockK
|
||||||
lateinit var notificationUtils: NotificationUtils
|
lateinit var notificationUtils: NotificationUtils
|
||||||
|
|
||||||
|
private val file = FakeFile()
|
||||||
|
|
||||||
@OverrideMockKs
|
@OverrideMockKs
|
||||||
lateinit var downloadMediaUseCase: DownloadMediaUseCase
|
lateinit var downloadMediaUseCase: DownloadMediaUseCase
|
||||||
|
|
||||||
|
@ -65,72 +67,69 @@ class DownloadMediaUseCaseTest {
|
||||||
MockKAnnotations.init(this)
|
MockKAnnotations.init(this)
|
||||||
mockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
mockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
||||||
mockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
mockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
||||||
mockkStatic(Uri::class)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
fun tearDown() {
|
fun tearDown() {
|
||||||
unmockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
unmockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
||||||
unmockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
unmockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
||||||
unmockkStatic(Uri::class)
|
file.tearDown()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given a file when calling execute then save the file in local with success`() = runBlockingTest {
|
fun `given a file when calling execute then save the file in local with success`() = runBlockingTest {
|
||||||
// Given
|
// Given
|
||||||
val file = mockk<File>()
|
|
||||||
val uri = mockk<Uri>()
|
val uri = mockk<Uri>()
|
||||||
val mimeType = "mimeType"
|
val mimeType = "mimeType"
|
||||||
val name = "name"
|
val name = "filename"
|
||||||
every { file.name } returns name
|
|
||||||
every { file.toUri() } returns uri
|
|
||||||
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
||||||
|
file.givenName(name)
|
||||||
|
file.givenUri(uri)
|
||||||
coEvery { saveMedia(any(), any(), any(), any(), any()) } just runs
|
coEvery { saveMedia(any(), any(), any(), any(), any()) } just runs
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val result = downloadMediaUseCase.execute(file)
|
val result = downloadMediaUseCase.execute(file.instance)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assert(result.isSuccess)
|
assert(result.isSuccess)
|
||||||
verifyAll {
|
verifyAll {
|
||||||
file.name
|
file.instance.name
|
||||||
file.toUri()
|
file.instance.toUri()
|
||||||
}
|
}
|
||||||
verify {
|
verify {
|
||||||
getMimeTypeFromUri(appContext, uri)
|
getMimeTypeFromUri(appContext, uri)
|
||||||
}
|
}
|
||||||
coVerify {
|
coVerify {
|
||||||
saveMedia(appContext, file, name, mimeType, notificationUtils)
|
saveMedia(appContext, file.instance, name, mimeType, notificationUtils)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `given a file when calling execute then save the file in local with error`() = runBlockingTest {
|
fun `given a file when calling execute then save the file in local with error`() = runBlockingTest {
|
||||||
// Given
|
// Given
|
||||||
val file = mockk<File>()
|
|
||||||
val uri = mockk<Uri>()
|
val uri = mockk<Uri>()
|
||||||
val mimeType = "mimeType"
|
val mimeType = "mimeType"
|
||||||
val name = "name"
|
val name = "filename"
|
||||||
val error = Throwable()
|
val error = Throwable()
|
||||||
every { file.name } returns name
|
file.givenName(name)
|
||||||
every { file.toUri() } returns uri
|
file.givenUri(uri)
|
||||||
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
||||||
coEvery { saveMedia(any(), any(), any(), any(), any()) } throws error
|
coEvery { saveMedia(any(), any(), any(), any(), any()) } throws error
|
||||||
|
|
||||||
// When
|
// When
|
||||||
val result = downloadMediaUseCase.execute(file)
|
val result = downloadMediaUseCase.execute(file.instance)
|
||||||
|
|
||||||
// Then
|
// Then
|
||||||
assert(result.isFailure && result.exceptionOrNull() == error)
|
assert(result.isFailure && result.exceptionOrNull() == error)
|
||||||
verifyAll {
|
verifyAll {
|
||||||
file.name
|
file.instance.name
|
||||||
file.toUri()
|
file.instance.toUri()
|
||||||
}
|
}
|
||||||
verify {
|
verify {
|
||||||
getMimeTypeFromUri(appContext, uri)
|
getMimeTypeFromUri(appContext, uri)
|
||||||
}
|
}
|
||||||
coVerify {
|
coVerify {
|
||||||
saveMedia(appContext, file, name, mimeType, notificationUtils)
|
saveMedia(appContext, file.instance, name, mimeType, notificationUtils)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
49
vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt
Normal file
49
vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2022 New Vector Ltd
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package im.vector.app.test.fakes
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import androidx.core.net.toUri
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.mockkStatic
|
||||||
|
import io.mockk.unmockkStatic
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class FakeFile {
|
||||||
|
|
||||||
|
val instance = mockk<File>()
|
||||||
|
|
||||||
|
init {
|
||||||
|
mockkStatic(Uri::class)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To be called after tests.
|
||||||
|
*/
|
||||||
|
fun tearDown() {
|
||||||
|
unmockkStatic(Uri::class)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenName(name: String) {
|
||||||
|
every { instance.name } returns name
|
||||||
|
}
|
||||||
|
|
||||||
|
fun givenUri(uri: Uri) {
|
||||||
|
every { instance.toUri() } returns uri
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue