dimanche 9 août 2020

Inconsistent behavior running Android Navigation tests

I'm running into inconsistent behavior executing Android Fragment Navigation tests.
Sometimes all the test are successful and sometimes some tests fail.

Basically I follow the example mention in documentation (https://developer.android.com/guide/navigation/navigation-testing#test_fragment_navigation) and my tests look like the following:

@RunWith(AndroidJUnit4::class)
class ExampleTestNavigation {

    private lateinit var navController: TestNavHostController

    @Before
    fun initNavController() {
        navController = TestNavHostController(ApplicationProvider.getApplicationContext())
        navController.setGraph(R.navigation.nav_graph)
    }

    @Test
    fun testNavigationFragmentAToFragmentB() {
        val fragmentA = launchFragmentInContainer<FragmentA>()
    
        navController.setCurrentDestination(R.id.fragmentA)
        fragmentA.onFragment { fragment ->
            Navigation.setViewNavController(fragment.requireView(), navController)
        }

        onView(withId(R.id.btn_A)).perform(click())
        Truth.assertThat(navController.currentDestination?.id).isEqualTo(R.id.fragmentB)
    }

    @Test
    fun testNavigationFragmentBToFragmentC() {
        val fragmentB = launchFragmentInContainer<FragmentB>()

        navController.setCurrentDestination(R.id.fragmentB)
        fragmentB.onFragment { fragment ->
            Navigation.setViewNavController(fragment.requireView(), navController)
        }

        onView(withId(R.id.btn_B)).perform(click())
        Truth.assertThat(navController.currentDestination?.id).isEqualTo(R.id.fragmentC)
    }
}

I've tried to run the tests on a dedicated Pixel 4 and an emulated Pixel 3 and ended up with the same inconsistent behavior.

Does anyone know about this issue? Or knows what I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire