Skip to content

Commit

Permalink
tests: fix flake in updateViewWithSavedPreferences
Browse files Browse the repository at this point in the history
We did not wait for `updateViewWithSavedPreferences` correctly in
`DeckPickerWidgetConfigTest.testLoadSavedPreferences`

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

Issue 17010
  • Loading branch information
david-allison committed Sep 18, 2024
1 parent c123be3 commit 3fd1c69
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -85,17 +84,14 @@ 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)

// 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -85,17 +84,14 @@ 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() })

// 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
Expand Down

0 comments on commit 3fd1c69

Please sign in to comment.