mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 04:52:00 +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.utils.saveMedia
|
||||
import im.vector.app.features.notifications.NotificationUtils
|
||||
import im.vector.app.test.fakes.FakeFile
|
||||
import im.vector.app.test.fakes.FakeSession
|
||||
import io.mockk.MockKAnnotations
|
||||
import io.mockk.coEvery
|
||||
|
@ -42,7 +43,6 @@ import org.junit.After
|
|||
import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
|
||||
class DownloadMediaUseCaseTest {
|
||||
|
||||
|
@ -57,6 +57,8 @@ class DownloadMediaUseCaseTest {
|
|||
@MockK
|
||||
lateinit var notificationUtils: NotificationUtils
|
||||
|
||||
private val file = FakeFile()
|
||||
|
||||
@OverrideMockKs
|
||||
lateinit var downloadMediaUseCase: DownloadMediaUseCase
|
||||
|
||||
|
@ -65,72 +67,69 @@ class DownloadMediaUseCaseTest {
|
|||
MockKAnnotations.init(this)
|
||||
mockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
||||
mockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
||||
mockkStatic(Uri::class)
|
||||
}
|
||||
|
||||
@After
|
||||
fun tearDown() {
|
||||
unmockkStatic("im.vector.app.core.utils.ExternalApplicationsUtilKt")
|
||||
unmockkStatic("im.vector.app.core.intent.VectorMimeTypeKt")
|
||||
unmockkStatic(Uri::class)
|
||||
file.tearDown()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a file when calling execute then save the file in local with success`() = runBlockingTest {
|
||||
// Given
|
||||
val file = mockk<File>()
|
||||
val uri = mockk<Uri>()
|
||||
val mimeType = "mimeType"
|
||||
val name = "name"
|
||||
every { file.name } returns name
|
||||
every { file.toUri() } returns uri
|
||||
val name = "filename"
|
||||
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
||||
file.givenName(name)
|
||||
file.givenUri(uri)
|
||||
coEvery { saveMedia(any(), any(), any(), any(), any()) } just runs
|
||||
|
||||
// When
|
||||
val result = downloadMediaUseCase.execute(file)
|
||||
val result = downloadMediaUseCase.execute(file.instance)
|
||||
|
||||
// Then
|
||||
assert(result.isSuccess)
|
||||
verifyAll {
|
||||
file.name
|
||||
file.toUri()
|
||||
file.instance.name
|
||||
file.instance.toUri()
|
||||
}
|
||||
verify {
|
||||
getMimeTypeFromUri(appContext, uri)
|
||||
}
|
||||
coVerify {
|
||||
saveMedia(appContext, file, name, mimeType, notificationUtils)
|
||||
saveMedia(appContext, file.instance, name, mimeType, notificationUtils)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `given a file when calling execute then save the file in local with error`() = runBlockingTest {
|
||||
// Given
|
||||
val file = mockk<File>()
|
||||
val uri = mockk<Uri>()
|
||||
val mimeType = "mimeType"
|
||||
val name = "name"
|
||||
val name = "filename"
|
||||
val error = Throwable()
|
||||
every { file.name } returns name
|
||||
every { file.toUri() } returns uri
|
||||
file.givenName(name)
|
||||
file.givenUri(uri)
|
||||
every { getMimeTypeFromUri(appContext, uri) } returns mimeType
|
||||
coEvery { saveMedia(any(), any(), any(), any(), any()) } throws error
|
||||
|
||||
// When
|
||||
val result = downloadMediaUseCase.execute(file)
|
||||
val result = downloadMediaUseCase.execute(file.instance)
|
||||
|
||||
// Then
|
||||
assert(result.isFailure && result.exceptionOrNull() == error)
|
||||
verifyAll {
|
||||
file.name
|
||||
file.toUri()
|
||||
file.instance.name
|
||||
file.instance.toUri()
|
||||
}
|
||||
verify {
|
||||
getMimeTypeFromUri(appContext, uri)
|
||||
}
|
||||
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