2

我正在尝试使用 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 中的按钮是否存在,但是单击失败并显示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.. 检查项目显示高度/宽度为 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


4

0 回答 0