|
1 | | -/** |
2 | | - * Copyright (C) 2010-2016 eBusiness Information, Excilys Group |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | | - * use this file except in compliance with the License. You may obtain a copy of |
6 | | - * the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed To in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
12 | | - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
13 | | - * License for the specific language governing permissions and limitations under |
14 | | - * the License. |
15 | | - */ |
16 | | -package org.robolectric; |
17 | | - |
18 | | -import static org.fest.reflect.core.Reflection.constructor; |
19 | | -import static org.fest.reflect.core.Reflection.field; |
20 | | -import static org.fest.reflect.core.Reflection.method; |
21 | | -import static org.fest.reflect.core.Reflection.type; |
22 | | - |
23 | | -import java.lang.reflect.Field; |
24 | | -import java.util.Collections; |
25 | | -import java.util.List; |
26 | | - |
27 | | -import org.fest.reflect.reference.TypeRef; |
28 | | -import org.junit.runner.Runner; |
29 | | -import org.junit.runners.Suite; |
30 | | -import org.robolectric.annotation.Config; |
31 | | -import org.robolectric.internal.SdkConfig; |
32 | | -import org.robolectric.internal.SdkEnvironment; |
33 | | -import org.robolectric.manifest.AndroidManifest; |
34 | | - |
35 | | -public class ParameterizedRobolectricTestRunnerWorkaround extends Suite { |
36 | | - |
37 | | - private Object delegate; // ParameterizedRobolectricTestRunner |
38 | | - private Class<?> runnerClass; // ParameterizedRobolectricTestRunner.class |
39 | | - private Class<?> testRunnerClass; // ParameterizedRobolectricTestRunner$TestClassRunnerForParameters.class |
40 | | - private RobolectricTestRunner testrunner; |
41 | | - private List<Runner> runners; // delegate.getChildren() |
42 | | - |
43 | | - public ParameterizedRobolectricTestRunnerWorkaround(Class<?> klass) throws Exception { |
44 | | - super(klass, Collections.<Runner> emptyList()); |
45 | | - |
46 | | - testrunner = new ClassLoaderCreatorRobolectricTestRunner(klass); |
47 | | - |
48 | | - Config config = testrunner.getConfig(klass.getMethods()[0]); |
49 | | - |
50 | | - AndroidManifest manifest = testrunner.getAppManifest(config); |
51 | | - SdkEnvironment environment = method("getEnvironment").withReturnType(SdkEnvironment.class).withParameterTypes(AndroidManifest.class, Config.class).in(testrunner).invoke(manifest, config); |
52 | | - |
53 | | - runnerClass = type(ParameterizedRobolectricTestRunner.class.getName()).withClassLoader(environment.getRobolectricClassLoader()).load(); |
54 | | - testRunnerClass = type(runnerClass.getName() + "$TestClassRunnerForParameters").withClassLoader(environment.getRobolectricClassLoader()).load(); |
55 | | - |
56 | | - Field lastTestRunnerClassField = field("lastTestRunnerClass").ofType(Class.class).in(RobolectricTestRunner.class).info(); |
57 | | - lastTestRunnerClassField.setAccessible(true); |
58 | | - lastTestRunnerClassField.set(testrunner, testRunnerClass); |
59 | | - |
60 | | - delegate = constructor().withParameterTypes(Class.class).in(runnerClass).newInstance(type(klass.getName()).withClassLoader(environment.getRobolectricClassLoader()).load()); |
61 | | - |
62 | | - Field lastSdkEnvironmentField = field("lastSdkEnvironment").ofType(SdkEnvironment.class).in(RobolectricTestRunner.class).info(); |
63 | | - lastSdkEnvironmentField.setAccessible(true); |
64 | | - |
65 | | - Field lastSdkConfigField = field("lastSdkConfig").ofType(SdkConfig.class).in(RobolectricTestRunner.class).info(); |
66 | | - lastSdkConfigField.setAccessible(true); |
67 | | - |
68 | | - runners = method("getChildren").withReturnType(new TypeRef<List<Runner>>() { |
69 | | - }).in(delegate).invoke(); |
70 | | - |
71 | | - for (Runner runner : runners) { |
72 | | - lastTestRunnerClassField.set(runner, testRunnerClass); |
73 | | - lastSdkEnvironmentField.set(runner, environment); |
74 | | - lastSdkConfigField.set(runner, environment.getSdkConfig()); |
75 | | - } |
76 | | - } |
77 | | - |
78 | | - @Override |
79 | | - protected List<Runner> getChildren() { |
80 | | - return runners; |
81 | | - } |
82 | | - |
83 | | - private static class ClassLoaderCreatorRobolectricTestRunner extends RobolectricTestRunner { |
84 | | - |
85 | | - ClassLoaderCreatorRobolectricTestRunner(Class<?> testClass) throws Exception { |
86 | | - super(testClass); |
87 | | -// TODO |
88 | | -// Map<Class<? extends RobolectricTestRunner>, EnvHolder> envHoldersByTestRunner = field("envHoldersByTestRunner") // |
89 | | -// .ofType(new TypeRef<Map<Class<? extends RobolectricTestRunner>, EnvHolder>>() { |
90 | | -// })// |
91 | | -// .in(this)// |
92 | | -// .get(); |
93 | | -// |
94 | | -// EnvHolder envHolder = envHoldersByTestRunner.get(RobolectricTestRunner.class); |
95 | | -// |
96 | | -// if (envHolder != null) { |
97 | | -// Field envHolderField = field("envHolder").ofType(EnvHolder.class).in(RobolectricTestRunner.class).info(); |
98 | | -// envHolderField.setAccessible(true); |
99 | | -// envHolderField.set(this, envHolder); |
100 | | -// } else { |
101 | | -// envHoldersByTestRunner.put(RobolectricTestRunner.class, envHolder); |
102 | | -// } |
103 | | - } |
104 | | - |
105 | | - @Override |
106 | | - protected void validateConstructor(List<Throwable> errors) { |
107 | | - validateOnlyOneConstructor(errors); |
108 | | - } |
109 | | - } |
110 | | -} |
0 commit comments