OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.testing.local; | 5 package org.chromium.testing.local; |
6 | 6 |
7 import org.junit.runners.model.InitializationError; | 7 import org.junit.runners.model.InitializationError; |
8 | 8 |
9 import org.robolectric.AndroidManifest; | |
10 import org.robolectric.DependencyResolver; | |
11 import org.robolectric.RobolectricTestRunner; | 9 import org.robolectric.RobolectricTestRunner; |
12 import org.robolectric.SdkConfig; | |
13 import org.robolectric.annotation.Config; | 10 import org.robolectric.annotation.Config; |
| 11 import org.robolectric.manifest.AndroidManifest; |
14 | 12 |
15 /** | 13 /** |
16 * A custom Robolectric Junit4 Test Runner. This test runner will load the | 14 * A custom Robolectric Junit4 Test Runner. This test runner will ignore the |
17 * "real" android jars from a local directory rather than use Maven to fetch | |
18 * them from the Maven Central repository. Additionally, it will ignore the | |
19 * API level written in the AndroidManifest as that can cause issues if | 15 * API level written in the AndroidManifest as that can cause issues if |
20 * robolectric does not support that API level. | 16 * Robolectric does not support that API level. The API level will be grabbed |
| 17 * from the robolectric Config annotation, or just be |
| 18 * |DEFAULT_ANDROID_API_LEVEL| |
21 */ | 19 */ |
22 public class LocalRobolectricTestRunner extends RobolectricTestRunner { | 20 public class LocalRobolectricTestRunner extends RobolectricTestRunner { |
23 | 21 |
24 private static final int ANDROID_API_LEVEL = 18; | 22 private static final int DEFAULT_ANDROID_API_LEVEL = 21; |
25 | 23 |
26 public LocalRobolectricTestRunner(Class<?> testClass) throws InitializationE
rror { | 24 public LocalRobolectricTestRunner(Class<?> testClass) throws InitializationE
rror { |
27 super(testClass); | 25 super(testClass); |
28 } | 26 } |
29 | 27 |
30 @Override | 28 @Override |
31 protected final DependencyResolver getJarResolver() { | 29 protected int pickSdkVersion(Config config, AndroidManifest appManifest) { |
32 return new RobolectricClasspathDependencyResolver(); | |
33 } | |
34 | |
35 @Override | |
36 protected SdkConfig pickSdkVersion(AndroidManifest appManifest, Config confi
g) { | |
37 // Pulling from the manifest is dangerous as the apk might target a vers
ion of | 30 // Pulling from the manifest is dangerous as the apk might target a vers
ion of |
38 // android that robolectric does not yet support. We still allow the API
level to | 31 // android that robolectric does not yet support. We still allow the API
level to |
39 // be overridden with the Config annotation. | 32 // be overridden with the Config annotation. |
40 return config.emulateSdk() < 0 | 33 if (config != null) { |
41 ? new SdkConfig(ANDROID_API_LEVEL) : super.pickSdkVersion(null,
config); | 34 if (config.sdk().length > 1) { |
| 35 throw new IllegalArgumentException( |
| 36 "RobolectricTestRunner does not support multiple values
for @Config.sdk"); |
| 37 } else if (config.sdk().length == 1) { |
| 38 return config.sdk()[0]; |
| 39 } |
| 40 } |
| 41 return DEFAULT_ANDROID_API_LEVEL; |
42 } | 42 } |
43 | 43 } |
44 @Override | |
45 protected int pickReportedSdkVersion(Config config, AndroidManifest appManif
est) { | |
46 return config.reportSdk() < 0 | |
47 ? ANDROID_API_LEVEL : super.pickReportedSdkVersion(config, appMa
nifest); | |
48 } | |
49 } | |
OLD | NEW |