Skip to content

Commit

Permalink
Merge branch 'main' into feature/signin-persistence-offline
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurChalard authored Dec 20, 2024
2 parents 2d36978 + 3d307d1 commit 718dcd0
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Portions of this code were generated and or inspired by the help of GitHub Copilot or Chatgpt
package com.github.se.travelpouch.ui.documents

import android.net.Uri
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
Expand All @@ -18,9 +20,12 @@ import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentReference
import java.time.LocalDate
import java.time.ZoneId
import kotlinx.coroutines.CompletableDeferred
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.anyString
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`

Expand All @@ -33,6 +38,7 @@ class DocumentListItemTest {
private lateinit var mockDocumentsManager: DocumentsManager
private lateinit var mockDataStore: DataStore<Preferences>
private lateinit var document: DocumentContainer
private lateinit var deferred: CompletableDeferred<Uri>

@get:Rule val composeTestRule = createComposeRule()

Expand All @@ -59,6 +65,10 @@ class DocumentListItemTest {
mockDocumentViewModel =
DocumentViewModel(mockDocumentRepository, mockDocumentsManager, mockDataStore)

deferred = CompletableDeferred()
`when`(mockDocumentsManager.getThumbnail(anyString(), anyString(), anyInt()))
.thenReturn(deferred)

mockDocumentViewModel.selectDocument(document)
}

Expand All @@ -84,36 +94,24 @@ class DocumentListItemTest {
.assertTextContains(document.title)
}

// @Test
// fun testsLoadingBehavior() {
// composeTestRule.setContent { DocumentListItem(document, mockDocumentViewModel) {} }
//
// // Test that the loading spinner is displayed and the document list item is not displayed
// composeTestRule
// .onNodeWithTag("loadingSpinner-ref_id", useUnmergedTree = true)
// .assertIsDisplayed()
// composeTestRule.onNodeWithTag("thumbnail-ref_id", useUnmergedTree =
// true).assertIsNotDisplayed()
//
// runBlocking {
// `when`(
// mockDocumentRepository.getThumbnailUrl(
// any(), anyInt(), anyOrNull<(String) -> Unit>(), anyOrNull(), anyBoolean()))
// .then {
// val onSuccess = it.arguments[2] as (String) -> Unit
// onSuccess("the-thumbnail-uri")
// }
// }
//
// // Add the thumbnail URI to the documentViewModel
// mockDocumentViewModel.getDocumentThumbnail(document, 150)
// composeTestRule.waitForIdle()
//
// // Test that the loading spinner is not displayed and the document list item is displayed
// composeTestRule
// .onNodeWithTag("loadingSpinner-ref_id", useUnmergedTree = true)
// .assertIsNotDisplayed()
// composeTestRule.onNodeWithTag("thumbnail-ref_id", useUnmergedTree =
// true).assertIsDisplayed()
// }
@Test
fun testsLoadingBehavior() {
composeTestRule.setContent { DocumentListItem(document, mockDocumentViewModel) {} }

// Test that the loading spinner is displayed and the document list item is not displayed
composeTestRule
.onNodeWithTag("loadingSpinner-ref_id", useUnmergedTree = true)
.assertIsDisplayed()
composeTestRule.onNodeWithTag("thumbnail-ref_id", useUnmergedTree = true).assertIsNotDisplayed()

// Add the thumbnail URI to the documentViewModel
deferred.complete(Uri.EMPTY)
composeTestRule.waitForIdle()

// Test that the loading spinner is not displayed and the document list item is displayed
composeTestRule
.onNodeWithTag("loadingSpinner-ref_id", useUnmergedTree = true)
.assertIsNotDisplayed()
composeTestRule.onNodeWithTag("thumbnail-ref_id", useUnmergedTree = true).assertIsDisplayed()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ import com.github.se.travelpouch.model.travels.TravelContainer
import com.github.se.travelpouch.ui.navigation.NavigationActions
import com.google.firebase.Timestamp
import com.google.firebase.firestore.DocumentReference
import kotlinx.coroutines.CompletableDeferred
import java.io.File
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.Mockito.anyInt
import org.mockito.Mockito.anyString
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`
import org.mockito.kotlin.any
Expand Down Expand Up @@ -123,6 +126,8 @@ class DocumentListTest {
mockDataStore = mock()
val documentViewModel =
DocumentViewModel(mockDocumentRepository, mockDocumentsManager, mockDataStore)
`when`(mockDocumentsManager.getThumbnail(anyString(), anyString(), anyInt()))
.thenReturn(CompletableDeferred())
mockDocumentViewModel = spy(documentViewModel)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ open class DocumentsManager(
* @param sourceRef The reference of the source file
* @param size The width of the thumbnail
*/
fun getThumbnail(travelRef: String, sourceRef: String, size: Int): Deferred<Uri> {
open fun getThumbnail(travelRef: String, sourceRef: String, size: Int): Deferred<Uri> {
return CoroutineScope(Dispatchers.IO).async {
val file = File(thumbsDirectory, "$sourceRef-$size")
if (file.exists()) {
Expand Down

0 comments on commit 718dcd0

Please sign in to comment.