android Jetpack 导航仪器测试在返回导航时失败

android jetpack navigation instrumented test fail on back navigation

我使用 Jetpack Navigation 组件 (androidx.navigation) 创建了一个简单的两片段示例应用程序。第一个片段导航到第二个片段,后者使用 OnBackPressedDispatcher.

覆盖后退按钮行为

activity布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/box_inset_layout_padding"
    tools:context=".navigationcontroller.NavigationControllerActivity">

    <fragment
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:id="@+id/nav_host"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />
</LinearLayout>

片段A:

class FragmentA : Fragment() {

    lateinit var buttonNavigation: Button

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_a, container, false)
        buttonNavigation = view.findViewById<Button>(R.id.button_navigation)
        buttonNavigation.setOnClickListener { Navigation.findNavController(requireActivity(), R.id.nav_host).navigate(R.id.fragmentB) }
        return view
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigationcontroller.FragmentA">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="fragment A" />

    <Button
        android:id="@+id/button_navigation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="go to B" />
</LinearLayout>

片段B:

class FragmentB : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_b, container, false)
        requireActivity().onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val textView = view.findViewById<TextView>(R.id.textView)
                textView.setText("backbutton pressed, press again to go back")
                this.isEnabled = false
            }
        })
        return view
    }
}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".navigationcontroller.FragmentA">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="fragment B" />
</FrameLayout>

当我手动测试应用程序时,FragmentB 中后退按钮的预期行为(第一次触摸更改没有导航的文本,第二次导航返回)工作正常。 我添加了仪器测试来检查 FragmentB 中的后退按钮行为,这就是问题开始出现的地方:

class NavigationControllerActivityTest {

    lateinit var fragmentScenario: FragmentScenario<FragmentB>
    lateinit var navController: TestNavHostController

    @Before
    fun setUp() {
        navController = TestNavHostController(ApplicationProvider.getApplicationContext())

        fragmentScenario = FragmentScenario.launchInContainer(FragmentB::class.java)
        fragmentScenario.onFragment(object : FragmentScenario.FragmentAction<FragmentB> {
            override fun perform(fragment: FragmentB) {
                Navigation.setViewNavController(fragment.requireView(), navController)
                navController.setLifecycleOwner(fragment.viewLifecycleOwner)
                navController.setOnBackPressedDispatcher(fragment.requireActivity().getOnBackPressedDispatcher())
                navController.setGraph(R.navigation.nav_graph)
                // simulate backstack from previous navigation
                navController.navigate(R.id.fragmentA)
                navController.navigate(R.id.fragmentB)
            }
        })
    }

    @Test
    fun whenButtonClickedOnce_TextChangedNoNavigation() {
        Espresso.pressBack()
        onView(withId(R.id.textView)).check(matches(withText("backbutton pressed, press again to go back")))
        assertEquals(R.id.fragmentB, navController.currentDestination?.id)
    }

    @Test
    fun whenButtonClickedTwice_NavigationHappens() {
        Espresso.pressBack()
        Espresso.pressBack()
        assertEquals(R.id.fragmentA, navController.currentDestination?.id)
    }
}

不幸的是,虽然 whenButtonClickedTwice_NavigationHappens 通过了,但 whenButtonClickedOnce_TextChangedNoNavigation 由于文本未更改而失败,就像从未调用过 OnBackPressedCallback 一样。由于应用程序在手动测试期间运行良好,因此测试代码肯定有问题。谁能帮帮我?

FragmentB 的 OnBackPressedCallback 被忽略的原因是 OnBackPressedDispatcher 对待其 OnBackPressedCallback 的方式。它们是 运行 作为命令链,这意味着最近注册的启用的将 'eat' 该事件,因此其他人将不会收到它。因此,最近在 FragmentScenario.onFragment() 内注册的回调(由 lifecycleOwner 启用,因此每当 Fragment 至少处于生命周期 STARTED 状态时。由于在按下后退按钮时片段在测试期间可见,回调始终在此时启用),将优先于先前在 FragmentB.onCreateView() 中注册的回调。 因此,TestNavHostController的回调必须在FragmentB.onCreateView()执行前加上。

这导致测试代码更改@Before方法:

