2014-02-20 10 views
6

Ich bin ein Neuling in Android und Robolectric. Ich versuche, (wie das Thema es anzeigt) ein Ziehereignis auf einem Viewpager zu simulieren.Wie simuliert man ein Widerstandsereignis auf einem ViewPager in Roboelectric?

Ich habe jemals diesen Code implementiert und es läuft auf dem Emulator.

Die Frage ist meistens: Wie kann ich Einheit testen? Hier

ist die Unit-Test:

/** 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); 
} 
} 

und die die Aktivität:

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); 
      } 
     } 
    } 
} 

und das ist das Layout:

<?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" /> 

Hat jemand eine Idee hat, wie Sie vorgehen oder um mir zu sagen, wo ist mein Fehler?

+0

konnten Sie die Seiten in viewpager wischen, wenn ja, bitte aktualisieren Sie es, ich habe auch Schwierigkeiten. –

Antwort

1

Ich denke, Sie, was Sie suchen, ist die Espresso Bibliothek.

Es simuliert UI-Ereignisse und lässt Sie den Status der Ansicht überprüfen, sobald Sie das Ereignis ausgelöst haben, und es ist wirklich einfach zu bedienen. Vergiss nicht, Animationen in den Entwicklereinstellungen zu deaktivieren.

Verwandte Themen