| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.services.completion.suggestion; | 5 library test.services.completion.suggestion; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/completion/completion_core.dart' |
| 10 show CompletionRequest; |
| 11 import 'package:analysis_server/src/analysis_server.dart'; |
| 9 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 10 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 13 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 14 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 12 import 'package:analysis_server/src/services/index/index.dart'; | 15 import 'package:analysis_server/src/services/index/index.dart'; |
| 13 import 'package:analysis_server/src/services/index/local_memory_index.dart'; | 16 import 'package:analysis_server/src/services/index/local_memory_index.dart'; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; | 17 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; | 18 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; |
| 16 import 'package:analyzer/src/generated/engine.dart'; | 19 import 'package:analyzer/src/generated/engine.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:test_reflective_loader/test_reflective_loader.dart'; | 21 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 19 import 'package:unittest/unittest.dart'; | 22 import 'package:unittest/unittest.dart'; |
| 20 | 23 |
| 21 import '../../abstract_single_unit.dart'; | 24 import '../../abstract_single_unit.dart'; |
| 25 import '../../operation/operation_queue_test.dart'; |
| 22 | 26 |
| 23 main() { | 27 main() { |
| 24 groupSep = ' | '; | 28 groupSep = ' | '; |
| 25 defineReflectiveTests(DartCompletionManagerTest); | 29 defineReflectiveTests(DartCompletionManagerTest); |
| 26 } | 30 } |
| 27 | 31 |
| 28 /** | 32 /** |
| 29 * Returns a [Future] that completes after pumping the event queue [times] | 33 * Returns a [Future] that completes after pumping the event queue [times] |
| 30 * times. By default, this should pump the event queue enough times to allow | 34 * times. By default, this should pump the event queue enough times to allow |
| 31 * any code to run, as long as it's not waiting on some external event. | 35 * any code to run, as long as it's not waiting on some external event. |
| 32 */ | 36 */ |
| 33 Future pumpEventQueue([int times = 20]) { | 37 Future pumpEventQueue([int times = 20]) { |
| 34 if (times == 0) return new Future.value(); | 38 if (times == 0) return new Future.value(); |
| 35 // We use a delayed future to allow microtask events to finish. The | 39 // We use a delayed future to allow microtask events to finish. The |
| 36 // Future.value or Future() constructors use scheduleMicrotask themselves and | 40 // Future.value or Future() constructors use scheduleMicrotask themselves and |
| 37 // would therefore not wait for microtask callbacks that are scheduled after | 41 // would therefore not wait for microtask callbacks that are scheduled after |
| 38 // invoking this method. | 42 // invoking this method. |
| 39 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); | 43 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 40 } | 44 } |
| 41 | 45 |
| 42 @reflectiveTest | 46 @reflectiveTest |
| 43 class DartCompletionManagerTest extends AbstractSingleUnitTest { | 47 class DartCompletionManagerTest extends AbstractSingleUnitTest { |
| 44 Index index; | 48 Index index; |
| 45 SearchEngineImpl searchEngine; | 49 SearchEngineImpl searchEngine; |
| 46 Source source; | 50 Source source; |
| 47 CompletionPerformance perf; | |
| 48 DartCompletionManager manager; | 51 DartCompletionManager manager; |
| 49 MockCompletionContributor contributor1; | 52 MockCompletionContributor contributor1; |
| 50 MockCompletionContributor contributor2; | 53 MockCompletionContributor contributor2; |
| 51 CompletionSuggestion suggestion1; | 54 CompletionSuggestion suggestion1; |
| 52 CompletionSuggestion suggestion2; | 55 CompletionSuggestion suggestion2; |
| 53 bool _continuePerformingAnalysis = true; | 56 bool _continuePerformingAnalysis = true; |
| 54 | 57 |
| 55 void resolveLibrary() { | 58 void resolveLibrary() { |
| 56 context.resolveCompilationUnit( | 59 context.resolveCompilationUnit( |
| 57 source, context.computeLibraryElement(source)); | 60 source, context.computeLibraryElement(source)); |
| 58 } | 61 } |
| 59 | 62 |
| 60 @override | 63 @override |
| 61 void setUp() { | 64 void setUp() { |
| 62 super.setUp(); | 65 super.setUp(); |
| 63 index = createLocalMemoryIndex(); | 66 index = createLocalMemoryIndex(); |
| 64 searchEngine = new SearchEngineImpl(index); | 67 searchEngine = new SearchEngineImpl(index); |
| 65 source = addSource('/does/not/exist.dart', ''); | 68 source = addSource('/does/not/exist.dart', ''); |
| 66 perf = new CompletionPerformance(); | |
| 67 manager = new DartCompletionManager.create(context, searchEngine, source); | 69 manager = new DartCompletionManager.create(context, searchEngine, source); |
| 68 suggestion1 = new CompletionSuggestion(CompletionSuggestionKind.INVOCATION, | 70 suggestion1 = new CompletionSuggestion(CompletionSuggestionKind.INVOCATION, |
| 69 DART_RELEVANCE_DEFAULT, "suggestion1", 1, 1, false, false); | 71 DART_RELEVANCE_DEFAULT, "suggestion1", 1, 1, false, false); |
| 70 suggestion2 = new CompletionSuggestion(CompletionSuggestionKind.IDENTIFIER, | 72 suggestion2 = new CompletionSuggestion(CompletionSuggestionKind.IDENTIFIER, |
| 71 DART_RELEVANCE_DEFAULT, "suggestion2", 2, 2, false, false); | 73 DART_RELEVANCE_DEFAULT, "suggestion2", 2, 2, false, false); |
| 72 new Future(_performAnalysis); | 74 new Future(_performAnalysis); |
| 73 } | 75 } |
| 74 | 76 |
| 75 @override | 77 @override |
| 76 void tearDown() { | 78 void tearDown() { |
| 77 _continuePerformingAnalysis = false; | 79 _continuePerformingAnalysis = false; |
| 78 } | 80 } |
| 79 | 81 |
| 80 test_compute_fastAndFull() { | 82 test_compute_fastAndFull() { |
| 81 contributor1 = new MockCompletionContributor(suggestion1, null); | 83 contributor1 = new MockCompletionContributor(suggestion1, null); |
| 82 contributor2 = new MockCompletionContributor(null, suggestion2); | 84 contributor2 = new MockCompletionContributor(null, suggestion2); |
| 83 manager.contributors = [contributor1, contributor2]; | 85 manager.contributors = [contributor1, contributor2]; |
| 84 int count = 0; | 86 int count = 0; |
| 85 bool done = false; | 87 bool done = false; |
| 86 CompletionRequest completionRequest = new CompletionRequest(0, perf); | 88 AnalysisServer server = new AnalysisServerMock(searchEngine: searchEngine); |
| 89 CompletionRequest completionRequest = |
| 90 new CompletionRequestImpl(server, context, source, 0); |
| 87 manager.results(completionRequest).listen((CompletionResult r) { | 91 manager.results(completionRequest).listen((CompletionResult r) { |
| 88 switch (++count) { | 92 switch (++count) { |
| 89 case 1: | 93 case 1: |
| 90 contributor1.assertCalls(context, source, 0, searchEngine); | 94 contributor1.assertCalls(context, source, 0, searchEngine); |
| 91 contributor2.assertCalls(context, source, 0, searchEngine); | 95 contributor2.assertCalls(context, source, 0, searchEngine); |
| 92 expect(r.last, isFalse); | 96 expect(r.last, isFalse); |
| 93 expect(r.suggestions, hasLength(1)); | 97 expect(r.suggestions, hasLength(1)); |
| 94 expect(r.suggestions, contains(suggestion1)); | 98 expect(r.suggestions, contains(suggestion1)); |
| 95 resolveLibrary(); | 99 resolveLibrary(); |
| 96 break; | 100 break; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 113 expect(done, isTrue); | 117 expect(done, isTrue); |
| 114 }); | 118 }); |
| 115 } | 119 } |
| 116 | 120 |
| 117 test_compute_fastOnly() { | 121 test_compute_fastOnly() { |
| 118 contributor1 = new MockCompletionContributor(suggestion1, null); | 122 contributor1 = new MockCompletionContributor(suggestion1, null); |
| 119 contributor2 = new MockCompletionContributor(suggestion2, null); | 123 contributor2 = new MockCompletionContributor(suggestion2, null); |
| 120 manager.contributors = [contributor1, contributor2]; | 124 manager.contributors = [contributor1, contributor2]; |
| 121 int count = 0; | 125 int count = 0; |
| 122 bool done = false; | 126 bool done = false; |
| 123 CompletionRequest completionRequest = new CompletionRequest(0, perf); | 127 AnalysisServer server = new AnalysisServerMock(searchEngine: searchEngine); |
| 128 CompletionRequest completionRequest = |
| 129 new CompletionRequestImpl(server, context, source, 0); |
| 124 manager.results(completionRequest).listen((CompletionResult r) { | 130 manager.results(completionRequest).listen((CompletionResult r) { |
| 125 switch (++count) { | 131 switch (++count) { |
| 126 case 1: | 132 case 1: |
| 127 contributor1.assertCalls(context, source, 0, searchEngine); | 133 contributor1.assertCalls(context, source, 0, searchEngine); |
| 128 contributor2.assertCalls(context, source, 0, searchEngine); | 134 contributor2.assertCalls(context, source, 0, searchEngine); |
| 129 expect(r.last, isTrue); | 135 expect(r.last, isTrue); |
| 130 expect(r.suggestions, hasLength(2)); | 136 expect(r.suggestions, hasLength(2)); |
| 131 expect(r.suggestions, contains(suggestion1)); | 137 expect(r.suggestions, contains(suggestion1)); |
| 132 expect(r.suggestions, contains(suggestion2)); | 138 expect(r.suggestions, contains(suggestion2)); |
| 133 break; | 139 break; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 @override | 195 @override |
| 190 Future<bool> computeFull(DartCompletionRequest request) { | 196 Future<bool> computeFull(DartCompletionRequest request) { |
| 191 this.request = request; | 197 this.request = request; |
| 192 fullCount++; | 198 fullCount++; |
| 193 if (fullSuggestion != null) { | 199 if (fullSuggestion != null) { |
| 194 request.addSuggestion(fullSuggestion); | 200 request.addSuggestion(fullSuggestion); |
| 195 } | 201 } |
| 196 return new Future.value(fullSuggestion != null); | 202 return new Future.value(fullSuggestion != null); |
| 197 } | 203 } |
| 198 } | 204 } |
| OLD | NEW |