Skip to content

Commit

Permalink
Merge branch 'main' into fix/white-stars
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienBousquieEPFL committed Dec 16, 2024
2 parents c1d333f + d9864d6 commit e842848
Show file tree
Hide file tree
Showing 13 changed files with 652 additions and 96 deletions.
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ dependencies {
implementation(libs.androidx.navigation.runtime.ktx)
implementation(libs.androidx.navigation.testing)
implementation(libs.test.core.ktx)
implementation (libs.androidx.constraintlayout.compose)

//Camera
implementation (libs.androidx.camera.camera2)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.github.lookupgroup27.lookup.ui.image

import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performImeAction
import androidx.compose.ui.test.performTextClearance
import androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.github.lookupgroup27.lookup.model.collection.CollectionRepository
import com.github.lookupgroup27.lookup.model.image.EditImageRepository
Expand All @@ -17,7 +22,10 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify

/**
Expand Down Expand Up @@ -65,6 +73,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -85,6 +94,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -106,6 +116,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -124,6 +135,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -146,6 +158,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -166,6 +179,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -187,6 +201,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -205,6 +220,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -223,6 +239,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -241,6 +258,7 @@ class EditImageScreenTest {
postAverageStar = 0.0,
postRatedByNb = 0,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
Expand All @@ -249,4 +267,148 @@ class EditImageScreenTest {

composeTestRule.onNodeWithTag("rated_by_collection").assertIsDisplayed()
}

@Test
fun testDescriptionBoxIsDisplayed() {
composeTestRule.setContent {
EditImageScreen(
postUri = "mock_image_url",
postAverageStar = 4.5,
postRatedByNb = 20,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
postsViewModel = postsViewModel)
}

composeTestRule.onNodeWithTag("description_text").assertIsDisplayed()
}

@Test
fun testEditFieldAppearsOnClick() {
composeTestRule.setContent {
EditImageScreen(
postUri = "mock_image_url",
postAverageStar = 4.5,
postRatedByNb = 20,
postUid = "mock_uid",
postDescription = "mock_description",
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
postsViewModel = postsViewModel)
}

// Simulate clicking the description box
composeTestRule.onNodeWithTag("description_text").performClick()

// Verify that the edit field appears
composeTestRule.onNodeWithTag("edit_description_field").assertIsDisplayed()
}

@Test
fun testOnDoneKeyboardActionDisplaysConfirmationDialogAndSavesDescription() {
val testPostUid = "mock_uid"
val initialDescription = "mock_description"
val updatedDescription = "updated_description"

composeTestRule.setContent {
EditImageScreen(
postUri = "mock_image_url",
postAverageStar = 4.5,
postRatedByNb = 20,
postUid = testPostUid,
postDescription = initialDescription,
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
postsViewModel = postsViewModel)
}

// Click on the description text to enter editing mode
composeTestRule.onNodeWithTag("description_text").performClick()

// Verify the edit field appears
composeTestRule.onNodeWithTag("edit_description_field").assertIsDisplayed()

// Input a new description (resetting any previous value first)
composeTestRule.onNodeWithTag("edit_description_field").performTextClearance()
composeTestRule.onNodeWithTag("edit_description_field").performTextInput(updatedDescription)

// Simulate the "Done" action
composeTestRule.onNodeWithTag("edit_description_field").performImeAction()

// Verify that the confirmation dialog appears
composeTestRule.onNodeWithText("Save Changes?").assertIsDisplayed()
composeTestRule.onNodeWithText("Do you want to save the new description?").assertIsDisplayed()

// Click on the "Save" button in the dialog
composeTestRule.onNodeWithText("Save").performClick()

// Verify that `updateDescription` is called with correct arguments
verify(postsRepository).updateDescription(eq(testPostUid), eq(updatedDescription), any(), any())

// Verify that the description text displays the updated description
composeTestRule.onNodeWithTag("description_text").assertTextEquals(updatedDescription)

// Verify that the editing mode has exited
composeTestRule.onNodeWithTag("edit_description_field").assertDoesNotExist()

// Ensure the confirmation dialog is dismissed
composeTestRule.onNodeWithText("Save Changes?").assertDoesNotExist()
}

@Test
fun testOnDoneKeyboardActionDisplaysConfirmationDialogAndDiscardsDescription() {
val testPostUid = "mock_uid"
val initialDescription = "mock_description"
val updatedDescription = "updated_description"

composeTestRule.setContent {
EditImageScreen(
postUri = "mock_image_url",
postAverageStar = 4.5,
postRatedByNb = 20,
postUid = testPostUid,
postDescription = initialDescription,
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
navigationActions = mockNavigationActions,
postsViewModel = postsViewModel)
}

// Click on the description text to enter editing mode
composeTestRule.onNodeWithTag("description_text").performClick()

// Verify the edit field appears
composeTestRule.onNodeWithTag("edit_description_field").assertIsDisplayed()

// Input a new description (resetting any previous value first)
composeTestRule.onNodeWithTag("edit_description_field").performTextClearance()
composeTestRule.onNodeWithTag("edit_description_field").performTextInput(updatedDescription)

// Simulate the "Done" action
composeTestRule.onNodeWithTag("edit_description_field").performImeAction()

// Verify that the confirmation dialog appears
composeTestRule.onNodeWithText("Save Changes?").assertIsDisplayed()
composeTestRule.onNodeWithText("Do you want to save the new description?").assertIsDisplayed()

// Click on the "Discard" button in the dialog
composeTestRule.onNodeWithText("Discard").performClick()

// Verify that `updateDescription` is NOT called
verify(postsRepository, times(0)).updateDescription(any(), any(), any(), any())

// Verify that the description text displays the initial description
composeTestRule.onNodeWithTag("description_text").assertTextEquals(initialDescription)

// Verify that the editing mode has exited
composeTestRule.onNodeWithTag("edit_description_field").assertDoesNotExist()

// Ensure the confirmation dialog is dismissed
composeTestRule.onNodeWithText("Save Changes?").assertDoesNotExist()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ class CollectionScreenTest {
postAverageStar: Float,
postRatedByNb: Int,
postUid: String,
postDescription: String,
route: String
) {
if (route == Route.EDIT_IMAGE &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,20 @@ fun LookUpApp() {
}

composable(
route = "${Route.EDIT_IMAGE}/{postUri}/{postAverageStar}/{postRatedByNb}/{postUid}",
route =
"${Route.EDIT_IMAGE}/{postUri}/{postAverageStar}/{postRatedByNb}/{postUid}/{postDescription}",
arguments =
listOf(
navArgument("postUri") { type = NavType.StringType },
navArgument("postAverageStar") { type = NavType.FloatType },
navArgument("postRatedByNb") { type = NavType.IntType },
navArgument("postUid") { type = NavType.StringType })) { backStackEntry ->
navArgument("postUid") { type = NavType.StringType },
navArgument("postDescription") { type = NavType.StringType })) { backStackEntry ->
val postUri = backStackEntry.arguments?.getString("postUri") ?: ""
val postAverageStar = backStackEntry.arguments?.getFloat("postAverageStar") ?: 0.0f
val postRatedByNb = backStackEntry.arguments?.getInt("postRatedByNb") ?: 0
val postUid = backStackEntry.arguments?.getString("postUid") ?: ""
val postDescription = backStackEntry.arguments?.getString("postDescription") ?: ""

EditImageScreen(
postUri = postUri,
Expand All @@ -164,6 +167,7 @@ fun LookUpApp() {
editImageViewModel = editImageViewModel,
collectionViewModel = collectionViewModel,
postsViewModel = postsViewModel,
postDescription = postDescription,
navigationActions = navigationActions)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ interface PostsRepository {
* @param onFailure Callback function invoked with an exception if the operation fails.
*/
fun updatePost(post: Post, onSuccess: () -> Unit, onFailure: (Exception) -> Unit)

/**
* Updates the description of an existing post in the repository.
*
* @param postUid The UID of the post to update.
* @param onSuccess Callback function invoked when the operation is successful.
* @param onFailure Callback function invoked with an exception if the operation fails.
*/
fun updateDescription(
postUid: String,
newDescription: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,23 @@ class PostsRepositoryFirestore(private val db: FirebaseFirestore) : PostsReposit
onFailure(it)
}
}

override fun updateDescription(
postUid: String,
newDescription: String,
onSuccess: () -> Unit,
onFailure: (Exception) -> Unit
) {
collection
.document(postUid)
.update("description", newDescription)
.addOnSuccessListener {
Log.d(tag, "Description updated successfully")
onSuccess()
}
.addOnFailureListener {
Log.e(tag, "Error updating description", it)
onFailure(it)
}
}
}
Loading

0 comments on commit e842848

Please sign in to comment.