/
RobolectricGradleTestRunner.java
68 lines (58 loc) · 2.43 KB
/
RobolectricGradleTestRunner.java
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
package com.jiahaoliuliu.robolectricsample;
import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.bytecode.ClassInfo;
import org.robolectric.bytecode.Setup;
import org.robolectric.res.Fs;
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
/**
* The list of the customized shadows classes name.
* Note they should be full qualified.
*/
private static final String[] CUSTOMIZED_SHADOWS_NAME = {
"com.jiahaoliuliu.robolectricsample.AppUtils"
};
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;
// Since the Android manifest is in another module, System.getProperty
// for "android.manifest", "android.resources" and "android.assets" returns null,
// so it is not possible use them.
// Instead of that, the directory are hard coded
private static final String APP_ROOT_DIRECTORY = "../app/src/main/";
private static final String ANDROID_MANIFEST_FILE_NAME = "AndroidManifest.xml";
private static final String RES_DIR = "res";
private static final String ASSETS_DIR = "assets";
public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
String manifestPath = APP_ROOT_DIRECTORY + ANDROID_MANIFEST_FILE_NAME;
String resDir = APP_ROOT_DIRECTORY + RES_DIR;
String assetsDir = APP_ROOT_DIRECTORY + ASSETS_DIR;
AndroidManifest manifest = new AndroidManifest(Fs.fileFromPath(manifestPath),
Fs.fileFromPath(resDir),
Fs.fileFromPath(assetsDir)) {
@Override
public int getTargetSdkVersion() {
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
}
};
return manifest;
}
@Override
public Setup createSetup() {
return new Setup() {
@Override
public boolean shouldInstrument(ClassInfo classInfo) {
for (String customizedShadowsName : CUSTOMIZED_SHADOWS_NAME) {
if (classInfo.getName().equalsIgnoreCase(customizedShadowsName)) {
return true;
}
}
return super.shouldInstrument(classInfo);
};
};
}
}