forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlutterTestRunner.java
More file actions
352 lines (304 loc) · 12.3 KB
/
FlutterTestRunner.java
File metadata and controls
352 lines (304 loc) · 12.3 KB
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
/*
* Copyright 2017 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.run.test;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSyntaxException;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.ExecutionResult;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessListener;
import com.intellij.execution.process.ProcessOutputTypes;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.GenericProgramRunner;
import com.intellij.execution.runners.RunContentBuilder;
import com.intellij.execution.ui.RunContentDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.wm.ToolWindowId;
import com.intellij.util.TimeoutUtil;
import com.intellij.xdebugger.XDebugProcess;
import com.intellij.xdebugger.XDebugProcessStarter;
import com.intellij.xdebugger.XDebugSession;
import com.intellij.xdebugger.XDebuggerManager;
import com.jetbrains.lang.dart.util.DartUrlResolver;
import io.flutter.FlutterUtils;
import io.flutter.ObservatoryConnector;
import io.flutter.logging.PluginLogger;
import io.flutter.run.FlutterPositionMapper;
import io.flutter.run.common.CommonTestConfigUtils;
import io.flutter.sdk.FlutterSdk;
import io.flutter.settings.FlutterSettings;
import io.flutter.utils.JsonUtils;
import io.flutter.utils.ProcessAdapter;
import io.flutter.utils.StdoutJsonParser;
import io.flutter.utils.VmServiceListenerAdapter;
import io.flutter.vmService.VmServiceConsumers;
import io.flutter.vmService.VmServiceConsumers.EmptyResumeConsumer;
import org.dartlang.vm.service.VmService;
import org.dartlang.vm.service.consumer.VMConsumer;
import org.dartlang.vm.service.element.ElementList;
import org.dartlang.vm.service.element.Event;
import org.dartlang.vm.service.element.EventKind;
import org.dartlang.vm.service.element.Isolate;
import org.dartlang.vm.service.element.IsolateRef;
import org.dartlang.vm.service.element.RPCError;
import org.dartlang.vm.service.element.VM;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
/**
* Runs a Flutter test configuration in the debugger.
*/
public class FlutterTestRunner extends GenericProgramRunner {
@NotNull
@Override
public String getRunnerId() {
return "FlutterDebugTestRunner";
}
@Override
public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
if (!(DefaultDebugExecutor.EXECUTOR_ID.equals(executorId) || ToolWindowId.RUN.equals(executorId)) ||
!(profile instanceof TestConfig config)) {
return false;
}
final FlutterSdk sdk = FlutterSdk.getFlutterSdk(config.getProject());
if (sdk == null) {
return false;
}
return config.getFields().getScope() != TestFields.Scope.DIRECTORY;
}
@Nullable
@Override
protected RunContentDescriptor doExecute(@NotNull RunProfileState state, @NotNull ExecutionEnvironment env)
throws ExecutionException {
if (env.getExecutor().getId().equals(ToolWindowId.RUN)) {
return run((TestLaunchState)state, env);
}
else {
return runInDebugger((TestLaunchState)state, env);
}
}
protected RunContentDescriptor run(@NotNull TestLaunchState launcher, @NotNull ExecutionEnvironment env)
throws ExecutionException {
final ExecutionResult executionResult = launcher.execute(env.getExecutor(), this);
final ObservatoryConnector connector = new Connector(executionResult.getProcessHandler());
ApplicationManager.getApplication().executeOnPooledThread(() -> {
// Poll, waiting for "flutter run" to give us a websocket.
// This is adapted from DartVmServiceDebugProcess::scheduleConnect.
String url = connector.getWebSocketUrl();
while (url == null) {
if (launcher.isTerminated()) {
return;
}
TimeoutUtil.sleep(100);
url = connector.getWebSocketUrl();
}
if (launcher.isTerminated()) {
return;
}
final VmService vmService;
try {
vmService = VmService.connect(url);
}
catch (IOException | RuntimeException e) {
if (!launcher.isTerminated()) {
launcher.notifyTextAvailable(
"Failed to connect to the VM service at: " + url + "\n" + e + "\n",
ProcessOutputTypes.STDERR);
}
return;
}
// Listen for debug 'PauseStart' events for isolates after the initial connect and resume those isolates.
vmService.streamListen(VmService.DEBUG_STREAM_ID, VmServiceConsumers.EMPTY_SUCCESS_CONSUMER);
vmService.addVmServiceListener(new VmServiceListenerAdapter() {
@Override
public void received(String streamId, Event event) {
if (EventKind.PauseStart.equals(event.getKind())) {
resumePausedAtStartIsolate(launcher, vmService, event.getIsolate());
}
}
});
// Resume any isolates paused at the initial connect.
vmService.getVM(new VMConsumer() {
@Override
public void received(VM response) {
final ElementList<IsolateRef> isolates = response.getIsolates();
for (IsolateRef isolateRef : isolates) {
resumePausedAtStartIsolate(launcher, vmService, isolateRef);
}
}
@Override
public void onError(RPCError error) {
if (!launcher.isTerminated()) {
launcher.notifyTextAvailable(
"Error connecting to VM: " + error.getCode() + " " + error.getMessage() + "\n",
ProcessOutputTypes.STDERR);
}
}
});
});
return new RunContentBuilder(executionResult, env).showRunContent(env.getContentToReuse());
}
private void resumePausedAtStartIsolate(@NotNull TestLaunchState launcher, @NotNull VmService vmService, @NotNull IsolateRef isolateRef) {
if (isolateRef.getIsSystemIsolate()) {
return;
}
vmService.getIsolate(isolateRef.getId(), new VmServiceConsumers.GetIsolateConsumerWrapper() {
@Override
public void received(Isolate isolate) {
final Event event = isolate.getPauseEvent();
final EventKind eventKind = event.getKind();
if (eventKind == EventKind.PauseStart) {
vmService.resume(isolateRef.getId(), new EmptyResumeConsumer() {
@Override
public void onError(RPCError error) {
if (!launcher.isTerminated()) {
launcher.notifyTextAvailable(
"Error resuming isolate " + isolateRef.getId() + ": " + error.getCode() + " " + error.getMessage() + "\n",
ProcessOutputTypes.STDERR);
}
}
});
}
}
});
}
protected RunContentDescriptor runInDebugger(@NotNull TestLaunchState launcher, @NotNull ExecutionEnvironment env)
throws ExecutionException {
// Start process and create console.
final ExecutionResult executionResult = launcher.execute(env.getExecutor(), this);
final ObservatoryConnector connector = new Connector(executionResult.getProcessHandler());
// Set up source file mapping.
final DartUrlResolver resolver = DartUrlResolver.getInstance(env.getProject(), launcher.getTestFileOrDir());
final FlutterPositionMapper.Analyzer analyzer = FlutterPositionMapper.Analyzer.create(env.getProject(), launcher.getTestFileOrDir());
final FlutterPositionMapper mapper = new FlutterPositionMapper(env.getProject(), launcher.getPubRoot().getRoot(), resolver, analyzer);
// Create the debug session.
final XDebuggerManager manager = XDebuggerManager.getInstance(env.getProject());
final XDebugSession session = manager.startSession(env, new XDebugProcessStarter() {
@Override
@NotNull
public XDebugProcess start(@NotNull final XDebugSession session) {
return new TestDebugProcess(env, session, executionResult, resolver, connector, mapper);
}
});
return session.getRunContentDescriptor();
}
/**
* Provides observatory URI, as received from the test process.
*/
private static final class Connector implements ObservatoryConnector {
private final StdoutJsonParser stdoutParser = new StdoutJsonParser();
private final ProcessListener listener;
private String observatoryUri;
public Connector(ProcessHandler handler) {
listener = new ProcessAdapter() {
@Override
public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType) {
if (!outputType.equals(ProcessOutputTypes.STDOUT)) {
return;
}
final String text = event.getText();
if (FlutterSettings.getInstance().isVerboseLogging()) {
LOG.info("[<-- " + text.trim() + "]");
}
stdoutParser.appendOutput(text);
for (String line : stdoutParser.getAvailableLines()) {
if (line.startsWith("[{")) {
line = line.trim();
final String json = line.substring(1, line.length() - 1);
dispatchJson(json);
}
}
}
@Override
public void processWillTerminate(@NotNull ProcessEvent event, boolean willBeDestroyed) {
handler.removeProcessListener(listener);
}
};
handler.addProcessListener(listener);
}
@Nullable
@Override
public String getWebSocketUrl() {
if (observatoryUri == null || !observatoryUri.startsWith("http:")) {
return null;
}
return CommonTestConfigUtils.convertHttpServiceProtocolToWs(observatoryUri);
}
@Nullable
@Override
public String getBrowserUrl() {
return observatoryUri;
}
@Nullable
@Override
public String getRemoteBaseUrl() {
return null;
}
@Override
public void onDebuggerPaused(@NotNull Runnable resume) {
}
@Override
public void onDebuggerResumed() {
}
private void dispatchJson(String json) {
final JsonObject obj;
try {
final JsonElement elem = JsonUtils.parseString(json);
obj = elem.getAsJsonObject();
}
catch (JsonSyntaxException e) {
FlutterUtils.warn(LOG, "Unable to parse JSON from Flutter test", e, true);
return;
}
final JsonPrimitive primId = obj.getAsJsonPrimitive("id");
if (primId != null) {
// Not an event.
LOG.info("Ignored JSON from Flutter test: " + json);
return;
}
final JsonPrimitive primEvent = obj.getAsJsonPrimitive("event");
if (primEvent == null) {
FlutterUtils.warn(LOG, "Missing event field in JSON from Flutter test: " + obj);
return;
}
final String eventName = primEvent.getAsString();
if (eventName == null) {
FlutterUtils.warn(LOG, "Unexpected event field in JSON from Flutter test: " + obj);
return;
}
final JsonObject params = obj.getAsJsonObject("params");
if (params == null) {
FlutterUtils.warn(LOG, "Missing parameters in event from Flutter test: " + obj);
return;
}
if (eventName.equals("test.startedProcess")) {
// Since Flutter release 3.34, "observatoryUri" is no longer available due to the removal of
// Observatory in favor of DevTools/VM Service.
// Since then, only the field "vmServiceUri" is given in this event (contrary to both fields
// on earlier versions).
final JsonPrimitive primVmServiceUri = params.getAsJsonPrimitive("vmServiceUri");
if (primVmServiceUri != null) {
observatoryUri = primVmServiceUri.getAsString();
}
else {
final JsonPrimitive primObservatoryUri = params.getAsJsonPrimitive("observatoryUri");
if (primObservatoryUri != null) {
observatoryUri = primObservatoryUri.getAsString();
}
}
}
}
}
private static final @NotNull Logger LOG = PluginLogger.createLogger(FlutterTestRunner.class);
}