OLD | NEW |
(Empty) | |
| 1 library streams_test; |
| 2 import 'package:unittest/unittest.dart'; |
| 3 import 'package:unittest/html_config.dart'; |
| 4 import 'dart:async'; |
| 5 import 'dart:html'; |
| 6 |
| 7 class StreamHelper { |
| 8 var _a; |
| 9 StreamHelper() { |
| 10 _a = new TextInputElement(); |
| 11 document.body.append(_a); |
| 12 } |
| 13 |
| 14 Element get element => _a; |
| 15 Stream<Event> get stream => _a.onFocus; |
| 16 |
| 17 // Causes an event on a to be fired. |
| 18 void pulse() { |
| 19 var event = new Event('focus'); |
| 20 _a.dispatchEvent(event); |
| 21 } |
| 22 } |
| 23 |
| 24 main() { |
| 25 useHtmlConfiguration(); |
| 26 |
| 27 test('simple', () { |
| 28 var helper = new StreamHelper(); |
| 29 |
| 30 var callCount = 0; |
| 31 helper.stream.listen((Event e) { |
| 32 ++callCount; |
| 33 }); |
| 34 |
| 35 helper.pulse(); |
| 36 expect(callCount, 1); |
| 37 }); |
| 38 |
| 39 test('broadcast', () { |
| 40 var stream = new DivElement().onClick; |
| 41 expect(stream.asBroadcastStream(), stream); |
| 42 expect(stream.isBroadcast, isTrue); |
| 43 }); |
| 44 |
| 45 // Validates that capturing events fire on parent before child. |
| 46 test('capture', () { |
| 47 var parent = new DivElement(); |
| 48 document.body.append(parent); |
| 49 |
| 50 var helper = new StreamHelper(); |
| 51 parent.append(helper.element); |
| 52 |
| 53 var childCallCount = 0; |
| 54 var parentCallCount = 0; |
| 55 Element.focusEvent.forTarget(parent, useCapture: true).listen((Event e) { |
| 56 ++parentCallCount; |
| 57 expect(childCallCount, 0); |
| 58 }); |
| 59 |
| 60 Element.focusEvent.forTarget(helper.element, useCapture: true).listen( |
| 61 (Event e) { |
| 62 ++childCallCount; |
| 63 expect(parentCallCount, 1); |
| 64 }); |
| 65 |
| 66 helper.pulse(); |
| 67 expect(childCallCount, 1); |
| 68 expect(parentCallCount, 1); |
| 69 }); |
| 70 |
| 71 test('cancel', () { |
| 72 var helper = new StreamHelper(); |
| 73 |
| 74 var callCount = 0; |
| 75 var subscription = helper.stream.listen((_) { |
| 76 ++callCount; |
| 77 }); |
| 78 |
| 79 helper.pulse(); |
| 80 expect(callCount, 1); |
| 81 |
| 82 subscription.cancel(); |
| 83 helper.pulse(); |
| 84 expect(callCount, 1); |
| 85 |
| 86 expect(() { |
| 87 subscription.onData((_) {}); |
| 88 }, throws); |
| 89 |
| 90 // Calling these after a cancel does nothing. |
| 91 subscription.cancel(); |
| 92 subscription.pause(); |
| 93 subscription.resume(); |
| 94 }); |
| 95 |
| 96 test('pause/resume', () { |
| 97 var helper = new StreamHelper(); |
| 98 |
| 99 var callCount = 0; |
| 100 var subscription = helper.stream.listen((_) { |
| 101 ++callCount; |
| 102 }); |
| 103 |
| 104 helper.pulse(); |
| 105 expect(callCount, 1); |
| 106 |
| 107 subscription.pause(); |
| 108 helper.pulse(); |
| 109 expect(callCount, 1); |
| 110 |
| 111 subscription.resume(); |
| 112 helper.pulse(); |
| 113 expect(callCount, 2); |
| 114 |
| 115 var completer = new Completer<int>.sync(); |
| 116 subscription.pause(completer.future); |
| 117 helper.pulse(); |
| 118 expect(callCount, 2); |
| 119 |
| 120 // Paused, should have no impact. |
| 121 subscription.pause(); |
| 122 helper.pulse(); |
| 123 subscription.resume(); |
| 124 helper.pulse(); |
| 125 expect(callCount, 2); |
| 126 |
| 127 completer.complete(0); |
| 128 helper.pulse(); |
| 129 expect(callCount, 3); |
| 130 |
| 131 // Not paused, but resuming once too often is ok. |
| 132 subscription.resume(); |
| 133 }); |
| 134 |
| 135 test('onData', () { |
| 136 var helper = new StreamHelper(); |
| 137 |
| 138 var callCountOne = 0; |
| 139 var subscription = helper.stream.listen((_) { |
| 140 ++callCountOne; |
| 141 }); |
| 142 |
| 143 helper.pulse(); |
| 144 expect(callCountOne, 1); |
| 145 |
| 146 var callCountTwo = 0; |
| 147 subscription.onData((_) { |
| 148 ++callCountTwo; |
| 149 }); |
| 150 |
| 151 helper.pulse(); |
| 152 expect(callCountOne, 1); |
| 153 expect(callCountTwo, 1); |
| 154 }); |
| 155 |
| 156 test('null onData', () { |
| 157 var helper = new StreamHelper(); |
| 158 |
| 159 var subscription = helper.stream.listen(null); |
| 160 helper.pulse(); |
| 161 |
| 162 var callCountOne = 0; |
| 163 subscription.onData((_) { |
| 164 ++callCountOne; |
| 165 }); |
| 166 helper.pulse(); |
| 167 expect(callCountOne, 1); |
| 168 |
| 169 subscription.onData(null); |
| 170 helper.pulse(); |
| 171 expect(callCountOne, 1); |
| 172 }); |
| 173 |
| 174 var stream = new StreamHelper().stream; |
| 175 // Streams have had some type-checking issues, these tests just validate that |
| 176 // those are OK. |
| 177 test('first', () { |
| 178 stream.first.then((_) {}); |
| 179 }); |
| 180 |
| 181 test('asBroadcastStream', () { |
| 182 stream.asBroadcastStream().listen((_) {}); |
| 183 }); |
| 184 |
| 185 test('where', () { |
| 186 stream.where((_) => true).listen((_) {}); |
| 187 }); |
| 188 |
| 189 test('map', () { |
| 190 stream.map((_) => null).listen((_) {}); |
| 191 }); |
| 192 |
| 193 test('reduce', () { |
| 194 stream.reduce((a, b) => null).then((_) {}); |
| 195 }); |
| 196 |
| 197 test('fold', () { |
| 198 stream.fold(null, (a, b) => null).then((_) {}); |
| 199 }); |
| 200 |
| 201 test('contains', () { |
| 202 stream.contains((_) => true).then((_) {}); |
| 203 }); |
| 204 |
| 205 test('every', () { |
| 206 stream.every((_) => true).then((_) {}); |
| 207 }); |
| 208 |
| 209 test('any', () { |
| 210 stream.any((_) => true).then((_) {}); |
| 211 }); |
| 212 |
| 213 test('length', () { |
| 214 stream.length.then((_) {}); |
| 215 }); |
| 216 |
| 217 test('isEmpty', () { |
| 218 stream.isEmpty.then((_) {}); |
| 219 }); |
| 220 |
| 221 test('toList', () { |
| 222 stream.toList().then((_) {}); |
| 223 }); |
| 224 |
| 225 test('toSet', () { |
| 226 stream.toSet().then((_) {}); |
| 227 }); |
| 228 |
| 229 test('take', () { |
| 230 stream.take(1).listen((_) {}); |
| 231 }); |
| 232 |
| 233 test('takeWhile', () { |
| 234 stream.takeWhile((_) => false).listen((_) {}); |
| 235 }); |
| 236 |
| 237 test('skip', () { |
| 238 stream.skip(0).listen((_) {}); |
| 239 }); |
| 240 |
| 241 test('skipWhile', () { |
| 242 stream.skipWhile((_) => false).listen((_) {}); |
| 243 }); |
| 244 |
| 245 test('distinct', () { |
| 246 stream.distinct((a, b) => false).listen((_) {}); |
| 247 }); |
| 248 |
| 249 test('first', () { |
| 250 stream.first.then((_) {}); |
| 251 }); |
| 252 |
| 253 test('last', () { |
| 254 stream.last.then((_) {}); |
| 255 }); |
| 256 |
| 257 test('single', () { |
| 258 stream.single.then((_) {}); |
| 259 }); |
| 260 |
| 261 test('firstWhere', () { |
| 262 stream.firstWhere((_) => true).then((_) {}); |
| 263 }); |
| 264 |
| 265 test('lastWhere', () { |
| 266 stream.lastWhere((_) => true).then((_) {}); |
| 267 }); |
| 268 |
| 269 test('singleWhere', () { |
| 270 stream.singleWhere((_) => true).then((_) {}); |
| 271 }); |
| 272 |
| 273 test('elementAt', () { |
| 274 stream.elementAt(0).then((_) {}); |
| 275 }); |
| 276 } |
OLD | NEW |