From 904e4826fa2def91f541331efd05028ae55119fa Mon Sep 17 00:00:00 2001 From: frankknoll Date: Sun, 15 Dec 2024 22:18:50 +0100 Subject: [PATCH] refactoring some tests --- .../fragments/search/SearchButtonClick.java | 70 +++++++++++ .../SettingsSearchParameterizedTest.java | 69 +++++++++++ .../fragments/search/SettingsSearchTest.java | 111 +----------------- 3 files changed, 145 insertions(+), 105 deletions(-) create mode 100644 OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SearchButtonClick.java create mode 100644 OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java diff --git a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SearchButtonClick.java b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SearchButtonClick.java new file mode 100644 index 00000000000..08cb22dadea --- /dev/null +++ b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SearchButtonClick.java @@ -0,0 +1,70 @@ +package net.osmand.plus.settings.fragments.search; + +import static androidx.test.espresso.Espresso.onData; +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.withClassName; +import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static net.osmand.test.common.Matchers.childAtPosition; +import static net.osmand.test.common.OsmAndDialogInteractions.skipAppStartDialogs; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.is; + +import android.view.View; + +import androidx.test.espresso.DataInteraction; + +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; + +import org.hamcrest.Matcher; + +class SearchButtonClick { + + public static void clickSearchButton(final OsmandApplication osmandApplication) { + skipAppStartDialogs(osmandApplication); + onView(mapMenuButton()).perform(click()); + settingsButton().perform(click()); + onView(searchButton()).perform(click()); + } + + private static Matcher mapMenuButton() { + return allOf( + withId(R.id.map_menu_button), + withContentDescription("Back to menu"), + childAtPosition( + allOf( + withId(R.id.map_hud_layout), + childAtPosition( + withId(R.id.map_hud_container), + 0)), + 10), + isDisplayed()); + } + + private static DataInteraction settingsButton() { + return onData(anything()) + .inAdapterView( + allOf( + withId(R.id.menuItems), + childAtPosition( + withId(R.id.drawer_relative_layout), + 0))) + .atPosition(13); + } + + private static Matcher searchButton() { + return allOf( + withId(R.id.action_button), + childAtPosition( + allOf(withId(R.id.actions_container), + childAtPosition( + withClassName(is("android.widget.LinearLayout")), + 2)), + 0), + isDisplayed()); + } +} diff --git a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java new file mode 100644 index 00000000000..aaf591e6538 --- /dev/null +++ b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchParameterizedTest.java @@ -0,0 +1,69 @@ +package net.osmand.plus.settings.fragments.search; + +import static androidx.test.espresso.Espresso.onView; +import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; +import static androidx.test.espresso.action.ViewActions.replaceText; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static net.osmand.plus.settings.fragments.search.SearchButtonClick.clickSearchButton; +import static net.osmand.plus.settings.fragments.search.SettingsSearchTest.hasSearchResultWithSubstring; +import static net.osmand.plus.settings.fragments.search.SettingsSearchTest.searchResultsView; +import static net.osmand.plus.settings.fragments.search.SettingsSearchTest.searchView; + +import androidx.annotation.StringRes; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.filters.LargeTest; + +import net.osmand.plus.R; +import net.osmand.plus.activities.MapActivity; +import net.osmand.test.common.AndroidTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; + +@LargeTest +@RunWith(Parameterized.class) +public class SettingsSearchParameterizedTest extends AndroidTest { + + @Parameterized.Parameter(value = 0) + public String description; + + @Parameterized.Parameter(value = 1) + public @StringRes int searchQueryId; + + @Parameterized.Parameters(name = "{0}") + public static Iterable data() { + return Arrays.asList( + new Object[][]{ + {"RecalculateRoute", R.string.recalculate_route}, + {"AnnouncementTimeBottomSheet_title", R.string.announcement_time_title}, + {"AnnouncementTimeBottomSheet_description", R.string.announcement_time_descr}, + {"FuelTankCapacityBottomSheet_description", R.string.fuel_tank_capacity_description}, + {"RecalculateRouteInDeviationBottomSheet_title", R.string.recalculate_route_in_deviation}, + {"RecalculateRouteInDeviationBottomSheet_description", R.string.select_distance_route_will_recalc}, + {"RecalculateRouteInDeviationBottomSheet_longDescription", R.string.recalculate_route_distance_promo} + }); + } + + @Rule + public ActivityScenarioRule mActivityScenarioRule = new ActivityScenarioRule<>(MapActivity.class); + + @Test + public void testSearchAndFind() { + testSearchAndFind(app.getResources().getString(searchQueryId)); + } + + private void testSearchAndFind(final String searchQuery) { + // Given + clickSearchButton(app); + + // When + onView(searchView()).perform(replaceText(searchQuery), closeSoftKeyboard()); + + // Then + onView(searchResultsView()).check(matches(hasSearchResultWithSubstring(searchQuery))); + } +} diff --git a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchTest.java b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchTest.java index e0dc570f20b..023d39085d4 100644 --- a/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchTest.java +++ b/OsmAnd/test/java/net/osmand/plus/settings/fragments/search/SettingsSearchTest.java @@ -1,32 +1,26 @@ package net.osmand.plus.settings.fragments.search; -import static androidx.test.espresso.Espresso.onData; import static androidx.test.espresso.Espresso.onView; -import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard; import static androidx.test.espresso.action.ViewActions.replaceText; import static androidx.test.espresso.assertion.ViewAssertions.matches; import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant; import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; import static androidx.test.espresso.matcher.ViewMatchers.withClassName; -import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription; import static androidx.test.espresso.matcher.ViewMatchers.withId; import static androidx.test.espresso.matcher.ViewMatchers.withSubstring; +import static net.osmand.plus.settings.fragments.search.SearchButtonClick.clickSearchButton; import static net.osmand.test.common.Matchers.childAtPosition; import static net.osmand.test.common.Matchers.recyclerViewHasItem; -import static net.osmand.test.common.OsmAndDialogInteractions.skipAppStartDialogs; import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.anything; import static org.hamcrest.Matchers.is; import android.view.View; -import androidx.test.espresso.DataInteraction; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; -import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; import net.osmand.test.common.AndroidTest; @@ -47,7 +41,7 @@ public class SettingsSearchTest extends AndroidTest { @Test public void shouldSearchAndFindProfileAppearanceSettings4EachApplicationMode() { // Given - clickSearchButton(); + clickSearchButton(app); // When onView(searchView()).perform(replaceText("profile appearance"), closeSoftKeyboard()); @@ -62,7 +56,7 @@ public void shouldSearchAndFindProfileAppearanceSettings4EachApplicationMode() { @Test public void shouldSearchAndFindSpeedCameraSettings4EachApplicationMode() { // Given - clickSearchButton(); + clickSearchButton(app); // When onView(searchView()).perform(replaceText("speed cameras"), closeSoftKeyboard()); @@ -74,101 +68,8 @@ public void shouldSearchAndFindSpeedCameraSettings4EachApplicationMode() { .forEach(path -> onView(searchResultsView()).check(matches(hasSearchResultWithSubstring(path)))); } - @Test - public void shouldSearchAndFindRecalculateRoutePreference() { - testSearchAndFind("Recalculate route"); - } - - @Test - public void test_search_within_AnnouncementTimeBottomSheet_title() { - testSearchAndFind(R.string.announcement_time_title); - } - - @Test - public void test_search_within_AnnouncementTimeBottomSheet_description() { - testSearchAndFind(R.string.announcement_time_descr); - } - - @Test - public void test_search_within_FuelTankCapacityBottomSheet_description() { - testSearchAndFind(R.string.fuel_tank_capacity_description); - } - - @Test - public void test_search_within_RecalculateRouteInDeviationBottomSheet_title() { - testSearchAndFind(R.string.recalculate_route_in_deviation); - } - - @Test - public void test_search_within_RecalculateRouteInDeviationBottomSheet_description() { - testSearchAndFind(R.string.select_distance_route_will_recalc); - } - - @Test - public void test_search_within_RecalculateRouteInDeviationBottomSheet_longDescription() { - testSearchAndFind(R.string.recalculate_route_distance_promo); - } - - private void testSearchAndFind(final int id) { - testSearchAndFind(app.getResources().getString(id)); - } - - // FK-TODO: move all tests invoking this test method to a new parameterized test class - private void testSearchAndFind(final String searchQuery) { - // Given - clickSearchButton(); - - // When - onView(searchView()).perform(replaceText(searchQuery), closeSoftKeyboard()); - - // Then - onView(searchResultsView()).check(matches(hasSearchResultWithSubstring(searchQuery))); - } - - private void clickSearchButton() { - skipAppStartDialogs(app); - onView(mapMenuButton()).perform(click()); - settingsButton().perform(click()); - onView(searchButton()).perform(click()); - } - - private static Matcher mapMenuButton() { - return allOf( - withId(R.id.map_menu_button), - withContentDescription("Back to menu"), - childAtPosition( - allOf(withId(R.id.map_hud_layout), - childAtPosition( - withId(R.id.map_hud_container), - 0)), - 10), - isDisplayed()); - } - - private static DataInteraction settingsButton() { - return onData(anything()) - .inAdapterView( - allOf( - withId(R.id.menuItems), - childAtPosition( - withId(R.id.drawer_relative_layout), - 0))) - .atPosition(13); - } - - private static Matcher searchButton() { - return allOf( - withId(R.id.action_button), - childAtPosition( - allOf(withId(R.id.actions_container), - childAtPosition( - withClassName(is("android.widget.LinearLayout")), - 2)), - 0), - isDisplayed()); - } - private static Matcher searchView() { + public static Matcher searchView() { return allOf( withClassName(is("android.widget.SearchView$SearchAutoComplete")), childAtPosition( @@ -181,13 +82,13 @@ private static Matcher searchView() { isDisplayed()); } - private static Matcher searchResultsView() { + public static Matcher searchResultsView() { return allOf( withId(SearchResultsFragmentUI.SEARCH_RESULTS_VIEW_ID), isDisplayed()); } - private static Matcher hasSearchResultWithSubstring(final String substring) { + public static Matcher hasSearchResultWithSubstring(final String substring) { return recyclerViewHasItem(hasDescendant(withSubstring(substring))); } }