簡體   English   中英

僅當視圖包含“浮動操作”按鈕時,小吃欄才顯示時,Android espresso測試會凍結

[英]Android espresso test freezes when snackbar is showing only when view contains Floating Action Button

我認為這是一個錯誤,但是我可能做錯了什么。

我的布局非常簡單,僅包含一個浮動操作按鈕:

<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更改為top: app:layout_anchorGravity="top|right|end"

  • 我完全刪除了FloatingActionButton

  • 將Snackbar的第一個參數更改為使用findViewById(android.R.id.content) 但這不會在小吃欄出現時將FloatingActionButton向上推。

正如我所說,我認為這是一個錯誤,但是如果我在這里做錯了,請告訴我。

在Snackbar與FAB按鈕交互時,在xml中構建布局的方式使UI不斷更新。

您發現的三個解決方案有一個共同點,它們消除了Snackbar和FAB按鈕之間的交互。 當您將引力設置為TOP時,Snackbar不會按該按鈕,因為它們位於屏幕的不同端。 當完全卸下FAB按鈕時,也沒有任何交互作用。 並且,當您更改視圖並傳遞給Snackbar構建器時,由於Snackbar顯示在按鈕頂部,因此也沒有任何交互。

如果在“設置”>“開發人員”選項中啟用了“ Show Surface Updates ”,則實際上會在顯示Snakcbar時看到UI正在更新。 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM