-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
AndroidInstrumentationReporter.java
289 lines (254 loc) · 10.1 KB
/
AndroidInstrumentationReporter.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
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
package cucumber.runtime.formatter;
import android.app.Instrumentation;
import android.os.Bundle;
import cucumber.api.Result;
import cucumber.api.TestCase;
import cucumber.api.event.ConcurrentEventListener;
import cucumber.api.event.EventHandler;
import cucumber.api.event.EventPublisher;
import cucumber.api.event.TestCaseFinished;
import cucumber.api.event.TestCaseStarted;
import cucumber.api.event.TestSourceRead;
import cucumber.api.event.TestStepFinished;
import cucumber.runtime.UndefinedStepsTracker;
import java.io.PrintWriter;
import java.io.StringWriter;
/**
* Reports the test results to the instrumentation through {@link Instrumentation#sendStatus(int, Bundle)} calls.
* A "test" represents the execution of a scenario or scenario example lifecycle, which includes the execution of
* following cucumber elements:
* <ul>
* <li>all before hooks</li>
* <li>all background steps</li>
* <li>all scenario / scenario example steps</li>
* <li>all after hooks</li>
* </ul>
*
* Test reports:
* <ul>
* <li>"OK", when all step results are either "PASSED" or "SKIPPED"</li>
* <li>"FAILURE", when any step result of the background or scenario was "FAILED"</li>
* <li>"ERROR", when any step of the background or scenario or any before or after
* hook threw an exception other than an {@link AssertionError}</li>
* </ul>
*/
public final class AndroidInstrumentationReporter implements ConcurrentEventListener {
/**
* Tests status keys.
*/
static class StatusKeys {
static final String TEST = "test";
static final String CLASS = "class";
static final String STACK = "stack";
static final String NUMTESTS = "numtests";
static final String CURRENT = "current";
}
/**
* Test result status codes.
*/
static class StatusCodes {
static final int FAILURE = -2;
static final int START = 1;
static final int ERROR = -1;
static final int OK = 0;
}
/**
* The collected TestSourceRead events.
*/
private final TestSourcesModel testSources = new TestSourcesModel();
/**
* The instrumentation to report to.
*/
private final Instrumentation instrumentation;
/**
* The total number of tests which will be executed.
*/
private int numberOfTests;
/**
* The severest step result of the current test execution.
* This might be a step or hook result.
*/
private Result severestResult;
/**
* The uri of the feature file of the current test case.
*/
private String currentUri;
/**
* The name of the current feature.
*/
private String currentFeatureName;
/**
* The name of the current test case.
*/
private String currentTestCaseName;
/**
* The number of the current test case (from 1 to {@link #numberOfTests}).
*/
private int currentTestCaseNumber;
/**
* The event handler for the {@link TestSourceRead} events.
*/
private final EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
@Override
public void receive(TestSourceRead event) {
testSourceRead(event);
}
};
/**
* The event handler for the {@link TestCaseStarted} events.
*/
private final EventHandler<TestCaseStarted> testCaseStartedHandler = new EventHandler<TestCaseStarted>() {
@Override
public void receive(TestCaseStarted event) {
startTestCase(event.testCase);
}
};
/**
* The event handler for the {@link TestStepFinished} events.
*/
private final EventHandler<TestStepFinished> testStepFinishedHandler = new EventHandler<TestStepFinished>() {
@Override
public void receive(TestStepFinished event) {
finishTestStep(event.result);
}
};
/**
* The event handler for the {@link TestCaseFinished} events.
*/
private final EventHandler<TestCaseFinished> testCaseFinishedHandler = new EventHandler<TestCaseFinished>() {
@Override
public void receive(TestCaseFinished event) {
finishTestCase();
}
};
private final UndefinedStepsTracker undefinedStepsTracker;
private final UniqueTestNameProvider<TestCase> uniqueTestNameProvider = new UniqueTestNameProvider<>();
/**
* Creates a new instance for the given parameters
*
* @param instrumentation the {@link android.app.Instrumentation} to report statuses to
*/
public AndroidInstrumentationReporter(final UndefinedStepsTracker undefinedStepsTracker, final Instrumentation instrumentation) {
this.undefinedStepsTracker = undefinedStepsTracker;
this.instrumentation = instrumentation;
}
@Override
public void setEventPublisher(final EventPublisher publisher) {
publisher.registerHandlerFor(TestSourceRead.class, testSourceReadHandler);
publisher.registerHandlerFor(TestCaseStarted.class, testCaseStartedHandler);
publisher.registerHandlerFor(TestCaseFinished.class, testCaseFinishedHandler);
publisher.registerHandlerFor(TestStepFinished.class, testStepFinishedHandler);
}
public void setNumberOfTests(final int numberOfTests) {
this.numberOfTests = numberOfTests;
}
void testSourceRead(final TestSourceRead event) {
testSources.addTestSourceReadEvent(event.uri, event);
}
void startTestCase(final TestCase testCase) {
if (!testCase.getUri().equals(currentUri)) {
currentUri = testCase.getUri();
currentFeatureName = testSources.getFeatureName(currentUri);
}
currentTestCaseNumber++;
// Since the names of test cases are not guaranteed to be unique, we must check for unique names
currentTestCaseName = uniqueTestNameProvider.calculateUniqueTestName(testCase, testCase.getName(), testCase.getUri());
resetSeverestResult();
final Bundle testStart = createBundle(currentFeatureName, currentTestCaseName);
instrumentation.sendStatus(StatusCodes.START, testStart);
}
void finishTestStep(final Result result) {
checkAndSetSeverestStepResult(result);
}
void finishTestCase() {
final Bundle testResult = createBundle(currentFeatureName, currentTestCaseName);
switch (severestResult.getStatus()) {
case FAILED:
if (severestResult.getError() instanceof AssertionError) {
testResult.putString(StatusKeys.STACK, severestResult.getErrorMessage());
instrumentation.sendStatus(StatusCodes.FAILURE, testResult);
} else {
testResult.putString(StatusKeys.STACK, getStackTrace(severestResult.getError()));
instrumentation.sendStatus(StatusCodes.ERROR, testResult);
}
break;
case AMBIGUOUS:
testResult.putString(StatusKeys.STACK, getStackTrace(severestResult.getError()));
instrumentation.sendStatus(StatusCodes.ERROR, testResult);
break;
case PENDING:
testResult.putString(StatusKeys.STACK, severestResult.getErrorMessage());
instrumentation.sendStatus(StatusCodes.ERROR, testResult);
break;
case PASSED:
case SKIPPED:
instrumentation.sendStatus(StatusCodes.OK, testResult);
break;
case UNDEFINED:
testResult.putString(StatusKeys.STACK, getStackTrace(new MissingStepDefinitionError(getLastSnippet())));
instrumentation.sendStatus(StatusCodes.ERROR, testResult);
break;
default:
throw new IllegalStateException("Unexpected result status: " + severestResult.getStatus());
}
}
/**
* Creates a template bundle for reporting the start and end of a test.
*
* @param path of the feature file of the current execution
* @param testCaseName of the test case of the current execution
* @return the new {@link Bundle}
*/
private Bundle createBundle(final String path, final String testCaseName) {
final Bundle bundle = new Bundle();
bundle.putInt(StatusKeys.NUMTESTS, numberOfTests);
bundle.putString(StatusKeys.CLASS, String.format("%s", path));
bundle.putString(StatusKeys.TEST, String.format("%s", testCaseName));
bundle.putInt(StatusKeys.CURRENT, currentTestCaseNumber);
return bundle;
}
/**
* Determines the last snippet for a detected undefined step.
*
* @return string representation of the snippet
*/
private String getLastSnippet() {
return undefinedStepsTracker.getSnippets().get(undefinedStepsTracker.getSnippets().size() - 1);
}
/**
* Resets the severest test result for the next scenario life cycle.
*/
private void resetSeverestResult() {
severestResult = null;
}
/**
* Checks if the given {@code result} is more severe than the current {@code severestResult} and
* updates the {@code severestResult} if that should be the case.
*
* @param result the {@link Result} to check
*/
private void checkAndSetSeverestStepResult(final Result result) {
final boolean firstResult = severestResult == null;
if (firstResult) {
severestResult = result;
return;
}
final boolean currentIsPassed = severestResult.is(Result.Type.PASSED);
final boolean nextIsNotPassed = !result.is(Result.Type.PASSED);
if (currentIsPassed && nextIsNotPassed) {
severestResult = result;
}
}
/**
* Creates a string representation of the given {@code throwable}'s stacktrace.
*
* @param throwable the {@link Throwable} to get the stacktrace from
* @return the stacktrace as a string
*/
private static String getStackTrace(final Throwable throwable) {
final StringWriter stringWriter = new StringWriter();
final PrintWriter printWriter = new PrintWriter(stringWriter, true);
throwable.printStackTrace(printWriter);
return stringWriter.getBuffer().toString();
}
}