forked from upstream-mirrors/OpenTracks
138 lines
5.0 KiB
Java
138 lines
5.0 KiB
Java
package de.dennisguse.opentracks;
|
|
|
|
|
|
import static androidx.test.espresso.Espresso.onView;
|
|
import static androidx.test.espresso.action.ViewActions.click;
|
|
import static androidx.test.espresso.matcher.ViewMatchers.assertThat;
|
|
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
|
|
import static androidx.test.espresso.matcher.ViewMatchers.withId;
|
|
import static org.hamcrest.CoreMatchers.is;
|
|
|
|
import android.content.Context;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
import android.view.ViewGroup;
|
|
import android.view.ViewParent;
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
|
import androidx.test.core.app.ApplicationProvider;
|
|
import androidx.test.espresso.NoMatchingViewException;
|
|
import androidx.test.espresso.UiController;
|
|
import androidx.test.espresso.ViewAction;
|
|
import androidx.test.espresso.ViewAssertion;
|
|
import androidx.test.espresso.ViewInteraction;
|
|
import androidx.test.espresso.action.MotionEvents;
|
|
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
|
import androidx.test.filters.LargeTest;
|
|
import androidx.test.rule.ActivityTestRule;
|
|
import androidx.test.rule.GrantPermissionRule;
|
|
|
|
import org.hamcrest.Description;
|
|
import org.hamcrest.Matcher;
|
|
import org.hamcrest.TypeSafeMatcher;
|
|
import org.junit.Rule;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
|
|
import de.dennisguse.opentracks.content.data.DataField;
|
|
import de.dennisguse.opentracks.content.data.Layout;
|
|
import de.dennisguse.opentracks.settings.PreferencesUtils;
|
|
|
|
@LargeTest
|
|
@RunWith(AndroidJUnit4.class)
|
|
public class EspressoCustomLayoutTest {
|
|
|
|
private final Context context = ApplicationProvider.getApplicationContext();
|
|
|
|
@Rule
|
|
public ActivityTestRule<TrackListActivity> mActivityTestRule = new ActivityTestRule<>(TrackListActivity.class);
|
|
|
|
@Rule
|
|
public GrantPermissionRule mGrantPermissionRule =
|
|
GrantPermissionRule.grant(
|
|
"android.permission.ACCESS_FINE_LOCATION");
|
|
|
|
@Test
|
|
public void customLayoutTest() {
|
|
// TrackListActivity: start recording
|
|
ViewInteraction trackControllerRecordButton = onView(withId(R.id.controller_record));
|
|
trackControllerRecordButton.perform(click());
|
|
|
|
// Get custom layout preferences and check all data fields are showed.
|
|
Layout layout = PreferencesUtils.getCustomLayout();
|
|
|
|
onView(withId(R.id.stats_recycler_view)).check(new RecyclerViewItemCountAssertion((int) layout.getFields().stream().filter(DataField::isVisible).count()));
|
|
|
|
// stop recording
|
|
ViewInteraction trackControllerStopButton = onView(withId(R.id.controller_stop));
|
|
trackControllerStopButton.perform(veryLongTouch(1600));
|
|
}
|
|
|
|
private static class RecyclerViewItemCountAssertion implements ViewAssertion {
|
|
private final int expectedCount;
|
|
|
|
public RecyclerViewItemCountAssertion(int expectedCount) {
|
|
this.expectedCount = expectedCount;
|
|
}
|
|
|
|
@Override
|
|
public void check(View view, NoMatchingViewException noViewFoundException) {
|
|
if (noViewFoundException != null) {
|
|
throw noViewFoundException;
|
|
}
|
|
|
|
RecyclerView recyclerView = (RecyclerView) view;
|
|
RecyclerView.Adapter<?> adapter = recyclerView.getAdapter();
|
|
assertThat(adapter.getItemCount(), is(expectedCount));
|
|
}
|
|
}
|
|
|
|
private static Matcher<View> childAtPosition(
|
|
final Matcher<View> parentMatcher, final int position) {
|
|
|
|
return new TypeSafeMatcher<>() {
|
|
@Override
|
|
public void describeTo(Description description) {
|
|
description.appendText("Child at position " + position + " in parent ");
|
|
parentMatcher.describeTo(description);
|
|
}
|
|
|
|
@Override
|
|
public boolean matchesSafely(View view) {
|
|
ViewParent parent = view.getParent();
|
|
return parent instanceof ViewGroup && parentMatcher.matches(parent)
|
|
&& view.equals(((ViewGroup) parent).getChildAt(position));
|
|
}
|
|
};
|
|
}
|
|
|
|
private static ViewAction veryLongTouch(final int duration_ms) {
|
|
return new ViewAction() {
|
|
@Override
|
|
public String getDescription() {
|
|
return "Perform long touch.";
|
|
}
|
|
|
|
@Override
|
|
public Matcher<View> getConstraints() {
|
|
return isDisplayed();
|
|
}
|
|
|
|
@Override
|
|
public void perform(UiController uiController, final View view) {
|
|
// Get view absolute position
|
|
int[] location = new int[2];
|
|
view.getLocationOnScreen(location);
|
|
|
|
// Offset coordinates by view position
|
|
float[] coordinates = new float[]{location[0] + 1, location[1] + 1};
|
|
|
|
// Send down event, pause, and send up
|
|
MotionEvent down = MotionEvents.sendDown(uiController, coordinates, new float[]{1f, 1f}).down;
|
|
uiController.loopMainThreadForAtLeast(duration_ms);
|
|
MotionEvents.sendUp(uiController, down, coordinates);
|
|
}
|
|
};
|
|
}
|
|
}
|