From aa34f5d55b5dff21135d445f340df03befb73ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rom=C3=A1n=20Mart=C3=ADnez?= Date: Fri, 21 May 2021 20:23:48 +0200 Subject: [PATCH] Bugfix: category change didn't update speed/pace correctly. Espresso test added to check that unit is properly. --- .../opentracks/EspressoCategoryUnitTest.java | 185 ++++++++++++++++++ .../StatisticsRecordingFragment.java | 2 +- 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/androidTest/java/de/dennisguse/opentracks/EspressoCategoryUnitTest.java diff --git a/src/androidTest/java/de/dennisguse/opentracks/EspressoCategoryUnitTest.java b/src/androidTest/java/de/dennisguse/opentracks/EspressoCategoryUnitTest.java new file mode 100644 index 000000000..3aa480813 --- /dev/null +++ b/src/androidTest/java/de/dennisguse/opentracks/EspressoCategoryUnitTest.java @@ -0,0 +1,185 @@ +package de.dennisguse.opentracks; + + +import android.view.View; +import android.view.ViewGroup; +import android.view.ViewParent; + +import androidx.test.espresso.DataInteraction; +import androidx.test.espresso.UiController; +import androidx.test.espresso.ViewAction; +import androidx.test.espresso.ViewInteraction; +import androidx.test.filters.LargeTest; +import androidx.test.rule.ActivityTestRule; +import androidx.test.rule.GrantPermissionRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +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.scrollTo; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +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.withText; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anything; +import static org.hamcrest.Matchers.is; + +/** + * Espresso test that checks the unit for activity category selected. + * Also it checks that unit is properly when user change from a pace activity to an speed one. + */ +@LargeTest +@RunWith(AndroidJUnit4.class) +public class EspressoCategoryUnitTest { + + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(TrackListActivity.class); + + @Rule + public GrantPermissionRule mGrantPermissionRule = + GrantPermissionRule.grant( + "android.permission.ACCESS_FINE_LOCATION"); + + @Test + public void changeBetweenCategoryUpdateProperlyUnitTest() { + // Start record. + ViewInteraction appCompatImageButton = onView( + allOf(withId(R.id.controller_record), withContentDescription("Record"), + childAtPosition( + allOf(withId(R.id.controller_container), + childAtPosition( + withId(R.id.controller_fragment), + 0)), + 0), + isDisplayed())); + appCompatImageButton.perform(click()); + + { + // Change to running activity (pace activity). + ViewInteraction appCompatImageView = onView( + allOf(withId(R.id.stats_activity_type_icon), + childAtPosition( + childAtPosition( + withClassName(is("android.widget.ScrollView")), + 0), + 7))); + appCompatImageView.perform(scrollTo(), click()); + + DataInteraction imageView = onData(anything()) + .inAdapterView(allOf(withId(R.id.choose_activity_type_grid_view), + childAtPosition( + withClassName(is("android.widget.LinearLayout")), + 0))) + .atPosition(1); + imageView.perform(click()); + } + + // Pace (min/km) in all fields. + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_speed_unit))); + textView.perform(waitFor(2000)); + textView.check(matches(withText("min/km"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_average_speed_unit))); + textView.check(matches(withText("min/km"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_max_speed_unit))); + textView.check(matches(withText("min/km"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_moving_speed_unit))); + textView.check(matches(withText("min/km"))); + } + + { + // Change to biking activity (speed activity). + ViewInteraction appCompatImageView = onView( + allOf(withId(R.id.stats_activity_type_icon), + childAtPosition( + childAtPosition( + withClassName(is("android.widget.ScrollView")), + 0), + 7))); + appCompatImageView.perform(scrollTo(), click()); + + DataInteraction imageView = onData(anything()) + .inAdapterView(allOf(withId(R.id.choose_activity_type_grid_view), + childAtPosition( + withClassName(is("android.widget.LinearLayout")), + 0))) + .atPosition(9); + imageView.perform(click()); + } + + // Speed (km/h) in all fields. + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_speed_unit))); + textView.perform(waitFor(2000)); + textView.check(matches(withText("km/h"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_average_speed_unit))); + textView.check(matches(withText("km/h"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_max_speed_unit))); + textView.check(matches(withText("km/h"))); + } + { + ViewInteraction textView = onView(allOf(withId(R.id.stats_moving_speed_unit))); + textView.check(matches(withText("km/h"))); + } + } + + private static Matcher childAtPosition( + final Matcher parentMatcher, final int position) { + + return new TypeSafeMatcher() { + @Override + public void describeTo(Description description) { + description.appendText("Child at position " + position + " in parent "); + parentMatcher.describeTo(description); + } + + @Override + public boolean matchesSafely(View view) { + ViewParent parent = view.getParent(); + return parent instanceof ViewGroup && parentMatcher.matches(parent) + && view.equals(((ViewGroup) parent).getChildAt(position)); + } + }; + } + + private static ViewAction waitFor(final long duration_ms) { + return new ViewAction() { + + @Override + public String getDescription() { + return "Wait for milliseconds."; + } + + @Override + public Matcher getConstraints() { + return isDisplayed(); + } + + @Override + public void perform(UiController uiController, final View view) { + uiController.loopMainThreadForAtLeast(duration_ms); + } + }; + } +} diff --git a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordingFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordingFragment.java index 337e8a346..aa4f216d8 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordingFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordingFragment.java @@ -307,12 +307,12 @@ public class StatisticsRecordingFragment extends Fragment { private void onRecordingDataChanged(TrackRecordingService.RecordingData recordingData) { String oldCategory = this.recordingData.getTrackCategory(); String newCategory = recordingData.getTrackCategory(); + this.recordingData = recordingData; if (!oldCategory.equals(newCategory)) { sharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferences, getString(R.string.stats_rate_key)); } - this.recordingData = recordingData; this.latestTrackPoint = recordingData.getLatestTrackPoint(); if (latestTrackPoint != null && latestTrackPoint.hasLocation() && !latestTrackPoint.isRecent()) { latestTrackPoint = null;