Skip to content

Commit

Permalink
refactoring some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KnollFrank committed Dec 15, 2024
1 parent 3e3e3d8 commit 904e482
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -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<View> 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<View> searchButton() {
return allOf(
withId(R.id.action_button),
childAtPosition(
allOf(withId(R.id.actions_container),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
2)),
0),
isDisplayed());
}
}
Original file line number Diff line number Diff line change
@@ -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<Object[]> 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<MapActivity> 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)));
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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());
Expand All @@ -62,7 +56,7 @@ public void shouldSearchAndFindProfileAppearanceSettings4EachApplicationMode() {
@Test
public void shouldSearchAndFindSpeedCameraSettings4EachApplicationMode() {
// Given
clickSearchButton();
clickSearchButton(app);

// When
onView(searchView()).perform(replaceText("speed cameras"), closeSoftKeyboard());
Expand All @@ -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<View> 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<View> 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<View> searchView() {
public static Matcher<View> searchView() {
return allOf(
withClassName(is("android.widget.SearchView$SearchAutoComplete")),
childAtPosition(
Expand All @@ -181,13 +82,13 @@ private static Matcher<View> searchView() {
isDisplayed());
}

private static Matcher<View> searchResultsView() {
public static Matcher<View> searchResultsView() {
return allOf(
withId(SearchResultsFragmentUI.SEARCH_RESULTS_VIEW_ID),
isDisplayed());
}

private static Matcher<View> hasSearchResultWithSubstring(final String substring) {
public static Matcher<View> hasSearchResultWithSubstring(final String substring) {
return recyclerViewHasItem(hasDescendant(withSubstring(substring)));
}
}

0 comments on commit 904e482

Please sign in to comment.