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 b332764ad..efb13d9b2 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 @@ -569,7 +569,8 @@ fun PictureTransformer( translationX = offset.x, translationY = offset.y ) - .transformable(state = state), + .transformable(state = state) + .testTag("profile-picture-image"), painter = BitmapPainter(picture.asImageBitmap()), contentDescription = "" ) 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 79ceeeb23..0a6db0151 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 @@ -1,19 +1,33 @@ package com.github.swent.echo.compose.authentication import android.graphics.Bitmap +import androidx.compose.ui.geometry.Offset import androidx.compose.ui.test.assertHasClickAction import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.assertPositionInRootIsEqualTo import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.performTouchInput +import androidx.compose.ui.test.swipe +import androidx.compose.ui.unit.dp import androidx.test.ext.junit.runners.AndroidJUnit4 +import com.github.swent.echo.authentication.AuthenticationService +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.repository.SimpleRepository +import com.github.swent.echo.viewmodels.authentication.CreateProfileViewModel +import io.mockk.every import io.mockk.mockk +import java.io.ByteArrayOutputStream +import junit.framework.TestCase import junit.framework.TestCase.assertNull import junit.framework.TestCase.assertTrue +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.runBlocking import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith @@ -87,6 +101,7 @@ class CreateProfileTest { .assertHasClickAction() composeTestRule.onNodeWithTag("profile-picture-delete").performClick() assertNull(changedPicture) + composeTestRule.onNodeWithTag("profile-picture-image").performClick() } @Test @@ -108,4 +123,40 @@ class CreateProfileTest { composeTestRule.onNodeWithTag("profile-picture-transformer-cancel").performClick() assertTrue(canceled) } + + @Test + fun profileCreationPictureTransformerGestureTest() { + val picture = Bitmap.createBitmap(300, 1000, Bitmap.Config.ARGB_8888) + var changedPicture: Bitmap = picture + composeTestRule.setContent { + PictureTransformer( + picture = picture, + onConfirm = { changedPicture = it }, + onCancel = {} + ) + } + val image = composeTestRule.onNodeWithTag("profile-picture-image") + image.assertIsDisplayed() + image.performTouchInput { this.swipe(Offset.Zero, Offset(50f, 50f)) } + image.assertPositionInRootIsEqualTo(15.dp, 15.dp) + } + + // this test needs to be run there because of the Bitmap class which require android + @Test + fun setProfilePictureViewModelWithBitmapTest() { + val authenticationService: AuthenticationService = mockk(relaxed = true) + val repository = SimpleRepository(authenticationService) + val mockedNetworkService = mockk() + every { mockedNetworkService.isOnline } returns MutableStateFlow(true) + var viewModel = + CreateProfileViewModel(authenticationService, repository, mockedNetworkService) + val picture = Bitmap.createBitmap(300, 300, Bitmap.Config.ARGB_8888) + viewModel.setPicture(picture) + TestCase.assertEquals(picture, viewModel.picture.value) + val outputStream = ByteArrayOutputStream() + picture.compress(Bitmap.CompressFormat.JPEG, 100, outputStream) + runBlocking { repository.setUserProfilePicture("", outputStream.toByteArray()) } + viewModel = CreateProfileViewModel(authenticationService, repository, mockedNetworkService) + TestCase.assertEquals(picture.byteCount, viewModel.picture.value?.byteCount) + } }