0
0
mirror of https://github.com/ankidroid/Anki-Android.git synced 2024-09-20 20:03:05 +02:00

test(DeckPickerContextMenu): Ensure arguments set

This removes the possibility that `getLong()` returns 0L
if the argument was not set.
This commit is contained in:
David Allison 2022-01-30 02:26:40 +00:00 committed by Mike Hardy
parent 02cbfff3cb
commit 7eba3a832d
4 changed files with 24 additions and 2 deletions

View File

@ -23,9 +23,10 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.ichi2.anki.DeckPicker
import com.ichi2.anki.R
import com.ichi2.anki.analytics.AnalyticsDialogFragment
import com.ichi2.utils.BundleUtils.requireLong
class DeckPickerConfirmDeleteDeckDialog : AnalyticsDialogFragment() {
val deckId get() = requireArguments().getLong("deckId")
val deckId get() = requireArguments().requireLong("deckId")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreate(savedInstanceState)

View File

@ -29,6 +29,7 @@ import com.ichi2.anki.StudyOptionsFragment.StudyOptionsListener
import com.ichi2.anki.analytics.AnalyticsDialogFragment
import com.ichi2.anki.dialogs.customstudy.CustomStudyDialog
import com.ichi2.libanki.Collection
import com.ichi2.utils.BundleUtils.requireLong
import com.ichi2.utils.ExtendedFragmentFactory
import com.ichi2.utils.FragmentFactoryUtils
import timber.log.Timber
@ -47,7 +48,7 @@ class DeckPickerContextMenu(private val collection: Collection) : AnalyticsDialo
}
/** The selected deck for the context menu */
private val deckId get() = requireArguments().getLong("did")
val deckId get() = requireArguments().requireLong("did")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
super.onCreate(savedInstanceState)

View File

@ -35,4 +35,18 @@ object BundleUtils {
null
} else bundle.getLong(key)
}
/**
* Retrieves a [Long] value from a [Bundle] using a key, throws if not found
*
* @param key A string key
* @return the value associated with [key]
* @throws IllegalStateException If [key] does not exist in the bundle
*/
fun Bundle.requireLong(key: String): Long {
if (!this.containsKey(key)) {
throw IllegalStateException("key: '$key' not found")
}
return getLong(key)
}
}

View File

@ -23,6 +23,7 @@ import com.afollestad.materialdialogs.MaterialDialog
import com.ichi2.anki.R
import com.ichi2.anki.RobolectricTest
import com.ichi2.libanki.Consts
import com.ichi2.testutils.assertThrows
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
@ -44,6 +45,11 @@ class DeckPickerContextMenuTest : RobolectricTest() {
}
}
@Test
fun ensure_cannot_be_instantiated_without_arguments() {
assertThrows<IllegalStateException> { DeckPickerContextMenu(col).deckId }
}
/**
* Allows testing the [MaterialDialog] returned from the [DeckPickerContextMenu]
*