Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertHasNoClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsEnabled
import androidx.compose.ui.test.assertIsNotEnabled
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.getUnclippedBoundsInRoot
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.isDialog
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
Expand All @@ -25,9 +27,15 @@ import androidx.compose.ui.test.performTextReplacement
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.controller.AppControllers
import com.example.lambda_bbb_events.controller.ReviewController
import com.example.lambda_bbb_events.controller.ReviewReactionController
import com.example.lambda_bbb_events.controller.ReviewReplyController
import com.example.lambda_bbb_events.model.campsite.Campsite
import com.example.lambda_bbb_events.model.repository.ReviewReactionRepository
import com.example.lambda_bbb_events.model.repository.ReviewReplyRepository
import com.example.lambda_bbb_events.model.repository.ReviewRepository
import com.example.lambda_bbb_events.model.review.Review
import com.example.lambda_bbb_events.model.review.ReviewReaction
import com.example.lambda_bbb_events.model.review.ReviewReply
import com.example.lambda_bbb_events.model.user.Admin
import com.example.lambda_bbb_events.model.user.CampsiteOwner
import com.example.lambda_bbb_events.model.user.Entrant
Expand All @@ -41,10 +49,12 @@ import io.mockk.mockk
import io.mockk.unmockkAll
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Date
import kotlin.math.abs

/**
* Tests reading, writing, editing, and deleting reviews on a campsite's page. The repository is
Expand All @@ -62,12 +72,25 @@ class ReviewSectionUITest {
/** The stored reviews. Compose state, like the repository's cache. */
private var stored by mutableStateOf<List<Review>>(emptyList())

private val replyRepository = mockk<ReviewReplyRepository>()
private val reactionRepository = mockk<ReviewReactionRepository>()
private var storedReplies by mutableStateOf<List<ReviewReply>>(emptyList())
private var storedReactions by mutableStateOf<List<ReviewReaction>>(emptyList())

private val campsite = Campsite(campsiteId = "site-1", ownerId = "owner-1", campsiteName = "Lakeside")
private val ed = Entrant(deviceId = "user-1", name = "Ed")

private fun review(userId: String, rating: Int, comment: String = "", day: Long = 1) =
Review("site-1_$userId", "site-1", userId, rating, comment, Date(day * 86_400_000))

private fun centerOf(tag: String): Pair<Float, Float> {
val bounds = composeTestRule.onNodeWithTag(tag).getUnclippedBoundsInRoot()
return (bounds.left + bounds.right).value / 2 to (bounds.top + bounds.bottom).value / 2
}

private fun reactionBy(userId: String, emoji: String) =
ReviewReaction(ReviewReaction.idFor("site-1_user-3", userId), "site-1_user-3", userId, emoji)

