Android Espresso, How can I execute onLongClick and slideDown?

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.

No TouchListener until onLongClick

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.

+2
java android testing android-espresso
source share
1 answer

I saw your comments on my question. In fact, I ran into the same problem at the moment (with the permission of INJECT_EVENTS and all my workarounds this failed). I used this code more than six months ago and it worked for me without any problems.

I can offer another solution for you, but I'm not sure that it will work 100%. Because I created it as super fast. I may have more time to look at it later.

However, try this solution:

  • Try writing your own GeneralSwipeAction. Look at the constructor:

     public GeneralSwipeAction(Swiper swiper, CoordinatesProvider startCoordinatesProvider, CoordinatesProvider endCoordinatesProvider, PrecisionDescriber precisionDescriber) { this.swiper = swiper; this.startCoordinatesProvider = startCoordinatesProvider; this.endCoordinatesProvider = endCoordinatesProvider; this.precisionDescriber = precisionDescriber; } 

Thus, the x, y position of the start / end point is sent through the CoordinatesProvider class. And this is an interface that returns an array of floats. The first float in the array is x, and the second is y.

  1. Create your own provider that implements the code provider:

     import android.support.test.espresso.action.CoordinatesProvider; import android.view.View; public class CustomisableCoordinatesProvider implements CoordinatesProvider { private int x; private int y; public CustomisableCoordinatesProvider(int x, int y) { this.x = x; this.y = y; } @Override public float[] calculateCoordinates(View view) { return new float[]{x,y}; } } 
  2. Deploy your own ViewAction that performs drag and drop using this CustomisableCoordinatesProvider:

     public static ViewAction drag(int startX, int startY, int endX, int endY) { return new GeneralSwipeAction( Swipe.FAST, new CustomisableCoordinatesProvider(startX, startY), new CustomisableCoordinatesProvider(endX, endY), Press.FINGER); } 
  3. And try using it like this:

     onView(withId(R.id.test_player)).perform(drag(0, 100, 0, 100)); 

I have not tested it. But it may work or help you, pointing in the right direction. Try it and tell me if this works, I'm curious :)

+1
source share

All Articles