Skip to content

Commit fb8ae55

Browse files
committed
Add some tests and fix bugs
1 parent ccd84fa commit fb8ae55

File tree

4 files changed

+117
-45
lines changed

4 files changed

+117
-45
lines changed

.idea/flutter-intellij.iml

Lines changed: 0 additions & 16 deletions
This file was deleted.

flutter-idea/src/io/flutter/analytics/Analytics.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ private void sendPayload(@NotNull String hitType, @NotNull Map<String, String> a
178178
if (flutterVersion.getVersionText() != null) {
179179
args.put("cd2", flutterVersion.getVersionText());
180180
}
181+
} else if (sdkVersion != null) {
182+
args.put("cd2", sdkVersion);
181183
}
182184

183185
// Record whether this client uses bazel.

flutter-idea/src/io/flutter/analytics/FlutterAnalysisServerListener.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,17 @@ public final class FlutterAnalysisServerListener extends AnalysisServerListenerA
5151
static final String ACCEPTED_COMPLETION = "acceptedCompletion";
5252
static final String REJECTED_COMPLETION = "rejectedCompletion";
5353
static final String E2E_IJ_COMPLETION_TIME = "e2eIJCompletionTime";
54+
static final String ERRORS = "errors";
55+
static final String WARNINGS = "warnings";
56+
static final String HINTS = "hints";
57+
static final String LINTS = "lints";
5458
static final String DURATION = "duration";
5559
static final String FAILURE = "failure";
5660
static final String SUCCESS = "success";
5761
static final String ERROR_TYPE_REQUEST = "R";
5862
static final String ERROR_TYPE_SERVER = "@";
5963

6064
static final String DAS_STATUS_EVENT_TYPE = "analysisServerStatus";
61-
static final String IS_BLAZE_SYNC_SUCCESSFUL = "isBlazeSyncSuccessful";
62-
static final String IS_DART_ACTIVE_LANG = "isDartActiveLang";
63-
static final String IS_DART_ONLY_ACTIVE_LANG = "isDartOnlyActiveLang";
64-
static final String IS_DART_WORKSPACE_TYPE = "isDartWorkspaceType";
65-
static final String IS_DERIVE_TARGETS_ENABLED = "isDeriveTargetsEnabled";
6665
static final String[] ERROR_TYPES = new String[]{
6766
AnalysisErrorType.CHECKED_MODE_COMPILE_TIME_ERROR,
6867
AnalysisErrorType.COMPILE_TIME_ERROR,
@@ -104,10 +103,7 @@ public final class FlutterAnalysisServerListener extends AnalysisServerListenerA
104103
this.pathToHighlightTimestamps = new HashMap<>();
105104
this.pathToOutlineTimestamps = new HashMap<>();
106105
this.requestToDetails = new HashMap<>();
107-
//noinspection ConstantConditions
108-
//if (!ApplicationManager.getApplication().isUnitTestMode()) {
109-
LookupManager.getInstance(project).addPropertyChangeListener(this::onPropertyChange);
110-
//}
106+
LookupManager.getInstance(project).addPropertyChangeListener(this::onPropertyChange);
111107

112108
this.fileEditorManagerListener = new FileEditorManagerListener() {
113109
@Override
@@ -350,15 +346,15 @@ public void serverStatus(AnalysisStatus analysisStatus, PubStatus pubStatus) {
350346
errorCount += extractCount(errorCounts, AnalysisErrorType.CHECKED_MODE_COMPILE_TIME_ERROR);
351347
errorCount += extractCount(errorCounts, AnalysisErrorType.COMPILE_TIME_ERROR);
352348
errorCount += extractCount(errorCounts, AnalysisErrorType.SYNTACTIC_ERROR);
353-
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, "ERRORS", errorCount);
349+
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, ERRORS, errorCount);
354350
int warningCount = 0;
355351
warningCount += extractCount(errorCounts, AnalysisErrorType.STATIC_TYPE_WARNING);
356352
warningCount += extractCount(errorCounts, AnalysisErrorType.STATIC_WARNING);
357-
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, "WARNINGS", warningCount);
353+
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, WARNINGS, warningCount);
358354
int hintCount = extractCount(errorCounts, AnalysisErrorType.HINT);
359-
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, "HINTS", hintCount);
355+
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, HINTS, hintCount);
360356
int lintCount = extractCount(errorCounts, AnalysisErrorType.LINT);
361-
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, "LINTS", lintCount);
357+
FlutterInitializer.getAnalytics().sendEventMetric(DAS_STATUS_EVENT_TYPE, LINTS, lintCount); // test: serverStatus()
362358
}
363359
}
364360

