Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FragmentScenario of DialogFragment, onCreateDialog not called

I want to test DialogFragment using androidx.fragment:fragment-testing lib. I call launchFragmentInContainer and moveToState(Lifecycle.State.RESUMED), but onCreateDialog is not called in this fragment.

    @Test
    fun `submit search - presenter state is changed`() {
        val p: PinCatsPresenter = F.presenter(PinCatsPresenter.COMPONENT_ID)!!

        launchFragmentInContainer<PinCatsDialog>().let { scenario ->
            scenario
                .moveToState(Lifecycle.State.RESUMED)
                .onFragment { fragment ->
                    assertFalse(p.state.isFiltered)

                    fragment.dialog!!.findViewById<SearchView>(R.id.search_field).let {
                        it.isIconified = false
                        it.setQuery("ea", true)
                    }
                    awaitUi()
                    assertTrue(p.state.isFiltered)
                    assertEquals(3, p.state.count)
                }
        }
    }

I debug the app, and ensured that onCreateDialog is called earlier than onResume, but in this test scenario onCreateDialog is not called, so fragment.dialog is null.

What should I call onFragmentScenario so my dialog would be created?

like image 849
Oleksandr Albul Avatar asked Oct 23 '25 16:10

Oleksandr Albul


1 Answers

This is described in the official documentation. We need to call launchFragment instead of launchFragmentInContainer:

   launchFragment<PinCatsDialog>().let { scenario ->
       scenario
           .moveToState(Lifecycle.State.RESUMED)
           .onFragment { fragment ->
               // Code here
           }
    }
like image 114
Oleksandr Albul Avatar answered Oct 26 '25 07:10

Oleksandr Albul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!