Skip to content

Commit

Permalink
Merge pull request #250 from SwEnt-Group13/feat/event-creation-offline
Browse files Browse the repository at this point in the history
Feat/event creation offline
  • Loading branch information
Zafouche authored Dec 5, 2024
2 parents 79be37e + af3ceb1 commit 97aa42b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ 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.performScrollTo
import androidx.core.content.ContextCompat
import androidx.core.content.ContextCompat.getSystemService
import androidx.navigation.NavHostController
import androidx.test.core.app.ApplicationProvider
import com.android.unio.R
import com.android.unio.TearDown
Expand All @@ -35,6 +35,7 @@ import com.android.unio.model.user.UserViewModel
import com.android.unio.ui.association.AssociationProfileScaffold
import com.android.unio.ui.association.AssociationProfileScreen
import com.android.unio.ui.navigation.NavigationAction
import com.android.unio.ui.navigation.Screen
import com.google.android.gms.tasks.Task
import com.google.firebase.Firebase
import com.google.firebase.firestore.CollectionReference
Expand All @@ -55,13 +56,13 @@ import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.verify
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`
import org.mockito.kotlin.any
import org.mockito.kotlin.verify

@HiltAndroidTest
@UninstallModules(FirebaseModule::class)
Expand All @@ -70,7 +71,7 @@ class AssociationProfileTest : TearDown() {
private lateinit var associations: List<Association>
private lateinit var events: List<Event>

private lateinit var navigationAction: NavigationAction
@MockK private lateinit var navigationAction: NavigationAction
private lateinit var eventViewModel: EventViewModel
private lateinit var userViewModel: UserViewModel
private lateinit var associationViewModel: AssociationViewModel
Expand Down Expand Up @@ -110,6 +111,10 @@ class AssociationProfileTest : TearDown() {
`when`(task.addOnSuccessListener(any())).thenReturn(task)
`when`(task.addOnFailureListener(any())).thenReturn(task)

// Mock the navigation action to do nothing
every { navigationAction.navigateTo(any<String>()) } returns Unit
every { navigationAction.goBack() } returns Unit

associations =
listOf(
MockAssociation.createMockAssociation(uid = "1"),
Expand All @@ -132,8 +137,6 @@ class AssociationProfileTest : TearDown() {
profilePicture = "",
)

navigationAction = NavigationAction(mock(NavHostController::class.java))

every { eventRepository.init(any()) } answers { (args[0] as () -> Unit).invoke() }

every { eventRepository.getEvents(any(), any()) } answers
Expand Down Expand Up @@ -338,11 +341,9 @@ class AssociationProfileTest : TearDown() {
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
}

`when`(navigationAction.navController.popBackStack()).thenReturn(true)

composeTestRule.onNodeWithTag(AssociationProfileTestTags.GO_BACK_BUTTON).performClick()

verify(navigationAction.navController).popBackStack()
verify { navigationAction.goBack() }
}

@Test
Expand Down Expand Up @@ -371,6 +372,42 @@ class AssociationProfileTest : TearDown() {
composeTestRule.onNodeWithTag(AssociationProfileTestTags.SCREEN).assertIsNotDisplayed()
}

@Test
fun testAddEventButtonOnline() {
every { connectivityManager?.activeNetwork } returns mockk<Network>()

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
}

composeTestRule.onNodeWithTag(AssociationProfileTestTags.ADD_EVENT_BUTTON).assertIsDisplayed()
composeTestRule
.onNodeWithTag(AssociationProfileTestTags.ADD_EVENT_BUTTON)
.performScrollTo()
.performClick()

verify { navigationAction.navigateTo(Screen.EVENT_CREATION) }
}

@Test
fun testAddEventButtonOffline() {
every { connectivityManager?.activeNetwork } returns null

composeTestRule.setContent {
AssociationProfileScaffold(
navigationAction, userViewModel, eventViewModel, associationViewModel) {}
}

composeTestRule.onNodeWithTag(AssociationProfileTestTags.ADD_EVENT_BUTTON).assertIsDisplayed()
composeTestRule
.onNodeWithTag(AssociationProfileTestTags.ADD_EVENT_BUTTON)
.performScrollTo()
.performClick()

verify(exactly = 0) { navigationAction.navigateTo(Screen.EVENT_CREATION) }
}

@Module
@InstallIn(SingletonComponent::class)
object FirebaseTestModule {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ private fun AssociationEvents(
eventViewModel: EventViewModel
) {
val context = LocalContext.current
val isConnected = Utils.checkInternetConnection(context)

var isSeeMoreClicked by remember { mutableStateOf(false) }

Expand Down Expand Up @@ -496,7 +497,15 @@ private fun AssociationEvents(
}
if (isAdmin) {
Button(
onClick = { navigationAction.navigateTo(Screen.EVENT_CREATION) },
onClick = {
if (isConnected) {
navigationAction.navigateTo(Screen.EVENT_CREATION)
} else {
Toast.makeText(
context, context.getString(R.string.no_internet_connection), Toast.LENGTH_SHORT)
.show()
}
},
modifier = Modifier.testTag(AssociationProfileTestTags.ADD_EVENT_BUTTON),
contentPadding = ButtonDefaults.ButtonWithIconContentPadding) {
Icon(
Expand Down

0 comments on commit 97aa42b

Please sign in to comment.