| 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.domain.completion; | 5 library test.domain.completion; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 10 import 'package:analysis_server/src/constants.dart'; | 10 import 'package:analysis_server/src/constants.dart'; |
| 11 import 'package:analysis_server/src/domain_completion.dart'; | 11 import 'package:analysis_server/src/domain_completion.dart'; |
| 12 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; | 12 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
| 13 import 'package:analysis_server/src/services/index/index.dart'; | 13 import 'package:analysis_server/src/services/index/index.dart'; |
| 14 import 'package:test/test.dart'; | 14 import 'package:test/test.dart'; |
| 15 | 15 |
| 16 import 'analysis_abstract.dart'; | 16 import 'analysis_abstract.dart'; |
| 17 import 'mocks.dart'; | |
| 18 | 17 |
| 19 class AbstractCompletionDomainTest extends AbstractAnalysisTest { | 18 class AbstractCompletionDomainTest extends AbstractAnalysisTest { |
| 20 String completionId; | 19 String completionId; |
| 21 int completionOffset; | 20 int completionOffset; |
| 22 int replacementOffset; | 21 int replacementOffset; |
| 23 int replacementLength; | 22 int replacementLength; |
| 23 Map<String, Completer<Null>> receivedSuggestionsCompleters = {}; |
| 24 List<CompletionSuggestion> suggestions = []; | 24 List<CompletionSuggestion> suggestions = []; |
| 25 bool suggestionsDone = false; | 25 bool suggestionsDone = false; |
| 26 Map<String, List<CompletionSuggestion>> allSuggestions = {}; | 26 Map<String, List<CompletionSuggestion>> allSuggestions = {}; |
| 27 | 27 |
| 28 String addTestFile(String content, {int offset}) { | 28 String addTestFile(String content, {int offset}) { |
| 29 completionOffset = content.indexOf('^'); | 29 completionOffset = content.indexOf('^'); |
| 30 if (offset != null) { | 30 if (offset != null) { |
| 31 expect(completionOffset, -1, reason: 'cannot supply offset and ^'); | 31 expect(completionOffset, -1, reason: 'cannot supply offset and ^'); |
| 32 completionOffset = offset; | 32 completionOffset = offset; |
| 33 return super.addTestFile(content); | 33 return super.addTestFile(content); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 void assertValidId(String id) { | 75 void assertValidId(String id) { |
| 76 expect(id, isNotNull); | 76 expect(id, isNotNull); |
| 77 expect(id.isNotEmpty, isTrue); | 77 expect(id.isNotEmpty, isTrue); |
| 78 } | 78 } |
| 79 | 79 |
| 80 @override | 80 @override |
| 81 Index createIndex() { | 81 Index createIndex() { |
| 82 return createMemoryIndex(); | 82 return createMemoryIndex(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 Future getSuggestions() { | 85 Future getSuggestions() async { |
| 86 return waitForTasksFinished().then((_) { | 86 await waitForTasksFinished(); |
| 87 Request request = | 87 |
| 88 new CompletionGetSuggestionsParams(testFile, completionOffset) | 88 Request request = |
| 89 .toRequest('0'); | 89 new CompletionGetSuggestionsParams(testFile, completionOffset) |
| 90 Response response = handleSuccessfulRequest(request); | 90 .toRequest('0'); |
| 91 completionId = response.id; | 91 Response response = await waitResponse(request); |
| 92 assertValidId(completionId); | 92 var result = new CompletionGetSuggestionsResult.fromResponse(response); |
| 93 return pumpEventQueue().then((_) { | 93 completionId = result.id; |
| 94 expect(suggestionsDone, isTrue); | 94 assertValidId(completionId); |
| 95 }); | 95 await _getResultsCompleter(completionId).future; |
| 96 }); | 96 expect(suggestionsDone, isTrue); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void processNotification(Notification notification) { | 99 processNotification(Notification notification) async { |
| 100 if (notification.event == COMPLETION_RESULTS) { | 100 if (notification.event == COMPLETION_RESULTS) { |
| 101 var params = new CompletionResultsParams.fromNotification(notification); | 101 var params = new CompletionResultsParams.fromNotification(notification); |
| 102 String id = params.id; | 102 String id = params.id; |
| 103 assertValidId(id); | 103 assertValidId(id); |
| 104 if (id == completionId) { | 104 replacementOffset = params.replacementOffset; |
| 105 expect(suggestionsDone, isFalse); | 105 replacementLength = params.replacementLength; |
| 106 replacementOffset = params.replacementOffset; | 106 suggestionsDone = params.isLast; |
| 107 replacementLength = params.replacementLength; | 107 expect(suggestionsDone, isNotNull); |
| 108 suggestionsDone = params.isLast; | 108 suggestions = params.results; |
| 109 expect(suggestionsDone, isNotNull); | 109 expect(allSuggestions.containsKey(id), isFalse); |
| 110 suggestions = params.results; | |
| 111 } | |
| 112 allSuggestions[id] = params.results; | 110 allSuggestions[id] = params.results; |
| 111 _getResultsCompleter(id).complete(null); |
| 113 } else if (notification.event == SERVER_ERROR) { | 112 } else if (notification.event == SERVER_ERROR) { |
| 114 fail('server error: ${notification.toJson()}'); | 113 fail('server error: ${notification.toJson()}'); |
| 115 } | 114 } |
| 116 } | 115 } |
| 117 | 116 |
| 118 @override | 117 @override |
| 119 void setUp() { | 118 void setUp() { |
| 120 super.setUp(); | 119 super.setUp(); |
| 121 createProject(); | 120 createProject(); |
| 122 handler = new CompletionDomainHandler(server); | 121 handler = new CompletionDomainHandler(server); |
| 123 } | 122 } |
| 123 |
| 124 Completer<Null> _getResultsCompleter(String id) { |
| 125 return receivedSuggestionsCompleters.putIfAbsent( |
| 126 id, () => new Completer<Null>()); |
| 127 } |
| 124 } | 128 } |
| OLD | NEW |