diff --git a/app/src/main/java/com/github/swent/echo/compose/authentication/CreateProfile.kt b/app/src/main/java/com/github/swent/echo/compose/authentication/CreateProfile.kt index 68cf9012c..93cbae458 100644 --- a/app/src/main/java/com/github/swent/echo/compose/authentication/CreateProfile.kt +++ b/app/src/main/java/com/github/swent/echo/compose/authentication/CreateProfile.kt @@ -244,7 +244,12 @@ fun ProfileCreationUI( modifier = Modifier.fillMaxWidth() ) }, - snackbarHost = { SnackbarHost(hostState = snackbarHostState) } + snackbarHost = { + SnackbarHost( + hostState = snackbarHostState, + modifier = Modifier.testTag("profile-creation-snackbar") + ) + } ) { innerPadding -> Box( modifier = modifier.fillMaxSize().padding(innerPadding).testTag("profile-creation"), diff --git a/app/src/test/java/com/github/swent/echo/compose/authentication/CreateProfileTest.kt b/app/src/test/java/com/github/swent/echo/compose/authentication/CreateProfileTest.kt index 4c6e5791f..07863bf2b 100644 --- a/app/src/test/java/com/github/swent/echo/compose/authentication/CreateProfileTest.kt +++ b/app/src/test/java/com/github/swent/echo/compose/authentication/CreateProfileTest.kt @@ -18,8 +18,14 @@ import com.github.swent.echo.connectivity.NetworkService import com.github.swent.echo.data.model.SectionEPFL import com.github.swent.echo.data.model.SemesterEPFL import com.github.swent.echo.data.model.Tag +import com.github.swent.echo.data.model.UserProfile +import com.github.swent.echo.data.repository.RepositoryImpl +import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException import com.github.swent.echo.data.repository.SimpleRepository +import com.github.swent.echo.ui.navigation.NavigationActions import com.github.swent.echo.viewmodels.authentication.CreateProfileViewModel +import com.github.swent.echo.viewmodels.tag.TagViewModel +import io.mockk.coEvery import io.mockk.every import io.mockk.mockk import java.io.ByteArrayOutputStream @@ -84,6 +90,34 @@ class CreateProfileTest { composeTestRule.onNodeWithTag("BA2").assertExists() } + @Test + fun `profile creation while network error shows error snackbar`() { + val authenticationService: AuthenticationService = mockk(relaxed = true) + val mockedRepository = mockk() + val mockedNetworkService = mockk() + every { mockedNetworkService.isOnline } returns MutableStateFlow(true) + coEvery { mockedRepository.getUserProfile(any()) } returns UserProfile.EMPTY + coEvery { mockedRepository.getUserProfilePicture(any()) } returns ByteArray(0) + + val viewModel = + CreateProfileViewModel(authenticationService, mockedRepository, mockedNetworkService) + + val mockedNavAction = mockk() + val mockedTagViewModel = mockk() + + composeTestRule.setContent { + ProfileCreationScreen( + viewModel = viewModel, + navAction = mockedNavAction, + tagviewModel = mockedTagViewModel + ) + } + coEvery { mockedRepository.setUserProfile(any()) } throws + RepositoryStoreWhileNoInternetException("test") + composeTestRule.onNodeWithTag("Save").performClick() + composeTestRule.onNodeWithTag("profile-creation-snackbar").assertIsDisplayed() + } + @Test fun profileCreationPictureIsCorrect() { val picture = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888) diff --git a/app/src/test/java/com/github/swent/echo/viewmodels/association/AssociationViewModelTest.kt b/app/src/test/java/com/github/swent/echo/viewmodels/association/AssociationViewModelTest.kt index 8641e22d5..9683d6208 100644 --- a/app/src/test/java/com/github/swent/echo/viewmodels/association/AssociationViewModelTest.kt +++ b/app/src/test/java/com/github/swent/echo/viewmodels/association/AssociationViewModelTest.kt @@ -11,6 +11,7 @@ import com.github.swent.echo.data.model.Tag import com.github.swent.echo.data.model.UserProfile import com.github.swent.echo.data.model.toAssociationHeader import com.github.swent.echo.data.repository.Repository +import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException import com.github.swent.echo.fakes.FakeAuthenticationService import io.mockk.coEvery import io.mockk.coVerify @@ -145,6 +146,13 @@ class AssociationViewModelTest { assert(associationViewModel.followedAssociations.value.contains(associationList[2])) associationViewModel.onFollowAssociationChanged(associationList[2]) assert(!associationViewModel.followedAssociations.value.contains(associationList[2])) + coEvery { mockedRepository.setUserProfile(any()) } throws RepositoryStoreWhileNoInternetException("test") + associationViewModel.onFollowAssociationChanged(associationList[2]) + scheduler.runCurrent() + assert(!associationViewModel.followedAssociations.value.contains(associationList[2])) + assert(associationViewModel.status.value is AssociationStatus.Error) + associationViewModel.resetErrorState() + assert(associationViewModel.status.value is AssociationStatus.Okay) } @Test diff --git a/app/src/test/java/com/github/swent/echo/viewmodels/event/EventViewModelTest.kt b/app/src/test/java/com/github/swent/echo/viewmodels/event/EventViewModelTest.kt index a048ca9a2..44727f2c7 100644 --- a/app/src/test/java/com/github/swent/echo/viewmodels/event/EventViewModelTest.kt +++ b/app/src/test/java/com/github/swent/echo/viewmodels/event/EventViewModelTest.kt @@ -12,6 +12,7 @@ import com.github.swent.echo.data.model.Location import com.github.swent.echo.data.model.Tag import com.github.swent.echo.data.model.UserProfile import com.github.swent.echo.data.repository.Repository +import com.github.swent.echo.data.repository.RepositoryStoreWhileNoInternetException import com.github.swent.echo.fakes.FakeAuthenticationService import io.mockk.coEvery import io.mockk.coVerify @@ -120,6 +121,7 @@ class EventViewModelTest { eventViewModel.setEvent(TEST_EVENT) eventViewModel.saveEvent() eventViewModel.saveEvent() + scheduler.runCurrent() verify { Log.w(any(), any() as String) } } @@ -132,6 +134,7 @@ class EventViewModelTest { ) eventViewModel.setEvent(event) eventViewModel.saveEvent() + scheduler.runCurrent() assertTrue(eventViewModel.status.value is EventStatus.Error) } @@ -140,6 +143,19 @@ class EventViewModelTest { val event = TEST_EVENT.copy(title = " ") eventViewModel.setEvent(event) eventViewModel.saveEvent() + scheduler.runCurrent() + assertTrue(eventViewModel.status.value is EventStatus.Error) + } + + @Test + fun saveWhileNetworkErrorChangesStatusToError() { + coEvery { mockedRepository.createEvent(TEST_EVENT) } throws + RepositoryStoreWhileNoInternetException("test") + coEvery { mockedRepository.setEvent(TEST_EVENT) } throws + RepositoryStoreWhileNoInternetException("test") + eventViewModel.setEvent(TEST_EVENT) + eventViewModel.saveEvent() + scheduler.runCurrent() assertTrue(eventViewModel.status.value is EventStatus.Error) }