Android:如何测试AppBarLayout内的SearchView

huangapple go评论80阅读模式
英文:

Android: How to test a searchView within an AppBarLayout

问题

这是我的代码:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/topAppBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:menu="@menu/top_app_bar"
    app:navigationIcon="@drawable/baseline_arrow_back_24">

    (...)

    <androidx.appcompat.widget.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

</com.google.android.material.appbar.MaterialToolbar>

如何使用 Barista 测试 SearchView?只要单击菜单中的搜索按钮,搜索框的可见性就会设置为可见。

菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/search"
        android:title="@string/search"
        android:contentDescription="@string/search"
        app:showAsAction="ifRoom"
        android:icon="@drawable/search" />

    <item
        android:id="@+id/config"
        android:title="@string/configuracion"
        android:contentDescription="Configuracion"
        app:showAsAction="ifRoom"
        android:icon="@drawable/options" />

</menu>
英文:

This is my code:

         &lt;com.google.android.material.appbar.MaterialToolbar
                android:id=&quot;@+id/topAppBar&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;?attr/actionBarSize&quot;
                app:menu=&quot;@menu/top_app_bar&quot;
                app:navigationIcon=&quot;@drawable/baseline_arrow_back_24&quot;&gt;

                (...)

                &lt;androidx.appcompat.widget.SearchView
                    android:id=&quot;@+id/searchView&quot;
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;match_parent&quot;
                    android:visibility=&quot;gone&quot; /&gt;

            &lt;/com.google.android.material.appbar.MaterialToolbar&gt;

How do I test the SearchView with Barista? The visibility is set to visible whenever the search button in the menu is clicked.

The menu:

&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;item
        android:id=&quot;@+id/search&quot;
        android:title=&quot;@string/search&quot;
        android:contentDescription=&quot;@string/search&quot;
        app:showAsAction=&quot;ifRoom&quot;
        android:icon=&quot;@drawable/search&quot;/&gt;

    &lt;item
        android:id=&quot;@+id/config&quot;
        android:title=&quot;@string/configuracion&quot;
        android:contentDescription=&quot;Configuracion&quot;
        app:showAsAction=&quot;ifRoom&quot;
        android:icon=&quot;@drawable/options&quot;/&gt;

&lt;/menu&gt;

答案1

得分: 1

要使用Barista测试SearchView,您可以按照以下步骤进行操作:

添加Barista依赖项:
确保在您的Android项目中添加了Barista依赖项。您可以将其添加到您的应用级build.gradle文件中:

dependencies {
    // 其他依赖项
    androidTestImplementation 'com.schibsted.spain:barista:3.10.0'
}

启用Espresso Idling Resource(如果需要):
如果您的SearchView触发了网络请求或其他异步任务,您可能需要设置一个Espresso Idling Resource。然而,对于简单的情况,您可能不需要这一步。

编写测试:
现在,您可以编写测试用例来检查SearchView在点击搜索按钮时的可见性。假设您有一个具有菜单中的SearchView的MainActivity。通过点击搜索菜单项来控制SearchView的可见性。

import android.widget.SearchView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertNotDisplayed
import org.junit.Rule
import org.junit.Test

class MainActivityTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Test
    fun testSearchViewVisibility() {
        // 最初,SearchView不应该显示
        assertNotDisplayed(R.id.searchView)

        // 点击搜索菜单项
        onView(withId(R.id.action_search)).perform(click())

        // 现在,SearchView应该显示
        assertDisplayed(R.id.searchView)

        // 根据需要执行与SearchView相关的任何其他操作或断言
        // 例如,您可能想要输入文本并提交查询。

        // 再次点击搜索菜单项以关闭SearchView(如果需要)
        onView(withId(R.id.action_search)).perform(click())

        // SearchView不应再次显示
        assertNotDisplayed(R.id.searchView)
    }
}

在这个示例中,我们使用Barista来断言SearchView的可见性。assertDisplayed检查视图是否可见,而assertNotDisplayed检查视图是否不可见。您还可以使用其他Barista函数与SearchView进行交互,如typeTextIntoSearchView、submitSearchView等,具体取决于您的特定用例。

请记住将MainActivity::class.java替换为您想要测试的实际活动类。还要确保测试中使用的资源ID(例如R.id.searchView、R.id.action_search)与您的应用中的正确视图对应。

英文:

To test the SearchView using Barista, you can follow these steps:

Add Barista Dependency:
Make sure you have added the Barista dependency in your Android project. You can add it to your app-level build.gradle file:

dependencies {
    // Other dependencies
    androidTestImplementation &#39;com.schibsted.spain:barista:3.10.0&#39;
}

Enable Espresso Idling Resource (if necessary):
If your SearchView triggers a network request or other asynchronous tasks, you might need to set up an Espresso Idling Resource. However, for simpler cases, you may not need this step.

Write the Test:
Now you can write the test case to check the visibility of the SearchView whenever the search button is clicked. Let's assume you have a MainActivity with a SearchView in the menu. The visibility of the SearchView is controlled by clicking a search menu item.

import android.widget.SearchView
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertDisplayed
import com.schibsted.spain.barista.assertion.BaristaVisibilityAssertions.assertNotDisplayed
import org.junit.Rule
import org.junit.Test

class MainActivityTest {

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Test
    fun testSearchViewVisibility() {
        // Initially, the SearchView should not be displayed
        assertNotDisplayed(R.id.searchView)

        // Click on the search menu item
        onView(withId(R.id.action_search)).perform(click())

        // Now, the SearchView should be displayed
        assertDisplayed(R.id.searchView)

        // Perform any additional actions or assertions related to the SearchView as needed
        // For example, you might want to type text and submit the query.

        // Click on the search menu item again to close the SearchView (if required)
        onView(withId(R.id.action_search)).perform(click())

        // The SearchView should not be displayed again
        assertNotDisplayed(R.id.searchView)
    }
}

In this example, we use Barista to assert the visibility of the SearchView. assertDisplayed checks that the view is visible, while assertNotDisplayed checks that the view is not visible. You can also use other Barista functions to interact with the SearchView, such as typeTextIntoSearchView, submitSearchView, etc., depending on your specific use case.

Remember to replace MainActivity::class.java with the actual activity class you want to test. Also, ensure that the resource IDs (e.g., R.id.searchView, R.id.action_search) used in the test correspond to the correct views in your app.

huangapple
  • 本文由 发表于 2023年7月27日 20:20:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定