55 */
66package 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 .*;
1210import com .intellij .codeInsight .lookup .impl .LookupImpl ;
1311import com .intellij .openapi .editor .Editor ;
1412import com .intellij .openapi .project .Project ;
1513import com .intellij .psi .PsiFile ;
14+ import com .intellij .psi .PsiManager ;
1615import 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 ;
1719import io .flutter .FlutterInitializer ;
1820import io .flutter .testing .CodeInsightProjectFixture ;
1921import io .flutter .testing .Testing ;
22+ import org .dartlang .analysis .server .protocol .AnalysisStatus ;
23+ import org .dartlang .analysis .server .protocol .PubStatus ;
2024import org .jetbrains .annotations .NotNull ;
2125import org .jetbrains .annotations .Nullable ;
2226import org .junit .After ;
2327import org .junit .Before ;
2428import org .junit .Rule ;
2529import 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" })
2838public 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