Skip to content

Commit

Permalink
Adds test cases for HomeScreenViewModel (#2600)
Browse files Browse the repository at this point in the history
  • Loading branch information
anandwana001 authored Aug 2, 2024
1 parent ded14ec commit 6731185
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2024 Google LLC
*
* 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
*
* https://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 com.google.android.ground.ui.home

import com.google.android.ground.BaseHiltTest
import com.google.android.ground.persistence.local.stores.LocalOfflineAreaStore
import com.google.android.ground.testNavigateTo
import com.google.android.ground.ui.common.Navigator
import com.sharedtest.FakeData.OFFLINE_AREA
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@HiltAndroidTest
@RunWith(RobolectricTestRunner::class)
class HomeScreenViewModelTest : BaseHiltTest() {

@Inject lateinit var navigator: Navigator
@Inject lateinit var homeScreenViewModel: HomeScreenViewModel
@Inject lateinit var localOfflineAreaStore: LocalOfflineAreaStore

@Test
fun `showAbout is navigating to correct screen`() = runWithTestDispatcher {
testNavigateTo(navigator.getNavigateRequests(), HomeScreenFragmentDirections.showAbout()) {
homeScreenViewModel.showAbout()
}
}

@Test
fun `showTermsOfService is navigating to correct screen`() = runWithTestDispatcher {
testNavigateTo(
navigator.getNavigateRequests(),
HomeScreenFragmentDirections.showTermsOfService(true),
) {
homeScreenViewModel.showTermsOfService()
}
}

@Test
fun `showSyncStatus is navigating to correct screen`() = runWithTestDispatcher {
testNavigateTo(navigator.getNavigateRequests(), HomeScreenFragmentDirections.showSyncStatus()) {
homeScreenViewModel.showSyncStatus()
}
}

@Test
fun `showSettings is navigating to correct screen`() = runWithTestDispatcher {
testNavigateTo(
navigator.getNavigateRequests(),
HomeScreenFragmentDirections.actionHomeScreenFragmentToSettingsActivity(),
) {
homeScreenViewModel.showSettings()
}
}

@Test
fun `showSurveySelector is navigating to correct screen`() = runWithTestDispatcher {
testNavigateTo(
navigator.getNavigateRequests(),
HomeScreenFragmentDirections.actionHomeScreenFragmentToSurveySelectorFragment(false),
) {
homeScreenViewModel.showSurveySelector()
}
}

@Test
fun `showOfflineAreas is navigating to correct screen when offlineAreas are not empty`() =
runWithTestDispatcher {
localOfflineAreaStore.insertOrUpdate(OFFLINE_AREA)
advanceUntilIdle()

testNavigateTo(
navigator.getNavigateRequests(),
HomeScreenFragmentDirections.showOfflineAreas(),
) {
homeScreenViewModel.showOfflineAreas()
}
}

@Test
fun `showOfflineAreas is navigating to correct screen when offlineAreas are empty`() =
runWithTestDispatcher {
testNavigateTo(
navigator.getNavigateRequests(),
HomeScreenFragmentDirections.showOfflineAreaSelector(),
) {
homeScreenViewModel.showOfflineAreas()
}
}
}
5 changes: 5 additions & 0 deletions sharedTest/src/main/kotlin/com/sharedtest/FakeData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.google.android.ground.model.geometry.LinearRing
import com.google.android.ground.model.geometry.MultiPolygon
import com.google.android.ground.model.geometry.Point
import com.google.android.ground.model.geometry.Polygon
import com.google.android.ground.model.imagery.OfflineArea
import com.google.android.ground.model.job.Job
import com.google.android.ground.model.job.Style
import com.google.android.ground.model.locationofinterest.LOI_NAME_PROPERTY
Expand All @@ -32,6 +33,7 @@ import com.google.android.ground.model.mutation.LocationOfInterestMutation
import com.google.android.ground.model.mutation.Mutation
import com.google.android.ground.model.task.MultipleChoice
import com.google.android.ground.model.task.Task
import com.google.android.ground.ui.map.Bounds
import com.google.android.ground.ui.map.Feature
import com.google.android.ground.ui.map.FeatureType
import com.google.android.ground.ui.map.gms.features.FeatureClusterItem
Expand Down Expand Up @@ -124,6 +126,9 @@ object FakeData {

val COORDINATES = Coordinates(42.0, 18.0)

val OFFLINE_AREA =
OfflineArea("id_1", OfflineArea.State.PENDING, Bounds(0.0, 0.0, 0.0, 0.0), "Test Area", 0..14)

fun newTask(
id: String = "",
type: Task.Type = Task.Type.TEXT,
Expand Down

0 comments on commit 6731185

Please sign in to comment.