From 3d307d1c5cad5a6a0a834c6d33861c998984cdec Mon Sep 17 00:00:00 2001 From: Arthur Chalard <49678951+ArthurChalard@users.noreply.github.com> Date: Fri, 20 Dec 2024 08:17:30 +0100 Subject: [PATCH] test: repair test for loading behavior (#270) * test: repair test for loading behavior * style: run ktfmt * test: fix consequent failure * test: fix other collateral damages on tests This is due to the getThumbnail function being open --------- Co-authored-by: Alexandre Spiess <92825698+Unsaved2@users.noreply.github.com> --- .../ui/documents/DocumentListItemTest.kt | 62 +++++++++---------- .../ui/documents/DocumentListTest.kt | 5 ++ .../model/documents/DocumentsManager.kt | 2 +- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListItemTest.kt b/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListItemTest.kt index ced2e1ff..4ee04bba 100644 --- a/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListItemTest.kt +++ b/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListItemTest.kt @@ -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 @@ -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` @@ -33,6 +38,7 @@ class DocumentListItemTest { private lateinit var mockDocumentsManager: DocumentsManager private lateinit var mockDataStore: DataStore private lateinit var document: DocumentContainer + private lateinit var deferred: CompletableDeferred @get:Rule val composeTestRule = createComposeRule() @@ -59,6 +65,10 @@ class DocumentListItemTest { mockDocumentViewModel = DocumentViewModel(mockDocumentRepository, mockDocumentsManager, mockDataStore) + deferred = CompletableDeferred() + `when`(mockDocumentsManager.getThumbnail(anyString(), anyString(), anyInt())) + .thenReturn(deferred) + mockDocumentViewModel.selectDocument(document) } @@ -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() + } } diff --git a/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListTest.kt b/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListTest.kt index d052e9ff..a5093997 100644 --- a/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListTest.kt +++ b/app/src/androidTest/java/com/github/se/travelpouch/ui/documents/DocumentListTest.kt @@ -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 @@ -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) } diff --git a/app/src/main/java/com/github/se/travelpouch/model/documents/DocumentsManager.kt b/app/src/main/java/com/github/se/travelpouch/model/documents/DocumentsManager.kt index 0efb1d62..c8f7eed7 100644 --- a/app/src/main/java/com/github/se/travelpouch/model/documents/DocumentsManager.kt +++ b/app/src/main/java/com/github/se/travelpouch/model/documents/DocumentsManager.kt @@ -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 { + open fun getThumbnail(travelRef: String, sourceRef: String, size: Int): Deferred { return CoroutineScope(Dispatchers.IO).async { val file = File(thumbsDirectory, "$sourceRef-$size") if (file.exists()) {