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'; |
| 8 import 'dart:convert'; |
7 import 'dart:io'; | 9 import 'dart:io'; |
8 import 'dart:async'; | |
9 | 10 |
10 /** | 11 /** |
11 * A mock [WebSocket] that immediately passes data to the listener. | 12 * A mock [WebSocket] for testing. |
12 */ | 13 */ |
13 class MockSocket<T> implements WebSocket { | 14 class MockSocket<T> implements WebSocket { |
14 var onData; | 15 |
| 16 final JsonEncoder jsonEncoder = const JsonEncoder(null); |
| 17 |
| 18 final JsonDecoder jsonDecoder = const JsonDecoder(null); |
| 19 |
| 20 StreamController controller = new StreamController(); |
| 21 MockSocket twin; |
| 22 Stream stream; |
| 23 |
| 24 factory MockSocket.pair() { |
| 25 MockSocket socket1 = new MockSocket(); |
| 26 MockSocket socket2 = new MockSocket(); |
| 27 socket1.twin = socket2; |
| 28 socket2.twin = socket1; |
| 29 socket1.stream = socket2.controller.stream; |
| 30 socket2.stream = socket1.controller.stream; |
| 31 return socket1; |
| 32 } |
| 33 |
| 34 MockSocket(); |
| 35 |
| 36 void add(T text) => controller.add(text); |
| 37 |
| 38 void allowMultipleListeners() { |
| 39 stream = stream.asBroadcastStream(); |
| 40 } |
| 41 |
| 42 Future close([int code, String reason]) => controller.close() |
| 43 .then((_) => twin.controller.close()); |
| 44 |
15 StreamSubscription<T> listen(void onData(T event), | 45 StreamSubscription<T> listen(void onData(T event), |
16 {Function onError, void onDone(), bool cancelOnError}) { | 46 { Function onError, void onDone(), bool cancelOnError}) => |
17 this.onData = onData; | 47 stream.listen(onData, onError: onError, onDone: onDone, |
18 return null; | 48 cancelOnError: cancelOnError); |
19 } | |
20 void add(T event) => onData(event); | |
21 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | |
22 } | |
23 | 49 |
24 /** | 50 Stream<T> where(bool test(T)) => stream.where(test); |
25 * A mock [WebSocket] for sending invalid JSON data and counting responses. | 51 |
26 */ | |
27 class InvalidJsonMockSocket<T> implements WebSocket { | |
28 int responseCount = 0; | |
29 var onData; | |
30 StreamSubscription<T> listen(void onData(T event), | |
31 {Function onError, void onDone(), bool cancelOnError}) { | |
32 this.onData = onData; | |
33 return null; | |
34 } | |
35 void addInvalid(T event) => onData(event); | |
36 void add(T event) { responseCount++; } | |
37 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); | 52 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
38 } | 53 } |
OLD | NEW |