I mean, I can do two things in sequence, but in the middle there is ACTION_UP MotionEvent, where I removed the OnTouchListener, which I use in the view
In my case, I need to avoid triggering this event.
I have a sports field with players, one of the use cases is moving the player after longClick, the function works well, but I can not reproduce the function in tests.

Here is the class code that I want to check, I tried to compose all the relevant code related to the question.
public class FootballFieldLayout extends RelativeLayout implements View.OnTouchListener { [...] public void addPlayer(FieldPlayer player) { final FieldPlayerView fpv = new FieldPlayerView(getContext(), player); addView(fpv); fpv.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { v.setOnTouchListener(FootballFieldLayout.this); return true; } }); } [...] public float dX=NO_DELTA, dY=NO_DELTA; @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_MOVE: if(dX == NO_DELTA || dY == NO_DELTA){ setDelta(view, event); } view.animate() .x(event.getRawX() + dX) .y(event.getRawY() + dY) .setDuration(0) .start(); break; case MotionEvent.ACTION_UP: view.setOnTouchListener(null); resetDelta(); break; default: return false; } return true; } }
Here is a test
public class MoveOnLongClickActivityTest { [...] @Test public void playerCanBeMovedVerticallyAfterLongClick() throws InterruptedException { onView(withId(R.id.activity_field)).check(matches(isDisplayed())); View view = mActivityRule.getActivity().findViewById(R.id.test_player); float[] beginCoord = {view.getX(), view.getY()}; onView(withId(R.id.test_player)).perform(longClick()); onView(withId(R.id.test_player)).perform(swipeDown()); float[] endCoord = {view.getX(), view.getY()}; assertThat(beginCoord[1], not(equalTo(endCoord[1]))); } }
I always use the ViewActions framework or use a recipe from another person who changes some things, but I never tried to build a complex ViewAction, and I got a little lost.
java android testing android-espresso
raultm
source share