OLD | NEW |
1 library WebSocketTest; | 1 library WebSocketTest; |
| 2 |
2 import 'package:unittest/unittest.dart'; | 3 import 'package:unittest/unittest.dart'; |
3 import 'package:unittest/html_individual_config.dart'; | 4 import 'package:unittest/html_individual_config.dart'; |
4 import 'dart:html'; | 5 import 'dart:html'; |
5 | 6 |
6 main() { | 7 main() { |
7 | |
8 useHtmlIndividualConfiguration(); | 8 useHtmlIndividualConfiguration(); |
9 | 9 |
10 group('supported', () { | 10 group('supported', () { |
11 test('supported', () { | 11 test('supported', () { |
12 expect(WebSocket.supported, true); | 12 expect(WebSocket.supported, true); |
13 }); | 13 }); |
14 }); | 14 }); |
15 | 15 |
16 group('websocket', () { | 16 group('websocket', () { |
17 var isWebSocket = predicate((x) => x is WebSocket, 'is a WebSocket'); | 17 var isWebSocket = predicate((x) => x is WebSocket, 'is a WebSocket'); |
18 var expectation = WebSocket.supported ? returnsNormally : throws; | 18 var expectation = WebSocket.supported ? returnsNormally : throws; |
19 | 19 |
20 test('constructorTest', () { | 20 test('constructorTest', () { |
21 expect(() { | 21 expect(() { |
22 var socket = new WebSocket('ws://localhost/ws', 'chat'); | 22 var socket = new WebSocket('ws://localhost/ws', 'chat'); |
23 expect(socket, isNotNull); | 23 expect(socket, isNotNull); |
24 expect(socket, isWebSocket); | 24 expect(socket, isWebSocket); |
25 }, expectation); | 25 }, expectation); |
26 }); | 26 }); |
27 | 27 |
28 if (WebSocket.supported) { | 28 if (WebSocket.supported) { |
29 test('echo', () { | 29 test('echo', () { |
30 var socket = new WebSocket('ws://${window.location.host}/ws'); | 30 var socket = new WebSocket('ws://${window.location.host}/ws'); |
31 | 31 |
32 socket.onOpen.first.then((_) { | 32 socket.onOpen.first.then((_) { |
33 socket.send('hello!'); | 33 socket.send('hello!'); |
34 }); | 34 }); |
35 | 35 |
36 return socket.onMessage.first.then((MessageEvent e) { | 36 return socket.onMessage.first.then((MessageEvent e) { |
37 expect(e.data, 'hello!'); | 37 expect(e.data, 'hello!'); |
38 socket.close(); | 38 socket.close(); |
39 }); | 39 }); |
40 }); | 40 }); |
41 | 41 |
42 test('error handling', () { | 42 test('error handling', () { |
43 var socket = new WebSocket('ws://${window.location.host}/ws'); | 43 var socket = new WebSocket('ws://${window.location.host}/ws'); |
44 socket.onOpen.first.then((_) => socket.send('close-with-error')); | 44 socket.onOpen.first.then((_) => socket.send('close-with-error')); |
45 return socket.onError.first.then((e) { | 45 return socket.onError.first.then((e) { |
46 print('$e was caught, yay!'); | 46 print('$e was caught, yay!'); |
47 socket.close(); | 47 socket.close(); |
48 }); | 48 }); |
49 }); | 49 }); |
50 } | 50 } |
51 }); | 51 }); |
52 } | 52 } |
OLD | NEW |