OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library test.services.completion.suggestion; | |
6 | |
7 import 'dart:async'; | |
8 | |
9 import 'package:analysis_services/completion/completion_manager.dart'; | |
10 import 'package:analysis_services/completion/completion_suggestion.dart'; | |
11 import 'package:analysis_services/index/index.dart'; | |
12 import 'package:analysis_services/index/local_memory_index.dart'; | |
13 import 'package:analysis_services/search/search_engine.dart'; | |
14 import 'package:analysis_services/src/completion/dart_completion_manager.dart'; | |
15 import 'package:analysis_services/src/search/search_engine.dart'; | |
16 import 'package:analysis_testing/abstract_single_unit.dart'; | |
17 import 'package:analysis_testing/reflective_tests.dart'; | |
18 import 'package:analyzer/src/generated/engine.dart'; | |
19 import 'package:analyzer/src/generated/source.dart'; | |
20 import 'package:unittest/unittest.dart'; | |
21 | |
22 main() { | |
23 groupSep = ' | '; | |
24 runReflectiveTests(DartCompletionManagerTest); | |
25 } | |
26 | |
27 /** | |
28 * Returns a [Future] that completes after pumping the event queue [times] | |
29 * times. By default, this should pump the event queue enough times to allow | |
30 * any code to run, as long as it's not waiting on some external event. | |
31 */ | |
32 Future pumpEventQueue([int times = 20]) { | |
33 if (times == 0) return new Future.value(); | |
34 // We use a delayed future to allow microtask events to finish. The | |
35 // Future.value or Future() constructors use scheduleMicrotask themselves and | |
36 // would therefore not wait for microtask callbacks that are scheduled after | |
37 // invoking this method. | |
38 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | |
39 } | |
40 | |
41 @ReflectiveTestCase() | |
42 class DartCompletionManagerTest extends AbstractSingleUnitTest { | |
43 Index index; | |
44 SearchEngineImpl searchEngine; | |
45 Source source; | |
46 DartCompletionManager manager; | |
47 MockCompletionComputer computer1; | |
48 MockCompletionComputer computer2; | |
49 CompletionSuggestion suggestion1; | |
50 CompletionSuggestion suggestion2; | |
51 | |
52 void resolveLibrary() { | |
53 context.resolveCompilationUnit( | |
54 source, | |
55 context.computeLibraryElement(source)); | |
56 } | |
57 | |
58 @override | |
59 void setUp() { | |
60 super.setUp(); | |
61 index = createLocalMemoryIndex(); | |
62 searchEngine = new SearchEngineImpl(index); | |
63 source = addSource('/does/not/exist.dart', ''); | |
64 manager = new DartCompletionManager(context, searchEngine, source, 17); | |
65 suggestion1 = new CompletionSuggestion( | |
66 CompletionSuggestionKind.CLASS, | |
67 CompletionRelevance.DEFAULT, | |
68 "suggestion1", | |
69 1, | |
70 1, | |
71 false, | |
72 false); | |
73 suggestion2 = new CompletionSuggestion( | |
74 CompletionSuggestionKind.CLASS, | |
75 CompletionRelevance.DEFAULT, | |
76 "suggestion2", | |
77 2, | |
78 2, | |
79 false, | |
80 false); | |
81 } | |
82 | |
83 test_compute_fastAndFull() { | |
84 computer1 = new MockCompletionComputer(suggestion1, null); | |
85 computer2 = new MockCompletionComputer(null, suggestion2); | |
86 manager.computers = [computer1, computer2]; | |
87 int count = 0; | |
88 bool done = false; | |
89 manager.results().listen((CompletionResult r) { | |
90 switch (++count) { | |
91 case 1: | |
92 computer1.assertCalls(context, source, 17, searchEngine); | |
93 computer2.assertCalls(context, source, 17, searchEngine); | |
94 expect(r.last, isFalse); | |
95 expect(r.suggestions, hasLength(1)); | |
96 expect(r.suggestions, contains(suggestion1)); | |
97 resolveLibrary(); | |
98 break; | |
99 case 2: | |
100 computer1.assertFull(0); | |
101 computer2.assertFull(1); | |
102 expect(r.last, isTrue); | |
103 expect(r.suggestions, hasLength(2)); | |
104 expect(r.suggestions, contains(suggestion1)); | |
105 expect(r.suggestions, contains(suggestion2)); | |
106 break; | |
107 default: | |
108 fail('unexpected'); | |
109 } | |
110 }, onDone: () { | |
111 done = true; | |
112 expect(count, equals(2)); | |
113 }); | |
114 return pumpEventQueue().then((_) { | |
115 expect(done, isTrue); | |
116 }); | |
117 } | |
118 | |
119 test_compute_fastOnly() { | |
120 computer1 = new MockCompletionComputer(suggestion1, null); | |
121 computer2 = new MockCompletionComputer(suggestion2, null); | |
122 manager.computers = [computer1, computer2]; | |
123 int count = 0; | |
124 bool done = false; | |
125 manager.results().listen((CompletionResult r) { | |
126 switch (++count) { | |
127 case 1: | |
128 computer1.assertCalls(context, source, 17, searchEngine); | |
129 computer2.assertCalls(context, source, 17, searchEngine); | |
130 expect(r.last, isTrue); | |
131 expect(r.suggestions, hasLength(2)); | |
132 expect(r.suggestions, contains(suggestion1)); | |
133 expect(r.suggestions, contains(suggestion2)); | |
134 break; | |
135 default: | |
136 fail('unexpected'); | |
137 } | |
138 }, onDone: () { | |
139 done = true; | |
140 expect(count, equals(1)); | |
141 }); | |
142 return pumpEventQueue().then((_) { | |
143 expect(done, isTrue); | |
144 }); | |
145 } | |
146 } | |
147 | |
148 class MockCompletionComputer extends DartCompletionComputer { | |
149 final CompletionSuggestion fastSuggestion; | |
150 final CompletionSuggestion fullSuggestion; | |
151 int fastCount = 0; | |
152 int fullCount = 0; | |
153 DartCompletionRequest request; | |
154 | |
155 MockCompletionComputer(this.fastSuggestion, this.fullSuggestion); | |
156 | |
157 assertCalls(AnalysisContext context, Source source, int offset, | |
158 SearchEngine searchEngine) { | |
159 expect(request.context, equals(context)); | |
160 expect(request.source, equals(source)); | |
161 expect(request.offset, equals(offset)); | |
162 expect(request.searchEngine, equals(searchEngine)); | |
163 expect(this.fastCount, equals(1)); | |
164 expect(this.fullCount, equals(0)); | |
165 } | |
166 | |
167 assertFull(int fullCount) { | |
168 expect(this.fastCount, equals(1)); | |
169 expect(this.fullCount, equals(fullCount)); | |
170 } | |
171 | |
172 @override | |
173 bool computeFast(DartCompletionRequest request) { | |
174 this.request = request; | |
175 fastCount++; | |
176 if (fastSuggestion != null) { | |
177 request.suggestions.add(fastSuggestion); | |
178 } | |
179 return fastSuggestion != null; | |
180 } | |
181 | |
182 @override | |
183 Future<bool> computeFull(DartCompletionRequest request) { | |
184 this.request = request; | |
185 fullCount++; | |
186 if (fullSuggestion != null) { | |
187 request.suggestions.add(fullSuggestion); | |
188 } | |
189 return new Future.value(fullSuggestion != null); | |
190 } | |
191 } | |
OLD | NEW |