vinoth
vinoth

Reputation: 225

Android Robotium - ViewPager swipe is not working

I wants to swipe the ViewPager. I used following code for page swipe. But unfortunately is not working.

Solo solo = new Solo(getInstrumentation(), getActivity());    
            int screenHeight = activityUtils.getCurrentActivity().getWindowManager().getDefaultDisplay()
                    .getHeight();
            int screenWidth = activityUtils.getCurrentActivity(false).getWindowManager().getDefaultDisplay()
                    .getWidth();
            float x = screenWidth / 3.0f * 2.0f;
            float y = screenHeight / 3.0f * 2.0f;

Try: 1

solo.swipe(x, 0, y, y, 40);

Try: 2

if (side == Side.LEFT)
    drag(0, x, y, y, 40);
else if (side == Side.RIGHT)
    drag(x, 0, y, y, 40);

Try: 3 Following method working but it's moving only single page and also it's moving very slowly.

 solo1.scrollToSide(Solo.RIGHT); 

Is there any option for fast swipe? Which one is best? Kindly share your ideas.

My code with Robotium 5.1

for (int count = 0; count < noOfPages; count++)
   solo.swipeToRight(count);

Methods for view pager swipes

private void swipeToLeft(int stepCount) {
    Display display = solo.getCurrentActivity().getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    float xStart = width - 10 ;
    float xEnd = 10;
    solo.drag(xStart, xEnd, height / 2, height / 2, stepCount);
}

private void swipeToRight(int stepCount) {
    Display display = solo.getCurrentActivity().getWindowManager().getDefaultDisplay();
    int width = display.getWidth();
    int height = display.getHeight();
    float xStart = 10 ;
    float xEnd = width - 10;
    solo.drag(xStart, xEnd, height / 2, height / 2, stepCount);
}

Upvotes: 1

Views: 2761

Answers (3)

Ilie Daniel Stefan
Ilie Daniel Stefan

Reputation: 137

solo.scrollToSide(Solo.RIGHT); This implementation is also fast enough...

Upvotes: 4

Renas
Renas

Reputation: 1919

This issue has been fixed in Robotium 5.1. You can download it from here:

https://code.google.com/p/robotium/wiki/Downloads?tm=2

Upvotes: 1

Paul Harris
Paul Harris

Reputation: 5809

Well the code in the robotium repo for scroll to side is:

public void scrollToSide(int side) {
        switch (side){
        case RIGHT: scroller.scrollToSide(Scroller.Side.RIGHT, 0.70F); break;
        case LEFT:  scroller.scrollToSide(Scroller.Side.LEFT, 0.70F);  break;
        }
    }



@SuppressWarnings("deprecation")
    public void scrollToSide(Side side, float scrollPosition) {
        int screenHeight = activityUtils.getCurrentActivity().getWindowManager().getDefaultDisplay()
                .getHeight();
        int screenWidth = activityUtils.getCurrentActivity(false).getWindowManager().getDefaultDisplay()
                .getWidth();
        float x = screenWidth * scrollPosition;
        float y = screenHeight / 2.0f;
        if (side == Side.LEFT)
            drag(0, x, y, y, 40);
        else if (side == Side.RIGHT)
            drag(x, 0, y, y, 40);
    }

public void drag(float fromX, float toX, float fromY, float toY, 
        int stepCount) {
    dialogUtils.hideSoftKeyboard(null, false, true);
    scroller.drag(fromX, toX, fromY, toY, stepCount);
}

So it looks like you must be close, my guess is the numbers you have used in your drag call are not able to make the view pager work. I would suggest trying th enumbers used by robotium EXCEPT for the last paramater which if you change should make the swipe happen quicker as this number is the ampount of events required to complete the swipe. You may need to experiment with the number.

Upvotes: 0

Related Questions