Home | History | Annotate | Download | only in robolectric
      1 package org.robolectric;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 
      5 import org.junit.Test;
      6 import org.junit.runner.RunWith;
      7 import org.robolectric.annotation.Config;
      8 import org.robolectric.annotation.Implementation;
      9 import org.robolectric.annotation.Implements;
     10 import org.robolectric.annotation.RealObject;
     11 import org.robolectric.annotation.internal.Instrument;
     12 import org.robolectric.shadow.api.Shadow;
     13 
     14 @RunWith(RobolectricTestRunner.class)
     15 @Config(sdk = Config.NEWEST_SDK)
     16 public class InvokeDynamicTest {
     17   @Test
     18   @Config(shadows = {DoNothingShadow.class})
     19   public void doNothing() {
     20     DoNothing nothing = new DoNothing();
     21     assertThat(nothing.identity(5)).isEqualTo(0);
     22   }
     23 
     24   @Test
     25   @Config(shadows = {RealShadow.class})
     26   public void directlyOn() {
     27     Real real = new Real();
     28     RealShadow shadow = Shadow.extract(real);
     29 
     30     assertThat(real.x).isEqualTo(-1);
     31     assertThat(shadow.x).isEqualTo(-2);
     32 
     33     real.setX(5);
     34     assertThat(real.x).isEqualTo(-5);
     35     assertThat(shadow.x).isEqualTo(5);
     36 
     37     Shadow.directlyOn(real, Real.class).setX(42);
     38     assertThat(real.x).isEqualTo(42);
     39     assertThat(shadow.x).isEqualTo(5);
     40   }
     41 
     42   @Test
     43   @Config(shadows = {RealShadow1.class})
     44   public void rebindShadow1() {
     45     RealCopy real = new RealCopy();
     46     real.setX(42);
     47     assertThat(real.x).isEqualTo(1);
     48   }
     49 
     50   @Test
     51   @Config(shadows = {RealShadow2.class})
     52   public void rebindShadow2() {
     53     RealCopy real = new RealCopy();
     54     real.setX(42);
     55     assertThat(real.x).isEqualTo(2);
     56   }
     57 
     58   @Instrument
     59   public static class Real {
     60     public int x = -1;
     61 
     62     public void setX(int x) {
     63       this.x = x;
     64     }
     65   }
     66 
     67   @Instrument
     68   public static class RealCopy {
     69     public int x;
     70 
     71     public void setX(int x) {
     72     }
     73   }
     74 
     75   @Implements(Real.class)
     76   public static class RealShadow {
     77     @RealObject Real real;
     78 
     79     public int x = -2;
     80 
     81     @Implementation
     82     protected void setX(int x) {
     83       this.x = x;
     84       real.x = -x;
     85     }
     86   }
     87 
     88   @Implements(RealCopy.class)
     89   public static class RealShadow1 {
     90     @RealObject RealCopy real;
     91 
     92     @Implementation
     93     protected void setX(int x) {
     94       real.x = 1;
     95     }
     96   }
     97 
     98   @Implements(RealCopy.class)
     99   public static class RealShadow2 {
    100     @RealObject RealCopy real;
    101 
    102     @Implementation
    103     protected void setX(int x) {
    104       real.x = 2;
    105     }
    106   }
    107 
    108   @Instrument
    109   public static class DoNothing {
    110     public int identity(int x) {
    111       return x;
    112     }
    113   }
    114 
    115   @Implements(value = DoNothing.class, callThroughByDefault = false)
    116   public static class DoNothingShadow {
    117 
    118   }
    119 }
    120