2014-02-20 2 views
6

저는 Android와 Robolectric의 초보자입니다. 뷰포트에서 드래그 이벤트를 시뮬레이션하려고합니다 (항목에서 가리키는대로).Roboelectric의 ViewPager에서 끌기 이벤트를 시뮬레이트하는 방법은 무엇입니까?

나는이 코드를 구현했으며 에뮬레이터에서 실행됩니다.

대부분 문제는 어떻게 테스트 할 수 있습니까?

/** imports are skipped **/ 

@RunWith(RobolectricTestRunner.class) 
@Config(manifest = "/src/main/AndroidManifest.xml") 
public class TestImages { 

private ImageActivity activity; 

@InjectResource(R.drawable.ic_launcher) 
private static Drawable image; 

@Spy 
private final PicturesModel picturesModel = spy(new PicturesModelImpl()); 

/** 
* Last touched view 
*/ 
private View lastTouchView; 

/** 
* Last touched X coordinate 
*/ 
private float lastTouchX; 
/** 
* Last touched Y coordinate 
*/ 
private float lastTouchY; 

@Before 
public void setup() { 
    MockitoAnnotations.initMocks(this); 

    final Module roboGuiceModule = RoboGuice.newDefaultRoboModule(Robolectric.application); 
    final Module pictureModule = Modules.override(roboGuiceModule).with(new PictureModule()); 
    final Module testModule = Modules.override(pictureModule).with(new TestPictureModule(picturesModel)); 

    RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE, testModule); 
    RoboInjector injector = RoboGuice.getInjector(Robolectric.application); 
    injector.injectMembersWithoutViews(this); 
} 

@Test 
public void scroll_image() { 
    final Intent intent = new Intent(Robolectric.getShadowApplication().getApplicationContext(), ImageActivity.class); 
    final Album album = new Album(1, "album_1", image); 
    intent.putExtra("album", album); 
    intent.putExtra("position", 2); 
    intent.putExtra("imageId", 2L); 

    // Get the activity 
    activity = Robolectric.buildActivity(ImageActivity.class).withIntent(intent).create().get(); 
    final Point point = new Point(); 
    activity.getWindowManager().getDefaultDisplay().getSize(point); 

    // Get a spy viewer otherwise viewPager's width is always 0 
    final ViewPager viewPager = spy((ViewPager) activity.findViewById(R.id.viewPager)); 
    viewPager.setVisibility(View.VISIBLE); 
    when(viewPager.getWidth()).thenReturn(point.x - 50); 

    // First item sent by viewPager before swipe 
    final int firstItem = viewPager.getCurrentItem(); 

    // Swipe 
    drag(viewPager, 10F, 10F, point.x - 60F, 10F); 

    // Next item after swipe 
    final int secondItem = viewPager.getCurrentItem(); 

    // Comparison 
    assertThat(firstItem).isEqualTo(2); 
    assertThat(secondItem).isEqualTo(3); 
} 

public void touchDown(View view, float x, float y) { 
    lastTouchX = x; 
    lastTouchY = y; 
    lastTouchView = view; 

    sendMotionEvent(view, MotionEvent.ACTION_DOWN, x, y); 
} 

public void touchMove(float x, float y) { 
    lastTouchX = x; 
    lastTouchY = y; 

    sendMotionEvent(lastTouchView, MotionEvent.ACTION_MOVE, x, y); 
} 

public void touchUp() { 
    sendMotionEvent(
      lastTouchView, MotionEvent.ACTION_UP, lastTouchX, lastTouchY); 

    lastTouchView = null; 
} 

public void drag(View view, float xStart, float yStart, 
       float xEnd, float yEnd) { 
    touchDown(view, xStart, yStart); 
    touchMove(xEnd, yEnd); 
    touchUp(); 
} 

private void sendMotionEvent(View view, int action, float x, float y) { 

    int[] screenOffset = new int[2]; 
    view.getLocationOnScreen(screenOffset); 

    MotionEvent event = MotionEvent.obtain(100, 200, action, 
      x + screenOffset[0], y + screenOffset[1], 0); 

    shadowOf(event).setPointerIds(1, 2); 
    shadowOf(event).setPointerIndex(1); 

    view.onTouchEvent(event); 
    view.dispatchTouchEvent(event); 
} 
} 

과 활동 : 여기

는 단위 테스트입니다

public class ImageActivity extends RoboActivity { 

    @Inject 
    private PicturesModel picturesModel; 

    @InjectView(R.id.viewPager) 
    private ViewPager viewPager; 

    private PagerAdapter pagerAdapter; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.image); 

     final Bundle bundle = getIntent().getExtras(); 
     if (bundle != null && !bundle.isEmpty()) { 
      final long id = (long) bundle.get("imageId"); 
      final int position = (int) bundle.get("position"); 
      final Picture picture = picturesModel.getPicture(id); 
      if (picture != null) { 

       pagerAdapter = new ImageAdapter(this, picturesModel.getAllPictures((Album) bundle.get("album"))); 
       viewPager.setAdapter(pagerAdapter); 
       viewPager.setCurrentItem(position, true); 
      } 
     } 
    } 
} 

이 레이아웃입니다 :

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/viewPager" /> 

사람이 어떻게 진행하는 아이디어를 가지고 있는가 내 실수는 어디 있니?

+0

보기 페이지에서 페이지를 스 와이프 할 수 있었습니까? 그렇다면 업데이트하십시오. 또한 어려움에 직면하고 있습니다. –

답변

1

나는 당신이 찾고있는 것이 Espresso 라이브러리라고 생각합니다.

UI 이벤트를 시뮬레이트하고 이벤트를 발생 시키면보기 상태를 확인할 수 있으므로 사용하기가 정말 쉽습니다. 개발자 설정에서 애니메이션을 전환하는 것을 잊지 마세요.

관련 문제