vendredi 26 juin 2020

How to test navigation from DialogFragment to another DialogFragment?

I'm using the Navigation component for my two DialogFragments and when I press a button on the first DialogFragment it is dismissed and then the second one is shown. I need to test that clicking this button will take me to the second dialog. I have a simple home fragment that is overlayed by the first DialogFragment at the start of the app. The following code is from the first DialogFragment.

 /**
 * Redirects users to another dialog after pressing button
 */
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    button.setOnClickListener {
        if (findNavController().currentDestination?.id == R.id.firstDialogFragment) {
            findNavController().navigateUp()
            val action = HomeFragmentDirections.actionHomeFragmentToSecondDialogFragment()
            findNavController().navigate(action)
        }
    }
}

This next bit of code comes from the developer's guide and only checks for the behavior of dismissing a DialogFragment back to the previous Fragment.

@RunWith(AndroidJUnit4::class)
class MyTestSuite {
    @Test fun testDismissDialogFragment() {
        // Assumes that "MyDialogFragment" extends the DialogFragment class.
        with(launchFragment<MyDialogFragment>()) {
            onFragment { fragment ->
                assertThat(fragment.dialog).isNotNull()
                assertThat(fragment.requireDialog().isShowing).isTrue()
                fragment.dismiss()
                fragment.requireFragmentManager().executePendingTransactions()
                assertThat(fragment.dialog).isNull()
            }

            // Assumes that the dialog had a button
            // containing the text "Cancel".
            onView(withText("Cancel")).check(doesNotExist())
        }
    }
}

I need some way to test the behavior of a DialogFragment's button and see that it dismisses itself and starts the second DialogFragment.

Aucun commentaire:

Enregistrer un commentaire