Skip to content

Commit

Permalink
Merge pull request #296 from SwEnt-Group13/refactor/fix-viewmodels
Browse files Browse the repository at this point in the history
Refactor/fix viewmodels
  • Loading branch information
armouldr authored Dec 13, 2024
2 parents 3e73b45 + 9f7fbd9 commit ecd2db5
Show file tree
Hide file tree
Showing 28 changed files with 526 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.android.unio.model.event.EventRepository
import com.android.unio.model.event.EventUserPictureRepositoryFirestore
import com.android.unio.model.event.EventViewModel
import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.search.SearchRepository
import com.android.unio.model.search.SearchViewModel
import com.android.unio.model.strings.test_tags.navigation.NavigationActionTestTags
Expand Down Expand Up @@ -36,6 +37,9 @@ class BottomNavigationTest : TearDown() {
@MockK private lateinit var associationRepositoryFirestore: AssociationRepositoryFirestore
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore

private lateinit var userRepository: UserRepository
private lateinit var userViewModel: UserViewModel
Expand All @@ -54,7 +58,8 @@ class BottomNavigationTest : TearDown() {
eventRepository,
imageRepository,
associationRepositoryFirestore,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)

userRepository = mock { UserRepositoryFirestore::class.java }
userViewModel = UserViewModel(userRepository, imageRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.android.unio.model.image.ImageViewModel
import com.android.unio.model.map.MapViewModel
import com.android.unio.model.map.nominatim.NominatimLocationRepository
import com.android.unio.model.map.nominatim.NominatimLocationSearchViewModel
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.search.SearchRepository
import com.android.unio.model.search.SearchViewModel
import com.android.unio.model.strings.test_tags.association.AssociationProfileTestTags
Expand Down Expand Up @@ -113,6 +114,9 @@ class ScreenDisplayingTest : TearDown() {
@MockK private lateinit var imageRepositoryFirestore: ImageRepositoryFirebaseStorage
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore

private lateinit var imageViewModel: ImageViewModel

Expand Down Expand Up @@ -161,7 +165,8 @@ class ScreenDisplayingTest : TearDown() {
eventRepository,
imageRepositoryFirestore,
associationRepositoryFirestore,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)
eventViewModel.loadEvents()
eventViewModel.selectEvent(events.first().uid)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.android.unio.model.firestore.firestoreReferenceListWith
import com.android.unio.model.follow.ConcurrentAssociationUserRepositoryFirestore
import com.android.unio.model.hilt.module.FirebaseModule
import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.strings.test_tags.association.AssociationProfileTestTags
import com.android.unio.model.user.User
import com.android.unio.model.user.UserRepositoryFirestore
Expand Down Expand Up @@ -96,6 +97,9 @@ class AssociationProfileTest : TearDown() {
@MockK private lateinit var imageRepository: ImageRepositoryFirebaseStorage
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore

@MockK private lateinit var connectivityManager: ConnectivityManager

Expand Down Expand Up @@ -216,7 +220,8 @@ class AssociationProfileTest : TearDown() {
eventRepository,
imageRepository,
associationRepository,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)

every { associationRepository.init(any()) } answers { firstArg<() -> Unit>().invoke() }
every { associationRepository.getAssociations(any(), any()) } answers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.android.unio.model.event.EventUserPictureRepositoryFirestore
import com.android.unio.model.event.EventViewModel
import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.notification.NotificationWorker
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.strings.test_tags.event.EventCardTestTags
import com.android.unio.model.strings.test_tags.event.EventDetailsTestTags
import com.android.unio.model.user.UserRepositoryFirestore
Expand Down Expand Up @@ -77,6 +78,9 @@ class EventCardTest : TearDown() {
@MockK private lateinit var associationRepository: AssociationRepositoryFirestore
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore
private lateinit var context: Context

@Before
Expand All @@ -88,13 +92,15 @@ class EventCardTest : TearDown() {
val user = MockUser.createMockUser(followedAssociations = associations, savedEvents = listOf())
every { NotificationWorker.schedule(any(), any()) } just runs
every { NotificationWorker.unschedule(any(), any()) } just runs

eventViewModel =
spyk(
EventViewModel(
eventRepository,
imageRepository,
associationRepository,
eventUserPictureRepositoryFirestore))
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore))
userViewModel = UserViewModel(userRepository, imageRepository)
every { userRepository.updateUser(user, any(), any()) } answers
{
Expand All @@ -115,8 +121,17 @@ class EventCardTest : TearDown() {
}
}

private fun setEventViewModel(events: List<Event>) {
every { eventRepository.getEvents(any(), any()) } answers
{
(it.invocation.args[0] as (List<Event>) -> Unit)(events)
}
eventViewModel.loadEvents()
}

@Test
fun testEventCardElementsExist() {
setEventViewModel(listOf(sampleEvent))
setEventScreen(sampleEvent)

composeTestRule
Expand Down Expand Up @@ -159,7 +174,9 @@ class EventCardTest : TearDown() {

@Test
fun testClickOnEventCard() {
setEventViewModel(listOf(sampleEvent))
setEventScreen(sampleEvent)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_TITLE, useUnmergedTree = true)
.assertIsDisplayed()
Expand All @@ -169,6 +186,7 @@ class EventCardTest : TearDown() {

@Test
fun testImageFallbackDisplayed() {
setEventViewModel(listOf(sampleEvent))
setEventScreen(sampleEvent)

// Check if the fallback image is displayed when no image is provided
Expand All @@ -180,7 +198,9 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithEmptyUid() {
val event = MockEvent.createMockEvent(uid = MockEvent.Companion.EdgeCaseUid.EMPTY.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_TITLE, useUnmergedTree = true)
.assertIsDisplayed() // Ensure the title exists
Expand All @@ -189,7 +209,9 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithEmptyTitle() {
val event = MockEvent.createMockEvent(title = MockEvent.Companion.EdgeCaseTitle.EMPTY.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_TITLE, useUnmergedTree = true)
.assertTextEquals(MockEvent.Companion.EdgeCaseTitle.EMPTY.value)
Expand All @@ -198,7 +220,9 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithInvalidImage() {
val event = MockEvent.createMockEvent(image = MockEvent.Companion.EdgeCaseImage.INVALID.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_IMAGE, useUnmergedTree = true)
.assertExists() // Expect image to use fallback
Expand All @@ -207,7 +231,9 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithValidImage() {
val event = MockEvent.createMockEvent(image = imgUrl)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_IMAGE, useUnmergedTree = true)
.assertExists() // Expect image to use fallback
Expand All @@ -218,7 +244,9 @@ class EventCardTest : TearDown() {
val event =
MockEvent.createMockEvent(
catchyDescription = MockEvent.Companion.EdgeCaseCatchyDescription.EMPTY.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_CATCHY_DESCRIPTION, useUnmergedTree = true)
.assertTextEquals("") // Expect empty catchy description
Expand All @@ -230,7 +258,9 @@ class EventCardTest : TearDown() {
MockEvent.createMockEvent(
catchyDescription =
MockEvent.Companion.EdgeCaseCatchyDescription.SPECIAL_CHARACTERS.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_CATCHY_DESCRIPTION, useUnmergedTree = true)
.assertTextEquals(MockEvent.Companion.EdgeCaseCatchyDescription.SPECIAL_CHARACTERS.value)
Expand All @@ -242,7 +272,10 @@ class EventCardTest : TearDown() {
MockEvent.createMockEvent(
startDate = MockEvent.Companion.EdgeCaseDate.PAST.value,
endDate = MockEvent.Companion.EdgeCaseDate.PAST.value)

setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_DATE, useUnmergedTree = true)
.assertExists()
Expand All @@ -251,7 +284,9 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithTodayStartDate() {
val event = MockEvent.createMockEvent(startDate = MockEvent.Companion.EdgeCaseDate.TODAY.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_DATE, useUnmergedTree = true)
.assertExists()
Expand All @@ -260,13 +295,15 @@ class EventCardTest : TearDown() {
@Test
fun testEventCardWithFutureStartDate() {
val event = MockEvent.createMockEvent(startDate = MockEvent.Companion.EdgeCaseDate.FUTURE.value)
setEventViewModel(listOf(event))
setEventScreen(event)

composeTestRule
.onNodeWithTag(EventCardTestTags.EVENT_DATE, useUnmergedTree = true)
.assertExists()
}

@Test
/*@Test
fun testEventCardSaveAndUnsaveEventOnline() {
var indicator = false
every { eventViewModel.updateEventWithoutImage(any(), any(), any()) } answers
Expand All @@ -277,17 +314,18 @@ class EventCardTest : TearDown() {
MockEvent.createMockEvent(
startDate = Timestamp(Date((Timestamp.now().seconds + 4 * 3600) * 1000)))
setEventViewModel(listOf(event))
setEventScreen(event)
composeTestRule.onNodeWithTag(EventDetailsTestTags.SAVE_BUTTON).assertExists().performClick()
Thread.sleep(500)
assert(indicator)
Thread.sleep(3000)
verify { NotificationWorker.schedule(any(), any()) }
composeTestRule.onNodeWithTag(EventDetailsTestTags.SAVE_BUTTON).assertExists().performClick()
composeTestRule.waitForIdle()
assert(!indicator)
}
}*/

@After
override fun tearDown() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.map.nominatim.NominatimApiService
import com.android.unio.model.map.nominatim.NominatimLocationRepository
import com.android.unio.model.map.nominatim.NominatimLocationSearchViewModel
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.search.SearchRepository
import com.android.unio.model.search.SearchViewModel
import com.android.unio.model.strings.TextLengthSamples
Expand Down Expand Up @@ -80,6 +81,9 @@ class EventCreationTest : TearDown() {
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore
@MockK
private lateinit var concurrentAssociationUserRepositoryFirestore:
ConcurrentAssociationUserRepositoryFirestore

Expand Down Expand Up @@ -111,7 +115,8 @@ class EventCreationTest : TearDown() {
eventRepository,
imageRepositoryFirestore,
associationRepositoryFirestore,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)

searchViewModel = spyk(SearchViewModel(searchRepository))
associationViewModel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.android.unio.model.event.EventRepositoryFirestore
import com.android.unio.model.event.EventUserPictureRepositoryFirestore
import com.android.unio.model.event.EventViewModel
import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.strings.test_tags.event.EventDetailsTestTags
import com.android.unio.model.user.User
import com.android.unio.ui.event.EventDetailsPicturePicker
Expand All @@ -33,6 +34,9 @@ class EventDetailsPicturePickerTest : TearDown() {
@MockK private lateinit var associationRepository: AssociationRepositoryFirestore
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore

private lateinit var eventViewModel: EventViewModel

Expand All @@ -51,7 +55,8 @@ class EventDetailsPicturePickerTest : TearDown() {
eventRepository,
imageRepository,
associationRepository,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.android.unio.model.event.EventUtils.formatTimestamp
import com.android.unio.model.event.EventViewModel
import com.android.unio.model.image.ImageRepositoryFirebaseStorage
import com.android.unio.model.map.MapViewModel
import com.android.unio.model.save.ConcurrentEventUserRepositoryFirestore
import com.android.unio.model.strings.FormatStrings.DAY_MONTH_FORMAT
import com.android.unio.model.strings.test_tags.event.EventDetailsTestTags
import com.android.unio.model.user.User
Expand Down Expand Up @@ -66,6 +67,9 @@ class EventDetailsTest : TearDown() {
@MockK private lateinit var imageRepository: ImageRepositoryFirebaseStorage
@MockK
private lateinit var eventUserPictureRepositoryFirestore: EventUserPictureRepositoryFirestore
@MockK
private lateinit var concurrentEventUserRepositoryFirestore:
ConcurrentEventUserRepositoryFirestore

private lateinit var eventViewModel: EventViewModel
private lateinit var userViewModel: UserViewModel
Expand Down Expand Up @@ -111,8 +115,13 @@ class EventDetailsTest : TearDown() {
eventRepository,
imageRepository,
associationRepository,
eventUserPictureRepositoryFirestore)
eventUserPictureRepositoryFirestore,
concurrentEventUserRepositoryFirestore)

every { eventRepository.getEvents(any(), any()) } answers
{
(it.invocation.args[0] as (List<Event>) -> Unit)(events)
}
every { userRepository.init(any()) } returns Unit
every { userRepository.getUserWithId("uid", any(), any()) } answers
{
Expand Down Expand Up @@ -223,12 +232,15 @@ class EventDetailsTest : TearDown() {
@Test
fun testButtonBehavior() {
setEventScreen(events[0])
eventViewModel.loadEvents()
// Share button
composeTestRule
.onNodeWithTag(EventDetailsTestTags.SHARE_BUTTON)
.assertDisplayComponentInScroll()

// Save button
println(events[0].uid)
println(eventViewModel.events.value)
composeTestRule.onNodeWithTag(EventDetailsTestTags.SAVE_BUTTON).assertDisplayComponentInScroll()
composeTestRule.onNodeWithTag(EventDetailsTestTags.SAVE_BUTTON).performClick()

Expand Down
Loading

0 comments on commit ecd2db5

Please sign in to comment.