0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-19 19:42:17 +02:00

tests: fix flake in updateViewWithSavedPreferences

We did not wait for `updateViewWithSavedPreferences` correctly in
`DeckPickerWidgetConfigTest.testLoadSavedPreferences`

Because we updated this in `DeckPickerWidgetConfig`,
also update the method in `CardAnalysisWidgetConfig`

Issue 17010
This commit is contained in:
David Allison 2024-09-18 17:09:43 +01:00
parent c123be3631
commit 3fd1c69ea8
4 changed files with 19 additions and 30 deletions

View File

@ -153,7 +153,7 @@ class CardAnalysisWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnac
showDeckSelectionDialog()
}
updateViewWithSavedPreferences()
lifecycleScope.launch { updateViewWithSavedPreferences() }
// Update the visibility of the "no decks" placeholder and the widget configuration container
updateViewVisibility()
@ -234,16 +234,15 @@ class CardAnalysisWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnac
}
/** Updates the view according to the saved preference for appWidgetId.*/
fun updateViewWithSavedPreferences() {
suspend fun updateViewWithSavedPreferences() {
val selectedDeckId = cardAnalysisWidgetPreferences.getSelectedDeckIdFromPreferences(appWidgetId) ?: return
lifecycleScope.launch {
val decks = fetchDecks()
val selectedDecks = decks.filter { it.deckId == selectedDeckId }
selectedDecks.forEach { deckAdapter.addDeck(it) }
updateViewVisibility()
updateFabVisibility()
updateSubmitButtonText()
}
val decks = fetchDecks()
val selectedDecks = decks.filter { it.deckId == selectedDeckId }
selectedDecks.forEach { deckAdapter.addDeck(it) }
updateViewVisibility()
updateFabVisibility()
updateSubmitButtonText()
}
/** Asynchronously displays the list of deck in the selection dialog. */

View File

@ -151,7 +151,7 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnackb
showDeckSelectionDialog()
}
updateViewWithSavedPreferences()
lifecycleScope.launch { updateViewWithSavedPreferences() }
// Update the visibility of the "no decks" placeholder and the widget configuration container
updateViewVisibility()
@ -282,17 +282,15 @@ class DeckPickerWidgetConfig : AnkiActivity(), DeckSelectionListener, BaseSnackb
}
/** Updates the view according to the saved preference for appWidgetId.*/
fun updateViewWithSavedPreferences() {
suspend fun updateViewWithSavedPreferences() {
val selectedDeckIds = deckPickerWidgetPreferences.getSelectedDeckIdsFromPreferences(appWidgetId)
if (selectedDeckIds.isNotEmpty()) {
lifecycleScope.launch {
val decks = fetchDecks()
val selectedDecks = decks.filter { it.deckId in selectedDeckIds }
selectedDecks.forEach { deckAdapter.addDeck(it) }
updateViewVisibility()
updateFabVisibility()
setupDoneButton()
}
val decks = fetchDecks()
val selectedDecks = decks.filter { it.deckId in selectedDeckIds }
selectedDecks.forEach { deckAdapter.addDeck(it) }
updateViewVisibility()
updateFabVisibility()
setupDoneButton()
}
}

View File

@ -31,7 +31,6 @@ import org.hamcrest.Matchers.equalTo
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
@RunWith(AndroidJUnit4::class)
class CardAnalysisWidgetConfigTest : RobolectricTest() {
@ -85,7 +84,7 @@ class CardAnalysisWidgetConfigTest : RobolectricTest() {
* `RecyclerView` displays the correct number of items based on the saved preferences.
*/
@Test
fun testLoadSavedPreferences() {
fun testLoadSavedPreferences() = runTest {
// Save decks to preferences
val deckId = 1L
widgetPreferences.saveSelectedDeck(1, deckId)
@ -93,9 +92,6 @@ class CardAnalysisWidgetConfigTest : RobolectricTest() {
// Load preferences
activity.updateViewWithSavedPreferences()
// Ensure all tasks on the UI thread are completed
Robolectric.flushForegroundThreadScheduler()
// Get the RecyclerView and its adapter
val recyclerView = activity.findViewById<RecyclerView>(R.id.recyclerViewSelectedDecks)
val adapter = recyclerView.adapter

View File

@ -31,7 +31,6 @@ import org.hamcrest.Matchers.equalTo
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
@RunWith(AndroidJUnit4::class)
class DeckPickerWidgetConfigTest : RobolectricTest() {
@ -85,7 +84,7 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {
* `RecyclerView` displays the correct number of items based on the saved preferences.
*/
@Test
fun testLoadSavedPreferences() {
fun testLoadSavedPreferences() = runTest {
// Save decks to preferences
val deckIds = listOf(1L)
widgetPreferences.saveSelectedDecks(1, deckIds.map { it.toString() })
@ -93,9 +92,6 @@ class DeckPickerWidgetConfigTest : RobolectricTest() {
// Load preferences
activity.updateViewWithSavedPreferences()
// Ensure all tasks on the UI thread are completed
Robolectric.flushForegroundThreadScheduler()
// Get the RecyclerView and its adapter
val recyclerView = activity.findViewById<RecyclerView>(R.id.recyclerViewSelectedDecks)
val adapter = recyclerView.adapter