Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yoan committed May 29, 2024
1 parent 5d7d998 commit 03dea5d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -87,6 +101,7 @@ class CreateProfileTest {
.assertHasClickAction()
composeTestRule.onNodeWithTag("profile-picture-delete").performClick()
assertNull(changedPicture)
composeTestRule.onNodeWithTag("profile-picture-image").performClick()
}

@Test
Expand All @@ -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<NetworkService>()
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)
}
}

0 comments on commit 03dea5d

Please sign in to comment.