private fun showSection(signedIn: User?, vararg reviews: Review) {
stored = reviews.toList()
every { repository.getReviewsForCampsite("site-1") } answers { stored }
Expand All @@ -82,6 +105,22 @@ class ReviewSectionUITest {
stored = stored.filter { it.reviewId != firstArg() }
}
every { controllers.reviews } returns ReviewController(repository)
storedReplies = emptyList()
storedReactions = emptyList()
every { replyRepository.newReplyId() } returns "reply-1"
every { replyRepository.getRepliesFor(any()) } answers { storedReplies.filter { it.reviewId == firstArg() } }
coEvery { replyRepository.add(any()) } answers { storedReplies = storedReplies + firstArg<ReviewReply>() }
every { reactionRepository.getReactionsFor(any()) } answers { storedReactions.filter { it.reviewId == firstArg() } }
every { reactionRepository.getCached(any()) } answers { storedReactions.find { it.reactionId == firstArg() } }
coEvery { reactionRepository.add(any()) } answers {
val saved = firstArg<ReviewReaction>()
storedReactions = storedReactions.filter { it.reactionId != saved.reactionId } + saved
}
coEvery { reactionRepository.delete(any<String>()) } answers {
storedReactions = storedReactions.filter { it.reactionId != firstArg() }
}
every { controllers.reviewReplies } returns ReviewReplyController(replyRepository)
every { controllers.reviewReactions } returns ReviewReactionController(reactionRepository)
every { controllers.users.currentUser } returns signedIn
every { controllers.users.getCachedUser(any()) } returns null
every { controllers.users.getCachedUser("user-1") } returns ed
Expand All @@ -106,6 +145,109 @@ class ReviewSectionUITest {
clearAllMocks()
}

@Test
fun emojiTrayStaysHiddenUntilTheReactButtonIsTapped() {
showSection(ed, review("user-3", 5, "Lovely"))

composeTestRule.onNodeWithTag("emojiTray").assertDoesNotExist()
click("addReaction")
composeTestRule.onNodeWithTag("emojiTray").assertIsDisplayed()
click("addReaction")
composeTestRule.onNodeWithTag("emojiTray").assertDoesNotExist()
}

@Test
fun tappedEmojiIsPlacedOnTheReviewAndTappingItRemovesIt() {
showSection(ed, review("user-3", 5, "Lovely"))

click("addReaction")
click("pickReaction_👍")
composeTestRule.onNodeWithTag("placedReaction_👍").assertIsDisplayed()
composeTestRule.onNodeWithTag("emojiTray").assertDoesNotExist()

click("placedReaction_👍")
composeTestRule.onNodeWithTag("placedReaction_👍").assertDoesNotExist()
}

@Test
fun sameEmojisShareOneSpotWithTheirCount() {
showSection(ed, review("user-3", 5, "Lovely"))
storedReactions = listOf(reactionBy("user-7", "😂"), reactionBy("user-8", "😂"), reactionBy("user-9", "😮"))

composeTestRule.onNodeWithTag("placedReaction_😂").assertTextContains("2", substring = true)
composeTestRule.onNodeWithTag("placedReaction_😮").assertIsDisplayed()
}

@Test
fun emojisStayPutWhenTheTrayOpensAndOnlyBottomOnesFollowTheBottomEdge() {
showSection(ed, review("user-3", 5, "Lovely"))
storedReactions = ReviewReaction.EMOJIS.mapIndexed { i, emoji -> reactionBy("user-${i + 10}", emoji) }
val before = ReviewReaction.EMOJIS.associateWith { centerOf("placedReaction_$it") }

click("addReaction")

val cardBottom = composeTestRule.onNodeWithTag("reviewCard").getUnclippedBoundsInRoot().bottom.value
val stayed = ReviewReaction.EMOJIS.filter { abs(centerOf("placedReaction_$it").second - cardBottom) > 1f }
assertTrue(stayed.isNotEmpty())
for (emoji in stayed) {
val (x, y) = centerOf("placedReaction_$emoji")
assertEquals(emoji, before.getValue(emoji).first, x, 1f)
assertEquals(emoji, before.getValue(emoji).second, y, 1f)
}
}

@Test
fun pickingTheSameEmojiFromTheTrayAgainRemovesIt() {
showSection(ed, review("user-3", 5, "Lovely"))

click("addReaction")
click("pickReaction_😮")
composeTestRule.onNodeWithTag("placedReaction_😮").assertIsDisplayed()

click("addReaction")
click("pickReaction_😮")
composeTestRule.onNodeWithTag("placedReaction_😮").assertDoesNotExist()
}

@Test
fun tappingSomeoneElsesEmojiAddsYoursToIt() {
showSection(ed, review("user-3", 5, "Lovely"))
storedReactions = listOf(reactionBy("user-9", "❤️"))

click("placedReaction_❤️")

composeTestRule.onNodeWithTag("placedReaction_❤️").assertTextContains("2", substring = true)
}

@Test
fun signedOutUsersCannotTapEmojis() {
showSection(null, review("user-3", 5, "Lovely"))
storedReactions = listOf(reactionBy("user-9", "❤️"))

composeTestRule.onNodeWithTag("placedReaction_❤️").assertIsDisplayed().assertHasNoClickAction()
}

@Test
fun replyIsShownUnderTheReviewWithItsAuthor() {
showSection(ed, review("user-3", 5, "Lovely"))

click("replyButton")
composeTestRule.onNodeWithTag("replyInput").performTextInput("Thanks for the tip")
click("sendReply")

composeTestRule.onNodeWithTag("replyItem").assertIsDisplayed()
composeTestRule.onNodeWithText("Thanks for the tip").assertIsDisplayed()
composeTestRule.onNodeWithTag("replyInput").assertDoesNotExist()
}

@Test
fun signedOutUsersCannotReactOrReply() {
showSection(null, review("user-3", 5, "Lovely"))

composeTestRule.onNodeWithTag("addReaction").assertDoesNotExist()
composeTestRule.onNodeWithTag("replyButton").assertDoesNotExist()
}

@Test
fun campsiteWithoutReviewsSaysSo() {
showSection(ed)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import com.example.lambda_bbb_events.model.services.PlaceSearch
* @property waitlistEntries Controller for waitlist operations.
* @property images Service for image processing.
* @property reviews Controller for campsite reviews.
* @property reviewReplies Controller for the replies under reviews.
* @property reviewReactions Controller for the emoji reactions to reviews.
* @property places Searches for places by name.
* @property messages Controller for messaging operations.
* @property notificationPreferences Controller for notification preferences.
Expand All @@ -31,6 +33,8 @@ class AppControllers(
val waitlistEntries: WaitlistEntryController,
val images: ImageProcessor,
val reviews: ReviewController,
val reviewReplies: ReviewReplyController = ReviewReplyController(),
val reviewReactions: ReviewReactionController = ReviewReactionController(),
val places: PlaceSearch = FallbackPlaceSearch(),
val notifications: NotificationController,
val messages: MessageController = MessageController(users),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package com.example.lambda_bbb_events.controller

import com.example.lambda_bbb_events.model.repository.ReviewReactionRepository
import com.example.lambda_bbb_events.model.review.Review
import com.example.lambda_bbb_events.model.review.ReviewReaction
import com.example.lambda_bbb_events.model.user.User

/**
* Controller for the emoji reactions to campsite reviews.
*
* @property reactionRepository The repository for [ReviewReaction].
*/
class ReviewReactionController(
val reactionRepository: ReviewReactionRepository = ReviewReactionRepository()
) {
/**
* Checks if a user can react to reviews. Anyone who is signed in can.
*
* @param user The user, or `null` if nobody is signed in.
*/
fun canReact(user: User?): Boolean = user != null

/**
* Counts how many users picked each emoji for a review, in the order of
* [ReviewReaction.EMOJIS]. Emojis nobody picked are left out.
*
* @param review The review.
*/
fun reactionCounts(review: Review): Map<String, Int> {
val reactions = reactionRepository.getReactionsFor(review.reviewId)
return ReviewReaction.EMOJIS
.associateWith { emoji -> reactions.count { it.emoji == emoji } }
.filterValues { it > 0 }
}

/**
* Gets a user's reaction to a review.
*
* @param review The review.
* @param user The user, or `null` if nobody is signed in.
* @return The reaction, or `null` if the user has not reacted.
*/
fun reactionOf(review: Review, user: User?): ReviewReaction? {
if (user == null) {
return null
}
return reactionRepository.getCached(ReviewReaction.idFor(review.reviewId, user.deviceId))
}

/**
* Reacts to a review with an emoji. A user has one reaction per review, so this replaces
* any emoji they picked before.
*
* @param review The review to react to.
* @param user The user reacting, or `null` if nobody is signed in.
* @param emoji The emoji picked, one of [ReviewReaction.EMOJIS].
* @return `true` if the reaction was saved, `false` otherwise.
*/
suspend fun react(review: Review, user: User?, emoji: String): Boolean {
if (user == null || emoji !in ReviewReaction.EMOJIS) {
return false
}
val reactionId = ReviewReaction.idFor(review.reviewId, user.deviceId)
return try {
reactionRepository.add(ReviewReaction(reactionId, review.reviewId, user.deviceId, emoji))
true
} catch (_: Exception) {
false
}
}

/**
* Removes a user's reaction from a review.
*
* @param review The review.
* @param user The user, or `null` if nobody is signed in.
* @return `true` if the reaction was removed, `false` otherwise.
*/
suspend fun removeReaction(review: Review, user: User?): Boolean {
if (user == null) {
return false
}
return try {
reactionRepository.delete(ReviewReaction.idFor(review.reviewId, user.deviceId))
true
} catch (_: Exception) {
false
}
}

/**
* Reacts to a review with an emoji, or removes the user's reaction if it is the emoji they
* already picked.
*
* @param review The review.
* @param user The user reacting, or `null` if nobody is signed in.
* @param emoji The emoji picked, one of [ReviewReaction.EMOJIS].
* @return `true` if the reaction was saved or removed, `false` otherwise.
*/
suspend fun toggleReaction(review: Review, user: User?, emoji: String): Boolean {
return if (reactionOf(review, user)?.emoji == emoji) {
removeReaction(review, user)
} else {
react(review, user, emoji)
}
}
}
Loading
Loading