androidtestingandroid-espressobarista

Android: How to test a searchView within an AppBarLayout


This is my code:

         <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>

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:

<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>

Solution

  • 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 'com.schibsted.spain:barista:3.10.0'
    }
    

    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.