Skip to content

Commit

Permalink
Merge pull request #244 from SwEnt-Group8/hotfix/fix-user-parks-not-u…
Browse files Browse the repository at this point in the history
…pdating

fix(LocationService): fixed currentpark not updating by taking park d…
  • Loading branch information
kzepfl authored Dec 19, 2024
2 parents 34396ec + 0adb0cf commit 24dc60b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class LocationService(

// Define the FusedLocationProviderClient to get location
private val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
private lateinit var currentParks: List<String>

fun startLocationUpdates(callback: LocationCallback) {

Expand Down Expand Up @@ -69,7 +70,8 @@ class LocationService(
user?.let {
// add new park in user list
userViewModel.getParksByUid(it.uid)
val currentParks = user.parks
currentParks = userViewModel.parks.value
Log.d("Localisation", "current user discovered park: $currentParks")
if (!currentParks.contains(park.id)) {
Log.d("Localisation", "park is new")
userViewModel.addNewPark(it.uid, park.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.google.android.gms.maps.model.LatLng
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
Expand All @@ -20,6 +21,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito
import org.mockito.Mockito.mock
import org.mockito.kotlin.whenever

@RunWith(AndroidJUnit4::class)
class LocationServiceTest {
Expand All @@ -38,7 +40,13 @@ class LocationServiceTest {
fun setup() {
Dispatchers.setMain(testDispatcher)
userRepository = mock(UserRepository::class.java)
userViewModel = UserViewModel(userRepository)
// Mock parks as a MutableStateFlow
val parksFlow = MutableStateFlow(listOf("oldPark"))
userViewModel =
mock(UserViewModel::class.java).apply {
whenever(parks).thenReturn(parksFlow) // Return the mocked flow
}

navigationActions = mock(NavigationActions::class.java)
scope = CoroutineScope(testDispatcher)
context = ApplicationProvider.getApplicationContext()
Expand All @@ -64,9 +72,9 @@ class LocationServiceTest {
testDispatcher.scheduler.advanceUntilIdle()

// Ensure check of parkID was called
Mockito.verify(userRepository, Mockito.times(1)).getParksByUid(user.uid)
Mockito.verify(userViewModel, Mockito.times(1)).getParksByUid(user.uid)
// Ensure addNewPark was called for "newPark"
Mockito.verify(userRepository, Mockito.times(1)).addNewPark(user.uid, "newPark")
Mockito.verify(userViewModel, Mockito.times(1)).addNewPark(user.uid, "newPark")
}

@Test
Expand All @@ -86,9 +94,9 @@ class LocationServiceTest {
testDispatcher.scheduler.advanceUntilIdle()

// Ensure check of parkID was called for nearby park
Mockito.verify(userRepository, Mockito.times(1)).getParksByUid(user.uid)
Mockito.verify(userViewModel, Mockito.times(1)).getParksByUid(user.uid)
// Ensure addNewPark was not called for "oldPark"
Mockito.verify(userRepository, Mockito.times(0)).addNewPark(user.uid, "oldPark")
Mockito.verify(userViewModel, Mockito.times(0)).addNewPark(user.uid, "oldPark")
}

@Test
Expand All @@ -108,10 +116,10 @@ class LocationServiceTest {
testDispatcher.scheduler.advanceUntilIdle()

// Ensure check of parkID was not called
Mockito.verify(userRepository, Mockito.times(0)).getParksByUid(user.uid)
Mockito.verify(userViewModel, Mockito.times(0)).getParksByUid(user.uid)
// Ensure addNewPark was not called for "farPark1"
Mockito.verify(userRepository, Mockito.times(0)).addNewPark(user.uid, "farPark1")
Mockito.verify(userViewModel, Mockito.times(0)).addNewPark(user.uid, "farPark1")
// Ensure addNewPark was not called for "farPark2"
Mockito.verify(userRepository, Mockito.times(0)).addNewPark(user.uid, "farPark2")
Mockito.verify(userViewModel, Mockito.times(0)).addNewPark(user.uid, "farPark2")
}
}

0 comments on commit 24dc60b

Please sign in to comment.