aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/test/java/org/robolectric/RobolectricTestRunnerMultiApiTest.java
blob: d50ca474b1cb6f78c70318d9b1a65e519c1fc439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package org.robolectric;

import static android.os.Build.VERSION_CODES.KITKAT;
import static android.os.Build.VERSION_CODES.LOLLIPOP;
import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
import static android.os.Build.VERSION_CODES.M;
import static android.os.Build.VERSION_CODES.N;
import static android.os.Build.VERSION_CODES.N_MR1;
import static android.os.Build.VERSION_CODES.O;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.robolectric.RobolectricTestRunner.defaultInjector;

import android.os.Build;
import com.google.common.collect.Range;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.JUnit4;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.InitializationError;
import org.robolectric.annotation.Config;
import org.robolectric.pluginapi.Sdk;
import org.robolectric.pluginapi.SdkPicker;
import org.robolectric.plugins.DefaultSdkPicker;
import org.robolectric.plugins.SdkCollection;
import org.robolectric.util.TestUtil;
import org.robolectric.util.inject.Injector;

@RunWith(JUnit4.class)
public class RobolectricTestRunnerMultiApiTest {

  private static final int[] APIS_FOR_TEST = {KITKAT, LOLLIPOP, LOLLIPOP_MR1, M, N, N_MR1, O};

  private static SdkPicker delegateSdkPicker;
  private static final Injector INJECTOR = defaultInjector()
      .bind(SdkPicker.class, (config, usesSdk) -> delegateSdkPicker.selectSdks(config, usesSdk))
      .build();

  private RobolectricTestRunner runner;
  private RunNotifier runNotifier;
  private MyRunListener runListener;

  private int numSupportedApis;
  private String priorResourcesMode;
  private String priorAlwaysInclude;

  private SdkCollection sdkCollection;

  @Before
  public void setUp() {
    numSupportedApis = APIS_FOR_TEST.length;

    runListener = new MyRunListener();
    runNotifier = new RunNotifier();
    runNotifier.addListener(runListener);
    sdkCollection = new SdkCollection(() -> map(APIS_FOR_TEST));
    delegateSdkPicker = new DefaultSdkPicker(sdkCollection, null);

    priorResourcesMode = System.getProperty("robolectric.resourcesMode");

    priorAlwaysInclude = System.getProperty("robolectric.alwaysIncludeVariantMarkersInTestName");
    System.clearProperty("robolectric.alwaysIncludeVariantMarkersInTestName");
  }

  @After
  public void tearDown() throws Exception {
    TestUtil.resetSystemProperty(
        "robolectric.alwaysIncludeVariantMarkersInTestName", priorAlwaysInclude);
  }

  @Test
  public void createChildrenForEachSupportedApi() throws Throwable {
    runner = runnerOf(TestWithNoConfig.class);
    assertThat(apisFor(runner.getChildren()))
        .containsExactly(KITKAT, LOLLIPOP, LOLLIPOP_MR1, M, N, N_MR1, O);
  }

  @Test
  public void withConfigSdkLatest_shouldUseLatestSupported() throws Throwable {
    runner = runnerOf(TestMethodWithNewestSdk.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(O);
  }

  @Test
  public void withConfigSdkAndMinMax_shouldUseMinMax() throws Throwable {
    runner = runnerOf(TestMethodWithSdkAndMinMax.class);
    try {
      runner.getChildren();
      fail();
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage())
          .contains(
              "sdk and minSdk/maxSdk may not be specified together"
                  + " (sdk=[19], minSdk=23, maxSdk=24)");
    }
  }

  @Test
  public void withEnabledSdks_createChildrenForEachSupportedSdk() throws Throwable {
    delegateSdkPicker = new DefaultSdkPicker(new SdkCollection(() -> map(19, 21)), null);

    runner = runnerOf(TestWithNoConfig.class);
    assertThat(runner.getChildren()).hasSize(2);
  }

  @Test
  public void shouldAddApiLevelToNameOfAllButHighestNumberedMethodName() throws Throwable {
    runner = runnerOf(TestMethodUpToAndIncludingN.class);
    assertThat(runner.getChildren().get(0).getName()).isEqualTo("testSomeApiLevel[19]");
    assertThat(runner.getChildren().get(1).getName()).isEqualTo("testSomeApiLevel[21]");
    assertThat(runner.getChildren().get(2).getName()).isEqualTo("testSomeApiLevel[22]");
    assertThat(runner.getChildren().get(3).getName()).isEqualTo("testSomeApiLevel[23]");
    assertThat(runner.getChildren().get(4).getName()).isEqualTo("testSomeApiLevel");
  }

  @Test
  public void noConfig() throws Throwable {
    runner = runnerOf(TestWithNoConfig.class);
    assertThat(apisFor(runner.getChildren()))
        .containsExactly(KITKAT, LOLLIPOP, LOLLIPOP_MR1, M, N, N_MR1, O);
    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    assertThat(runListener.finished).hasSize(numSupportedApis);
  }

