| 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 mocks; | 5 library mocks; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 @MirrorsUsed(targets: 'mocks', override: '*') | 10 @MirrorsUsed(targets: 'mocks', override: '*') |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 /** | 126 /** |
| 127 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. | 127 * A mock [ServerCommunicationChannel] for testing [AnalysisServer]. |
| 128 */ | 128 */ |
| 129 class MockServerChannel implements ServerCommunicationChannel { | 129 class MockServerChannel implements ServerCommunicationChannel { |
| 130 StreamController<Request> requestController = new StreamController<Request>(); | 130 StreamController<Request> requestController = new StreamController<Request>(); |
| 131 StreamController<Response> responseController = new StreamController<Response>
(); | 131 StreamController<Response> responseController = new StreamController<Response>
(); |
| 132 StreamController<Notification> notificationController = new StreamController<N
otification>(sync: true); | 132 StreamController<Notification> notificationController = new StreamController<N
otification>(sync: true); |
| 133 | 133 |
| 134 List<Response> responsesReceived = []; | 134 List<Response> responsesReceived = []; |
| 135 List<Notification> notificationsReceived = []; | 135 List<Notification> notificationsReceived = []; |
| 136 bool _closed = false; |
| 136 | 137 |
| 137 MockServerChannel() { | 138 MockServerChannel() { |
| 138 } | 139 } |
| 139 | 140 |
| 140 @override | 141 @override |
| 141 void listen(void onRequest(Request request), {Function onError, void onDone()}
) { | 142 void listen(void onRequest(Request request), {Function onError, void onDone()}
) { |
| 142 requestController.stream.listen(onRequest, onError: onError, onDone: onDone)
; | 143 requestController.stream.listen(onRequest, onError: onError, onDone: onDone)
; |
| 143 } | 144 } |
| 144 | 145 |
| 145 @override | 146 @override |
| 146 void sendNotification(Notification notification) { | 147 void sendNotification(Notification notification) { |
| 148 // Don't deliver notifications after the connection is closed. |
| 149 if (_closed) { |
| 150 return; |
| 151 } |
| 147 notificationsReceived.add(notification); | 152 notificationsReceived.add(notification); |
| 148 // Wrap send notification in future to simulate websocket | 153 // Wrap send notification in future to simulate websocket |
| 149 // TODO(scheglov) ask Dan why and decide what to do | 154 // TODO(scheglov) ask Dan why and decide what to do |
| 150 // new Future(() => notificationController.add(notification)); | 155 // new Future(() => notificationController.add(notification)); |
| 151 notificationController.add(notification); | 156 notificationController.add(notification); |
| 152 } | 157 } |
| 153 | 158 |
| 154 /** | 159 /** |
| 155 * Simulate request/response pair. | 160 * Simulate request/response pair. |
| 156 */ | 161 */ |
| 157 Future<Response> sendRequest(Request request) { | 162 Future<Response> sendRequest(Request request) { |
| 163 // No further requests should be sent after the connection is closed. |
| 164 if (_closed) { |
| 165 throw new Exception('sendRequest after connection closed'); |
| 166 } |
| 158 // Wrap send request in future to simulate websocket | 167 // Wrap send request in future to simulate websocket |
| 159 new Future(() => requestController.add(request)); | 168 new Future(() => requestController.add(request)); |
| 160 return waitForResponse(request); | 169 return waitForResponse(request); |
| 161 } | 170 } |
| 162 | 171 |
| 163 @override | 172 @override |
| 164 void sendResponse(Response response) { | 173 void sendResponse(Response response) { |
| 174 // Don't deliver responses after the connection is closed. |
| 175 if (_closed) { |
| 176 return; |
| 177 } |
| 165 responsesReceived.add(response); | 178 responsesReceived.add(response); |
| 166 // Wrap send response in future to simulate websocket | 179 // Wrap send response in future to simulate websocket |
| 167 new Future(() => responseController.add(response)); | 180 new Future(() => responseController.add(response)); |
| 168 } | 181 } |
| 169 | 182 |
| 170 void expectMsgCount({responseCount: 0, notificationCount: 0}) { | 183 void expectMsgCount({responseCount: 0, notificationCount: 0}) { |
| 171 expect(responsesReceived, hasLength(responseCount)); | 184 expect(responsesReceived, hasLength(responseCount)); |
| 172 expect(notificationsReceived, hasLength(notificationCount)); | 185 expect(notificationsReceived, hasLength(notificationCount)); |
| 173 } | 186 } |
| 174 | 187 |
| 175 Future<Response> waitForResponse(Request request) { | 188 Future<Response> waitForResponse(Request request) { |
| 176 String id = request.id; | 189 String id = request.id; |
| 177 pumpEventQueue().then((_) { | 190 pumpEventQueue().then((_) { |
| 178 responseController.addError(new NoResponseException(request)); | 191 responseController.addError(new NoResponseException(request)); |
| 179 }); | 192 }); |
| 180 return responseController.stream.firstWhere((response) { | 193 return responseController.stream.firstWhere((response) { |
| 181 return response.id == id; | 194 return response.id == id; |
| 182 }); | 195 }); |
| 183 } | 196 } |
| 197 |
| 198 @override |
| 199 void close() { |
| 200 _closed = true; |
| 201 } |
| 184 } | 202 } |
| 185 | 203 |
| 186 /** | 204 /** |
| 187 * A mock [AnalysisContext] for testing [AnalysisServer]. | 205 * A mock [AnalysisContext] for testing [AnalysisServer]. |
| 188 */ | 206 */ |
| 189 class MockAnalysisContext extends Mock implements AnalysisContext { | 207 class MockAnalysisContext extends Mock implements AnalysisContext { |
| 190 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 208 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 191 } | 209 } |
| 192 | 210 |
| 193 /** | 211 /** |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 /** | 344 /** |
| 327 * Dependency list that will be returned by the next call to [computePackageMa
p]. | 345 * Dependency list that will be returned by the next call to [computePackageMa
p]. |
| 328 */ | 346 */ |
| 329 Set<String> dependencies = new Set<String>(); | 347 Set<String> dependencies = new Set<String>(); |
| 330 | 348 |
| 331 @override | 349 @override |
| 332 PackageMapInfo computePackageMap(resource.Folder folder) { | 350 PackageMapInfo computePackageMap(resource.Folder folder) { |
| 333 return new PackageMapInfo(packageMap, dependencies); | 351 return new PackageMapInfo(packageMap, dependencies); |
| 334 } | 352 } |
| 335 } | 353 } |
| OLD | NEW |