1 package org.robolectric.annotation.processing.generator; 2 3 import static com.google.common.truth.Truth.assertThat; 4 import static org.mockito.Mockito.mock; 5 import static org.mockito.Mockito.when; 6 7 import java.io.PrintWriter; 8 import java.io.StringWriter; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.List; 12 import javax.annotation.processing.ProcessingEnvironment; 13 import org.junit.Before; 14 import org.junit.Test; 15 import org.junit.runner.RunWith; 16 import org.junit.runners.JUnit4; 17 import org.robolectric.annotation.processing.RobolectricModel; 18 import org.robolectric.annotation.processing.RobolectricModel.ResetterInfo; 19 20 /** Tests for {@link ShadowProviderGenerator} */ 21 @RunWith(JUnit4.class) 22 public class ShadowProviderGeneratorTest { 23 24 private RobolectricModel model; 25 private ShadowProviderGenerator generator; 26 private StringWriter writer; 27 28 @Before 29 public void setUp() throws Exception { 30 model = mock(RobolectricModel.class); 31 generator = new ShadowProviderGenerator(model, mock(ProcessingEnvironment.class), "the.package", true); 32 writer = new StringWriter(); 33 } 34 35 @Test 36 public void resettersAreOnlyCalledIfSdkMatches() throws Exception { 37 when(model.getVisibleShadowTypes()).thenReturn(Collections.emptyList()); 38 39 List<ResetterInfo> resetterInfos = new ArrayList<>(); 40 resetterInfos.add(resetterInfo("ShadowThing", 19, 20, "reset19To20")); 41 resetterInfos.add(resetterInfo("ShadowThing", -1, 18, "resetMax18")); 42 resetterInfos.add(resetterInfo("ShadowThing", 21, -1, "resetMin21")); 43 when(model.getResetters()).thenReturn(resetterInfos); 44 45 generator.generate(new PrintWriter(writer)); 46 47 assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 19 && org.robolectric.RuntimeEnvironment.getApiLevel() <= 20) ShadowThing.reset19To20();"); 48 assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() >= 21) ShadowThing.resetMin21();"); 49 assertThat(writer.toString()).contains("if (org.robolectric.RuntimeEnvironment.getApiLevel() <= 18) ShadowThing.resetMax18();"); 50 } 51 52 private ResetterInfo resetterInfo(String shadowName, int minSdk, int maxSdk, String methodName) { 53 ResetterInfo resetterInfo = mock(ResetterInfo.class); 54 when(resetterInfo.getMinSdk()).thenReturn(minSdk); 55 when(resetterInfo.getMaxSdk()).thenReturn(maxSdk); 56 when(resetterInfo.getMethodCall()).thenReturn(shadowName + "." + methodName + "();"); 57 return resetterInfo; 58 } 59 } 60