  @Test
  public void classConfigWithSdkGroup() throws Throwable {
    runner = runnerOf(TestClassConfigWithSdkGroup.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(KITKAT, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    // Test method should be run for KitKat and N
    assertThat(runListener.finished).hasSize(2);
  }

  @Test
  public void methodConfigWithSdkGroup() throws Throwable {
    runner = runnerOf(TestMethodConfigWithSdkGroup.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(KITKAT, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    // Test method should be run for KitKat and N
    assertThat(runListener.finished).hasSize(2);
  }

  @Test
  public void classConfigMinSdk() throws Throwable {
    runner = runnerOf(TestClassNAndUp.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(N, N_MR1, O);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    int sdksAfterAndIncludingLollipop = 3;
    assertThat(runListener.finished).hasSize(sdksAfterAndIncludingLollipop);
  }

  @Test
  public void classConfigMaxSdk() throws Throwable {
    runner = runnerOf(TestClassUpToAndIncludingN.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(KITKAT, LOLLIPOP, LOLLIPOP_MR1, M, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    int sdksUpToAndIncludingLollipop = 5;
    assertThat(runListener.finished).hasSize(sdksUpToAndIncludingLollipop);
  }

  @Test
  public void classConfigWithMinSdkAndMaxSdk() throws Throwable {
    runner = runnerOf(TestClassBetweenLollipopMr1AndN.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(LOLLIPOP_MR1, M, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    // Since test method should only be run once
    int sdksInclusivelyBetweenJellyBeanMr2AndLollipop = 3;
    assertThat(runListener.finished).hasSize(sdksInclusivelyBetweenJellyBeanMr2AndLollipop);
  }

  @Test
  public void methodConfigMinSdk() throws Throwable {
    runner = runnerOf(TestMethodNAndUp.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(N, N_MR1, O);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    int sdksAfterAndIncludingLollipop = 3;
    assertThat(runListener.finished).hasSize(sdksAfterAndIncludingLollipop);
  }

  @Test
  public void methodConfigMaxSdk() throws Throwable {
    runner = runnerOf(TestMethodUpToAndIncludingN.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(KITKAT, LOLLIPOP, LOLLIPOP_MR1, M, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    int sdksUpToAndIncludingLollipop = 5;
    assertThat(runListener.finished).hasSize(sdksUpToAndIncludingLollipop);
  }

  @Test
  public void methodConfigWithMinSdkAndMaxSdk() throws Throwable {
    runner = runnerOf(TestMethodBetweenLollipopMr1AndN.class);
    assertThat(apisFor(runner.getChildren())).containsExactly(LOLLIPOP_MR1, M, N);

    runner.run(runNotifier);

    assertThat(runListener.ignored).isEmpty();
    int sdksInclusivelyBetweenJellyBeanMr2AndLollipop = 3;
    assertThat(runListener.finished).hasSize(sdksInclusivelyBetweenJellyBeanMr2AndLollipop);
  }

  ///////////////////////////

  @Nonnull
  private RobolectricTestRunner runnerOf(Class<?> testClass) throws InitializationError {
    return new RobolectricTestRunner(testClass, INJECTOR);
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestWithNoConfig {
    @Test public void test() {}
  }

  @Config(sdk = {KITKAT, N})
  public static class TestClassConfigWithSdkGroup {
    @Test public void testShouldRunApi18() {
      assertThat(Build.VERSION.SDK_INT).isIn(Range.closed(KITKAT, N));
    }
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestMethodConfigWithSdkGroup {
    @Config(sdk = {KITKAT, N})
    @Test
    public void testShouldRunApi16() {
      assertThat(Build.VERSION.SDK_INT).isIn(Range.closed(KITKAT, N));
    }
  }

  @Config(minSdk = N)
  public static class TestClassNAndUp {
    @Test public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isAtLeast(N);
    }
  }

  @Config(maxSdk = N)
  public static class TestClassUpToAndIncludingN {
    @Test public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isAtMost(N);
    }
  }

  @Config(minSdk = LOLLIPOP_MR1, maxSdk = N)
  public static class TestClassBetweenLollipopMr1AndN {
    @Test public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isIn(Range.closed(LOLLIPOP_MR1, N));
    }
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestMethodNAndUp {
    @Config(minSdk = N)
    @Test
    public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isAtLeast(N);
    }
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestMethodUpToAndIncludingN {
    @Config(maxSdk = N)
    @Test
    public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isAtMost(N);
    }
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestMethodBetweenLollipopMr1AndN {
    @Config(minSdk = LOLLIPOP_MR1, maxSdk = N)
    @Test
    public void testSomeApiLevel() {
      assertThat(Build.VERSION.SDK_INT).isIn(Range.closed(LOLLIPOP_MR1, N));
    }
  }

  public static class TestMethodWithNewestSdk {
    @Config(sdk = Config.NEWEST_SDK)
    @Test
    public void testWithLatest() {
      assertThat(Build.VERSION.SDK_INT).isEqualTo(O);
    }
  }

  @Config(sdk = Config.ALL_SDKS)
  public static class TestMethodWithSdkAndMinMax {
    @Config(sdk = KITKAT, minSdk = M, maxSdk = N)
    @Test
    public void testWithKitKatAndLollipop() {
      assertThat(Build.VERSION.SDK_INT).isIn(Range.closed(M, N));
    }
  }

  private static List<Integer> apisFor(List<FrameworkMethod> children) {
    List<Integer> apis = new ArrayList<>();
    for (FrameworkMethod child : children) {
      apis.add(
          ((RobolectricTestRunner.RobolectricFrameworkMethod) child).getSdk().getApiLevel());
    }
    return apis;
  }

  private static class MyRunListener extends RunListener {
    private List<String> started = new ArrayList<>();
    private List<String> finished = new ArrayList<>();
    private List<String> ignored = new ArrayList<>();

    @Override
    public void testStarted(Description description) throws Exception {
      started.add(description.getDisplayName());
    }

    @Override
    public void testFinished(Description description) throws Exception {
      finished.add(description.getDisplayName());
    }

    @Override
    public void testIgnored(Description description) throws Exception {
      ignored.add(description.getDisplayName());
    }
  }

  private List<Sdk> map(int... sdkInts) {
    SdkCollection allSdks = TestUtil.getSdkCollection();
    return Arrays.stream(sdkInts).mapToObj(allSdks::getSdk).collect(Collectors.toList());
  }
}