| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 stream_state_helper; | 5 library stream_state_helper; |
| 6 | 6 |
| 7 import "../../../pkg/unittest/lib/unittest.dart"; | 7 import "../../../pkg/unittest/lib/unittest.dart"; |
| 8 import "dart:async"; | 8 import "dart:async"; |
| 9 import "dart:collection"; | 9 import "dart:collection"; |
| 10 | 10 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 _streamTest._expectError(this, error, action); | 39 _streamTest._expectError(this, error, action); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void expectDone([void action()]) { | 42 void expectDone([void action()]) { |
| 43 _streamTest._expectDone(this, action); | 43 _streamTest._expectDone(this, action); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 class StreamProtocolTest { | 47 class StreamProtocolTest { |
| 48 bool trace = false; | 48 bool trace = false; |
| 49 // If not a broadcast stream, the onComplete is called automatically by | 49 final bool isBroadcast; |
| 50 // the first onCancel. | 50 final bool isAsBroadcast; |
| 51 bool isBroadcast; | |
| 52 StreamController _controller; | 51 StreamController _controller; |
| 53 Stream _controllerStream; | 52 Stream _controllerStream; |
| 54 // Most recent subscription created. Used as default for pause/resume. | 53 // Most recent subscription created. Used as default for pause/resume. |
| 55 SubscriptionProtocolTest _latestSubscription; | 54 SubscriptionProtocolTest _latestSubscription; |
| 56 List<Event> _expectations = new List<Event>(); | 55 List<Event> _expectations = new List<Event>(); |
| 57 int _nextExpectationIndex = 0; | 56 int _nextExpectationIndex = 0; |
| 58 int _subscriptionIdCounter = 0; | 57 int _subscriptionIdCounter = 0; |
| 59 Function _onComplete; | 58 Function _onComplete; |
| 60 | 59 |
| 61 StreamProtocolTest.broadcast({ bool sync: false }) | 60 StreamProtocolTest.broadcast({ bool sync: false }) |
| 62 : isBroadcast = true { | 61 : isBroadcast = true, isAsBroadcast = false { |
| 63 _controller = new StreamController.broadcast( | 62 _controller = new StreamController.broadcast( |
| 64 sync: sync, | 63 sync: sync, |
| 65 onListen: _onListen, | 64 onListen: _onListen, |
| 66 onCancel: _onCancel); | 65 onCancel: _onCancel); |
| 67 _controllerStream = _controller.stream; | 66 _controllerStream = _controller.stream; |
| 68 _onComplete = expectAsync0((){ | 67 _onComplete = expectAsync0((){ |
| 69 _onComplete = null; // Being null marks the test as being complete. | 68 _onComplete = null; // Being null marks the test as being complete. |
| 70 }); | 69 }); |
| 71 } | 70 } |
| 72 | 71 |
| 73 StreamProtocolTest({ bool broadcast: false, bool sync: false }) | 72 StreamProtocolTest({ bool sync: false }) |
| 74 : isBroadcast = false { | 73 : isBroadcast = false, isAsBroadcast = false { |
| 75 _controller = new StreamController( | 74 _controller = new StreamController( |
| 76 sync: sync, | 75 sync: sync, |
| 77 onListen: _onListen, | 76 onListen: _onListen, |
| 78 onPause: _onPause, | 77 onPause: _onPause, |
| 79 onResume: _onResume, | 78 onResume: _onResume, |
| 80 onCancel: _onCancel); | 79 onCancel: _onCancel); |
| 81 if (broadcast) { | 80 _controllerStream = _controller.stream; |
| 82 _controllerStream = _controller.stream.asBroadcastStream(); | |
| 83 } else { | |
| 84 _controllerStream = _controller.stream; | |
| 85 } | |
| 86 _onComplete = expectAsync0((){ | 81 _onComplete = expectAsync0((){ |
| 87 _onComplete = null; // Being null marks the test as being complete. | 82 _onComplete = null; // Being null marks the test as being complete. |
| 88 }); | 83 }); |
| 84 } |
| 85 |
| 86 StreamProtocolTest.asBroadcast({ bool sync: false }) |
| 87 : isBroadcast = false, isAsBroadcast = true { |
| 88 _controller = new StreamController( |
| 89 sync: sync, |
| 90 onListen: _onListen, |
| 91 onPause: _onPause, |
| 92 onResume: _onResume, |
| 93 onCancel: _onCancel); |
| 94 _controllerStream = _controller.stream.asBroadcastStream( |
| 95 onListen: _onBroadcastListen, |
| 96 onCancel: _onBroadcastCancel); |
| 97 _onComplete = expectAsync0((){ |
| 98 _onComplete = null; // Being null marks the test as being complete. |
| 99 }); |
| 89 } | 100 } |
| 90 | 101 |
| 91 // Actions on the stream and controller. | 102 // Actions on the stream and controller. |
| 92 void add(var data) { _controller.add(data); } | 103 void add(var data) { _controller.add(data); } |
| 93 void error(var error) { _controller.addError(error); } | 104 void error(var error) { _controller.addError(error); } |
| 94 void close() { _controller.close(); } | 105 void close() { _controller.close(); } |
| 95 | 106 |
| 96 SubscriptionProtocolTest listen({bool cancelOnError : false}) { | 107 SubscriptionProtocolTest listen({bool cancelOnError : false}) { |
| 97 int subscriptionId = _subscriptionIdCounter++; | 108 int subscriptionId = _subscriptionIdCounter++; |
| 98 | 109 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 } | 209 } |
| 199 | 210 |
| 200 void _onCancel() { | 211 void _onCancel() { |
| 201 if (trace) print("[Cancelled]"); | 212 if (trace) print("[Cancelled]"); |
| 202 _withNextExpectation((Event expect) { | 213 _withNextExpectation((Event expect) { |
| 203 if (!expect.matchCancel()) { | 214 if (!expect.matchCancel()) { |
| 204 _fail("Expected: $expect\n" | 215 _fail("Expected: $expect\n" |
| 205 "Found: [Cancelled]\n${expect._stackTrace}"); | 216 "Found: [Cancelled]\n${expect._stackTrace}"); |
| 206 } | 217 } |
| 207 }); | 218 }); |
| 208 if (!isBroadcast) terminate(); | 219 } |
| 220 |
| 221 void _onBroadcastListen(StreamSubscription sub) { |
| 222 if (trace) print("[BroadcastListen]"); |
| 223 _withNextExpectation((Event expect) { |
| 224 if (!expect.matchBroadcastListen(sub)) { |
| 225 _fail("Expected: $expect\n" |
| 226 "Found: [BroadcastListen]\n${expect._stackTrace}"); |
| 227 } |
| 228 }); |
| 229 } |
| 230 |
| 231 void _onBroadcastCancel(StreamSubscription sub) { |
| 232 if (trace) print("[BroadcastCancel]"); |
| 233 _withNextExpectation((Event expect) { |
| 234 if (!expect.matchBroadcastCancel(sub)) { |
| 235 _fail("Expected: $expect\n" |
| 236 "Found: [BroadcastCancel]\n${expect._stackTrace}"); |
| 237 } |
| 238 }); |
| 209 } | 239 } |
| 210 | 240 |
| 211 void _withNextExpectation(void action(Event expect)) { | 241 void _withNextExpectation(void action(Event expect)) { |
| 212 if (_nextExpectationIndex == _expectations.length) { | 242 if (_nextExpectationIndex == _expectations.length) { |
| 213 _nextExpectationIndex++; | 243 _nextExpectationIndex++; |
| 214 action(new MismatchEvent()); | 244 action(new MismatchEvent()); |
| 215 } else { | 245 } else { |
| 216 Event next = _expectations[_nextExpectationIndex++]; | 246 Event next = _expectations[_nextExpectationIndex++]; |
| 217 action(next); | 247 action(next); |
| 218 } | 248 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 } | 312 } |
| 283 | 313 |
| 284 void expectCancel([void action()]) { | 314 void expectCancel([void action()]) { |
| 285 if (_onComplete == null) { | 315 if (_onComplete == null) { |
| 286 _fail("Adding expectation after completing"); | 316 _fail("Adding expectation after completing"); |
| 287 } | 317 } |
| 288 _expectations.add( | 318 _expectations.add( |
| 289 new CancelCallbackEvent(action)); | 319 new CancelCallbackEvent(action)); |
| 290 } | 320 } |
| 291 | 321 |
| 322 void expectBroadcastListen([void action(StreamSubscription sub)]) { |
| 323 if (_onComplete == null) { |
| 324 _fail("Adding expectation after completing"); |
| 325 } |
| 326 if (!isAsBroadcast) throw new StateError("Not an asBroadcast stream"); |
| 327 _expectations.add(new BroadcastListenCallbackEvent(action)); |
| 328 } |
| 329 |
| 330 void expectBroadcastCancel([void action(StreamSubscription sub)]) { |
| 331 if (_onComplete == null) { |
| 332 _fail("Adding expectation after completing"); |
| 333 } |
| 334 if (!isAsBroadcast) throw new StateError("Not an asBroadcast stream"); |
| 335 _expectations.add(new BroadcastCancelCallbackEvent(action)); |
| 336 } |
| 337 |
| 338 void expectBroadcastListenOpt([void action(StreamSubscription sub)]) { |
| 339 if (_onComplete == null) { |
| 340 _fail("Adding expectation after completing"); |
| 341 } |
| 342 if (!isAsBroadcast) return; |
| 343 _expectations.add(new BroadcastListenCallbackEvent(action)); |
| 344 } |
| 345 |
| 346 void expectBroadcastCancelOpt([void action(StreamSubscription sub)]) { |
| 347 if (_onComplete == null) { |
| 348 _fail("Adding expectation after completing"); |
| 349 } |
| 350 if (!isAsBroadcast) return; |
| 351 _expectations.add(new BroadcastCancelCallbackEvent(action)); |
| 352 } |
| 353 |
| 292 void _fail(String message) { | 354 void _fail(String message) { |
| 293 if (_nextExpectationIndex == 0) { | 355 if (_nextExpectationIndex == 0) { |
| 294 throw "Unexpected event:\n$message\nNo earlier events matched."; | 356 throw "Unexpected event:\n$message\nNo earlier events matched."; |
| 295 } | 357 } |
| 296 StringBuffer buf = new StringBuffer(); | 358 StringBuffer buf = new StringBuffer(); |
| 297 for (int i = 0; i < _expectations.length; i++) { | 359 for (int i = 0; i < _expectations.length; i++) { |
| 298 if (i == _nextExpectationIndex - 1) { | 360 if (i == _nextExpectationIndex - 1) { |
| 299 buf.write("->"); | 361 buf.write("->"); |
| 300 } else { | 362 } else { |
| 301 buf.write(" "); | 363 buf.write(" "); |
| 302 } | 364 } |
| 303 buf.write(_expectations[i]); | 365 buf.write(_expectations[i]); |
| 304 buf.write("\n"); | 366 buf.write("\n"); |
| 305 } | 367 } |
| 306 throw "Unexpected event:\n$message\nAll expectations:\n$buf"; | 368 throw "Unexpected event:\n$message\nAll expectations:\n$buf"; |
| 307 } | 369 } |
| 308 } | 370 } |
| 309 | 371 |
| 310 class Event { | 372 class Event { |
| 311 Function _action; | 373 Function _action; |
| 312 StackTrace _stackTrace; | 374 StackTrace _stackTrace; |
| 313 Event(void action()) | 375 Event(void action()) |
| 314 : _action = (action == null) ? null : expectAsync0(action) { | 376 : _action = (action == null) ? null : expectAsync0(action) { |
| 315 try { throw 0; } catch (_, s) { _stackTrace = s; } | 377 try { throw 0; } catch (_, s) { _stackTrace = s; } |
| 316 } | 378 } |
| 379 Event.broadcast(void action(StreamSubscription sub)) |
| 380 : _action = (action == null) ? null : expectAsync1(action) { |
| 381 try { throw 0; } catch (_, s) { _stackTrace = s; } |
| 382 } |
| 317 | 383 |
| 318 bool matchData(int id, var data) { | 384 bool matchData(int id, var data) { |
| 319 return false; | 385 return false; |
| 320 } | 386 } |
| 321 | 387 |
| 322 bool matchError(int id, e) { | 388 bool matchError(int id, e) { |
| 323 return false; | 389 return false; |
| 324 } | 390 } |
| 325 | 391 |
| 326 bool matchDone(int id) { | 392 bool matchDone(int id) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 344 if (_action != null) _action(); | 410 if (_action != null) _action(); |
| 345 return true; | 411 return true; |
| 346 } | 412 } |
| 347 | 413 |
| 348 bool matchCancel() { | 414 bool matchCancel() { |
| 349 if (!_testCancel()) return false; | 415 if (!_testCancel()) return false; |
| 350 if (_action != null) _action(); | 416 if (_action != null) _action(); |
| 351 return true; | 417 return true; |
| 352 } | 418 } |
| 353 | 419 |
| 420 bool matchBroadcastListen(StreamSubscription sub) { |
| 421 if (!_testBroadcastListen()) return false; |
| 422 if (_action != null) _action(sub); |
| 423 return true; |
| 424 } |
| 425 |
| 426 bool matchBroadcastCancel(StreamSubscription sub) { |
| 427 if (!_testBroadcastCancel()) return false; |
| 428 if (_action != null) _action(sub); |
| 429 return true; |
| 430 } |
| 431 |
| 354 bool _testData(_) => false; | 432 bool _testData(_) => false; |
| 355 bool _testError(_) => false; | 433 bool _testError(_) => false; |
| 356 bool _testDone() => false; | 434 bool _testDone() => false; |
| 357 bool _testPause() => false; | 435 bool _testPause() => false; |
| 358 bool _testResume() => false; | 436 bool _testResume() => false; |
| 359 bool _testSubscribe() => false; | 437 bool _testSubscribe() => false; |
| 360 bool _testCancel() => false; | 438 bool _testCancel() => false; |
| 439 bool _testBroadcastListen() => false; |
| 440 bool _testBroadcastCancel() => false; |
| 361 } | 441 } |
| 362 | 442 |
| 363 class SubscriptionEvent extends Event { | 443 class SubscriptionEvent extends Event { |
| 364 SubscriptionProtocolTest subscription; | 444 SubscriptionProtocolTest subscription; |
| 365 SubscriptionEvent(this.subscription, void action()) : super(action); | 445 SubscriptionEvent(this.subscription, void action()) : super(action); |
| 366 | 446 |
| 367 bool matchData(int id, var data) { | 447 bool matchData(int id, var data) { |
| 368 if (subscription != null && subscription.id != id) return false; | 448 if (subscription != null && subscription.id != id) return false; |
| 369 if (!_testData(data)) return false; | 449 if (!_testData(data)) return false; |
| 370 if (_action != null) _action(); | 450 if (_action != null) _action(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 bool _testSubscribe() => true; | 512 bool _testSubscribe() => true; |
| 433 String toString() => "[Subscribed]"; | 513 String toString() => "[Subscribed]"; |
| 434 } | 514 } |
| 435 | 515 |
| 436 class CancelCallbackEvent extends Event { | 516 class CancelCallbackEvent extends Event { |
| 437 CancelCallbackEvent(void action()) : super(action); | 517 CancelCallbackEvent(void action()) : super(action); |
| 438 bool _testCancel() => true; | 518 bool _testCancel() => true; |
| 439 String toString() => "[Cancelled]"; | 519 String toString() => "[Cancelled]"; |
| 440 } | 520 } |
| 441 | 521 |
| 522 class _BroadcastEventCallback extends Event { |
| 523 Function _action; |
| 524 _BroadcastEventCallback(); |
| 525 } |
| 526 |
| 527 class BroadcastCancelCallbackEvent extends Event { |
| 528 BroadcastCancelCallbackEvent(void action(StreamSubscription sub)) |
| 529 : super.broadcast(action); |
| 530 bool _testBroadcastCancel() => true; |
| 531 String toString() => "[BroadcastCancel]"; |
| 532 } |
| 533 |
| 534 class BroadcastListenCallbackEvent extends Event { |
| 535 BroadcastListenCallbackEvent(void action(StreamSubscription sub)) |
| 536 : super.broadcast(action); |
| 537 bool _testBroadcastListen() => true; |
| 538 String toString() => "[BroadcastListen]"; |
| 539 } |
| 540 |
| 442 /** Event matcher that matches any other event. */ | 541 /** Event matcher that matches any other event. */ |
| 443 class LogAnyEvent extends Event { | 542 class LogAnyEvent extends Event { |
| 444 String _actual = "*Not matched yet*"; | 543 String _actual = "*Not matched yet*"; |
| 445 | 544 |
| 446 LogAnyEvent(void action()) : super(action); | 545 LogAnyEvent(void action()) : super(action); |
| 447 | 546 |
| 448 bool _testData(var data) { | 547 bool _testData(var data) { |
| 449 _actual = "*[Data $data]"; | 548 _actual = "*[Data $data]"; |
| 450 return true; | 549 return true; |
| 451 } | 550 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 473 bool _testSubcribe() { | 572 bool _testSubcribe() { |
| 474 _actual = "*[Subscribed]"; | 573 _actual = "*[Subscribed]"; |
| 475 return true; | 574 return true; |
| 476 } | 575 } |
| 477 | 576 |
| 478 bool _testCancel() { | 577 bool _testCancel() { |
| 479 _actual = "*[Cancelled]"; | 578 _actual = "*[Cancelled]"; |
| 480 return true; | 579 return true; |
| 481 } | 580 } |
| 482 | 581 |
| 582 bool _testBroadcastListen() { |
| 583 _actual = "*[BroadcastListen]"; |
| 584 return true; |
| 585 } |
| 586 |
| 587 bool _testBroadcastCancel() { |
| 588 _actual = "*[BroadcastCancel]"; |
| 589 return true; |
| 590 } |
| 591 |
| 483 /** Returns a representation of the event it was tested against. */ | 592 /** Returns a representation of the event it was tested against. */ |
| 484 String toString() => _actual; | 593 String toString() => _actual; |
| 485 } | 594 } |
| OLD | NEW |