【问题标题】:Android espresso test freezes when snackbar is showing only when view contains Floating Action Button仅当视图包含浮动操作按钮时才显示小吃栏时,Android espresso 测试冻结
【发布时间】:2018-04-19 13:36:18
【问题描述】:

我认为这是一个错误,但我可能做错了什么。

我的布局很简单,只包含一个浮动操作按钮:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/contentCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/layoutWithoutContent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

</LinearLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/stopButton"
    android:src="@drawable/ic_stop_white_24px"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    app:layout_anchor="@id/layoutWithoutContent"
    app:backgroundTint="@color/colorPrimary"
    app:layout_anchorGravity="bottom|right|end"/>

我的活动只继承了onCreate方法,包含:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CoordinatorLayout coordinatorLayout = findViewById(R.id.contentCoordinatorLayout);
    Snackbar.make(coordinatorLayout, R.string.snackbar_message, Snackbar.LENGTH_LONG).show();

}

我写了一个 Espresso 测试,它只验证 Snackbar 在半秒内显示

@Rule
public ActivityTestRule<MainActivity> mainActivity = new ActivityTestRule<>(MainActivity.class, true, true);

@Test
public void testSnackbarIsShown() throws Exception {        onView(withText(R.string.snackbar_message)).check(matches(isDisplayed()));
}

当我调试此测试时,onView(withText(R.string.snackbar_message)).check(matches(isDisplayed())); 会在 小吃栏消失后执行。

到目前为止,我已经找到了 3 种解决此问题的方法,问题在以下情况下得到解决:

  • 我将 FloatingActionButton 的 anchorGravity 更改为顶部:app:layout_anchorGravity="top|right|end"

  • 我完全移除了 FloatingActionButton

  • 将 Snackbar 的第一个参数更改为使用 findViewById(android.R.id.content)。但这不会在小吃栏出现时将 FloatingActionButton 向上推。

正如我所说,我认为这是一个错误,但如果我在这里做错了什么,请告诉我。

【问题讨论】:

  • 使用sleep() 方法是错误的。您应该使用一些 ViewAction 循环测试胎面。另一件事是浓缩咖啡不适用于动画。在设备开发者选项上打开动画,然后尝试启动测试。
  • @KarolKulbaka 动画关闭,没有睡眠,测试也挂起。
  • 你试过ActivityMonitor 吗? (developer.android.com/reference/android/app/…) 喜欢,等待活动出现,并且只有在检查您的小吃店之后。
  • Anton Malmygin,我已经尝试过了,但正如我在问题中所说,问题似乎是在小吃栏消失之前我的测试代码都没有执行。

标签: android floating-action-button android-espresso coordinator-layout


【解决方案1】:

当 Snackbar 与 FAB 按钮交互时,您在 xml 中构建布局的方式会使 UI 不断更新。

您发现的三个解决方案有一个共同点,它们消除了 Snackbar 和 FAB 按钮之间的交互。当您将重力设置为 TOP 时,Snackbar 不会按下按钮,因为它们位于屏幕的不同端。当您完全删除 FAB 按钮时,也没有交互。当您更改传递给 Snackbar 构建器的视图时,也没有交互,因为 Snackbar 显示在按钮顶部。

如果您在“设置”>“开发人员选项”中启用Show Surface Updates,您实际上会看到 UI 正在更新,同时显示 Snakcbar。 Espresso 轮询 UI 线程,试图找出空闲时间。由于 Snackbar 使 UI 线程保持忙碌,因此 Espresso 在 Snackbar 消失之前不会运行测试代码。

似乎与:

app:layout_anchor="@id/layoutWithoutContent"
app:layout_anchorGravity="bottom|right|end"

如果您将它们替换为:

android:layout_gravity="bottom|end"

生成的布局看起来相同并且测试运行正确。如果您启用了Show Surface Updates,您将看到在显示 Snackbar 时 UI 没有更新。

【讨论】:

  • 我不敢相信我自己没有弄清楚这一点。即使是浮动操作按钮的 Android 文档也显示了带有锚点的示例。非常感谢您的帮助,这解决了问题!
猜你喜欢
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 2018-10-28
相关资源
最近更新 更多