OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 XHRTest; | 5 library XHRTest; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_individual_config.dart'; | 7 import '../../pkg/unittest/lib/html_individual_config.dart'; |
| 8 import 'dart:async'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import 'dart:json' as json; | 10 import 'dart:json' as json; |
10 | 11 |
11 void fail(message) { | 12 void fail(message) { |
12 guardAsync(() { | 13 guardAsync(() { |
13 expect(false, isTrue, reason: message); | 14 expect(false, isTrue, reason: message); |
14 }); | 15 }); |
15 } | 16 } |
16 | 17 |
17 main() { | 18 main() { |
18 useHtmlIndividualConfiguration(); | 19 useHtmlIndividualConfiguration(); |
19 var url = "/tests/html/xhr_cross_origin_data.txt"; | 20 var url = "/tests/html/xhr_cross_origin_data.txt"; |
20 | 21 |
21 void validate200Response(xhr) { | 22 void validate200Response(xhr) { |
22 expect(xhr.status, equals(200)); | 23 expect(xhr.status, equals(200)); |
23 var data = json.parse(xhr.response); | 24 var data = json.parse(xhr.responseText); |
24 expect(data, contains('feed')); | 25 expect(data, contains('feed')); |
25 expect(data['feed'], contains('entry')); | 26 expect(data['feed'], contains('entry')); |
26 expect(data, isMap); | 27 expect(data, isMap); |
27 } | 28 } |
28 | 29 |
29 void validate404(xhr) { | 30 void validate404(xhr) { |
30 expect(xhr.status, equals(404)); | 31 expect(xhr.status, equals(404)); |
31 expect(xhr.responseText, equals('')); | 32 expect(xhr.responseText, equals('')); |
32 } | 33 } |
33 | 34 |
34 group('supported_HttpRequestProgressEvent', () { | 35 group('supported_HttpRequestProgressEvent', () { |
35 test('supported', () { | 36 test('supported', () { |
36 expect(HttpRequestProgressEvent.supported, isTrue); | 37 expect(HttpRequestProgressEvent.supported, isTrue); |
37 }); | 38 }); |
38 }); | 39 }); |
39 | 40 |
| 41 group('supported_onProgress', () { |
| 42 test('supported', () { |
| 43 expect(HttpRequest.supportsProgressEvent, isTrue); |
| 44 }); |
| 45 }); |
| 46 |
| 47 group('supported_onLoadEnd', () { |
| 48 test('supported', () { |
| 49 expect(HttpRequest.supportsLoadEndEvent, isTrue); |
| 50 }); |
| 51 }); |
| 52 |
40 group('xhr', () { | 53 group('xhr', () { |
41 test('XHR No file', () { | 54 test('XHR No file', () { |
42 HttpRequest xhr = new HttpRequest(); | 55 HttpRequest xhr = new HttpRequest(); |
43 xhr.open("GET", "NonExistingFile", true); | 56 xhr.open("GET", "NonExistingFile", true); |
44 xhr.onReadyStateChange.listen(expectAsyncUntil1((event) { | 57 xhr.onReadyStateChange.listen(expectAsyncUntil1((event) { |
45 if (xhr.readyState == HttpRequest.DONE) { | 58 if (xhr.readyState == HttpRequest.DONE) { |
46 validate404(xhr); | 59 validate404(xhr); |
47 } | 60 } |
48 }, () => xhr.readyState == HttpRequest.DONE)); | 61 }, () => xhr.readyState == HttpRequest.DONE)); |
49 xhr.send(); | 62 xhr.send(); |
50 }); | 63 }); |
51 | 64 |
52 test('XHR file', () { | 65 test('XHR file', () { |
| 66 var loadEndCalled = false; |
| 67 |
53 var xhr = new HttpRequest(); | 68 var xhr = new HttpRequest(); |
54 xhr.open('GET', url, true); | 69 xhr.open('GET', url, true); |
55 xhr.onReadyStateChange.listen(expectAsyncUntil1((e) { | 70 xhr.onReadyStateChange.listen(expectAsyncUntil1((e) { |
56 if (xhr.readyState == HttpRequest.DONE) { | 71 if (xhr.readyState == HttpRequest.DONE) { |
57 validate200Response(xhr); | 72 validate200Response(xhr); |
| 73 |
| 74 Timer.run(expectAsync0(() { |
| 75 expect(loadEndCalled, HttpRequest.supportsLoadEndEvent); |
| 76 })); |
58 } | 77 } |
59 }, () => xhr.readyState == HttpRequest.DONE)); | 78 }, () => xhr.readyState == HttpRequest.DONE)); |
60 | 79 |
61 xhr.onLoadEnd.listen(expectAsync1((ProgressEvent e) { | 80 xhr.onLoadEnd.listen((ProgressEvent e) { |
62 expect(e.currentTarget, xhr); | 81 loadEndCalled = true; |
63 expect(e.target, xhr); | 82 }); |
64 })); | |
65 xhr.send(); | 83 xhr.send(); |
66 }); | 84 }); |
67 | 85 |
68 test('XHR.request No file', () { | 86 test('XHR.request No file', () { |
69 HttpRequest.request('NonExistingFile').then( | 87 HttpRequest.request('NonExistingFile').then( |
70 (_) { fail('Request should not have succeeded.'); }, | 88 (_) { fail('Request should not have succeeded.'); }, |
71 onError: expectAsync1((e) { | 89 onError: expectAsync1((e) { |
72 var xhr = e.error.target; | 90 var xhr = e.error.target; |
73 expect(xhr.readyState, equals(HttpRequest.DONE)); | 91 expect(xhr.readyState, equals(HttpRequest.DONE)); |
74 validate404(xhr); | 92 validate404(xhr); |
75 })); | 93 })); |
76 }); | 94 }); |
77 | 95 |
78 test('XHR.request file', () { | 96 test('XHR.request file', () { |
79 HttpRequest.request(url).then(expectAsync1((xhr) { | 97 HttpRequest.request(url).then(expectAsync1((xhr) { |
80 expect(xhr.readyState, equals(HttpRequest.DONE)); | 98 expect(xhr.readyState, equals(HttpRequest.DONE)); |
81 validate200Response(xhr); | 99 validate200Response(xhr); |
82 })); | 100 })); |
83 }); | 101 }); |
84 | 102 |
85 test('XHR.request onProgress', () { | 103 test('XHR.request onProgress', () { |
86 var progressCalled = false; | 104 var progressCalled = false; |
87 HttpRequest.request(url, | 105 HttpRequest.request(url, |
88 onProgress: (_) { | 106 onProgress: (_) { |
89 progressCalled = true; | 107 progressCalled = true; |
90 }).then(expectAsync1( | 108 }).then(expectAsync1( |
91 (xhr) { | 109 (xhr) { |
92 expect(xhr.readyState, equals(HttpRequest.DONE)); | 110 expect(xhr.readyState, equals(HttpRequest.DONE)); |
93 expect(progressCalled, isTrue); | 111 expect(progressCalled, HttpRequest.supportsProgressEvent); |
94 validate200Response(xhr); | 112 validate200Response(xhr); |
95 })); | 113 })); |
96 }); | 114 }); |
97 | 115 |
98 test('XHR.request withCredentials No file', () { | 116 test('XHR.request withCredentials No file', () { |
99 HttpRequest.request('NonExistingFile', withCredentials: true).then( | 117 HttpRequest.request('NonExistingFile', withCredentials: true).then( |
100 (_) { fail('Request should not have succeeded.'); }, | 118 (_) { fail('Request should not have succeeded.'); }, |
101 onError: expectAsync1((e) { | 119 onError: expectAsync1((e) { |
102 var xhr = e.error.target; | 120 var xhr = e.error.target; |
103 expect(xhr.readyState, equals(HttpRequest.DONE)); | 121 expect(xhr.readyState, equals(HttpRequest.DONE)); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 test('HttpRequestProgressEvent', () { | 157 test('HttpRequestProgressEvent', () { |
140 var expectation = HttpRequestProgressEvent.supported ? | 158 var expectation = HttpRequestProgressEvent.supported ? |
141 returnsNormally : throws; | 159 returnsNormally : throws; |
142 expect(() { | 160 expect(() { |
143 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); | 161 var event = new Event.eventType('XMLHttpRequestProgressEvent', ''); |
144 expect(event is HttpRequestProgressEvent, isTrue); | 162 expect(event is HttpRequestProgressEvent, isTrue); |
145 }, expectation); | 163 }, expectation); |
146 }); | 164 }); |
147 }); | 165 }); |
148 } | 166 } |
OLD | NEW |