@@ -440,7 +436,7 @@ public void itemSelected(@NotNull LookupEvent event) {
440436
int prefixLength = lookup.getPrefixLength(event.getItem());
441437

442438
if (isDartLookupEvent(event)) {
443-
logCompletion(selection, prefixLength, ACCEPTED_COMPLETION);
439+
logCompletion(selection, prefixLength, ACCEPTED_COMPLETION); // test: acceptedCompletion()
444440
}
445441
}
446442

@@ -466,7 +462,7 @@ public void beforeQuickFixInvoked(@NotNull DartQuickFix intention, @NotNull Edit
466462
List<String> errorsOnLine =
467463
pathToErrors.containsKey(path) ? pathToErrors.get(path).stream().filter(error -> error.getLocation().getStartLine() == lineNumber)
468464
.map(AnalysisError::getCode).collect(Collectors.toList()) : ImmutableList.of();
469-
FlutterInitializer.getAnalytics().sendEventMetric(QUICK_FIX, intention.getText(), errorsOnLine.size());
465+
FlutterInitializer.getAnalytics().sendEventMetric(QUICK_FIX, intention.getText(), errorsOnLine.size()); // test: quickFix()
470466
}
471467
}
472468

@@ -501,21 +497,22 @@ public void onResponse(String jsonString) {
501497
assert logEntry != null;
502498
// Log the "sdkVersion" only if it was provided in the event
503499
if (StringUtil.isEmpty(sdkVersionValue)) {
504-
FlutterInitializer.getAnalytics().sendEvent(ANALYSIS_SERVER_LOG, logEntry);
500+
FlutterInitializer.getAnalytics().sendEvent(ANALYSIS_SERVER_LOG, logEntry); // test: dasListenerLogging()
505501
}
506-
else {
502+
else { // test: dasListenerLoggingWithSdk()
507503
FlutterInitializer.getAnalytics().sendEventWithSdk(ANALYSIS_SERVER_LOG, logEntry, sdkVersionValue);
508504
}
509505
}
510506
}
511507
if (response.get("id") == null) {
512508
return;
513509
}
514-
String id = Objects.requireNonNull(response.get("id")).getAsString();
510+
//noinspection ConstantConditions
511+
String id = response.get("id").getAsString();
515512
RequestDetails details = requestToDetails.get(id);
516513
if (details != null) {
517514
FlutterInitializer.getAnalytics()
518-
.sendTiming(ROUND_TRIP_TIME, details.method(),
515+
.sendTiming(ROUND_TRIP_TIME, details.method(), // test: dasListenerTiming()
519516
Objects.requireNonNull(Duration.between(details.startTime(), Instant.now())).toMillis());
520517
}
521518
requestToDetails.remove(id);

flutter-idea/testSrc/unit/io/flutter/analytics/FlutterAnalysisServerListenerTest.java

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@
55
*/
66
package io.flutter.analytics;
77

8-
import com.intellij.codeInsight.lookup.LookupArranger;
9-
import com.intellij.codeInsight.lookup.LookupElement;
10-
import com.intellij.codeInsight.lookup.LookupEvent;
11-
import com.intellij.codeInsight.lookup.LookupManager;
8+
import com.intellij.codeInsight.completion.PrefixMatcher;
9+
import com.intellij.codeInsight.lookup.*;
1210
import com.intellij.codeInsight.lookup.impl.LookupImpl;
1311
import com.intellij.openapi.editor.Editor;
1412
import com.intellij.openapi.project.Project;
1513
import com.intellij.psi.PsiFile;
14+
import com.intellij.psi.PsiManager;
1615
import com.intellij.testFramework.fixtures.CodeInsightTestFixture;
16+
import com.jetbrains.lang.dart.analyzer.DartAnalysisServerService;
17+
import com.jetbrains.lang.dart.fixes.DartQuickFix;
18+
import com.jetbrains.lang.dart.fixes.DartQuickFixSet;
1719
import io.flutter.FlutterInitializer;
1820
import io.flutter.testing.CodeInsightProjectFixture;
1921
import io.flutter.testing.Testing;
22+
import org.dartlang.analysis.server.protocol.AnalysisStatus;
23+
import org.dartlang.analysis.server.protocol.PubStatus;
2024
import org.jetbrains.annotations.NotNull;
2125
import org.jetbrains.annotations.Nullable;
2226
import org.junit.After;
2327
import org.junit.Before;
2428
import org.junit.Rule;
2529
import org.junit.Test;
2630

31+
import java.util.Map;
32+
33+
import static io.flutter.analytics.FlutterAnalysisServerListener.*;
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
36+
2737
@SuppressWarnings({"LocalCanBeFinal"})
2838
public class FlutterAnalysisServerListenerTest {
2939
private static final String fileContents = "void main() {\n" +
@@ -53,6 +63,8 @@ public void setUp() {
5363
mainFile = innerFixture.addFileToProject("lib/main.dart", fileContents);
5464
transport = new MockAnalyticsTransport();
5565
analytics = new Analytics("123e4567-e89b-12d3-a456-426655440000", "1.0", "IntelliJ CE", "2021.3.2");
66+
analytics.setTransport(transport);
67+
analytics.setCanSend(true);
5668
FlutterInitializer.setAnalytics(analytics);
5769
fasl = FlutterAnalysisServerListener.getInstance(project);
5870
}
@@ -63,18 +75,95 @@ public void tearDown() {
6375
}
6476

6577
@Test
66-
public void test() throws Exception {
67-
//noinspection ConstantConditions
68-
Testing.runOnDispatchThread(() -> innerFixture.openFileInEditor(mainFile.getVirtualFile()));
69-
Editor editor = innerFixture.getEditor();
70-
assert editor != null;
78+
public void acceptedCompletion() throws Exception {
79+
Editor editor = editor();
7180
Testing.runOnDispatchThread(() -> {
7281
editor.getSelectionModel().setSelection(18, 18);
7382
fasl.lookupSelectionHandler = new FlutterAnalysisServerListener.LookupSelectionHandler();
7483
LookupImpl lookup = new LookupImpl(project, editor, new LookupArranger.DefaultArranger());
84+
LookupItem item = new LookupItem(LookupItem.TYPE_TEXT_ATTR, "gr");
85+
lookup.addItem(item, PrefixMatcher.ALWAYS_TRUE);
7586
lookup.addLookupListener(fasl.lookupSelectionHandler);
76-
LookupEvent lookupEvent = new LookupEvent(lookup, false);
87+
LookupEvent lookupEvent = new LookupEvent(lookup, item, 'o');
7788
fasl.lookupSelectionHandler.itemSelected(lookupEvent);
7889
});
90+
assertEquals(1, transport.sentValues.size());
91+
Map<String, String> map = transport.sentValues.get(0);
92+
assertEquals("acceptedCompletion", map.get("ec"));
93+
assertEquals("gr", map.get("ea"));
94+
assertEquals("0", map.get("ev"));
95+
}
96+
97+
@SuppressWarnings("ConstantConditions")
98+
@Test
99+
public void serverStatus() throws Exception {
100+
fasl.serverStatus(new AnalysisStatus(false, null), new PubStatus(false));
101+
assertEquals(4, transport.sentValues.size());
102+
checkStatus(transport.sentValues.get(0), ERRORS, "0");
103+
checkStatus(transport.sentValues.get(1), WARNINGS, "0");
104+
checkStatus(transport.sentValues.get(2), HINTS, "0");
105+
checkStatus(transport.sentValues.get(3), LINTS, "0");
106+
}
107+
108+
private void checkStatus(@NotNull Map<String, String> map, String label, String value) {
109+
assertEquals("analysisServerStatus", map.get("ec"));
110+
assertEquals(label, map.get("ea"));
111+
assertEquals(value, map.get("ev"));
112+
}
113+
114+
@Test
115+
public void quickFix() throws Exception {
116+
Editor editor = editor();
117+
DartAnalysisServerService analysisServer = DartAnalysisServerService.getInstance(project);
118+
PsiManager manager = PsiManager.getInstance(project);
119+
DartQuickFixSet quickFixSet = new DartQuickFixSet(manager, mainFile.getVirtualFile(), 18, null);
120+
DartQuickFix fix = new DartQuickFix(quickFixSet, 0);
121+
analysisServer.fireBeforeQuickFixInvoked(fix, editor, mainFile);
122+
assertEquals(1, transport.sentValues.size());
123+
Map<String, String> map = transport.sentValues.get(0);
124+
assertEquals(QUICK_FIX, map.get("ec"));
125+
assertEquals("", map.get("ea"));
126+
assertEquals("0", map.get("ev"));
127+
}
128+
129+
@Test
130+
public void dasListenerTiming() throws Exception {
131+
fasl.requestListener.onRequest("{\"method\":\"test\",\"id\":\"2\"}");
132+
fasl.responseListener.onResponse("{\"event\":\"none\",\"id\":\"2\"}");
133+
assertEquals(1, transport.sentValues.size());
134+
Map<String, String> map = transport.sentValues.get(0);
135+
assertEquals(ROUND_TRIP_TIME, map.get("utc"));
136+
assertEquals("test", map.get("utv"));
137+
assertNotNull(map.get("utt")); // Not checking a computed value, duration.
138+
}
139+
140+
@Test
141+
public void dasListenerLogging() throws Exception {
142+
fasl.requestListener.onRequest("{\"method\":\"test\",\"id\":\"2\"}");
143+
fasl.responseListener.onResponse("{\"event\":\"server.log\",\"params\":{\"entry\":{\"time\":\"1\",\"kind\":\"\",\"data\":\"\"}}}");
144+
assertEquals(1, transport.sentValues.size());
145+
Map<String, String> map = transport.sentValues.get(0);
146+
assertEquals(ANALYSIS_SERVER_LOG, map.get("ec"));
147+
assertEquals("time|1|kind||data|", map.get("ea"));
148+
}
149+
150+
@Test
151+
public void dasListenerLoggingWithSdk() throws Exception {
152+
fasl.requestListener.onRequest("{\"method\":\"test\",\"id\":\"2\"}");
153+
fasl.responseListener.onResponse("{\"event\":\"server.log\",\"params\":{\"entry\":{\"time\":\"1\",\"kind\":\"\",\"data\":\"\",\"sdkVersion\":\"1\"}}}");
154+
assertEquals(1, transport.sentValues.size());
155+
Map<String, String> map = transport.sentValues.get(0);
156+
assertEquals(ANALYSIS_SERVER_LOG, map.get("ec"));
157+
assertEquals("time|1|kind||data|", map.get("ea"));
158+
assertEquals("1", map.get("cd2"));
159+
}
160+
161+
@NotNull
162+
private Editor editor() throws Exception{
163+
//noinspection ConstantConditions
164+
Testing.runOnDispatchThread(() -> innerFixture.openFileInEditor(mainFile.getVirtualFile()));
165+
Editor e = innerFixture.getEditor();
166+
assert e != null;
167+
return e;
79168
}
80169
}

0 commit comments

Comments
 (0)