Skip to content

Commit

Permalink
fixed ui added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anandwana001 committed Nov 28, 2024
1 parent 480ab6a commit 1d03cde
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ package com.google.android.ground.ui.offlineareas.selector

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.AlertDialog
Expand All @@ -32,38 +32,35 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.android.ground.R

@Composable
fun DownloadProgressDialog(viewModel: OfflineAreaSelectorViewModel, onDismiss: () -> Unit) {
val progress by viewModel.downloadProgress.observeAsState(0)
val maxProgress by viewModel.downloadProgressMax.observeAsState(100)
val progress by viewModel.downloadProgress.observeAsState(0f)

AlertDialog(
containerColor = MaterialTheme.colorScheme.surface,
onDismissRequest = {},

Check warning on line 47 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt#L47

Added line #L47 was not covered by tests
title = {
Text(
stringResource(R.string.offline_map_imagery_download_progress_dialog_title, progress),
stringResource(
R.string.offline_map_imagery_download_progress_dialog_title,
(progress * 100).toInt(),
),
color = MaterialTheme.colorScheme.onSurface,
)

Check warning on line 55 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/DownloadProgressDialog.kt#L55

Added line #L55 was not covered by tests
},
text = {
Column {
val animatedProgress by
animateFloatAsState(
targetValue = progress.toFloat() / maxProgress,
animationSpec = tween(durationMillis = 300),
)
animateFloatAsState(targetValue = progress, animationSpec = tween(durationMillis = 300))

LinearProgressIndicator(
modifier =
Modifier.background(
shape = RoundedCornerShape(4.dp),
color = MaterialTheme.colorScheme.primary,
),
modifier = Modifier.fillMaxWidth().clip(RoundedCornerShape(8.dp)).testTag("progressBar"),
progress = { animatedProgress },
color = MaterialTheme.colorScheme.primary,
trackColor = MaterialTheme.colorScheme.surfaceVariant,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ internal constructor(
private val offlineAreaSizeLoadingSymbol =
resources.getString(R.string.offline_area_size_loading_symbol)
val isDownloadProgressVisible = MutableLiveData(false)
val downloadProgressMax = MutableLiveData(0)
val downloadProgress = MutableLiveData(0)
val downloadProgress = MutableLiveData(0f)
val bottomText = MutableLiveData<String?>(null)
val downloadButtonEnabled = MutableLiveData(false)

Expand All @@ -94,13 +93,16 @@ internal constructor(
}

isDownloadProgressVisible.value = true
downloadProgress.value = 0
downloadProgress.value = 0f

Check warning on line 96 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L96

Added line #L96 was not covered by tests
viewModelScope.launch(ioDispatcher) {
offlineAreaRepository.downloadTiles(viewport!!).collect { (bytesDownloaded, totalBytes) ->
// Set total bytes / max value on first iteration.
if (downloadProgressMax.value != totalBytes) downloadProgressMax.postValue(totalBytes)
// Add number of bytes downloaded to progress.
downloadProgress.postValue(bytesDownloaded)
val progressValue =

Check warning on line 99 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L99

Added line #L99 was not covered by tests
if (totalBytes > 0) {
(bytesDownloaded.toFloat() / totalBytes.toFloat()).coerceIn(0f, 1f)

Check warning on line 101 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L101

Added line #L101 was not covered by tests
} else {
0f

Check warning on line 103 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L103

Added line #L103 was not covered by tests
}
downloadProgress.postValue(progressValue)

Check warning on line 105 in ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt

View check run for this annotation

Codecov / codecov/patch

ground/src/main/java/com/google/android/ground/ui/offlineareas/selector/OfflineAreaSelectorViewModel.kt#L105

Added line #L105 was not covered by tests
}
isDownloadProgressVisible.postValue(false)
navigator.navigate(OfflineAreaSelectorFragmentDirections.offlineAreaBackToHomescreen())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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.offlineareas.selector

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.google.android.ground.BaseHiltTest
import com.google.android.ground.R
import dagger.hilt.android.testing.HiltAndroidTest
import javax.inject.Inject
import kotlin.test.Test
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

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

@get:Rule override val composeTestRule = createAndroidComposeRule<ComponentActivity>()

@Inject lateinit var viewModel: OfflineAreaSelectorViewModel

@Test
fun downloadProgressDialog_DisplaysTitleCorrectly() {
composeTestRule.setContent { DownloadProgressDialog(viewModel) {} }

composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(
R.string.offline_map_imagery_download_progress_dialog_title,
0,
)
)
.assertIsDisplayed()
}

@Test
fun downloadProgressDialog_DisplaysCorrectMessage() {
composeTestRule.setContent { DownloadProgressDialog(viewModel) {} }

composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(
R.string.offline_map_imagery_download_progress_dialog_message
)
)
.assertIsDisplayed()
}

@Test
fun downloadProgressDialog_CallsOnDismissOnDismissButtonClick() {
var isDismissed = false

composeTestRule.setContent { DownloadProgressDialog(viewModel) { isDismissed = true } }

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

assertTrue(isDismissed)
}

@Test
fun downloadProgressDialog_DisplaysCorrectTitleForProgress() {
viewModel.downloadProgress.value = 0.5f

composeTestRule.setContent { DownloadProgressDialog(viewModel) {} }

composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(
R.string.offline_map_imagery_download_progress_dialog_title,
50,
)
)
.assertIsDisplayed()

viewModel.downloadProgress.value = 1f

composeTestRule
.onNodeWithText(
composeTestRule.activity.getString(
R.string.offline_map_imagery_download_progress_dialog_title,
100,
)
)
.assertIsDisplayed()
}
}

0 comments on commit 1d03cde

Please sign in to comment.