Skip to content

Commit

Permalink
Merge branch 'main' into feature/post-c
Browse files Browse the repository at this point in the history
  • Loading branch information
othbcq authored Dec 19, 2024
2 parents 4af0abb + f01eaa0 commit 76a257a
Show file tree
Hide file tree
Showing 70 changed files with 2,033 additions and 544 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.github.lookupgroup27.lookup

import android.location.Location
import androidx.test.core.app.ApplicationProvider
import com.github.lookupgroup27.lookup.model.location.LocationProvider

/** TestLocationProvider allows for manual setting of location values. */
class TestLocationProvider : LocationProvider(ApplicationProvider.getApplicationContext()) {
fun setLocation(latitude: Double?, longitude: Double?) {
if (latitude != null && longitude != null) {
currentLocation.value =
Location("test").apply {
this.latitude = latitude
this.longitude = longitude
}
} else {
currentLocation.value = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollTo
import androidx.test.core.app.ApplicationProvider
import androidx.test.rule.GrantPermissionRule
import com.github.lookupgroup27.lookup.TestLocationProvider
import com.github.lookupgroup27.lookup.model.location.LocationProvider
import com.github.lookupgroup27.lookup.model.location.LocationProviderSingleton
import com.github.lookupgroup27.lookup.model.post.Post
import com.github.lookupgroup27.lookup.model.post.PostsRepository
import com.github.lookupgroup27.lookup.model.profile.ProfileRepository
Expand All @@ -22,6 +24,8 @@ import com.github.lookupgroup27.lookup.ui.navigation.TopLevelDestinations
import com.github.lookupgroup27.lookup.ui.post.PostsViewModel
import com.github.lookupgroup27.lookup.ui.profile.ProfileViewModel
import com.google.firebase.auth.FirebaseAuth
import io.mockk.every
import io.mockk.mockkObject
import org.junit.Before
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -64,38 +68,43 @@ class FeedScreenTest {
// Mock posts
val testPosts =
listOf(
Post(
Post( // Post created by the logged-in user
uid = "1",
uri = "http://example.com/1.jpg",
username = testUserProfile.email, // Post created by the logged-in user
username = testUserProfile.email,
userMail = testUserProfile.email,
latitude = 37.7749, // San Francisco
longitude = -122.4194,
description = "This is a test description"),
Post(
uid = "2",
uri = "http://example.com/2.jpg",
username = "User2", // Post created by another user
userMail = "User2",
latitude = 34.0522, // Los Angeles
longitude = -118.2437,
description = "This is another test description"),
Post(
uid = "3",
uri = "http://example.com/3.jpg",
username = "User3", // Another user's post
username = "User3",
userMail = "User3", // Another user's post
latitude = 36.7783, // Fresno (closer to SF)
longitude = -119.4179,
description = "This is yet another test description"),
Post(
uid = "4",
uri = "User4",
username = "user4@example.com", // Another user's post
userMail = "User4",
latitude = 40.7128, // New York City (farther from SF than LA or Fresno)
longitude = -74.0060,
description = "This is a test description"),
Post(
uid = "5",
uri = "User5",
username = "user5@example.com", // Another user's post
userMail = "User5",
latitude = -33.8688, // Sydney, Australia (farthest from SF)
longitude = 151.2093,
description = "This is a test description"))
Expand Down Expand Up @@ -138,18 +147,30 @@ class FeedScreenTest {
`when`(navigationActions.currentRoute()).thenReturn(Screen.FEED)

locationProvider = LocationProvider(context, mutableStateOf(null))
}

/**
* Helper function to set up the FeedScreen with the given list of nearby posts.
*
* @param initialNearbyPosts The list of posts to display initially on the feed screen.
*
* This function simplifies test setup by allowing each test to specify the initial state of the
* feed. It handles the rendering of the FeedScreen with the specified posts and ensures a
* consistent setup across all tests.
*/
private fun setFeedScreenContent(initialNearbyPosts: List<Post>) {
composeTestRule.setContent {
FeedScreen(
postsViewModel = postsViewModel,
navigationActions = navigationActions,
profileViewModel = profileViewModel,
initialNearbyPosts = testPosts)
initialNearbyPosts = initialNearbyPosts)
}
}

@Test
fun testFeedScreenDisplaysNearbyPosts() {
setFeedScreenContent(testPosts)

// Assert each post item is displayed
composeTestRule.onNodeWithTag("PostItem_2").assertExists()
Expand All @@ -163,12 +184,14 @@ class FeedScreenTest {

@Test
fun testFeedExcludesLoggedInUserPosts() {
setFeedScreenContent(testPosts)
// Assert that the post by the logged-in user is not displayed
composeTestRule.onNodeWithTag("PostItem_1").assertDoesNotExist()
}

@Test
fun testBottomNavigationMenuIsDisplayed() {
setFeedScreenContent(testPosts)

// Verify the bottom navigation menu is displayed
composeTestRule
Expand All @@ -179,6 +202,7 @@ class FeedScreenTest {

@Test
fun testStarClickDisplaysAverageRating() {
setFeedScreenContent(testPosts)
// Perform click on the first star icon of a post with uid "1"
composeTestRule
.onNodeWithTag("Star_2_2")
Expand All @@ -191,6 +215,7 @@ class FeedScreenTest {

@Test
fun testStarClickCallsUpdatePost() {
setFeedScreenContent(testPosts)
// Perform click on the first star of post with uid "1"
composeTestRule.onNodeWithTag("Star_2_2").performClick()
postsViewModel.updatePost(testPost)
Expand All @@ -211,6 +236,7 @@ class FeedScreenTest {

@Test
fun testNavigationToFeedBlockedForLoggedOutUser() {
setFeedScreenContent(testPosts)
// Mock the user as not logged in
mockAuth = org.mockito.kotlin.mock()
whenever(mockAuth.currentUser).thenReturn(null)
Expand All @@ -224,15 +250,74 @@ class FeedScreenTest {

@Test
fun testAddressIsDisplayed() {
setFeedScreenContent(testPosts)
// Verify that the address is displayed for each post
composeTestRule.onNodeWithTag("AddressTag_2").performScrollTo().assertIsDisplayed()
composeTestRule.onNodeWithTag("AddressTag_5").assertDoesNotExist()
}

@Test
fun testDescriptionIsDisplayed() {
setFeedScreenContent(testPosts)
// Verify that the description is displayed for each post
composeTestRule.onNodeWithTag("DescriptionTag_2").performScrollTo().assertIsDisplayed()
composeTestRule.onNodeWithTag("DescriptionTag_5").assertDoesNotExist()
}

@Test
fun testFeedDisplaysNoImagesMessageWhenPostsAreEmpty() {
// Arrange: Mock location provider and permissions
val testLocationProvider = TestLocationProvider()
testLocationProvider.setLocation(37.7749, -122.4194) // Mocked location

mockkObject(LocationProviderSingleton)
every { LocationProviderSingleton.getInstance(any()) } returns testLocationProvider

// Act: Render the FeedScreen with no posts
setFeedScreenContent(emptyList())

// Wait for the location to emit
composeTestRule.waitForIdle()

// Assert: "No images available" message is displayed
composeTestRule.onNodeWithTag("feed_no_images_available").assertExists().assertIsDisplayed()
}

@Test
fun testFeedDisplaysLoadingIndicatorWhenLocationIsNull() {
// Arrange: Mock location provider and permissions
val testLocationProvider = TestLocationProvider()
testLocationProvider.setLocation(null, null) // No location emitted

mockkObject(LocationProviderSingleton)
every { LocationProviderSingleton.getInstance(any()) } returns testLocationProvider

// Act: Render the FeedScreen
setFeedScreenContent(emptyList())

// Wait for the composition to stabilize
composeTestRule.waitForIdle()

// Assert: Loading indicator (CircularProgressIndicator) is displayed
composeTestRule.onNodeWithTag("loading_indicator_test_tag").assertExists().assertIsDisplayed()
}

@Test
fun testFeedDisplaysNoImagesMessageWithPlaceholderImage() {
// Arrange: Mock location provider and permissions
val testLocationProvider = TestLocationProvider()
testLocationProvider.setLocation(37.7749, -122.4194) // Mocked location

mockkObject(LocationProviderSingleton)
every { LocationProviderSingleton.getInstance(any()) } returns testLocationProvider

// Act: Render the FeedScreen with no posts
setFeedScreenContent(emptyList())

// Wait for any updates to complete
composeTestRule.waitForIdle()

// Assert: Placeholder image is displayed
composeTestRule.onNodeWithTag("no_images_placeholder").assertExists().assertIsDisplayed()
}
}
Loading

0 comments on commit 76a257a

Please sign in to comment.