Home | History | Annotate | Download | only in shadows
      1 package com.xtremelabs.robolectric.shadows;
      2 
      3 import android.view.MotionEvent;
      4 import com.xtremelabs.robolectric.WithTestDefaultsRunner;
      5 import org.junit.Before;
      6 import org.junit.Test;
      7 import org.junit.runner.RunWith;
      8 
      9 import static com.xtremelabs.robolectric.Robolectric.shadowOf;
     10 import static org.hamcrest.CoreMatchers.equalTo;
     11 import static org.junit.Assert.assertEquals;
     12 import static org.junit.Assert.assertThat;
     13 
     14 @RunWith(WithTestDefaultsRunner.class)
     15 public class MotionEventTest {
     16     private MotionEvent event;
     17     private ShadowMotionEvent shadowMotionEvent;
     18 
     19     @Before
     20     public void setUp() throws Exception {
     21         event = MotionEvent.obtain(100, 200, MotionEvent.ACTION_MOVE, 5.0f, 10.0f, 0);
     22         shadowMotionEvent = shadowOf(event);
     23     }
     24 
     25     @Test
     26 	public void addingSecondPointerSetsCount() {
     27         assertThat(event.getX(0), equalTo(5.0f));
     28 		assertThat(event.getY(0), equalTo(10.0f));
     29 		assertThat(event.getPointerCount(), equalTo(1));
     30 
     31 		shadowOf(event).setPointer2( 20.0f, 30.0f );
     32 
     33 		assertThat(event.getX(1), equalTo(20.0f));
     34 		assertThat(event.getY(1), equalTo(30.0f));
     35 		assertThat(event.getPointerCount(), equalTo(2));
     36 	}
     37 
     38     @Test
     39     public void canSetPointerIdsByIndex() {
     40         shadowMotionEvent.setPointer2(20.0f, 30.0f);
     41         shadowMotionEvent.setPointerIds(2, 5);
     42         assertEquals(2, event.getPointerId(0));
     43         assertEquals(5, event.getPointerId(1));
     44     }
     45 
     46     @Test
     47     public void indexShowsUpInAction() {
     48         shadowMotionEvent.setPointerIndex(1);
     49         assertEquals(1 << MotionEvent.ACTION_POINTER_ID_SHIFT | MotionEvent.ACTION_MOVE, event.getAction());
     50     }
     51 
     52     @Test
     53     public void canGetActionIndex() {
     54         assertEquals(0, event.getActionIndex());
     55         shadowMotionEvent.setPointerIndex(1);
     56         assertEquals(1, event.getActionIndex());
     57     }
     58 
     59     @Test
     60     public void getActionMaskedStripsPointerIndexFromAction() {
     61         assertEquals(MotionEvent.ACTION_MOVE, event.getActionMasked());
     62         shadowMotionEvent.setPointerIndex(1);
     63         assertEquals(MotionEvent.ACTION_MOVE, event.getActionMasked());
     64     }
     65 
     66     @Test
     67     public void canFindPointerIndexFromId() {
     68         shadowMotionEvent.setPointer2(20.0f, 30.0f);
     69         shadowMotionEvent.setPointerIds(2, 1);
     70         assertEquals(0, event.findPointerIndex(2));
     71         assertEquals(1, event.findPointerIndex(1));
     72         assertEquals(-1, event.findPointerIndex(3));
     73     }
     74 
     75     @Test
     76     public void canSetMotionEventLocation() throws Exception {
     77         assertEquals(5.0f, event.getX(), 0.0f);
     78         assertEquals(10.0f, event.getY(), 0.0f);
     79         shadowMotionEvent.setLocation(10.0f, 20.0f);
     80         assertEquals(10.0f, event.getX(), 0.0f);
     81         assertEquals(20.0f, event.getY(), 0.0f);
     82     }
     83 }
     84