GestureHandler:public class MainActivity extends Activity {public GestureHandler gestureHandler = new GestureHandler();public GestureDetector detector ;public TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.hello_world);detector = new GestureDetector(this, gestureHandler);}@Overridepublic boolean onTouchEvent(MotionEvent event) {detector.onTouchEvent(event);tv.setText("Gesture: " + gestureHandler.gesture);return true;}}
Test:public class GestureHandler extends SimpleOnGestureListener{public String gesture ="";@Overridepublic boolean onDown(MotionEvent e) {gesture = "onDown";return true;}}
@RunWith(RobolectricTestRunner.class)public class SimpleGestureDetectorTest {private boolean eventSent = false;MainActivity activity;@Beforepublic void setUp() {activity = Robolectric.buildActivity(MainActivity.class).create().get();}@Testpublic void testCurrentThreadActionDown() {currentThreadActionDown(200, 400);assertEquals("onDown", activity.gestureHandler.gesture);}@Testpublic void testUiThreadActionDown() throws InterruptedException {final Object mutex = new Object();activity.runOnUiThread(new Runnable() {@Overridepublic void run() {currentThreadActionDown(200, 400);synchronized (mutex) {eventSent = true;mutex.notifyAll();}}});synchronized (mutex) {if (!eventSent) {mutex.wait(5000);}if (!eventSent) {fail("Event not sent in UI thread");}}// Assert that the event was properly forwardedassertEquals("onDown", activity.gestureHandler.gesture);}private void currentThreadActionDown(float x, float y) {long downTime = SystemClock.uptimeMillis();// Obtain MotionEvent objectMotionEvent motionEvent = MotionEvent.obtain(downTime, // time of first// downdownTime, // time of eventMotionEvent.ACTION_DOWN, // type of eventx, y, // event origin0 // meta state);// send event directly to the activityactivity.onTouchEvent(motionEvent);motionEvent.recycle();}}
I can't speak to what the actual implementation of GestureDetector in Robolectric is, but I will say that you are probably testing the wrong thing. If you know that the GestureDetector calls your onTouchEvent method then you should be calling that method directly from your test. This way you are testing how your application reacts to an event rather than testing the fact that Android calls the callback. This is the spirit of unit tests. You want to test how your code handles situations rather than how Android behaves.
Your test should probably be something more like this:
MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
@Test
public void testActionDownSetsTextViewText() {
activity.gestureHandler.gesture = "onDown";
activity.onTouchEvent(MotionEvent.obtain(20, System.currentTimeMillis(), ACTION_DOWN, 200, 400, 0));
assertEquals("Gesture: onDown", activity.tv.getText().toString());
}
--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.