diff --git a/carioca-hilt/carioca-hilt-fragment/src/androidTest/java/com/rubensousa/carioca/hilt/HiltFragmentScenarioTest.kt b/carioca-hilt/carioca-hilt-fragment/src/androidTest/java/com/rubensousa/carioca/hilt/HiltFragmentScenarioTest.kt index 47867b22..49c56a5b 100644 --- a/carioca-hilt/carioca-hilt-fragment/src/androidTest/java/com/rubensousa/carioca/hilt/HiltFragmentScenarioTest.kt +++ b/carioca-hilt/carioca-hilt-fragment/src/androidTest/java/com/rubensousa/carioca/hilt/HiltFragmentScenarioTest.kt @@ -1,6 +1,11 @@ package com.rubensousa.carioca.hilt +import android.graphics.Color import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.DialogFragment import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.Lifecycle @@ -48,6 +53,21 @@ class HiltFragmentScenarioTest { assertThat(testDependency).isSameInstanceAs(providedDependency) } + @Test + fun testViewModelOfDialogFragmentReceivesInjectedDependency() { + // given + val scenario = launchHiltFragment() + + // when + var testDependency: TestDependency? = null + scenario.onFragment { fragment -> + testDependency = fragment.getTestDependency() + } + + // then + assertThat(testDependency).isSameInstanceAs(providedDependency) + } + @Test fun testFragmentMovesToDifferentLifecycleStates() { // given @@ -74,7 +94,7 @@ class HiltFragmentScenarioTest { fun testActivityRemovesFragmentWhenItGetsDestroyed() { // given val scenario = launchHiltFragment() - val activity = scenario.withFragment { requireActivity() } + val activity = scenario.withFragment { requireActivity() } // when scenario.moveToState(Lifecycle.State.DESTROYED) @@ -141,6 +161,29 @@ class HiltFragmentScenarioTest { return currentState } + @AndroidEntryPoint + class TestDialogFragment : DialogFragment() { + + private val viewModel by viewModels() + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View { + return View(inflater.context).apply { + layoutParams = ViewGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.MATCH_PARENT + ) + setBackgroundColor(Color.YELLOW) + } + } + + fun getTestDependency() = viewModel.getTestDependency() + + } + @AndroidEntryPoint class TestFragment : Fragment() {