forked from upstream-mirrors/OpenTracks
Make tests less flacky
This commit is contained in:
@@ -162,7 +162,6 @@ dependencies {
|
|||||||
androidTestImplementation 'androidx.test:rules:1.7.0'
|
androidTestImplementation 'androidx.test:rules:1.7.0'
|
||||||
androidTestImplementation 'androidx.test:runner:1.7.0'
|
androidTestImplementation 'androidx.test:runner:1.7.0'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.7.0'
|
||||||
androidTestImplementation 'org.mockito:mockito-android:5.23.0'
|
|
||||||
|
|
||||||
androidTestUtil 'androidx.test:orchestrator:1.6.1'
|
androidTestUtil 'androidx.test:orchestrator:1.6.1'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,76 +2,117 @@ package de.dennisguse.opentracks;
|
|||||||
|
|
||||||
|
|
||||||
import static androidx.test.espresso.Espresso.onView;
|
import static androidx.test.espresso.Espresso.onView;
|
||||||
import static androidx.test.espresso.action.ViewActions.click;
|
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
||||||
import static androidx.test.espresso.action.ViewActions.longClick;
|
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
|
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
|
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withParentIndex;
|
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
||||||
|
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
|
||||||
|
import static androidx.test.espresso.matcher.RootMatchers.isPlatformPopup;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClick;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClickOnAncestor;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performLongClickOnAncestor;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilDisplayed;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilRecyclerViewItemCount;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.withRecyclerViewItemCount;
|
||||||
|
import static de.dennisguse.opentracks.util.TestEnvironmentUtils.resetTrackRecordingServiceAndDeleteTracks;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.renameStoppedTrack;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.startRecordingFromTrackList;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.stopRecordingToTrackStopped;
|
||||||
import static org.hamcrest.Matchers.allOf;
|
import static org.hamcrest.Matchers.allOf;
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.childAtPosition;
|
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.waitFor;
|
|
||||||
|
|
||||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.filters.LargeTest;
|
import androidx.test.filters.LargeTest;
|
||||||
import androidx.test.rule.GrantPermissionRule;
|
import androidx.test.rule.GrantPermissionRule;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class EspressoDeleteTrackTest {
|
public class EspressoDeleteTrackTest {
|
||||||
|
|
||||||
|
private static final Duration UI_TIMEOUT = Duration.ofSeconds(10);
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public ActivityScenarioRule<TrackListActivity> mActivityTestRule = new ActivityScenarioRule<>(TrackListActivity.class);
|
public ActivityScenarioRule<TrackListActivity> mActivityTestRule = new ActivityScenarioRule<>(TrackListActivity.class);
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
||||||
|
|
||||||
@Test
|
@Before
|
||||||
public void espressoDeleteTrackTest() {
|
public void setUp() {
|
||||||
// TrackListActivity: start recording
|
resetTrackRecordingServiceAndDeleteTracks();
|
||||||
onView(withId(R.id.track_list_fab_action))
|
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
// TrackRecordingActivity
|
|
||||||
onView(withId(R.id.track_recording_fab_action))
|
|
||||||
// wait; stay recording
|
|
||||||
.perform(waitFor(5000))
|
|
||||||
// stop;
|
|
||||||
.perform(longClick());
|
|
||||||
|
|
||||||
// TrackStoppedActivity
|
|
||||||
onView(withId(R.id.finish_button))
|
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
// select track
|
|
||||||
onView(allOf(withParent(withId(R.id.track_list)), withParentIndex(0)))
|
|
||||||
.perform(longClick());
|
|
||||||
|
|
||||||
// open menu and delete selected track
|
|
||||||
onView(
|
|
||||||
allOf(withContentDescription("More options"),
|
|
||||||
childAtPosition(
|
|
||||||
childAtPosition(
|
|
||||||
withId(androidx.appcompat.R.id.action_mode_bar),
|
|
||||||
1),
|
|
||||||
2),
|
|
||||||
isDisplayed()))
|
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
onView(withText("Delete"))
|
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
onView(withText("OK"))
|
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
// tracklist is empty now
|
|
||||||
onView(allOf(withText("Start recording your next adventure here"), isDisplayed()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deleteTrackFromTrackStoppedActivity() {
|
||||||
|
recordTrackAndOpenTrackStoppedActivity();
|
||||||
|
|
||||||
|
// TrackStoppedActivity: discard the just-recorded track directly
|
||||||
|
onView(withId(R.id.discard_button))
|
||||||
|
.perform(performClick());
|
||||||
|
onView(withText(android.R.string.ok))
|
||||||
|
.inRoot(isDialog())
|
||||||
|
.perform(performClick());
|
||||||
|
waitUntilDisplayed(withId(R.id.track_list), UI_TIMEOUT);
|
||||||
|
waitUntilRecyclerViewItemCount(withId(R.id.track_list), 0, UI_TIMEOUT);
|
||||||
|
|
||||||
|
// the recycler is empty again after discarding the only recorded track
|
||||||
|
onView(withId(R.id.track_list))
|
||||||
|
.check(matches(withRecyclerViewItemCount(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void deleteTrackFromSelectedTrackInTrackList() {
|
||||||
|
String uniqueTrackName = "DeleteFromTrackList-" + System.currentTimeMillis();
|
||||||
|
|
||||||
|
recordTrackAndOpenTrackStoppedActivity();
|
||||||
|
renameStoppedTrack(uniqueTrackName, UI_TIMEOUT);
|
||||||
|
|
||||||
|
onView(withId(R.id.finish_button))
|
||||||
|
.perform(performClick());
|
||||||
|
waitUntilDisplayed(withId(R.id.track_list), UI_TIMEOUT);
|
||||||
|
waitUntilDisplayed(allOf(withId(R.id.track_list_item_name), withText(uniqueTrackName)), UI_TIMEOUT);
|
||||||
|
waitUntilRecyclerViewItemCount(withId(R.id.track_list), 1, UI_TIMEOUT);
|
||||||
|
|
||||||
|
onView(allOf(withId(R.id.track_list_item_name), withText(uniqueTrackName)))
|
||||||
|
.perform(performLongClickOnAncestor());
|
||||||
|
|
||||||
|
clickDeleteSelectedTrackAction();
|
||||||
|
onView(withText(android.R.string.ok))
|
||||||
|
.inRoot(isDialog())
|
||||||
|
.perform(performClick());
|
||||||
|
waitUntilRecyclerViewItemCount(withId(R.id.track_list), 0, UI_TIMEOUT);
|
||||||
|
|
||||||
|
onView(withId(R.id.track_list))
|
||||||
|
.check(matches(withRecyclerViewItemCount(0)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void recordTrackAndOpenTrackStoppedActivity() {
|
||||||
|
startRecordingFromTrackList(UI_TIMEOUT);
|
||||||
|
stopRecordingToTrackStopped(UI_TIMEOUT, R.id.discard_button, R.id.finish_button);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clickDeleteSelectedTrackAction() {
|
||||||
|
try {
|
||||||
|
waitUntilDisplayed(withId(R.id.list_context_menu_delete), UI_TIMEOUT);
|
||||||
|
onView(withId(R.id.list_context_menu_delete))
|
||||||
|
.perform(performClick());
|
||||||
|
} catch (AssertionError ignored) {
|
||||||
|
onView(allOf(
|
||||||
|
withContentDescription(androidx.appcompat.R.string.abc_action_menu_overflow_description),
|
||||||
|
isDescendantOfA(withId(androidx.appcompat.R.id.action_mode_bar))))
|
||||||
|
.perform(performClick());
|
||||||
|
onView(withText(R.string.menu_delete))
|
||||||
|
.inRoot(isPlatformPopup())
|
||||||
|
.perform(performClickOnAncestor());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,34 +2,43 @@ package de.dennisguse.opentracks;
|
|||||||
|
|
||||||
|
|
||||||
import static androidx.test.espresso.Espresso.onView;
|
import static androidx.test.espresso.Espresso.onView;
|
||||||
import static androidx.test.espresso.Espresso.openContextualActionModeOverflowMenu;
|
|
||||||
import static androidx.test.espresso.action.ViewActions.click;
|
|
||||||
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
|
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
|
||||||
import static androidx.test.espresso.action.ViewActions.longClick;
|
|
||||||
import static androidx.test.espresso.action.ViewActions.replaceText;
|
import static androidx.test.espresso.action.ViewActions.replaceText;
|
||||||
import static androidx.test.espresso.action.ViewActions.scrollTo;
|
import static androidx.test.espresso.action.ViewActions.scrollTo;
|
||||||
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
||||||
|
import static androidx.test.espresso.matcher.RootMatchers.isPlatformPopup;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||||
|
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
|
||||||
|
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClick;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClickOnAncestor;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilDisplayed;
|
||||||
|
import static de.dennisguse.opentracks.util.TestEnvironmentUtils.resetTrackRecordingServiceAndDeleteTracks;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.startRecordingFromTrackList;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.stopRecordingToTrackStopped;
|
||||||
import static org.hamcrest.Matchers.allOf;
|
import static org.hamcrest.Matchers.allOf;
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.waitFor;
|
|
||||||
|
|
||||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.filters.LargeTest;
|
import androidx.test.filters.LargeTest;
|
||||||
import androidx.test.rule.GrantPermissionRule;
|
import androidx.test.rule.GrantPermissionRule;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class EspressoEditTrackRecordingTest {
|
public class EspressoEditTrackRecordingTest {
|
||||||
|
|
||||||
|
private static final Duration UI_TIMEOUT = Duration.ofSeconds(10);
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final LocaleRule mLocaleRule = new LocaleRule(Locale.ENGLISH);
|
public final LocaleRule mLocaleRule = new LocaleRule(Locale.ENGLISH);
|
||||||
|
|
||||||
@@ -39,27 +48,27 @@ public class EspressoEditTrackRecordingTest {
|
|||||||
@Rule
|
@Rule
|
||||||
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
resetTrackRecordingServiceAndDeleteTracks();
|
||||||
|
}
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@Test
|
@Test
|
||||||
public void espressoEditTrackRecordingTest() {
|
public void espressoEditTrackRecordingTest() {
|
||||||
{
|
startRecordingFromTrackList(UI_TIMEOUT);
|
||||||
// TrackListActivity: start recording
|
|
||||||
onView(withId(R.id.track_list_fab_action))
|
|
||||||
.perform(click());
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
// TrackRecordingActivity
|
// TrackRecordingActivity
|
||||||
|
|
||||||
// wait; stay recording
|
|
||||||
onView(withId(R.id.track_recording_fab_action))
|
|
||||||
.perform(waitFor(4000));
|
|
||||||
|
|
||||||
// open menu
|
// open menu
|
||||||
openContextualActionModeOverflowMenu();
|
onView(allOf(
|
||||||
|
withContentDescription(androidx.appcompat.R.string.abc_action_menu_overflow_description),
|
||||||
// Click the item.
|
isDescendantOfA(withId(R.id.bottom_app_bar))))
|
||||||
|
.perform(performClick());
|
||||||
onView(withText(R.string.menu_edit))
|
onView(withText(R.string.menu_edit))
|
||||||
.perform(click());
|
.inRoot(isPlatformPopup())
|
||||||
|
.perform(performClickOnAncestor());
|
||||||
|
waitUntilDisplayed(withId(R.id.track_edit_name), UI_TIMEOUT);
|
||||||
|
|
||||||
// change name for "New Name"
|
// change name for "New Name"
|
||||||
onView(withId(R.id.track_edit_name))
|
onView(withId(R.id.track_edit_name))
|
||||||
@@ -70,11 +79,11 @@ public class EspressoEditTrackRecordingTest {
|
|||||||
|
|
||||||
// save edition
|
// save edition
|
||||||
onView(withId(R.id.track_edit_save))
|
onView(withId(R.id.track_edit_save))
|
||||||
.perform(click());
|
.perform(performClick());
|
||||||
|
waitUntilDisplayed(withId(R.id.track_recording_fab_action), UI_TIMEOUT);
|
||||||
|
|
||||||
// stop;
|
// stop;
|
||||||
onView(withId(R.id.track_recording_fab_action))
|
stopRecordingToTrackStopped(UI_TIMEOUT, R.id.resume_button, R.id.finish_button);
|
||||||
.perform(longClick());
|
|
||||||
|
|
||||||
// it's on track stopped activity and there are two buttons
|
// it's on track stopped activity and there are two buttons
|
||||||
onView(withId(R.id.resume_button))
|
onView(withId(R.id.resume_button))
|
||||||
|
|||||||
@@ -1,100 +1,142 @@
|
|||||||
package de.dennisguse.opentracks;
|
package de.dennisguse.opentracks;
|
||||||
|
|
||||||
import static androidx.test.espresso.Espresso.onView;
|
import static androidx.test.espresso.Espresso.onView;
|
||||||
import static androidx.test.espresso.action.ViewActions.click;
|
|
||||||
import static androidx.test.espresso.action.ViewActions.longClick;
|
|
||||||
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isClickable;
|
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
|
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withParentIndex;
|
import static de.dennisguse.opentracks.util.EspressoUtils.performLongClickOnAncestor;
|
||||||
import static org.hamcrest.Matchers.allOf;
|
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.selectTabAtIndex;
|
import static de.dennisguse.opentracks.util.EspressoUtils.selectTabAtIndex;
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.waitFor;
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilDisplayed;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitForResumedViewCondition;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.withSelectedTab;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.withViewPagerCurrentItem;
|
||||||
|
import static de.dennisguse.opentracks.util.TestEnvironmentUtils.resetTrackRecordingServiceAndDeleteTracks;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.finishStoppedTrack;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.resumeStoppedTrack;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.startRecordingFromTrackList;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackUiTestUtils.stopRecordingToTrackStopped;
|
||||||
|
import static org.hamcrest.Matchers.allOf;
|
||||||
|
|
||||||
|
import android.util.Pair;
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import androidx.test.espresso.ViewInteraction;
|
import androidx.test.espresso.ViewInteraction;
|
||||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.filters.LargeTest;
|
import androidx.test.filters.LargeTest;
|
||||||
import androidx.test.rule.GrantPermissionRule;
|
import androidx.test.rule.GrantPermissionRule;
|
||||||
|
|
||||||
|
import org.hamcrest.Matcher;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.RuleChain;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
||||||
|
import de.dennisguse.opentracks.data.ContentProviderUtils;
|
||||||
|
import de.dennisguse.opentracks.data.models.Track;
|
||||||
|
import de.dennisguse.opentracks.data.models.TrackBuilder;
|
||||||
|
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class EspressoUITest {
|
public class EspressoUITest {
|
||||||
|
|
||||||
@Rule
|
private static final Duration UI_TIMEOUT = Duration.ofSeconds(10);
|
||||||
public ActivityScenarioRule<TrackListActivity> mActivityTestRule = new ActivityScenarioRule<>(TrackListActivity.class);
|
|
||||||
|
private final ActivityScenarioRule<TrackListActivity> activityRule = new ActivityScenarioRule<>(TrackListActivity.class);
|
||||||
|
private final GrantPermissionRule grantPermissionRule = TestUtil.createGrantPermissionRule();
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
public RuleChain rules = RuleChain.outerRule(grantPermissionRule)
|
||||||
|
.around(activityRule);
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
resetTrackRecordingServiceAndDeleteTracks();
|
||||||
|
}
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@Test
|
@Test
|
||||||
public void record_stop_resume_stop_finish() {
|
public void record_stop_resume_stop_finish() {
|
||||||
|
startRecordingFromTrackList(UI_TIMEOUT);
|
||||||
{
|
{
|
||||||
// TrackListActivity: start recording
|
stopRecordingToTrackStopped(UI_TIMEOUT, R.id.resume_button);
|
||||||
onView(withId(R.id.track_list_fab_action))
|
resumeStoppedTrack(UI_TIMEOUT);
|
||||||
.perform(click());
|
stopRecordingToTrackStopped(UI_TIMEOUT, R.id.finish_button);
|
||||||
}
|
finishStoppedTrack(UI_TIMEOUT);
|
||||||
{
|
|
||||||
// TrackRecordingActivity: wait to record some time and then stop
|
|
||||||
onView(withId(R.id.track_recording_fab_action))
|
|
||||||
.perform(waitFor(5000))
|
|
||||||
.perform(longClick());
|
|
||||||
|
|
||||||
// TrackStoppedActivity: resume
|
onView(withId(R.id.track_list))
|
||||||
onView(allOf(withId(R.id.resume_button), isClickable()))
|
.check(matches(isDisplayed()));
|
||||||
.perform(click());
|
|
||||||
|
|
||||||
// TrackRecordingActivity: wait and then stop
|
|
||||||
onView(withId(R.id.track_recording_fab_action))
|
|
||||||
.perform(waitFor(5000))
|
|
||||||
.perform(longClick());
|
|
||||||
|
|
||||||
// TrackStoppedActivity
|
|
||||||
onView(withId(R.id.finish_button))
|
|
||||||
.perform(click());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@Test
|
@Test
|
||||||
public void record_move_through_tabs() {
|
public void record_move_through_tabs() {
|
||||||
{
|
startRecordingFromTrackList(UI_TIMEOUT);
|
||||||
// TrackListActivity: start recording
|
waitUntilDisplayed(withId(R.id.track_detail_activity_tablayout), UI_TIMEOUT);
|
||||||
onView(withId(R.id.track_list_fab_action))
|
waitUntilDisplayed(withId(R.id.track_detail_activity_view_pager), UI_TIMEOUT);
|
||||||
.perform(click());
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
// TrackRecordingActivity
|
// TrackRecordingActivity
|
||||||
ViewInteraction tabLayout = onView(withId(R.id.track_detail_activity_tablayout));
|
ViewInteraction tabLayout = onView(withId(R.id.track_detail_activity_tablayout));
|
||||||
|
|
||||||
tabLayout.perform(selectTabAtIndex(1));
|
tabLayout.perform(selectTabAtIndex(1));
|
||||||
tabLayout.perform(waitFor(1000));
|
waitUntilTabSelectionSettlesAt(1);
|
||||||
|
|
||||||
tabLayout.perform(selectTabAtIndex(2));
|
tabLayout.perform(selectTabAtIndex(2));
|
||||||
tabLayout.perform(waitFor(1000));
|
waitUntilTabSelectionSettlesAt(2);
|
||||||
|
|
||||||
tabLayout.perform(selectTabAtIndex(3));
|
tabLayout.perform(selectTabAtIndex(3));
|
||||||
tabLayout.perform(waitFor(1000));
|
waitUntilTabSelectionSettlesAt(3);
|
||||||
|
|
||||||
tabLayout.perform(selectTabAtIndex(0));
|
tabLayout.perform(selectTabAtIndex(0));
|
||||||
tabLayout.perform(waitFor(1000));
|
waitUntilTabSelectionSettlesAt(0);
|
||||||
|
|
||||||
// stop
|
// stop
|
||||||
onView(withId(R.id.track_recording_fab_action)).
|
stopRecordingToTrackStopped(UI_TIMEOUT, R.id.resume_button, R.id.finish_button);
|
||||||
perform(longClick());
|
|
||||||
|
onView(withId(R.id.resume_button))
|
||||||
|
.check(matches(isDisplayed()));
|
||||||
|
onView(withId(R.id.finish_button))
|
||||||
|
.check(matches(isDisplayed()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@LargeTest
|
@LargeTest
|
||||||
@Test
|
@Test
|
||||||
public void selectAndDeleteTrack() {
|
public void selectAndDeleteTrack() {
|
||||||
|
Pair<Track, List<TrackPoint>> pair = TestDataUtil.createTrack(new Track.Id(System.currentTimeMillis()), 5);
|
||||||
|
String uniqueTrackName = "SelectAndDeleteTrack-" + System.currentTimeMillis();
|
||||||
|
TrackBuilder trackBuilder = new TrackBuilder(pair.first);
|
||||||
|
trackBuilder.setName(uniqueTrackName);
|
||||||
|
TestDataUtil.insertTrackWithLocations(new ContentProviderUtils(ApplicationProvider.getApplicationContext()), trackBuilder.getTrack(), pair.second);
|
||||||
|
activityRule.getScenario().recreate();
|
||||||
|
waitUntilDisplayed(allOf(withId(R.id.track_list_item_name), withText(uniqueTrackName)), UI_TIMEOUT);
|
||||||
|
|
||||||
onView(withId(R.id.track_list)).check(matches(isDisplayed()));
|
onView(withId(R.id.track_list)).check(matches(isDisplayed()));
|
||||||
onView(allOf(withParent(withId(R.id.track_list)), withParentIndex(0))).perform(longClick());
|
onView(allOf(withId(R.id.track_list_item_name), withText(uniqueTrackName)))
|
||||||
|
.perform(performLongClickOnAncestor());
|
||||||
|
|
||||||
|
onView(withId(androidx.appcompat.R.id.action_mode_bar))
|
||||||
|
.check(matches(isDisplayed()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitUntilTabSelectionSettlesAt(int index) {
|
||||||
|
waitForResumedViewCondition(
|
||||||
|
withId(R.id.track_detail_activity_tablayout),
|
||||||
|
view -> withSelectedTab(index).matches(view),
|
||||||
|
"selected tab to be " + index,
|
||||||
|
UI_TIMEOUT);
|
||||||
|
waitForResumedViewCondition(
|
||||||
|
withId(R.id.track_detail_activity_view_pager),
|
||||||
|
view -> withViewPagerCurrentItem(index).matches(view),
|
||||||
|
"ViewPager current item to be " + index,
|
||||||
|
UI_TIMEOUT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+62
-46
@@ -20,22 +20,19 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.ContentUris;
|
import android.content.ContentUris;
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
|
import android.database.MatrixCursor;
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -78,7 +75,6 @@ import de.dennisguse.opentracks.util.FileUtils;
|
|||||||
* @author Bartlomiej Niechwiej
|
* @author Bartlomiej Niechwiej
|
||||||
* @author Youtao Liu
|
* @author Youtao Liu
|
||||||
*/
|
*/
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
|
||||||
public class CustomContentProviderUtilsTest {
|
public class CustomContentProviderUtilsTest {
|
||||||
|
|
||||||
private static final String NAME_PREFIX = "test name";
|
private static final String NAME_PREFIX = "test name";
|
||||||
@@ -90,12 +86,6 @@ public class CustomContentProviderUtilsTest {
|
|||||||
private final Context context = ApplicationProvider.getApplicationContext();
|
private final Context context = ApplicationProvider.getApplicationContext();
|
||||||
private ContentProviderUtils contentProviderUtils;
|
private ContentProviderUtils contentProviderUtils;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private Cursor cursorMock;
|
|
||||||
|
|
||||||
@Mock
|
|
||||||
private ContentResolver contentResolverMock;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
contentProviderUtils = new ContentProviderUtils(context);
|
contentProviderUtils = new ContentProviderUtils(context);
|
||||||
@@ -104,21 +94,21 @@ public class CustomContentProviderUtilsTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLocationIterator_noPoints() {
|
public void testLocationIterator_noPoints() {
|
||||||
testIterator(new Track.Id(1), 0);
|
assertEquals(0, testIterator(new Track.Id(1), 0).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLocationIterator_noAscending() {
|
public void testLocationIterator_noAscending() {
|
||||||
testIterator(new Track.Id(1), 50);
|
assertEquals(50, testIterator(new Track.Id(1), 50).size());
|
||||||
testIterator(new Track.Id(2), 50);
|
assertEquals(50, testIterator(new Track.Id(2), 50).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLocationIterator_largeTrack() {
|
public void testLocationIterator_largeTrack() {
|
||||||
testIterator(new Track.Id(1), 20000 / 2);
|
assertEquals(20000 / 2, testIterator(new Track.Id(1), 20000 / 2).size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testIterator(Track.Id trackId, int numPoints) {
|
private List<TrackPoint> testIterator(Track.Id trackId, int numPoints) {
|
||||||
TrackPoint.Id lastPointId = initializeTrack(trackId, numPoints);
|
TrackPoint.Id lastPointId = initializeTrack(trackId, numPoints);
|
||||||
List<TrackPoint> locations = new ArrayList<>(numPoints);
|
List<TrackPoint> locations = new ArrayList<>(numPoints);
|
||||||
try (TrackPointIterator it = contentProviderUtils.getTrackPointIterator(trackId, null)) {
|
try (TrackPointIterator it = contentProviderUtils.getTrackPointIterator(trackId, null)) {
|
||||||
@@ -131,6 +121,7 @@ public class CustomContentProviderUtilsTest {
|
|||||||
}
|
}
|
||||||
assertEquals(numPoints, locations.size());
|
assertEquals(numPoints, locations.size());
|
||||||
}
|
}
|
||||||
|
return locations;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TrackPoint.Id initializeTrack(Track.Id id, int numPoints) {
|
private TrackPoint.Id initializeTrack(Track.Id id, int numPoints) {
|
||||||
@@ -187,27 +178,49 @@ public class CustomContentProviderUtilsTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testCreateTrack() {
|
public void testCreateTrack() {
|
||||||
Track.Id trackId = new Track.Id(System.currentTimeMillis());
|
Track.Id trackId = new Track.Id(System.currentTimeMillis());
|
||||||
|
|
||||||
int columnIndex = 1;
|
|
||||||
// Id
|
|
||||||
when(cursorMock.getColumnIndexOrThrow(TracksColumns._ID)).thenReturn(columnIndex);
|
|
||||||
when(cursorMock.isNull(columnIndex)).thenReturn(false);
|
|
||||||
when(cursorMock.getLong(columnIndex)).thenReturn(trackId.id());
|
|
||||||
|
|
||||||
//Uuid
|
|
||||||
columnIndex++;
|
|
||||||
when(cursorMock.getColumnIndexOrThrow(TracksColumns.UUID)).thenReturn(columnIndex);
|
|
||||||
when(cursorMock.isNull(columnIndex)).thenReturn(false);
|
|
||||||
when(cursorMock.getBlob(columnIndex)).thenReturn(UUIDUtils.toBytes(UUID.randomUUID()));
|
|
||||||
|
|
||||||
// Name
|
|
||||||
columnIndex++;
|
|
||||||
String name = NAME_PREFIX + trackId.id();
|
String name = NAME_PREFIX + trackId.id();
|
||||||
when(cursorMock.getColumnIndexOrThrow(TracksColumns.NAME)).thenReturn(columnIndex);
|
|
||||||
when(cursorMock.isNull(columnIndex)).thenReturn(false);
|
|
||||||
when(cursorMock.getString(columnIndex)).thenReturn(name);
|
|
||||||
|
|
||||||
Track track = ContentProviderUtils.createTrack(cursorMock);
|
MatrixCursor cursor = new MatrixCursor(new String[]{
|
||||||
|
TracksColumns._ID,
|
||||||
|
TracksColumns.UUID,
|
||||||
|
TracksColumns.NAME,
|
||||||
|
TracksColumns.DESCRIPTION,
|
||||||
|
TracksColumns.ACTIVITY_TYPE,
|
||||||
|
TracksColumns.ACTIVITY_TYPE_LOCALIZED,
|
||||||
|
TracksColumns.STARTTIME,
|
||||||
|
TracksColumns.STARTTIME_OFFSET,
|
||||||
|
TracksColumns.STOPTIME,
|
||||||
|
TracksColumns.TOTALDISTANCE,
|
||||||
|
TracksColumns.TOTALTIME,
|
||||||
|
TracksColumns.MOVINGTIME,
|
||||||
|
TracksColumns.MAXSPEED,
|
||||||
|
TracksColumns.MIN_ALTITUDE,
|
||||||
|
TracksColumns.MAX_ALTITUDE,
|
||||||
|
TracksColumns.ALTITUDE_GAIN,
|
||||||
|
TracksColumns.ALTITUDE_LOSS,
|
||||||
|
});
|
||||||
|
cursor.addRow(new Object[]{
|
||||||
|
trackId.id(),
|
||||||
|
UUIDUtils.toBytes(UUID.randomUUID()),
|
||||||
|
name,
|
||||||
|
"",
|
||||||
|
ActivityType.UNKNOWN.getId(),
|
||||||
|
"",
|
||||||
|
1_000L,
|
||||||
|
ZoneOffset.UTC.getTotalSeconds(),
|
||||||
|
2_000L,
|
||||||
|
123.45f,
|
||||||
|
1_000L,
|
||||||
|
500L,
|
||||||
|
12.34f,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
});
|
||||||
|
assertTrue(cursor.moveToFirst());
|
||||||
|
|
||||||
|
Track track = ContentProviderUtils.createTrack(cursor);
|
||||||
assertEquals(trackId, track.id());
|
assertEquals(trackId, track.id());
|
||||||
assertEquals(name, track.name());
|
assertEquals(name, track.name());
|
||||||
}
|
}
|
||||||
@@ -449,8 +462,6 @@ public class CustomContentProviderUtilsTest {
|
|||||||
marker.setDescription(TEST_DESC);
|
marker.setDescription(TEST_DESC);
|
||||||
contentProviderUtils.insertMarker(marker.getMarker());
|
contentProviderUtils.insertMarker(marker.getMarker());
|
||||||
|
|
||||||
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(contentResolverMock);
|
|
||||||
|
|
||||||
Marker.Id markerId = new Marker.Id(System.currentTimeMillis());
|
Marker.Id markerId = new Marker.Id(System.currentTimeMillis());
|
||||||
marker.setId(markerId);
|
marker.setId(markerId);
|
||||||
ContentValues contentValues = contentProviderUtils.createContentValues(marker.getMarker(), trackId);
|
ContentValues contentValues = contentProviderUtils.createContentValues(marker.getMarker(), trackId);
|
||||||
@@ -1184,7 +1195,7 @@ public class CustomContentProviderUtilsTest {
|
|||||||
assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f);
|
assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testGetSensorStats_randomData(int totalPoints, boolean withStartSegments) {
|
private Pair<SensorStatistics, TestSensorDataUtil.SensorDataStats> testGetSensorStats_randomData(int totalPoints, boolean withStartSegments) {
|
||||||
// given
|
// given
|
||||||
Instant start = Instant.now();
|
Instant start = Instant.now();
|
||||||
TestSensorDataUtil sensorDataUtil = new TestSensorDataUtil();
|
TestSensorDataUtil sensorDataUtil = new TestSensorDataUtil();
|
||||||
@@ -1207,22 +1218,27 @@ public class CustomContentProviderUtilsTest {
|
|||||||
SensorStatistics sensorStatistics = contentProviderUtils.getSensorStats(trackId);
|
SensorStatistics sensorStatistics = contentProviderUtils.getSensorStats(trackId);
|
||||||
TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats();
|
TestSensorDataUtil.SensorDataStats stats = sensorDataUtil.computeStats();
|
||||||
|
|
||||||
// then
|
return Pair.create(sensorStatistics, stats);
|
||||||
assertEquals(sensorStatistics.avgHeartRate().getBPM(), stats.avgHr, 0.01f);
|
|
||||||
assertEquals(sensorStatistics.maxHeartRate().getBPM(), stats.maxHr, 0.01f);
|
|
||||||
assertEquals(sensorStatistics.avgCadence().getRPM(), stats.avgCadence, 0.01f);
|
|
||||||
assertEquals(sensorStatistics.maxCadence().getRPM(), stats.maxCadence, 0.01f);
|
|
||||||
assertEquals(sensorStatistics.avgPower().getW(), stats.avgPower, 0.01f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetSensorStats_veryLongActivity12h() {
|
public void testGetSensorStats_veryLongActivity12h() {
|
||||||
testGetSensorStats_randomData(43200 / 6, false);
|
Pair<SensorStatistics, TestSensorDataUtil.SensorDataStats> result = testGetSensorStats_randomData(43200 / 6, false);
|
||||||
|
assertEquals(result.first.avgHeartRate().getBPM(), result.second.avgHr, 0.01f);
|
||||||
|
assertEquals(result.first.maxHeartRate().getBPM(), result.second.maxHr, 0.01f);
|
||||||
|
assertEquals(result.first.avgCadence().getRPM(), result.second.avgCadence, 0.01f);
|
||||||
|
assertEquals(result.first.maxCadence().getRPM(), result.second.maxCadence, 0.01f);
|
||||||
|
assertEquals(result.first.avgPower().getW(), result.second.avgPower, 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetSensorStats_withSeveralRandomStartSegments() {
|
public void testGetSensorStats_withSeveralRandomStartSegments() {
|
||||||
testGetSensorStats_randomData(5000, true);
|
Pair<SensorStatistics, TestSensorDataUtil.SensorDataStats> result = testGetSensorStats_randomData(5000, true);
|
||||||
|
assertEquals(result.first.avgHeartRate().getBPM(), result.second.avgHr, 0.01f);
|
||||||
|
assertEquals(result.first.maxHeartRate().getBPM(), result.second.maxHr, 0.01f);
|
||||||
|
assertEquals(result.first.avgCadence().getRPM(), result.second.avgCadence, 0.01f);
|
||||||
|
assertEquals(result.first.maxCadence().getRPM(), result.second.maxCadence, 0.01f);
|
||||||
|
assertEquals(result.first.avgPower().getW(), result.second.avgPower, 0.01f);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TrackPoint getLastValidTrackPoint(Track.Id trackId) {
|
private TrackPoint getLastValidTrackPoint(Track.Id trackId) {
|
||||||
|
|||||||
+54
-37
@@ -1,24 +1,23 @@
|
|||||||
package de.dennisguse.opentracks.services;
|
package de.dennisguse.opentracks.services;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.mockito.Mockito.times;
|
|
||||||
import static org.mockito.Mockito.verify;
|
|
||||||
import static org.mockito.Mockito.when;
|
|
||||||
|
|
||||||
|
import android.app.NotificationChannel;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.junit.MockitoJUnitRunner;
|
|
||||||
|
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.R;
|
||||||
import de.dennisguse.opentracks.data.models.ActivityType;
|
import de.dennisguse.opentracks.data.models.ActivityType;
|
||||||
import de.dennisguse.opentracks.data.models.Altitude;
|
import de.dennisguse.opentracks.data.models.Altitude;
|
||||||
import de.dennisguse.opentracks.data.models.Distance;
|
import de.dennisguse.opentracks.data.models.Distance;
|
||||||
@@ -29,32 +28,24 @@ import de.dennisguse.opentracks.data.models.Track;
|
|||||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||||
import de.dennisguse.opentracks.settings.UnitSystem;
|
import de.dennisguse.opentracks.settings.UnitSystem;
|
||||||
|
|
||||||
@RunWith(MockitoJUnitRunner.class)
|
|
||||||
public class TrackRecordingServiceNotificationManagerTest {
|
public class TrackRecordingServiceNotificationManagerTest {
|
||||||
|
|
||||||
|
private static final String CHANNEL_ID = "TrackRecordingServiceNotificationManagerTest";
|
||||||
|
|
||||||
private final Context context = ApplicationProvider.getApplicationContext();
|
private final Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
private final NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|
||||||
|
|
||||||
@Mock
|
@After
|
||||||
private NotificationCompat.Builder notificationCompatBuilder;
|
public void tearDown() {
|
||||||
|
notificationManager.cancelAll();
|
||||||
@Mock
|
}
|
||||||
private NotificationManager notificationManager;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void updateLocation_triggersAlertOnlyOnFirstInaccurateLocation() {
|
public void updateLocation_triggersAlertOnlyOnFirstInaccurateLocation() {
|
||||||
TrackPoint trackPointMock = new TrackPoint(TrackPoint.Type.TRACKPOINT,
|
notificationManager.createNotificationChannel(new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_DEFAULT));
|
||||||
new Position(
|
|
||||||
null,
|
|
||||||
null, null, Distance.of(999f),
|
|
||||||
Altitude.WGS84.of(10), null,
|
|
||||||
null,
|
|
||||||
Speed.ZERO)
|
|
||||||
);
|
|
||||||
|
|
||||||
when(notificationCompatBuilder.setOnlyAlertOnce(anyBoolean()))
|
TrackingNotificationBuilder notificationBuilder = new TrackingNotificationBuilder(context, CHANNEL_ID);
|
||||||
.thenReturn(notificationCompatBuilder);
|
TrackRecordingServiceNotificationManager subject = new TrackRecordingServiceNotificationManager(notificationManager, notificationBuilder);
|
||||||
|
|
||||||
TrackRecordingServiceNotificationManager subject = new TrackRecordingServiceNotificationManager(notificationManager, notificationCompatBuilder);
|
|
||||||
subject.setUnitSystem(UnitSystem.METRIC);
|
subject.setUnitSystem(UnitSystem.METRIC);
|
||||||
|
|
||||||
Track track = new Track(
|
Track track = new Track(
|
||||||
@@ -68,16 +59,42 @@ public class TrackRecordingServiceNotificationManagerTest {
|
|||||||
Statistics.DEFAULT
|
Statistics.DEFAULT
|
||||||
);
|
);
|
||||||
|
|
||||||
RecordingData data = new RecordingData(track, trackPointMock, null, null);
|
subject.updateTrackPoint(context, createRecordingData(track, Distance.of(999f)), Distance.of(100));
|
||||||
|
subject.updateTrackPoint(context, createRecordingData(track, Distance.of(999f)), Distance.of(100));
|
||||||
// when
|
subject.updateTrackPoint(context, createRecordingData(track, Distance.of(999f)), Distance.of(1000));
|
||||||
subject.updateTrackPoint(context, data, Distance.of(100));
|
subject.updateTrackPoint(context, createRecordingData(track, Distance.of(999f)), Distance.of(100));
|
||||||
subject.updateTrackPoint(context, data, Distance.of(100));
|
assertEquals(java.util.Arrays.asList(false, true, true, true, true, true, false, true), notificationBuilder.onlyAlertOnceValues);
|
||||||
subject.updateTrackPoint(context, data, Distance.of(1000));
|
|
||||||
subject.updateTrackPoint(context, data, Distance.of(100));
|
|
||||||
|
|
||||||
// then
|
|
||||||
verify(notificationCompatBuilder, times(6)).setOnlyAlertOnce(true);
|
|
||||||
verify(notificationCompatBuilder, times(2)).setOnlyAlertOnce(false);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private RecordingData createRecordingData(Track track, Distance horizontalAccuracy) {
|
||||||
|
TrackPoint trackPoint = new TrackPoint(
|
||||||
|
TrackPoint.Type.TRACKPOINT,
|
||||||
|
new Position(
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
horizontalAccuracy,
|
||||||
|
Altitude.WGS84.of(10),
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
Speed.ZERO
|
||||||
|
)
|
||||||
|
);
|
||||||
|
return new RecordingData(track, trackPoint, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final class TrackingNotificationBuilder extends NotificationCompat.Builder {
|
||||||
|
private final List<Boolean> onlyAlertOnceValues = new ArrayList<>();
|
||||||
|
|
||||||
|
private TrackingNotificationBuilder(Context context, String channelId) {
|
||||||
|
super(context, channelId);
|
||||||
|
setSmallIcon(R.drawable.ic_logo_color_24dp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NotificationCompat.Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
|
||||||
|
onlyAlertOnceValues.add(onlyAlertOnce);
|
||||||
|
return super.setOnlyAlertOnce(onlyAlertOnce);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+38
@@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.dennisguse.opentracks.services;
|
package de.dennisguse.opentracks.services;
|
||||||
|
|
||||||
|
import static de.dennisguse.opentracks.util.LiveDataTestUtils.waitForValue;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
@@ -41,6 +42,7 @@ import org.junit.Rule;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
@@ -56,6 +58,8 @@ import de.dennisguse.opentracks.sensors.GpsStatusValue;
|
|||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class TrackRecordingServiceStateMachineTest {
|
public class TrackRecordingServiceStateMachineTest {
|
||||||
|
|
||||||
|
private static final Duration WAIT_TIMEOUT = Duration.ofSeconds(2);
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(5, TimeUnit.SECONDS);
|
public final ServiceTestRule mServiceRule = ServiceTestRule.withTimeout(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
@@ -123,6 +127,7 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
service.tryStartSensors();
|
service.tryStartSensors();
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_ENABLED);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals(TrackRecordingService.STATUS_DEFAULT, service.getRecordingStatusObservable().getValue());
|
assertEquals(TrackRecordingService.STATUS_DEFAULT, service.getRecordingStatusObservable().getValue());
|
||||||
@@ -131,6 +136,7 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
service.stopSensors();
|
service.stopSensors();
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_NONE);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals(TrackRecordingService.STATUS_DEFAULT, service.getRecordingStatusObservable().getValue());
|
assertEquals(TrackRecordingService.STATUS_DEFAULT, service.getRecordingStatusObservable().getValue());
|
||||||
@@ -146,6 +152,9 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
Track.Id trackId = service.startNewTrack();
|
Track.Id trackId = service.startNewTrack();
|
||||||
|
waitUntilRecordingStatusIs(new RecordingStatus(trackId));
|
||||||
|
waitUntilRecordingDataIsNotDefault();
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_ENABLED);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertTrue(service.isRecording());
|
assertTrue(service.isRecording());
|
||||||
@@ -154,9 +163,13 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
assertEquals(GpsStatusValue.GPS_ENABLED, service.getGpsStatusObservable().getValue());
|
assertEquals(GpsStatusValue.GPS_ENABLED, service.getGpsStatusObservable().getValue());
|
||||||
|
|
||||||
service.endCurrentTrack();
|
service.endCurrentTrack();
|
||||||
|
waitUntilRecordingStatusIs(TrackRecordingService.STATUS_DEFAULT);
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_NONE);
|
||||||
|
|
||||||
// when
|
// when
|
||||||
service.resumeTrack(trackId);
|
service.resumeTrack(trackId);
|
||||||
|
waitUntilRecordingStatusIs(new RecordingStatus(trackId));
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_ENABLED);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertTrue(service.isRecording());
|
assertTrue(service.isRecording());
|
||||||
@@ -167,6 +180,8 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
service.endCurrentTrack();
|
service.endCurrentTrack();
|
||||||
|
waitUntilRecordingStatusIs(TrackRecordingService.STATUS_DEFAULT);
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_NONE);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertFalse(service.isRecording());
|
assertFalse(service.isRecording());
|
||||||
@@ -177,6 +192,8 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
service.resumeTrack(trackId);
|
service.resumeTrack(trackId);
|
||||||
|
waitUntilRecordingStatusIs(new RecordingStatus(trackId));
|
||||||
|
waitUntilGpsStatusIs(GpsStatusValue.GPS_ENABLED);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertTrue(service.isRecording());
|
assertTrue(service.isRecording());
|
||||||
@@ -254,4 +271,25 @@ public class TrackRecordingServiceStateMachineTest {
|
|||||||
assertNotNull(trackId);
|
assertNotNull(trackId);
|
||||||
assertNull(newTrackId);
|
assertNull(newTrackId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void waitUntilRecordingDataIsNotDefault() {
|
||||||
|
waitForValue(service.getRecordingDataObservable(),
|
||||||
|
recordingData -> !RecordingData.NOT_RECORDING.equals(recordingData),
|
||||||
|
WAIT_TIMEOUT,
|
||||||
|
"recording data to leave NOT_RECORDING");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitUntilGpsStatusIs(GpsStatusValue expected) {
|
||||||
|
waitForValue(service.getGpsStatusObservable(),
|
||||||
|
expected::equals,
|
||||||
|
WAIT_TIMEOUT,
|
||||||
|
"gps status to become " + expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitUntilRecordingStatusIs(RecordingStatus expected) {
|
||||||
|
waitForValue(service.getRecordingStatusObservable(),
|
||||||
|
expected::equals,
|
||||||
|
WAIT_TIMEOUT,
|
||||||
|
"recording status to become " + expected);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+30
-21
@@ -2,12 +2,12 @@ package de.dennisguse.opentracks.services;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static de.dennisguse.opentracks.services.TrackRecordingService.RECORDING_DATA_UPDATE_INTERVAL;
|
import static de.dennisguse.opentracks.services.TrackRecordingService.RECORDING_DATA_UPDATE_INTERVAL;
|
||||||
|
import static de.dennisguse.opentracks.util.LiveDataTestUtils.waitForValue;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
import androidx.core.location.LocationManagerCompat;
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.rule.GrantPermissionRule;
|
import androidx.test.rule.GrantPermissionRule;
|
||||||
@@ -30,6 +30,7 @@ import de.dennisguse.opentracks.data.models.Speed;
|
|||||||
import de.dennisguse.opentracks.data.models.TrackPoint;
|
import de.dennisguse.opentracks.data.models.TrackPoint;
|
||||||
import de.dennisguse.opentracks.io.file.importer.ExportImportTest;
|
import de.dennisguse.opentracks.io.file.importer.ExportImportTest;
|
||||||
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
import de.dennisguse.opentracks.io.file.importer.TrackPointAssert;
|
||||||
|
import de.dennisguse.opentracks.sensors.GpsHandler;
|
||||||
import de.dennisguse.opentracks.sensors.driver.GpsInternal;
|
import de.dennisguse.opentracks.sensors.driver.GpsInternal;
|
||||||
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
import de.dennisguse.opentracks.services.handlers.TrackPointCreator;
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ public class TrackRecordingServiceUITest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getDataForUI_gps() throws TimeoutException, InterruptedException {
|
public void getDataForUI_gps() throws TimeoutException {
|
||||||
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class)))
|
TrackRecordingService service = ((TrackRecordingService.Binder) mServiceRule.bindService(new Intent(context, TrackRecordingService.class)))
|
||||||
.getService();
|
.getService();
|
||||||
RecordingData recordingData;
|
RecordingData recordingData;
|
||||||
@@ -63,34 +64,23 @@ public class TrackRecordingServiceUITest {
|
|||||||
|
|
||||||
// when
|
// when
|
||||||
service.startNewTrack();
|
service.startNewTrack();
|
||||||
//We do not want the real GPS to interfere.
|
disconnectRealGps(trackPointCreator.getSensorManager().getGpsHandler());
|
||||||
GpsInternal gpsInternal = (GpsInternal)trackPointCreator.getSensorManager().getGpsHandler().driver;
|
trackPointCreator.getSensorManager().reset();
|
||||||
LocationManagerCompat.removeUpdates(gpsInternal.locationManager, gpsInternal.locationListenerCompat);
|
RecordingData initialRecordingData = waitUntilLatestTrackPointMatches(service, Instant.parse("2020-02-02T02:02:02Z"), false);
|
||||||
Thread.sleep(RECORDING_DATA_UPDATE_INTERVAL.toMillis());
|
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals(new TrackPoint(
|
TrackPoint initialTrackPoint = initialRecordingData.latestTrackPoint();
|
||||||
null,
|
assertEquals(TrackPoint.Type.TRACKPOINT, initialTrackPoint.type());
|
||||||
TrackPoint.Type.TRACKPOINT,
|
assertEquals(Instant.parse("2020-02-02T02:02:02Z"), initialTrackPoint.getTime());
|
||||||
Position.of( Instant.parse("2020-02-02T02:02:02Z")),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null
|
|
||||||
),
|
|
||||||
service.getRecordingDataObservable().getValue().latestTrackPoint());
|
|
||||||
|
|
||||||
|
|
||||||
// when
|
// when
|
||||||
// In this step, the data is saved (and Aggregator.resetAggregated called), but data should still be shown in the UI.
|
// In this step, the data is saved (and Aggregator.resetAggregated called), but data should still be shown in the UI.
|
||||||
trackPointCreator.setClock("2020-02-02T02:02:03Z");
|
trackPointCreator.setClock("2020-02-02T02:02:03Z");
|
||||||
ExportImportTest.sendLocation(trackPointCreator, "2020-02-02T02:02:03Z", 3.1234567, 14.0014567, 10, 13, 15, 1020.25, 1f);
|
ExportImportTest.sendLocation(trackPointCreator, "2020-02-02T02:02:03Z", 3.1234567, 14.0014567, 10, 13, 15, 1020.25, 1f);
|
||||||
|
recordingData = waitUntilLatestTrackPointMatches(service, Instant.parse("2020-02-02T02:02:03Z"), true);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
Thread.sleep(RECORDING_DATA_UPDATE_INTERVAL.toMillis());
|
|
||||||
recordingData = service.getRecordingDataObservable().getValue();
|
|
||||||
new TrackPointAssert().assertEquals(new TrackPoint(
|
new TrackPointAssert().assertEquals(new TrackPoint(
|
||||||
null,
|
null,
|
||||||
TrackPoint.Type.TRACKPOINT,
|
TrackPoint.Type.TRACKPOINT,
|
||||||
@@ -109,4 +99,23 @@ public class TrackRecordingServiceUITest {
|
|||||||
),
|
),
|
||||||
recordingData.latestTrackPoint());
|
recordingData.latestTrackPoint());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private void disconnectRealGps(GpsHandler gpsHandler) {
|
||||||
|
if (gpsHandler.driver != null) {
|
||||||
|
gpsHandler.driver.disconnect();
|
||||||
|
}
|
||||||
|
gpsHandler.onConnected(null, GpsInternal.LOCATION_PROVIDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RecordingData waitUntilLatestTrackPointMatches(TrackRecordingService service, Instant expectedTime, boolean requiresLocation) {
|
||||||
|
return waitForValue(service.getRecordingDataObservable(),
|
||||||
|
recordingData -> {
|
||||||
|
TrackPoint latestTrackPoint = recordingData != null ? recordingData.latestTrackPoint() : null;
|
||||||
|
return latestTrackPoint != null
|
||||||
|
&& expectedTime.equals(latestTrackPoint.getTime())
|
||||||
|
&& (!requiresLocation || latestTrackPoint.position().hasLocation());
|
||||||
|
},
|
||||||
|
RECORDING_DATA_UPDATE_INTERVAL.multipliedBy(3),
|
||||||
|
"latest track point to reach " + expectedTime + (requiresLocation ? " with location" : ""));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+4
-2
@@ -556,6 +556,7 @@ public class VoiceAnnouncementUtilsTest {
|
|||||||
PreferencesUtils.setVoiceAnnounceLapHeartRate(true);
|
PreferencesUtils.setVoiceAnnounceLapHeartRate(true);
|
||||||
|
|
||||||
Pair<Track, Statistics> data = buildTrackWithTrackPoints();
|
Pair<Track, Statistics> data = buildTrackWithTrackPoints();
|
||||||
|
int lapHeartRate = Math.round(data.second.avgHeartRate().getBPM());
|
||||||
|
|
||||||
SensorStatistics sensorStatistics = new SensorStatistics(HeartRate.of(180f), HeartRate.of(180f), null, null, null, null);
|
SensorStatistics sensorStatistics = new SensorStatistics(HeartRate.of(180f), HeartRate.of(180f), null, null, null, null);
|
||||||
|
|
||||||
@@ -573,7 +574,7 @@ public class VoiceAnnouncementUtilsTest {
|
|||||||
String announcement = VoiceAnnouncementUtils.createStatistics(context, data.first, dataSet, UnitSystem.METRIC, true, data.second, sensorStatistics).toString();
|
String announcement = VoiceAnnouncementUtils.createStatistics(context, data.first, dataSet, UnitSystem.METRIC, true, data.second, sensorStatistics).toString();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals("12:16 AM. Total distance 14.2 kilometers. 16 minutes 39 seconds. Average moving speed 51.2 kilometers per hour. Lap speed 51.2 kilometers per hour. Average heart rate 180 bpm. Lap heart rate 133 bpm.", announcement);
|
assertEquals("12:16 AM. Total distance 14.2 kilometers. 16 minutes 39 seconds. Average moving speed 51.2 kilometers per hour. Lap speed 51.2 kilometers per hour. Average heart rate 180 bpm. Lap heart rate " + lapHeartRate + " bpm.", announcement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -589,6 +590,7 @@ public class VoiceAnnouncementUtilsTest {
|
|||||||
PreferencesUtils.setVoiceAnnounceLapSpeedPace(false);
|
PreferencesUtils.setVoiceAnnounceLapSpeedPace(false);
|
||||||
|
|
||||||
Pair<Track, Statistics> data = buildTrackWithTrackPoints();
|
Pair<Track, Statistics> data = buildTrackWithTrackPoints();
|
||||||
|
int lapHeartRate = Math.round(data.second.avgHeartRate().getBPM());
|
||||||
|
|
||||||
SensorStatistics sensorStatistics = new SensorStatistics(HeartRate.of(180f), HeartRate.of(180f), null, null, null, null);
|
SensorStatistics sensorStatistics = new SensorStatistics(HeartRate.of(180f), HeartRate.of(180f), null, null, null, null);
|
||||||
|
|
||||||
@@ -606,7 +608,7 @@ public class VoiceAnnouncementUtilsTest {
|
|||||||
String announcement = VoiceAnnouncementUtils.createStatistics(context, data.first, dataSet, UnitSystem.METRIC, true, data.second, sensorStatistics).toString();
|
String announcement = VoiceAnnouncementUtils.createStatistics(context, data.first, dataSet, UnitSystem.METRIC, true, data.second, sensorStatistics).toString();
|
||||||
|
|
||||||
// then
|
// then
|
||||||
assertEquals("12:16 AM. Current heart rate 60 bpm. Average heart rate 180 bpm. Lap heart rate 133 bpm.", announcement);
|
assertEquals("12:16 AM. Current heart rate 60 bpm. Average heart rate 180 bpm. Lap heart rate " + lapHeartRate + " bpm.", announcement);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
+43
-22
@@ -1,36 +1,41 @@
|
|||||||
package de.dennisguse.opentracks.ui.aggregatedStatistics;
|
package de.dennisguse.opentracks.ui.aggregatedStatistics;
|
||||||
|
|
||||||
import static androidx.test.espresso.Espresso.onView;
|
import static androidx.test.espresso.Espresso.onView;
|
||||||
import static androidx.test.espresso.action.ViewActions.click;
|
|
||||||
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
import static androidx.test.espresso.assertion.ViewAssertions.matches;
|
||||||
|
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
|
import static androidx.test.espresso.matcher.ViewMatchers.hasDescendant;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
|
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
import static androidx.test.espresso.matcher.ViewMatchers.withText;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClick;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilDisplayed;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilRecyclerViewItemCount;
|
||||||
|
import static de.dennisguse.opentracks.util.TestEnvironmentUtils.resetTrackRecordingServiceAndDeleteTracks;
|
||||||
|
import static de.dennisguse.opentracks.util.TestEnvironmentUtils.stopTrackRecordingService;
|
||||||
import static org.hamcrest.Matchers.allOf;
|
import static org.hamcrest.Matchers.allOf;
|
||||||
import static de.dennisguse.opentracks.util.EspressoUtils.waitFor;
|
|
||||||
|
|
||||||
import android.util.Pair;
|
import android.util.Pair;
|
||||||
|
import android.view.View;
|
||||||
|
|
||||||
import androidx.test.core.app.ApplicationProvider;
|
import androidx.test.core.app.ActivityScenario;
|
||||||
import androidx.test.espresso.matcher.ViewMatchers;
|
|
||||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
|
||||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||||
import androidx.test.filters.LargeTest;
|
import androidx.test.filters.LargeTest;
|
||||||
import androidx.test.rule.GrantPermissionRule;
|
import androidx.test.rule.GrantPermissionRule;
|
||||||
|
|
||||||
import org.hamcrest.core.IsInstanceOf;
|
import org.hamcrest.core.IsInstanceOf;
|
||||||
|
import org.hamcrest.Matcher;
|
||||||
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.dennisguse.opentracks.R;
|
import de.dennisguse.opentracks.R;
|
||||||
import de.dennisguse.opentracks.TestUtil;
|
import de.dennisguse.opentracks.TestUtil;
|
||||||
import de.dennisguse.opentracks.TrackListActivity;
|
|
||||||
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
import de.dennisguse.opentracks.content.data.TestDataUtil;
|
||||||
import de.dennisguse.opentracks.data.ContentProviderUtils;
|
import de.dennisguse.opentracks.data.ContentProviderUtils;
|
||||||
import de.dennisguse.opentracks.data.models.Track;
|
import de.dennisguse.opentracks.data.models.Track;
|
||||||
@@ -41,53 +46,69 @@ import de.dennisguse.opentracks.data.models.TrackPoint;
|
|||||||
@RunWith(AndroidJUnit4.class)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class EspressoAggregatedFilterTest {
|
public class EspressoAggregatedFilterTest {
|
||||||
|
|
||||||
private final String ACTIVITY_TYPE_LOCALIZED = "activityTypeLocalized";
|
private static final Duration UI_TIMEOUT = Duration.ofSeconds(10);
|
||||||
|
|
||||||
@Rule
|
private final String ACTIVITY_TYPE_LOCALIZED = "activityTypeLocalized";
|
||||||
public ActivityScenarioRule<TrackListActivity> mActivityTestRule = new ActivityScenarioRule<>(TrackListActivity.class);
|
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
public GrantPermissionRule mGrantPermissionRule = TestUtil.createGrantPermissionRule();
|
||||||
|
|
||||||
|
private ActivityScenario<AggregatedStatisticsActivity> scenario;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
|
resetTrackRecordingServiceAndDeleteTracks();
|
||||||
|
ContentProviderUtils contentProviderUtils = new ContentProviderUtils(androidx.test.core.app.ApplicationProvider.getApplicationContext());
|
||||||
|
|
||||||
Pair<Track, List<TrackPoint>> pair = TestDataUtil.createTrack(new Track.Id(System.currentTimeMillis()), 20);
|
Pair<Track, List<TrackPoint>> pair = TestDataUtil.createTrack(new Track.Id(System.currentTimeMillis()), 20);
|
||||||
TrackBuilder trackBuilder = new TrackBuilder(pair.first);
|
TrackBuilder trackBuilder = new TrackBuilder(pair.first);
|
||||||
trackBuilder.setActivityTypeLocalized(ACTIVITY_TYPE_LOCALIZED);
|
trackBuilder.setActivityTypeLocalized(ACTIVITY_TYPE_LOCALIZED);
|
||||||
TestDataUtil.insertTrackWithLocations(new ContentProviderUtils(ApplicationProvider.getApplicationContext()), trackBuilder.getTrack(), pair.second);
|
TestDataUtil.insertTrackWithLocations(contentProviderUtils, trackBuilder.getTrack(), pair.second);
|
||||||
|
|
||||||
|
scenario = ActivityScenario.launch(AggregatedStatisticsActivity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
stopTrackRecordingService();
|
||||||
|
if (scenario != null) {
|
||||||
|
scenario.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void espressoAggregatedFilterTest() {
|
public void espressoAggregatedFilterTest() {
|
||||||
// open AggregatedStatisticsActivity through toolbar's menu item
|
waitUntilDisplayed(withId(R.id.bottom_app_bar), UI_TIMEOUT);
|
||||||
onView(ViewMatchers.withId(R.id.bottom_app_bar))
|
waitUntilDisplayed(withId(R.id.aggregated_statistics_filter), UI_TIMEOUT);
|
||||||
.check(matches(isDisplayed()));
|
waitUntilRecyclerViewItemCount(withId(R.id.aggregated_stats_list), 1, UI_TIMEOUT);
|
||||||
onView(withId(R.id.bottom_app_bar))
|
waitUntilDisplayed(allOf(withId(R.id.aggregated_stats_type_label), withText(ACTIVITY_TYPE_LOCALIZED)), UI_TIMEOUT);
|
||||||
.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
|
// open FilterDialogFragment through the current bottom app bar action item
|
||||||
onView(withId(R.id.bottom_app_bar))
|
onView(withId(R.id.bottom_app_bar))
|
||||||
.check(matches(isDisplayed()));
|
.check(matches(isDisplayed()));
|
||||||
onView(withId(R.id.bottom_app_bar))
|
onView(withId(R.id.bottom_app_bar))
|
||||||
.check(matches(hasDescendant(withId(R.id.aggregated_statistics_filter))));
|
.check(matches(hasDescendant(withId(R.id.aggregated_statistics_filter))));
|
||||||
onView(withId(R.id.aggregated_statistics_filter))
|
onView(withId(R.id.aggregated_statistics_filter))
|
||||||
.perform(click());
|
.perform(performClick());
|
||||||
|
|
||||||
// check there's a checkbox with ACTIVITY_TYPE_LOCALIZED text
|
// check there's a checkbox with ACTIVITY_TYPE_LOCALIZED text
|
||||||
onView(allOf(withId(R.id.filter_dialog_check_button), withText(ACTIVITY_TYPE_LOCALIZED),
|
Matcher<View> activityTypeFilter = allOf(withId(R.id.filter_dialog_check_button), withText(ACTIVITY_TYPE_LOCALIZED),
|
||||||
withParent(allOf(withId(R.id.filter_items),
|
withParent(allOf(withId(R.id.filter_items),
|
||||||
withParent(IsInstanceOf.instanceOf(android.view.ViewGroup.class)))),
|
withParent(IsInstanceOf.instanceOf(android.view.ViewGroup.class)))),
|
||||||
isDisplayed()))
|
isDisplayed());
|
||||||
.perform(waitFor(2000));
|
onView(activityTypeFilter)
|
||||||
|
.inRoot(isDialog())
|
||||||
|
.check(matches(isDisplayed()));
|
||||||
|
|
||||||
// check there's an edit text for "from date"
|
// check there's an edit text for "from date"
|
||||||
onView(withId(R.id.filter_date_edit_text_from))
|
onView(withId(R.id.filter_date_edit_text_from))
|
||||||
|
.inRoot(isDialog())
|
||||||
.check(matches(isDisplayed()));
|
.check(matches(isDisplayed()));
|
||||||
|
|
||||||
// check there's an edit text for "to date"
|
// check there's an edit text for "to date"
|
||||||
onView(withId(R.id.filter_date_edit_text_to))
|
onView(withId(R.id.filter_date_edit_text_to))
|
||||||
|
.inRoot(isDialog())
|
||||||
.check(matches(isDisplayed()));
|
.check(matches(isDisplayed()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+67
-22
@@ -35,7 +35,10 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
float distanceInterval = 1000f;
|
float distanceInterval = 1000f;
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(50, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(50, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, 50, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +52,10 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
float distanceInterval = 1000f;
|
float distanceInterval = 1000f;
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(200, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(200, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, 200, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -63,7 +69,10 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
float distanceInterval = 3000f;
|
float distanceInterval = 3000f;
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(3000, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(3000, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, 3000, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -77,7 +86,10 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
float distanceInterval = 3000f;
|
float distanceInterval = 3000f;
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(1000, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(1000, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, 1000, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,7 +103,10 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
float distanceInterval = 1000f;
|
float distanceInterval = 1000f;
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(10000, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(10000, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, 10000, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -122,15 +137,18 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
Pair<Track, List<TrackPoint>> trackWithStats = new Pair<>(dummyTrack, trackPoints);
|
Pair<Track, List<TrackPoint>> trackWithStats = new Pair<>(dummyTrack, trackPoints);
|
||||||
|
|
||||||
// when and then
|
// when and then
|
||||||
whenAndThen(trackWithStats.first, trackWithStats.second, numberOfPoints, distanceInterval);
|
IntervalStatisticsComputation computation = computeIntervalStatistics(trackWithStats.first, trackWithStats.second, distanceInterval);
|
||||||
|
assertIntervalStatisticsComputation(computation, numberOfPoints, distanceInterval);
|
||||||
|
assertEquals((int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval), computation.intervalList.size());
|
||||||
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void whenAndThen(int numberOfPoints, float distanceInterval) {
|
private IntervalStatisticsComputation computeIntervalStatistics(int numberOfPoints, float distanceInterval) {
|
||||||
Pair<Track, List<TrackPoint>> trackWithStats = buildTrackWithTrackPoints(numberOfPoints);
|
Pair<Track, List<TrackPoint>> trackWithStats = buildTrackWithTrackPoints(numberOfPoints);
|
||||||
whenAndThen(trackWithStats.first, trackWithStats.second, numberOfPoints, distanceInterval);
|
return computeIntervalStatistics(trackWithStats.first, trackWithStats.second, distanceInterval);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void whenAndThen(Track track, List<TrackPoint> trackPoints, int numberOfPoints, float distanceInterval) {
|
private IntervalStatisticsComputation computeIntervalStatistics(Track track, List<TrackPoint> trackPoints, float distanceInterval) {
|
||||||
IntervalStatisticsUpdater intervalStatistics = new IntervalStatisticsUpdater(Distance.of(distanceInterval));
|
IntervalStatisticsUpdater intervalStatistics = new IntervalStatisticsUpdater(Distance.of(distanceInterval));
|
||||||
|
|
||||||
intervalStatistics.addTrackPoints(trackPoints.iterator());
|
intervalStatistics.addTrackPoints(trackPoints.iterator());
|
||||||
@@ -157,23 +175,27 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// then
|
return new IntervalStatisticsComputation(track, intervalList, totalDistance, totalTime, totalGain, totalLoss);
|
||||||
assertEquals(track.statistics().totalDuration().toSeconds(), totalTime, 0.01);
|
}
|
||||||
assertEquals(track.statistics().totalDistance().toM(), totalDistance.toM(), 0.01);
|
|
||||||
assertEquals(intervalList.size(), (int) Math.ceil(track.statistics().totalDistance().toM() / distanceInterval));
|
private void assertIntervalStatisticsComputation(IntervalStatisticsComputation computation, int numberOfPoints, float distanceInterval) {
|
||||||
if (totalGain != null && totalLoss != null) {
|
assertEquals(computation.track.statistics().totalDuration().toSeconds(), computation.totalTime, 0.01);
|
||||||
assertEquals(totalGain, numberOfPoints * TestDataUtil.ALTITUDE_GAIN, 0.1);
|
assertEquals(computation.track.statistics().totalDistance().toM(), computation.totalDistance.toM(), 0.01);
|
||||||
assertEquals(totalLoss, numberOfPoints * TestDataUtil.ALTITUDE_LOSS, 0.1);
|
assertEquals(computation.intervalList.size(), (int) Math.ceil(computation.track.statistics().totalDistance().toM() / distanceInterval));
|
||||||
|
if (computation.totalGain != null && computation.totalLoss != null) {
|
||||||
|
assertEquals(computation.totalGain, numberOfPoints * TestDataUtil.ALTITUDE_GAIN, 0.1);
|
||||||
|
assertEquals(computation.totalLoss, numberOfPoints * TestDataUtil.ALTITUDE_LOSS, 0.1);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
assertTrue(intervalStatistics.getIntervalList().stream().noneMatch(i -> i.altitudeGainLoss() != null));
|
assertTrue(computation.intervalList.stream().noneMatch(i -> i.altitudeGainLoss() != null));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < intervalList.size() - 1; i++) {
|
Distance remainingDistance = computation.totalDistance;
|
||||||
assertEquals(intervalList.get(i).totalDistance().toM(), distanceInterval, 0.001);
|
for (int i = 0; i < computation.intervalList.size() - 1; i++) {
|
||||||
totalDistance = totalDistance.minus(intervalList.get(i).totalDistance());
|
assertEquals(computation.intervalList.get(i).totalDistance().toM(), distanceInterval, 0.001);
|
||||||
|
remainingDistance = remainingDistance.minus(computation.intervalList.get(i).totalDistance());
|
||||||
}
|
}
|
||||||
assertEquals(intervalList.get(intervalList.size() - 1).totalDistance().toM(), totalDistance.toM(), 0.01);
|
assertEquals(computation.intervalList.get(computation.intervalList.size() - 1).totalDistance().toM(), remainingDistance.toM(), 0.01);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Pair<Track, List<TrackPoint>> buildTrackWithTrackPoints(int numberOfPoints) {
|
private static Pair<Track, List<TrackPoint>> buildTrackWithTrackPoints(int numberOfPoints) {
|
||||||
@@ -195,4 +217,27 @@ public class IntervalStatisticsUpdaterTest {
|
|||||||
|
|
||||||
return new Pair<>(track, trackPoints);
|
return new Pair<>(track, trackPoints);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
private static final class IntervalStatisticsComputation {
|
||||||
|
private final Track track;
|
||||||
|
private final List<Statistics> intervalList;
|
||||||
|
private final Distance totalDistance;
|
||||||
|
private final float totalTime;
|
||||||
|
private final Float totalGain;
|
||||||
|
private final Float totalLoss;
|
||||||
|
|
||||||
|
private IntervalStatisticsComputation(Track track,
|
||||||
|
List<Statistics> intervalList,
|
||||||
|
Distance totalDistance,
|
||||||
|
float totalTime,
|
||||||
|
Float totalGain,
|
||||||
|
Float totalLoss) {
|
||||||
|
this.track = track;
|
||||||
|
this.intervalList = intervalList;
|
||||||
|
this.totalDistance = totalDistance;
|
||||||
|
this.totalTime = totalTime;
|
||||||
|
this.totalGain = totalGain;
|
||||||
|
this.totalLoss = totalLoss;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.Application;
|
||||||
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
|
||||||
|
import androidx.test.runner.lifecycle.Stage;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
public final class ActivityTestUtils {
|
||||||
|
|
||||||
|
private ActivityTestUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T extends Activity> T waitForResumedActivity(Class<T> activityClass, Duration timeout) {
|
||||||
|
T resumedActivity = getResumedActivity(activityClass);
|
||||||
|
if (resumedActivity != null) {
|
||||||
|
return resumedActivity;
|
||||||
|
}
|
||||||
|
|
||||||
|
Application application = ApplicationProvider.getApplicationContext();
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
AtomicReference<T> resumedActivityRef = new AtomicReference<>();
|
||||||
|
|
||||||
|
Application.ActivityLifecycleCallbacks callbacks = new Application.ActivityLifecycleCallbacks() {
|
||||||
|
@Override
|
||||||
|
public void onActivityResumed(Activity activity) {
|
||||||
|
if (activityClass.isInstance(activity)) {
|
||||||
|
resumedActivityRef.set(activityClass.cast(activity));
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityStarted(Activity activity) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityPaused(Activity activity) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityStopped(Activity activity) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onActivityDestroyed(Activity activity) {
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
|
||||||
|
application.registerActivityLifecycleCallbacks(callbacks);
|
||||||
|
|
||||||
|
T currentResumedActivity = findResumedActivity(activityClass);
|
||||||
|
if (currentResumedActivity != null) {
|
||||||
|
resumedActivityRef.set(currentResumedActivity);
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
|
||||||
|
fail("Timed out waiting for resumed activity " + activityClass.getSimpleName());
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
fail("Interrupted while waiting for resumed activity " + activityClass.getSimpleName());
|
||||||
|
} finally {
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> application.unregisterActivityLifecycleCallbacks(callbacks));
|
||||||
|
}
|
||||||
|
|
||||||
|
return resumedActivityRef.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T extends Activity> T getResumedActivity(Class<T> activityClass) {
|
||||||
|
AtomicReference<T> resumedActivity = new AtomicReference<>();
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> resumedActivity.set(findResumedActivity(activityClass)));
|
||||||
|
return resumedActivity.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T extends Activity> T findResumedActivity(Class<T> activityClass) {
|
||||||
|
for (Activity activity : ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)) {
|
||||||
|
if (activityClass.isInstance(activity)) {
|
||||||
|
return activityClass.cast(activity);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,25 @@
|
|||||||
package de.dennisguse.opentracks.util;
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.allOf;
|
||||||
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
||||||
|
import static androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.SystemClock;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
|
||||||
import android.view.ViewParent;
|
import android.view.ViewParent;
|
||||||
import android.widget.ListView;
|
import android.widget.AdapterView;
|
||||||
|
|
||||||
|
import androidx.recyclerview.widget.RecyclerView;
|
||||||
|
import androidx.test.espresso.PerformException;
|
||||||
import androidx.test.espresso.UiController;
|
import androidx.test.espresso.UiController;
|
||||||
import androidx.test.espresso.ViewAction;
|
import androidx.test.espresso.ViewAction;
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
|
||||||
|
import androidx.test.runner.lifecycle.Stage;
|
||||||
|
import androidx.test.espresso.util.HumanReadables;
|
||||||
|
import androidx.test.espresso.util.TreeIterables;
|
||||||
|
import androidx.viewpager2.widget.ViewPager2;
|
||||||
|
|
||||||
import com.google.android.material.tabs.TabLayout;
|
import com.google.android.material.tabs.TabLayout;
|
||||||
|
|
||||||
@@ -16,61 +27,114 @@ import org.hamcrest.Description;
|
|||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
import org.hamcrest.TypeSafeMatcher;
|
import org.hamcrest.TypeSafeMatcher;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class EspressoUtils {
|
public class EspressoUtils {
|
||||||
|
|
||||||
public static Matcher<View> childAtPosition(
|
private static final long VIEW_POLLING_INTERVAL_MS = 50;
|
||||||
final Matcher<View> parentMatcher, final int position) {
|
|
||||||
|
|
||||||
|
public static Matcher<View> withRecyclerViewItemCount(final int count) {
|
||||||
return new TypeSafeMatcher<>() {
|
return new TypeSafeMatcher<>() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesSafely(View view) {
|
||||||
|
if (!(view instanceof RecyclerView recyclerView)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
RecyclerView.Adapter<?> adapter = recyclerView.getAdapter();
|
||||||
|
return adapter != null && adapter.getItemCount() == count;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void describeTo(Description description) {
|
public void describeTo(Description description) {
|
||||||
description.appendText("Child at position " + position + " in parent ");
|
description.appendText("RecyclerView should have " + count + " items");
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Matcher<View> withListSize (final int size) {
|
public static Matcher<View> withSelectedTab(final int index) {
|
||||||
return new TypeSafeMatcher<>() {
|
return new TypeSafeMatcher<>() {
|
||||||
@Override
|
@Override
|
||||||
public boolean matchesSafely(final View view) {
|
protected boolean matchesSafely(View view) {
|
||||||
return ((ListView) view).getCount() == size;
|
return view instanceof TabLayout tabLayout
|
||||||
|
&& tabLayout.getSelectedTabPosition() == index;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void describeTo(final Description description) {
|
public void describeTo(Description description) {
|
||||||
description.appendText("ListView should have " + size + " items");
|
description.appendText("TabLayout should have selected tab " + index);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ViewAction waitFor(final long duration_ms) {
|
public static Matcher<View> withViewPagerCurrentItem(final int index) {
|
||||||
return new ViewAction() {
|
return new TypeSafeMatcher<>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getDescription() {
|
protected boolean matchesSafely(View view) {
|
||||||
return "Wait for milliseconds.";
|
return view instanceof ViewPager2 viewPager2
|
||||||
|
&& viewPager2.getCurrentItem() == index
|
||||||
|
&& viewPager2.getScrollState() == ViewPager2.SCROLL_STATE_IDLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Matcher<View> getConstraints() {
|
public void describeTo(Description description) {
|
||||||
return isDisplayed();
|
description.appendText("ViewPager2 should be idle on item " + index);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void perform(UiController uiController, final View view) {
|
|
||||||
uiController.loopMainThreadForAtLeast(duration_ms);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void waitForResumedView(final Matcher<View> viewMatcher, final Duration timeout) {
|
||||||
|
waitForResumedViewCondition(viewMatcher, ignored -> true, "view to match " + viewMatcher, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitUntilDisplayed(final Matcher<View> viewMatcher, final Duration timeout) {
|
||||||
|
waitForResumedView(allOf(viewMatcher, isDisplayed()), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitUntilRecyclerViewItemCount(final Matcher<View> viewMatcher, final int expectedCount, final Duration timeout) {
|
||||||
|
waitForResumedViewCondition(
|
||||||
|
allOf(viewMatcher, isDisplayed()),
|
||||||
|
view -> withRecyclerViewItemCount(expectedCount).matches(view),
|
||||||
|
"RecyclerView item count to be " + expectedCount,
|
||||||
|
timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitForResumedViewCondition(final Matcher<View> viewMatcher,
|
||||||
|
final Predicate<View> condition,
|
||||||
|
final String conditionDescription,
|
||||||
|
final Duration timeout) {
|
||||||
|
long deadline = SystemClock.uptimeMillis() + timeout.toMillis();
|
||||||
|
|
||||||
|
do {
|
||||||
|
InstrumentationRegistry.getInstrumentation().waitForIdleSync();
|
||||||
|
|
||||||
|
AtomicBoolean matched = new AtomicBoolean(false);
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
|
||||||
|
for (Activity activity : ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED)) {
|
||||||
|
View decorView = activity.getWindow() != null ? activity.getWindow().getDecorView() : null;
|
||||||
|
if (decorView == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (View child : TreeIterables.breadthFirstViewTraversal(decorView)) {
|
||||||
|
if (viewMatcher.matches(child) && condition.test(child)) {
|
||||||
|
matched.set(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (matched.get()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} while (SystemClock.uptimeMillis() < deadline);
|
||||||
|
|
||||||
|
throw new AssertionError("Timed out waiting for " + conditionDescription);
|
||||||
|
}
|
||||||
|
|
||||||
public static ViewAction selectTabAtIndex(final int index) {
|
public static ViewAction selectTabAtIndex(final int index) {
|
||||||
return new ViewAction() {
|
return new ViewAction() {
|
||||||
@Override
|
@Override
|
||||||
@@ -91,4 +155,135 @@ public class EspressoUtils {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ViewAction performClick() {
|
||||||
|
return new ViewAction() {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "invoke View.performClick()";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Matcher<View> getConstraints() {
|
||||||
|
return isDisplayed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform(UiController uiController, View view) {
|
||||||
|
if (!view.performClick()) {
|
||||||
|
throw new PerformException.Builder()
|
||||||
|
.withActionDescription(getDescription())
|
||||||
|
.withViewDescription(HumanReadables.describe(view))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
uiController.loopMainThreadUntilIdle();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ViewAction performClickOnAncestor() {
|
||||||
|
return new ViewAction() {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "invoke performClick() on the matched view or its ancestor";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Matcher<View> getConstraints() {
|
||||||
|
return isAssignableFrom(View.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform(UiController uiController, View view) {
|
||||||
|
View current = view;
|
||||||
|
while (current != null) {
|
||||||
|
if (current.performClick()) {
|
||||||
|
uiController.loopMainThreadUntilIdle();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewParent parent = current.getParent();
|
||||||
|
current = parent instanceof View ? (View) parent : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = view;
|
||||||
|
while (current != null) {
|
||||||
|
ViewParent parent = current.getParent();
|
||||||
|
if (parent instanceof AdapterView<?> adapterView) {
|
||||||
|
int position = adapterView.getPositionForView(current);
|
||||||
|
if (position != AdapterView.INVALID_POSITION
|
||||||
|
&& adapterView.performItemClick(current, position, adapterView.getAdapter().getItemId(position))) {
|
||||||
|
uiController.loopMainThreadUntilIdle();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
current = parent instanceof View ? (View) parent : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new PerformException.Builder()
|
||||||
|
.withActionDescription(getDescription())
|
||||||
|
.withViewDescription(HumanReadables.describe(view))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ViewAction performLongClick() {
|
||||||
|
return new ViewAction() {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "invoke View.performLongClick()";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Matcher<View> getConstraints() {
|
||||||
|
return isDisplayed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform(UiController uiController, View view) {
|
||||||
|
if (!view.performLongClick()) {
|
||||||
|
throw new PerformException.Builder()
|
||||||
|
.withActionDescription(getDescription())
|
||||||
|
.withViewDescription(HumanReadables.describe(view))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
uiController.loopMainThreadUntilIdle();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ViewAction performLongClickOnAncestor() {
|
||||||
|
return new ViewAction() {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "invoke performLongClick() on the matched view or its ancestor";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Matcher<View> getConstraints() {
|
||||||
|
return isDisplayed();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform(UiController uiController, View view) {
|
||||||
|
View current = view;
|
||||||
|
while (current != null) {
|
||||||
|
if (current.performLongClick()) {
|
||||||
|
uiController.loopMainThreadUntilIdle();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ViewParent parent = current.getParent();
|
||||||
|
current = parent instanceof View ? (View) parent : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new PerformException.Builder()
|
||||||
|
.withActionDescription(getDescription())
|
||||||
|
.withViewDescription(HumanReadables.describe(view))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import androidx.lifecycle.LiveData;
|
||||||
|
import androidx.lifecycle.Observer;
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
|
public final class LiveDataTestUtils {
|
||||||
|
|
||||||
|
private LiveDataTestUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T waitForValue(LiveData<T> liveData,
|
||||||
|
Predicate<T> predicate,
|
||||||
|
Duration timeout,
|
||||||
|
String description) {
|
||||||
|
AtomicReference<T> lastValue = new AtomicReference<>(liveData.getValue());
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
Observer<T> observer = value -> {
|
||||||
|
lastValue.set(value);
|
||||||
|
if (predicate.test(value)) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
|
||||||
|
liveData.observeForever(observer);
|
||||||
|
|
||||||
|
T currentValue = liveData.getValue();
|
||||||
|
lastValue.set(currentValue);
|
||||||
|
if (predicate.test(currentValue)) {
|
||||||
|
latch.countDown();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!latch.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
|
||||||
|
fail("Timed out waiting for " + description + ". Last value: " + lastValue.get());
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
fail("Interrupted while waiting for " + description);
|
||||||
|
} finally {
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> liveData.removeObserver(observer));
|
||||||
|
}
|
||||||
|
|
||||||
|
return lastValue.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.data.ContentProviderUtils;
|
||||||
|
import de.dennisguse.opentracks.services.TrackRecordingService;
|
||||||
|
|
||||||
|
public final class TestEnvironmentUtils {
|
||||||
|
|
||||||
|
private TestEnvironmentUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopTrackRecordingService() {
|
||||||
|
Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
context.stopService(new Intent(context, TrackRecordingService.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteAllTracks() {
|
||||||
|
Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
new ContentProviderUtils(context).deleteAllTracks(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void resetTrackRecordingServiceAndDeleteTracks() {
|
||||||
|
stopTrackRecordingService();
|
||||||
|
deleteAllTracks();
|
||||||
|
}
|
||||||
|
}
|
||||||
+50
@@ -0,0 +1,50 @@
|
|||||||
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import androidx.test.core.app.ApplicationProvider;
|
||||||
|
import androidx.test.platform.app.InstrumentationRegistry;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.services.TrackRecordingService;
|
||||||
|
import de.dennisguse.opentracks.services.TrackRecordingServiceConnection;
|
||||||
|
|
||||||
|
public final class TrackRecordingServiceTestUtils {
|
||||||
|
|
||||||
|
private TrackRecordingServiceTestUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void waitForRecordingState(boolean isRecording, Duration timeout) {
|
||||||
|
Context context = ApplicationProvider.getApplicationContext();
|
||||||
|
CountDownLatch connectedLatch = new CountDownLatch(1);
|
||||||
|
AtomicReference<TrackRecordingService> serviceRef = new AtomicReference<>();
|
||||||
|
TrackRecordingServiceConnection connection = new TrackRecordingServiceConnection((service, self) -> {
|
||||||
|
serviceRef.set(service);
|
||||||
|
connectedLatch.countDown();
|
||||||
|
});
|
||||||
|
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> connection.bind(context));
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!connectedLatch.await(timeout.toMillis(), TimeUnit.MILLISECONDS)) {
|
||||||
|
fail("Timed out waiting to bind TrackRecordingService");
|
||||||
|
}
|
||||||
|
|
||||||
|
LiveDataTestUtils.waitForValue(serviceRef.get().getRecordingStatusObservable(),
|
||||||
|
status -> status != null && status.isRecording() == isRecording,
|
||||||
|
timeout,
|
||||||
|
"recording state to become " + isRecording);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
fail("Interrupted while waiting for recording state " + isRecording);
|
||||||
|
} finally {
|
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> connection.unbind(context));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package de.dennisguse.opentracks.util;
|
||||||
|
|
||||||
|
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.action.ViewActions.scrollTo;
|
||||||
|
import static androidx.test.espresso.matcher.ViewMatchers.isClickable;
|
||||||
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
||||||
|
import static de.dennisguse.opentracks.util.ActivityTestUtils.waitForResumedActivity;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performClick;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.performLongClick;
|
||||||
|
import static de.dennisguse.opentracks.util.EspressoUtils.waitUntilDisplayed;
|
||||||
|
import static de.dennisguse.opentracks.util.TrackRecordingServiceTestUtils.waitForRecordingState;
|
||||||
|
import static org.hamcrest.Matchers.allOf;
|
||||||
|
|
||||||
|
import androidx.annotation.IdRes;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import de.dennisguse.opentracks.R;
|
||||||
|
import de.dennisguse.opentracks.TrackListActivity;
|
||||||
|
import de.dennisguse.opentracks.TrackRecordingActivity;
|
||||||
|
import de.dennisguse.opentracks.TrackStoppedActivity;
|
||||||
|
|
||||||
|
public final class TrackUiTestUtils {
|
||||||
|
|
||||||
|
private TrackUiTestUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void startRecordingFromTrackList(Duration timeout) {
|
||||||
|
onView(withId(R.id.track_list_fab_action))
|
||||||
|
.perform(performClick());
|
||||||
|
waitForResumedActivity(TrackRecordingActivity.class, timeout);
|
||||||
|
waitForRecordingState(true, timeout);
|
||||||
|
waitUntilDisplayed(withId(R.id.track_recording_fab_action), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void stopRecordingToTrackStopped(Duration timeout, @IdRes int... expectedVisibleViewIds) {
|
||||||
|
onView(withId(R.id.track_recording_fab_action))
|
||||||
|
.perform(performLongClick());
|
||||||
|
waitForResumedActivity(TrackStoppedActivity.class, timeout);
|
||||||
|
waitUntilDisplayed(withId(R.id.track_edit_name), timeout);
|
||||||
|
for (int viewId : expectedVisibleViewIds) {
|
||||||
|
waitUntilDisplayed(withId(viewId), timeout);
|
||||||
|
}
|
||||||
|
onView(withId(R.id.track_edit_name))
|
||||||
|
.perform(closeSoftKeyboard());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void resumeStoppedTrack(Duration timeout) {
|
||||||
|
onView(allOf(withId(R.id.resume_button), isClickable()))
|
||||||
|
.perform(performClick());
|
||||||
|
waitForResumedActivity(TrackRecordingActivity.class, timeout);
|
||||||
|
waitForRecordingState(true, timeout);
|
||||||
|
waitUntilDisplayed(withId(R.id.track_recording_fab_action), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void finishStoppedTrack(Duration timeout) {
|
||||||
|
onView(withId(R.id.finish_button))
|
||||||
|
.perform(performClick());
|
||||||
|
waitForResumedActivity(TrackListActivity.class, timeout);
|
||||||
|
waitUntilDisplayed(withId(R.id.track_list), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void renameStoppedTrack(String name, Duration timeout) {
|
||||||
|
waitUntilDisplayed(withId(R.id.track_edit_name), timeout);
|
||||||
|
onView(withId(R.id.track_edit_name))
|
||||||
|
.perform(scrollTo(), replaceText(name), closeSoftKeyboard());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user