From 094e62c95bd26b1d87ad296c1fef69c9790c6bd3 Mon Sep 17 00:00:00 2001 From: Maxime Naturel Date: Wed, 9 Mar 2022 14:03:12 +0100 Subject: [PATCH] Adding unit tests for use case --- .../location/LocationSharingFragment.kt | 2 - .../domain/usecase/CompareLocationsUseCase.kt | 1 - .../usecase/CompareLocationsUseCaseTest.kt | 83 +++++++++++++++++++ 3 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 vector/src/test/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCaseTest.kt diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt index 130fe01c77..348be7d023 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingFragment.kt @@ -154,8 +154,6 @@ class LocationSharingFragment @Inject constructor( } private fun initOptionsPicker() { - // TODO - // unit tests // set no option at start views.shareLocationOptionsPicker.render() views.shareLocationOptionsPicker.optionPinned.debouncedClicks { diff --git a/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt b/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt index 3c3f1bb8f6..91738541be 100644 --- a/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt +++ b/vector/src/main/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCase.kt @@ -34,7 +34,6 @@ class CompareLocationsUseCase @Inject constructor( private val session: Session ) { - // TODO unit test /** * Compare the 2 given locations. * @return true when they are really close and could be considered as the same location, false otherwise diff --git a/vector/src/test/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCaseTest.kt b/vector/src/test/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCaseTest.kt new file mode 100644 index 0000000000..015a27b0c8 --- /dev/null +++ b/vector/src/test/java/im/vector/app/features/location/domain/usecase/CompareLocationsUseCaseTest.kt @@ -0,0 +1,83 @@ +/* + * 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.features.location.domain.usecase + +import com.airbnb.mvrx.test.MvRxTestRule +import im.vector.app.features.location.LocationData +import im.vector.app.test.fakes.FakeSession +import io.mockk.MockKAnnotations +import io.mockk.impl.annotations.OverrideMockKs +import kotlinx.coroutines.test.runBlockingTest +import org.junit.Before +import org.junit.Rule +import org.junit.Test + +class CompareLocationsUseCaseTest { + + @get:Rule + val mvRxTestRule = MvRxTestRule() + + private val session = FakeSession() + + @OverrideMockKs + lateinit var compareLocationsUseCase: CompareLocationsUseCase + + @Before + fun setUp() { + MockKAnnotations.init(this) + } + + @Test + fun `given 2 very near locations when calling execute then these locations are considered as equal`() = runBlockingTest { + // Given + val location1 = LocationData( + latitude = 48.858269, + longitude = 2.294551, + uncertainty = null + ) + val location2 = LocationData( + latitude = 48.858275, + longitude = 2.294547, + uncertainty = null + ) + // When + val areEqual = compareLocationsUseCase.execute(location1, location2) + + // Then + assert(areEqual) + } + + @Test + fun `given 2 far away locations when calling execute then these locations are considered as not equal`() = runBlockingTest { + // Given + val location1 = LocationData( + latitude = 48.858269, + longitude = 2.294551, + uncertainty = null + ) + val location2 = LocationData( + latitude = 48.861777, + longitude = 2.289348, + uncertainty = null + ) + // When + val areEqual = compareLocationsUseCase.execute(location1, location2) + + // Then + assert(areEqual.not()) + } +}