Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static com.google.common.truth.Truth.assertThat;
      4 import static org.junit.Assert.assertNull;
      5 import static org.junit.Assert.assertSame;
      6 import static org.robolectric.Shadows.shadowOf;
      7 
      8 import android.view.MotionEvent;
      9 import android.view.ScaleGestureDetector;
     10 import androidx.test.core.app.ApplicationProvider;
     11 import androidx.test.ext.junit.runners.AndroidJUnit4;
     12 import org.junit.Before;
     13 import org.junit.Test;
     14 import org.junit.runner.RunWith;
     15 
     16 @RunWith(AndroidJUnit4.class)
     17 public class ShadowScaleGestureDetectorTest {
     18 
     19   private ScaleGestureDetector detector;
     20   private MotionEvent motionEvent;
     21 
     22   @Before
     23   public void setUp() throws Exception {
     24     detector = new ScaleGestureDetector(ApplicationProvider.getApplicationContext(), null);
     25     motionEvent = MotionEvent.obtain(-1, -1, MotionEvent.ACTION_UP, 100, 30, -1);
     26   }
     27 
     28   @Test
     29   public void test_getOnTouchEventMotionEvent() throws Exception {
     30     detector.onTouchEvent(motionEvent);
     31     assertSame(motionEvent, shadowOf(detector).getOnTouchEventMotionEvent());
     32   }
     33 
     34   @Test
     35   public void test_getScaleFactor() throws Exception {
     36     shadowOf(detector).setScaleFactor(2.0f);
     37     assertThat(2.0f).isEqualTo(detector.getScaleFactor());
     38   }
     39 
     40   @Test
     41   public void test_getFocusXY() throws Exception {
     42     shadowOf(detector).setFocusXY(2.0f, 3.0f);
     43     assertThat(2.0f).isEqualTo(detector.getFocusX());
     44     assertThat(3.0f).isEqualTo(detector.getFocusY());
     45   }
     46 
     47   @Test
     48   public void test_getListener() throws Exception {
     49     TestOnGestureListener listener = new TestOnGestureListener();
     50     assertSame(
     51         listener,
     52         shadowOf(new ScaleGestureDetector(ApplicationProvider.getApplicationContext(), listener))
     53             .getListener());
     54   }
     55 
     56   @Test
     57   public void test_reset() throws Exception {
     58     assertDefaults();
     59 
     60     detector.onTouchEvent(motionEvent);
     61     shadowOf(detector).setFocusXY(3f, 3f);
     62     shadowOf(detector).setScaleFactor(4f);
     63     assertSame(motionEvent, shadowOf(detector).getOnTouchEventMotionEvent());
     64 
     65     shadowOf(detector).reset();
     66 
     67     assertDefaults();
     68   }
     69 
     70   private void assertDefaults() {
     71     assertNull(shadowOf(detector).getOnTouchEventMotionEvent());
     72     assertThat(1f).isEqualTo(detector.getScaleFactor());
     73     assertThat(0f).isEqualTo(detector.getFocusX());
     74     assertThat(0f).isEqualTo(detector.getFocusY());
     75   }
     76 
     77   private static class TestOnGestureListener implements ScaleGestureDetector.OnScaleGestureListener {
     78     @Override
     79     public boolean onScale(ScaleGestureDetector detector) {
     80       return false;
     81     }
     82 
     83     @Override
     84     public boolean onScaleBegin(ScaleGestureDetector detector) {
     85       return false;
     86     }
     87 
     88     @Override
     89     public void onScaleEnd(ScaleGestureDetector detector) {
     90     }
     91   }
     92 }
     93