@Before
fun setUp() {
    navController = TestNavHostController(ApplicationProvider.getApplicationContext())

    fragmentScenario = FragmentScenario.launchInContainer(FragmentB::class.java, initialState = Lifecycle.State.CREATED)
    fragmentScenario.onFragment(object : FragmentScenario.FragmentAction<FragmentB> {
        override fun perform(fragment: FragmentB) {
            navController.setLifecycleOwner(fragment.requireActivity())
            navController.setOnBackPressedDispatcher(fragment.requireActivity().getOnBackPressedDispatcher())
            navController.setGraph(R.navigation.nav_graph)
            // simulate backstack from previous navigation
            navController.navigate(R.id.fragmentA)
            navController.navigate(R.id.fragmentB)
        }
    })
    fragmentScenario.moveToState(Lifecycle.State.RESUMED)
    fragmentScenario.onFragment(object : FragmentScenario.FragmentAction<FragmentB> {
        override fun perform(fragment: FragmentB) {
            Navigation.setViewNavController(fragment.requireView(), navController)
        }
    })
}

最重要的变化是在 CREATED 状态(而不是默认的 RESUMED)启动片段,以便能够在 onCreateView().

之前对其进行修改

此外,请注意 Navigation.setViewNavController() 在将片段移动到 RESUMED 状态后在单独的 onFragment() 中是 运行 - 它接受 View 参数,因此它不能在 onCreateView()

之前使用

如果您正在尝试测试您的 OnBackPressedCallback 逻辑,最好直接进行,而不是尝试测试导航与默认 activity 的 [=14] 之间的交互=].

这意味着你想打破 activity 的 OnBackPressedDispatcher (requireActivity().onBackPressedDispatcher) 和你的 Fragment 之间的硬依赖,而不是注入 OnBackPressedDispatcher,从而允许您提供测试特定实例:

class FragmentB(val onBackPressedDispatcher: OnBackPressedDispatcher) : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_b, container, false)
        onBackPressedDispatcher.addCallback(object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val textView = view.findViewById<TextView>(R.id.textView)
                textView.setText("backbutton pressed, press again to go back")
                this.isEnabled = false
            }
        })
        return view
    }
}

这允许您拥有生产代码 provide a FragmentFactory:

class MyFragmentFactory(val activity: FragmentActivity) : FragmentFactory() {
    override fun instantiate(classLoader: ClassLoader, className: String): Fragment =
        when (loadFragmentClass(classLoader, className)) {
            FragmentB::class.java -> FragmentB(activity.onBackPressedDispatcher)
            else -> super.instantiate(classLoader, className)
        }
}

// Your activity would use this via:
override fun onCreate(savedInstanceState: Bundle?) {
    supportFragmentManager.fragmentFactory = MyFragmentFactory(this)
    super.onCreate(savedInstanceState)
    // ...
}

这意味着您可以编写如下测试:

class NavigationControllerActivityTest {

    lateinit var fragmentScenario: FragmentScenario<FragmentB>
    lateinit var onBackPressedDispatcher: OnBackPressedDispatcher
    lateinit var navController: TestNavHostController

    @Before
    fun setUp() {
        navController = TestNavHostController(ApplicationProvider.getApplicationContext())

        // Create a test specific OnBackPressedDispatcher,
        // giving you complete control over its behavior
        onBackPressedDispatcher = OnBackPressedDispatcher()

        // Here we use the launchInContainer method that
        // generates a FragmentFactory from a constructor,
        // automatically figuring out what class you want
        fragmentScenario = launchFragmentInContainer {
            FragmentB(onBackPressedDispatcher)
        }
        fragmentScenario.onFragment(object : FragmentScenario.FragmentAction<FragmentB> {
            override fun perform(fragment: FragmentB) {
                Navigation.setViewNavController(fragment.requireView(), navController)
                navController.setGraph(R.navigation.nav_graph)
                // Set the current destination to fragmentB
                navController.setCurrentDestination(R.id.fragmentB)
            }
        })
    }

    @Test
    fun whenButtonClickedOnce_FragmentInterceptsBack() {
        // Assert that your FragmentB has already an enabled OnBackPressedCallback
        assertTrue(onBackPressedDispatcher.hasEnabledCallbacks())

        // Now trigger the OnBackPressedDispatcher
        onBackPressedDispatcher.onBackPressed()
        onView(withId(R.id.textView)).check(matches(withText("backbutton pressed, press again to go back")))

        // Check that FragmentB has disabled its Callback
        // ensuring that the onBackPressed() will do the default behavior
        assertFalse(onBackPressedDispatcher.hasEnabledCallbacks())
    }
}

这避免了测试 Navigation 的代码,并专注于测试 您的 代码,特别是您与 OnBackPressedDispatcher.

的交互