From 562780a169c4150b297838b2c4736b61be8594bc Mon Sep 17 00:00:00 2001 From: Maxime Naturel Date: Mon, 28 Feb 2022 11:55:14 +0100 Subject: [PATCH] Adding a FakeFile class for unit tests --- .../usecase/DownloadMediaUseCaseTest.kt | 37 +++++++------- .../java/im/vector/app/test/fakes/FakeFile.kt | 49 +++++++++++++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt diff --git a/vector/src/test/java/im/vector/app/features/media/domain/usecase/DownloadMediaUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/media/domain/usecase/DownloadMediaUseCaseTest.kt index 44c0d2cfd0..2fa8c7d5f7 100644 --- a/vector/src/test/java/im/vector/app/features/media/domain/usecase/DownloadMediaUseCaseTest.kt +++ b/vector/src/test/java/im/vector/app/features/media/domain/usecase/DownloadMediaUseCaseTest.kt @@ -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() val uri = mockk() 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() val uri = mockk() 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) } } } diff --git a/vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt b/vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt new file mode 100644 index 0000000000..652d3f93fd --- /dev/null +++ b/vector/src/test/java/im/vector/app/test/fakes/FakeFile.kt @@ -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() + + 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 + } +}