Android studio 如何测试从DialogFragment到另一个DialogFragment的导航?
我正在为我的两个DialogFragment使用导航组件,当我按下第一个DialogFragment上的按钮时,它将被取消,然后显示第二个。我需要测试点击这个按钮是否会进入第二个对话框。我有一个简单的主片段,它被应用程序开始时的第一个DialogFragment覆盖。以下代码来自第一个DialogFragmentAndroid studio 如何测试从DialogFragment到另一个DialogFragment的导航?,android-studio,kotlin,testing,android-dialogfragment,Android Studio,Kotlin,Testing,Android Dialogfragment,我正在为我的两个DialogFragment使用导航组件,当我按下第一个DialogFragment上的按钮时,它将被取消,然后显示第二个。我需要测试点击这个按钮是否会进入第二个对话框。我有一个简单的主片段,它被应用程序开始时的第一个DialogFragment覆盖。以下代码来自第一个DialogFragment /** * Redirects users to another dialog after pressing button */ override fun onViewCreat
/**
* 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)
}
}
}
@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())
}
}
}
下一段代码来自《开发人员指南》,只检查将DialogFragment回退到上一个片段的行为
@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())
}
}
}
@RunWith(AndroidJUnit4::class)
类MyTestSuite{
@测试乐趣testDismissDialogFragment(){
//假设“MyDialogFragment”扩展了DialogFragment类。
使用(launchFragment()){
onFragment{fragment->
assertThat(fragment.dialog).isNotNull()
assertThat(fragment.requireDialog().isShowing.isTrue())
fragment.disclose()
fragment.requireFragmentManager().executePendingTransactions()
assertThat(fragment.dialog).isNull()
}
//假设对话框有一个按钮
//包含文本“取消”。
onView(带有文本(“取消”)。检查(doesNotExist())
}
}
}
我需要一些方法来测试DialogFragment按钮的行为,看看它是否会自动关闭,并启动第二个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)
}
}
}
@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())
}
}
}
当我测试被点击的按钮时,第一个DialogFragment被正确地取消,但是第二个DialogFragment没有启动。我已经使用了Espresso和UiAutomator,点击确实发生了,但是阅读代码片段中关于测试DialogFragments的解释,它说
“即使对话框是图形片段的实例,也可以使用launchFragment()方法,以便在对话框本身中填充对话框的元素,而不是在启动对话框的活动中填充。”
是我无法检查第二个DialogFragment是否存在的原因,因为它是一个图形片段的实例,并且我的第一个DialogFragment按钮的click listener无法实现launchFragment()对于第二个DialogFragment?讲述您面临的确切问题。我使用了示例测试代码,但没有使用fragment.Disclease(),而是使用UiAutomator单击,这将带我进入下一个DialogFragment。单击应该会关闭第一个DialogFragment,然后启动第二个DialogFragment。正常运行时,应用程序工作正常,但我的问题出现在测试中没有启动第二个DialogFragment,因此任何用于检查是否启动第二个DialogFragment的断言都失败。