From c33c2de51dbd4bd4a2c03188be41cc196f3426cd Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Sun, 18 Jan 2026 18:28:49 +0100 Subject: [PATCH] Cleanup: EspressoUI should always lookup UI elements. --- .../opentracks/EspressoDeleteTrackTest.java | 15 +++++--- .../EspressoEditTrackRecordingTest.java | 35 +++++++++--------- .../dennisguse/opentracks/EspressoUITest.java | 12 +++--- .../EspressoAggregatedFilterTest.java | 37 +++++++++++-------- 4 files changed, 55 insertions(+), 44 deletions(-) diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java index 5375641f5..ebee9d368 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoDeleteTrackTest.java @@ -36,7 +36,8 @@ public class EspressoDeleteTrackTest { @Test public void espressoDeleteTrackTest() { // TrackListActivity: start recording - onView(withId(R.id.track_list_fab_action)).perform(click()); + onView(withId(R.id.track_list_fab_action)) + .perform(click()); // TrackRecordingActivity onView(withId(R.id.track_recording_fab_action)) @@ -46,10 +47,12 @@ public class EspressoDeleteTrackTest { .perform(longClick()); // TrackStoppedActivity - onView(withId(R.id.finish_button)).perform(click()); + onView(withId(R.id.finish_button)) + .perform(click()); // select track - onView(allOf(withParent(withId(R.id.track_list)), withParentIndex(0))).perform(longClick()); + onView(allOf(withParent(withId(R.id.track_list)), withParentIndex(0))) + .perform(longClick()); // open menu and delete selected track onView( @@ -62,9 +65,11 @@ public class EspressoDeleteTrackTest { isDisplayed())) .perform(click()); - onView(withText("Delete")).perform(click()); + onView(withText("Delete")) + .perform(click()); - onView(withText("OK")).perform(click()); + onView(withText("OK")) + .perform(click()); // tracklist is empty now onView(allOf(withText("Start recording your next adventure here"), isDisplayed())); diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoEditTrackRecordingTest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoEditTrackRecordingTest.java index f8b3dcaf8..adcfa5c83 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/EspressoEditTrackRecordingTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoEditTrackRecordingTest.java @@ -15,7 +15,6 @@ import static androidx.test.espresso.matcher.ViewMatchers.withText; import static org.hamcrest.Matchers.allOf; import static de.dennisguse.opentracks.util.EspressoUtils.waitFor; -import androidx.test.espresso.ViewInteraction; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; @@ -45,41 +44,43 @@ public class EspressoEditTrackRecordingTest { public void espressoEditTrackRecordingTest() { { // TrackListActivity: start recording - ViewInteraction fabRecordButton = onView(withId(R.id.track_list_fab_action)); - fabRecordButton.perform(click()); + onView(withId(R.id.track_list_fab_action)) + .perform(click()); } { // TrackRecordingActivity - ViewInteraction trackControllerStopButton = onView(withId(R.id.track_recording_fab_action)); // wait; stay recording - trackControllerStopButton.perform(waitFor(4000)); + onView(withId(R.id.track_recording_fab_action)) + .perform(waitFor(4000)); // open menu openContextualActionModeOverflowMenu(); // Click the item. - onView(withText(R.string.menu_edit)).perform(click()); + onView(withText(R.string.menu_edit)) + .perform(click()); // change name for "New Name" - ViewInteraction textInputEditText = onView(withId(R.id.track_edit_name)); - textInputEditText.perform(scrollTo(), replaceText("New Name")); + onView(withId(R.id.track_edit_name)) + .perform(scrollTo(), replaceText("New Name")); - ViewInteraction textInputEditText2 = onView(allOf(withId(R.id.track_edit_name), withText("New Name"), isDisplayed())); - textInputEditText2.perform(closeSoftKeyboard()); + onView(allOf(withId(R.id.track_edit_name), withText("New Name"), isDisplayed())) + .perform(closeSoftKeyboard()); // save edition - ViewInteraction appCompatButton = onView(withId(R.id.track_edit_save)); - appCompatButton.perform(click()); + onView(withId(R.id.track_edit_save)) + .perform(click()); // stop; - trackControllerStopButton.perform(longClick()); + onView(withId(R.id.track_recording_fab_action)) + .perform(longClick()); // it's on track stopped activity and there are two buttons - ViewInteraction resumeButton = onView(withId(R.id.resume_button)); - resumeButton.check(matches(isDisplayed())); - ViewInteraction finishButton = onView(withId(R.id.finish_button)); - finishButton.check(matches(isDisplayed())); + onView(withId(R.id.resume_button)) + .check(matches(isDisplayed())); + onView(withId(R.id.finish_button)) + .check(matches(isDisplayed())); } } } diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java index b74fb0ed6..42dc0b9c2 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoUITest.java @@ -37,8 +37,8 @@ public class EspressoUITest { public void record_stop_resume_stop_finish() { { // TrackListActivity: start recording - ViewInteraction trackControllerRecordButton = onView(withId(R.id.track_list_fab_action)); - trackControllerRecordButton.perform(click()); + onView(withId(R.id.track_list_fab_action)) + .perform(click()); } { // TrackRecordingActivity: wait to record some time and then stop @@ -66,13 +66,12 @@ public class EspressoUITest { public void record_move_through_tabs() { { // TrackListActivity: start recording - ViewInteraction trackControllerRecordButton = onView(withId(R.id.track_list_fab_action)); - trackControllerRecordButton.perform(click()); + onView(withId(R.id.track_list_fab_action)) + .perform(click()); } { // TrackRecordingActivity ViewInteraction tabLayout = onView(withId(R.id.track_detail_activity_tablayout)); - ViewInteraction trackControllerStopButton = onView(withId(R.id.track_recording_fab_action)); tabLayout.perform(selectTabAtIndex(1)); tabLayout.perform(waitFor(1000)); @@ -87,7 +86,8 @@ public class EspressoUITest { tabLayout.perform(waitFor(1000)); // stop - trackControllerStopButton.perform(longClick()); + onView(withId(R.id.track_recording_fab_action)). + perform(longClick()); } } diff --git a/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/EspressoAggregatedFilterTest.java b/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/EspressoAggregatedFilterTest.java index 55962a21b..5bbe07b5f 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/EspressoAggregatedFilterTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/EspressoAggregatedFilterTest.java @@ -14,7 +14,6 @@ import static de.dennisguse.opentracks.util.EspressoUtils.waitFor; import android.util.Pair; import androidx.test.core.app.ApplicationProvider; -import androidx.test.espresso.ViewInteraction; import androidx.test.espresso.matcher.ViewMatchers; import androidx.test.ext.junit.rules.ActivityScenarioRule; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -61,28 +60,34 @@ public class EspressoAggregatedFilterTest { @Test public void espressoAggregatedFilterTest() { // open AggregatedStatisticsActivity through toolbar's menu item - onView(ViewMatchers.withId(R.id.bottom_app_bar)).check(matches(isDisplayed())); - onView(withId(R.id.bottom_app_bar)).check(matches(hasDescendant(withId(R.id.aggregated_stats_button)))); - onView(withId(R.id.aggregated_stats_button)).perform(click()); + onView(ViewMatchers.withId(R.id.bottom_app_bar)) + .check(matches(isDisplayed())); + onView(withId(R.id.bottom_app_bar)) + .check(matches(hasDescendant(withId(R.id.aggregated_stats_button)))); + onView(withId(R.id.aggregated_stats_button)) + .perform(click()); // open FilterDialogFragment through toolbar's menu item - onView(withId(R.id.bottom_app_bar)).check(matches(isDisplayed())); - onView(withId(R.id.bottom_app_bar)).check(matches(hasDescendant(withId(R.id.aggregated_statistics_filter)))); - onView(withId(R.id.aggregated_statistics_filter)).perform(click()); + onView(withId(R.id.bottom_app_bar)) + .check(matches(isDisplayed())); + onView(withId(R.id.bottom_app_bar)) + .check(matches(hasDescendant(withId(R.id.aggregated_statistics_filter)))); + onView(withId(R.id.aggregated_statistics_filter)) + .perform(click()); // check there's a checkbox with ACTIVITY_TYPE_LOCALIZED text - ViewInteraction checkBox = onView( - allOf(withId(R.id.filter_dialog_check_button), withText(ACTIVITY_TYPE_LOCALIZED), - withParent(allOf(withId(R.id.filter_items), - withParent(IsInstanceOf.instanceOf(android.view.ViewGroup.class)))), - isDisplayed())); - checkBox.perform(waitFor(2000)); + onView(allOf(withId(R.id.filter_dialog_check_button), withText(ACTIVITY_TYPE_LOCALIZED), + withParent(allOf(withId(R.id.filter_items), + withParent(IsInstanceOf.instanceOf(android.view.ViewGroup.class)))), + isDisplayed())) + .perform(waitFor(2000)); // check there's an edit text for "from date" - onView(withId(R.id.filter_date_edit_text_from)).check(matches(isDisplayed())); + onView(withId(R.id.filter_date_edit_text_from)) + .check(matches(isDisplayed())); // check there's an edit text for "to date" - onView(withId(R.id.filter_date_edit_text_to)).check(matches(isDisplayed())); + onView(withId(R.id.filter_date_edit_text_to)) + .check(matches(isDisplayed())); } - }