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