Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Robolectric和androidx.Testing测试BottomSheetDialog项click()_Android_Robolectric_Androidx Test - Fatal编程技术网

使用Robolectric和androidx.Testing测试BottomSheetDialog项click()

使用Robolectric和androidx.Testing测试BottomSheetDialog项click(),android,robolectric,androidx-test,Android,Robolectric,Androidx Test,我正在尝试使用androidx.test和Robolectric测试BottomSheetDialog,但在尝试单击对话框中的按钮时遇到了问题 onView(withId(R.id.fab_add)).perform(click()) onView(withId(R.id.button_add_activity)) .inRoot(isDialog()) .check(matches(isClickable())) onView(withI

我正在尝试使用androidx.test和Robolectric测试BottomSheetDialog,但在尝试单击对话框中的按钮时遇到了问题

   onView(withId(R.id.fab_add)).perform(click())
   onView(withId(R.id.button_add_activity))
         .inRoot(isDialog())
         .check(matches(isClickable()))

   onView(withId(R.id.button_add_activity))
         .inRoot(isDialog())
         .perform(click())
我通过点击按钮打开对话框,然后,我可以验证BottomSheetDialog中的按钮是否存在,但是单击失败,原因是:java.lang.RuntimeException:操作将不会执行,因为目标视图与以下一个或多个约束不匹配:至少90%的视图区域显示给用户。。检查项目时显示高度/宽度为0

我尝试过设置固定大小,并在显示工作表的单击和工作表内的单击按钮之间睡觉。我还尝试了一些不同的布局选项

private fun onAddItemClick() {
    val dialogView: View = layoutInflater.inflate(R.layout.bottom_sheet_agenda_add, null)
    val dialog = BottomSheetDialog(requireContext())

    dialogView.button_add_activity.setOnClickListener {
        dialog.dismiss()
        // Do action
    }
    dialogView.button_add_availability.setOnClickListener {
        dialog.dismiss()
        // Do action
    }
    dialogView.button_add_job.setOnClickListener {
        dialog.dismiss()
        // Do action
    }
    dialog.setContentView(dialogView)
    dialog.show()
}
bottomsheet_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" android:padding="16dp">

<com.google.android.material.button.MaterialButton
android:id="@+id/button_add_activity"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:text="Add Activity"
app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_add_job"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Job"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toBottomOf="@id/button_add_activity" />

<com.google.android.material.button.MaterialButton
android:id="@+id/button_add_availability"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Availability"
app:layout_constraintTop_toBottomOf="@id/button_add_job" />

</androidx.constraintlayout.widget.ConstraintLayout>
androidx.test.espresso.PerformException: Error performing 'single click' on view 'with id: com.company.name.presentation.test:id/button_add_activity'.

at androidx.test.espresso.PerformException$Builder.build(PerformException.java:86)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:87)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:322)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:178)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:119)
at com.company.name.presentation.ui.agenda.AgendaFragmentTest$When adding an Activity$1.perform(AgendaFragmentTest.kt:73)
at com.company.name.presentation.ui.agenda.AgendaFragmentTest$When adding an Activity$1.perform(AgendaFragmentTest.kt:23)
at androidx.test.core.app.ActivityScenario.lambda$onActivity$2$ActivityScenario(ActivityScenario.java:551)
at androidx.test.core.app.ActivityScenario$$Lambda$4.run(Unknown Source)
at androidx.test.core.app.ActivityScenario.onActivity(ActivityScenario.java:561)
at com.company.name.presentation.ui.agenda.AgendaFragmentTest.When adding an Activity(AgendaFragmentTest.kt:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at androidx.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:134)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
at least 90 percent of the view's area is displayed to the user.
Target view: "MaterialButton{id=2131230847, res-name=button_add_activity, visibility=VISIBLE, width=0, height=0, has-focus=true, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=true, is-focusable=true, is-layout-requested=false, is-selected=false, layout-params=androidx.constraintlayout.widget.ConstraintLayout$LayoutParams@6ef0abb1, tag=null, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=Add Activity, input-type=0, ime-target=false, has-links=false, is-checked=false}"
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:252)
at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:65)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:158)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:155)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.$$robo$$android_os_Handler$handleCallback(Handler.java:739)
at android.os.Handler.handleCallback(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$dispatchMessage(Handler.java:95)
at android.os.Handler.dispatchMessage(Handler.java)
at org.robolectric.shadows.ShadowLegacyMessageQueue.dispatchMessage(ShadowLegacyMessageQueue.java:157)
at org.robolectric.shadows.ShadowLegacyMessageQueue.access$200(ShadowLegacyMessageQueue.java:42)
at org.robolectric.shadows.ShadowLegacyMessageQueue$1.run(ShadowLegacyMessageQueue.java:135)
at org.robolectric.util.Scheduler.runOrQueueRunnable(Scheduler.java:359)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:163)
at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:152)
at org.robolectric.shadows.ShadowLegacyMessageQueue.enqueueMessage(ShadowLegacyMessageQueue.java:142)
at android.os.MessageQueue.enqueueMessage(MessageQueue.java)
at android.os.Handler.$$robo$$android_os_Handler$enqueueMessage(Handler.java:631)
at android.os.Handler.enqueueMessage(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageAtTime(Handler.java:600)
at android.os.Handler.sendMessageAtTime(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$sendMessageDelayed(Handler.java:570)
at android.os.Handler.sendMessageDelayed(Handler.java)
at android.os.Handler.$$robo$$android_os_Handler$post(Handler.java:326)
at android.os.Handler.post(Handler.java)
at androidx.test.espresso.base.BaseLayerModule$1.execute(BaseLayerModule.java:92)
at androidx.test.espresso.ViewInteraction.postAsynchronouslyOnUiThread(ViewInteraction.java:312)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:166)
... 26 more