Skip to content

Commit

Permalink
Added test for HomeScreenFragment (#2584)
Browse files Browse the repository at this point in the history
* added test

* remove unnecessary import

* string fix

---------

Co-authored-by: Shobhit Agarwal <ashobhit@google.com>
  • Loading branch information
anandwana001 and shobhitagarwal1612 authored Aug 2, 2024
1 parent 6731185 commit 105220b
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import com.google.android.ground.R
import com.google.android.ground.databinding.HomeScreenFragBinding
import com.google.android.ground.databinding.NavDrawerHeaderBinding
import com.google.android.ground.model.User
import com.google.android.ground.repository.LocationOfInterestRepository
import com.google.android.ground.repository.SurveyRepository
import com.google.android.ground.repository.UserRepository
import com.google.android.ground.ui.common.AbstractFragment
import com.google.android.ground.ui.common.BackPressListener
Expand All @@ -58,10 +56,7 @@ class HomeScreenFragment :
// TODO: It's not obvious which locations of interest are in HomeScreen vs MapContainer;
// make this more intuitive.

@Inject lateinit var locationOfInterestRepository: LocationOfInterestRepository
@Inject lateinit var userRepository: UserRepository
@Inject lateinit var surveyRepository: SurveyRepository

private lateinit var binding: HomeScreenFragBinding
private lateinit var homeScreenViewModel: HomeScreenViewModel
private lateinit var user: User
Expand Down Expand Up @@ -114,7 +109,7 @@ class HomeScreenFragment :
val navHeader = binding.navView.getHeaderView(0)
val headerBinding = NavDrawerHeaderBinding.bind(navHeader)
headerBinding.user = userRepository.getAuthenticatedUser()
surveyRepository.activeSurveyFlow.collect {
homeScreenViewModel.surveyRepository.activeSurveyFlow.collect {
if (it == null) {
headerBinding.surveyInfo.visibility = View.GONE
headerBinding.noSurveysInfo.visibility = View.VISIBLE
Expand Down Expand Up @@ -172,7 +167,7 @@ class HomeScreenFragment :
}
if (showSignOutDialog.value) {
SignOutConfirmationDialog(showUserDetailsDialog, showSignOutDialog) {
userRepository.signOut()
homeScreenViewModel.signOut()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.google.android.ground.persistence.local.room.converter.SubmissionDelt
import com.google.android.ground.repository.OfflineAreaRepository
import com.google.android.ground.repository.SubmissionRepository
import com.google.android.ground.repository.SurveyRepository
import com.google.android.ground.repository.UserRepository
import com.google.android.ground.ui.common.AbstractViewModel
import com.google.android.ground.ui.common.Navigator
import com.google.android.ground.ui.common.SharedViewModel
Expand All @@ -42,7 +43,8 @@ internal constructor(
private val navigator: Navigator,
private val offlineAreaRepository: OfflineAreaRepository,
private val submissionRepository: SubmissionRepository,
private val surveyRepository: SurveyRepository,
val surveyRepository: SurveyRepository,
val userRepository: UserRepository,
) : AbstractViewModel() {

private val _openDrawerRequests: MutableSharedFlow<Unit> = MutableSharedFlow()
Expand Down Expand Up @@ -116,4 +118,8 @@ internal constructor(
fun showTermsOfService() {
navigator.navigate(HomeScreenFragmentDirections.showTermsOfService(true))
}

fun signOut() {
viewModelScope.launch { userRepository.signOut() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@
package com.google.android.ground.ui.home

import android.content.Context
import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavDirections
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.swipeUp
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.DrawerMatchers.isClosed
import androidx.test.espresso.contrib.DrawerMatchers.isOpen
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
Expand All @@ -39,9 +47,11 @@ import com.sharedtest.FakeData
import com.squareup.picasso.Picasso
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlin.test.assertFalse
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.ParameterizedRobolectricTestRunner
Expand All @@ -50,7 +60,7 @@ import org.robolectric.RobolectricTestRunner
abstract class AbstractHomeScreenFragmentTest : BaseHiltTest() {

@Inject lateinit var localSurveyStore: LocalSurveyStore
private lateinit var fragment: HomeScreenFragment
lateinit var fragment: HomeScreenFragment
private var initializedPicasso = false

@Before
Expand Down Expand Up @@ -82,6 +92,10 @@ abstract class AbstractHomeScreenFragmentTest : BaseHiltTest() {
onView(withId(R.id.nav_view)).check(matches(ViewMatchers.isDisplayed()))
}

protected fun swipeUpDrawer() {
onView(withId(R.id.drawer_layout)).perform(swipeUp())
}

protected fun verifyDrawerOpen() {
computeScrollForDrawerLayout()
onView(withId(R.id.drawer_layout)).check(matches(isOpen()))
Expand Down Expand Up @@ -119,6 +133,12 @@ class HomeScreenFragmentTest : AbstractHomeScreenFragmentTest() {

@Inject lateinit var surveyRepository: SurveyRepository

/**
* composeTestRule has to be created in the specific test file in order to access the required
* activity. [composeTestRule.activity]
*/
@get:Rule override val composeTestRule = createAndroidComposeRule<ComponentActivity>()

private val surveyWithoutBasemap: Survey =
Survey(
"SURVEY",
Expand All @@ -129,12 +149,88 @@ class HomeScreenFragmentTest : AbstractHomeScreenFragmentTest() {
)

@Test
fun `offline map imagery menu is always enabled`() = runWithTestDispatcher {
fun `all menu item is always enabled`() = runWithTestDispatcher {
surveyRepository.selectedSurveyId = surveyWithoutBasemap.id
advanceUntilIdle()

openDrawer()
onView(withId(R.id.nav_offline_areas)).check(matches(isEnabled()))
onView(withId(R.id.sync_status)).check(matches(isEnabled()))
onView(withId(R.id.nav_settings)).check(matches(isEnabled()))
onView(withId(R.id.about)).check(matches(isEnabled()))

swipeUpDrawer()

onView(withId(R.id.terms_of_service)).check(matches(isEnabled()))
onView(withId(R.id.version_text)).check(matches(isDisplayed()))
}

@Test
fun `signOut dialog is Displayed`() = runWithTestDispatcher {
openDrawer()

onView(withId(R.id.user_image)).check(matches(isDisplayed()))
openSignOutDialog()

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.assertIsDisplayed()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close))
.assertIsDisplayed()

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close))
.performClick()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.close))
.assertIsNotDisplayed()

openSignOutWarningDialog()

advanceUntilIdle()

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out_dialog_title))
.assertIsDisplayed()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out_dialog_body))
.assertIsDisplayed()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.cancel))
.assertIsDisplayed()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.assertIsDisplayed()

composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.cancel))
.performClick()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.cancel))
.assertIsNotDisplayed()

openSignOutWarningDialog()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.performClick()
composeTestRule
.onNodeWithText(composeTestRule.activity.getString(R.string.sign_out))
.assertIsNotDisplayed()
}

@Test
fun `onBack() should return false and do nothing`() {
assertFalse(fragment.onBack())
}

private fun openSignOutDialog() {
onView(withId(R.id.user_image)).perform(click())
}

private fun openSignOutWarningDialog() {
openSignOutDialog()
composeTestRule.onNodeWithText("Sign out").performClick()
}
}

Expand Down

0 comments on commit 105220b

Please sign in to comment.