| 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 barback.test.utils; | 5 library barback.test.utils; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert' show Encoding; | 8 import 'dart:convert' show Encoding; |
| 9 | 9 |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 /// Does not update it in the graph. | 151 /// Does not update it in the graph. |
| 152 void modifyAsset(String name, String contents) { | 152 void modifyAsset(String name, String contents) { |
| 153 schedule(() { | 153 schedule(() { |
| 154 _provider._modifyAsset(name, contents); | 154 _provider._modifyAsset(name, contents); |
| 155 }, "modify asset $name"); | 155 }, "modify asset $name"); |
| 156 } | 156 } |
| 157 | 157 |
| 158 /// Schedules an error to be generated when loading the asset identified by | 158 /// Schedules an error to be generated when loading the asset identified by |
| 159 /// [name]. | 159 /// [name]. |
| 160 /// | 160 /// |
| 161 /// Does not update the asset in the graph. | 161 /// Does not update the asset in the graph. If [async] is true, the error is |
| 162 void setAssetError(String name) { | 162 /// thrown asynchronously. |
| 163 void setAssetError(String name, {bool async: true}) { |
| 163 schedule(() { | 164 schedule(() { |
| 164 _provider._setAssetError(name); | 165 _provider._setAssetError(name, async); |
| 165 }, "set error for asset $name"); | 166 }, "set error for asset $name"); |
| 166 } | 167 } |
| 167 | 168 |
| 168 /// Schedules a pause of the internally created [PackageProvider]. | 169 /// Schedules a pause of the internally created [PackageProvider]. |
| 169 /// | 170 /// |
| 170 /// All asset requests that the [PackageGraph] makes to the provider after this | 171 /// All asset requests that the [PackageGraph] makes to the provider after this |
| 171 /// will not complete until [resumeProvider] is called. | 172 /// will not complete until [resumeProvider] is called. |
| 172 void pauseProvider() { | 173 void pauseProvider() { |
| 173 schedule(() => _provider._pause(), "pause provider"); | 174 schedule(() => _provider._pause(), "pause provider"); |
| 174 } | 175 } |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 /// An [AssetProvider] that provides the given set of assets. | 464 /// An [AssetProvider] that provides the given set of assets. |
| 464 class MockProvider implements PackageProvider { | 465 class MockProvider implements PackageProvider { |
| 465 Iterable<String> get packages => _assets.keys; | 466 Iterable<String> get packages => _assets.keys; |
| 466 | 467 |
| 467 final Map<String, AssetSet> _assets; | 468 final Map<String, AssetSet> _assets; |
| 468 | 469 |
| 469 /// The set of assets for which [MockLoadException]s should be emitted if | 470 /// The set of assets for which [MockLoadException]s should be emitted if |
| 470 /// they're loaded. | 471 /// they're loaded. |
| 471 final _errors = new Set<AssetId>(); | 472 final _errors = new Set<AssetId>(); |
| 472 | 473 |
| 474 /// The set of assets for which synchronous [MockLoadException]s should be |
| 475 /// emitted if they're loaded. |
| 476 final _syncErrors = new Set<AssetId>(); |
| 477 |
| 473 /// The completer that [getAsset()] is waiting on to complete when paused. | 478 /// The completer that [getAsset()] is waiting on to complete when paused. |
| 474 /// | 479 /// |
| 475 /// If `null` it will return the asset immediately. | 480 /// If `null` it will return the asset immediately. |
| 476 Completer _pauseCompleter; | 481 Completer _pauseCompleter; |
| 477 | 482 |
| 478 /// Tells the provider to wait during [getAsset] until [complete()] | 483 /// Tells the provider to wait during [getAsset] until [complete()] |
| 479 /// is called. | 484 /// is called. |
| 480 /// | 485 /// |
| 481 /// Lets you test the asynchronous behavior of loading. | 486 /// Lets you test the asynchronous behavior of loading. |
| 482 void _pause() { | 487 void _pause() { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 | 525 |
| 521 // If there are no assets or transformers, add a dummy package. This better | 526 // If there are no assets or transformers, add a dummy package. This better |
| 522 // simulates the real world, where there'll always be at least the | 527 // simulates the real world, where there'll always be at least the |
| 523 // entrypoint package. | 528 // entrypoint package. |
| 524 return assetMap.isEmpty ? {"app": new AssetSet()} : assetMap; | 529 return assetMap.isEmpty ? {"app": new AssetSet()} : assetMap; |
| 525 } | 530 } |
| 526 | 531 |
| 527 void _modifyAsset(String name, String contents) { | 532 void _modifyAsset(String name, String contents) { |
| 528 var id = new AssetId.parse(name); | 533 var id = new AssetId.parse(name); |
| 529 _errors.remove(id); | 534 _errors.remove(id); |
| 535 _syncErrors.remove(id); |
| 530 (_assets[id.package][id] as _MockAsset).contents = contents; | 536 (_assets[id.package][id] as _MockAsset).contents = contents; |
| 531 } | 537 } |
| 532 | 538 |
| 533 void _setAssetError(String name) { | 539 void _setAssetError(String name, bool async) { |
| 534 _errors.add(new AssetId.parse(name)); | 540 (async ? _errors : _syncErrors).add(new AssetId.parse(name)); |
| 535 } | 541 } |
| 536 | 542 |
| 537 List<AssetId> listAssets(String package, {String within}) { | 543 List<AssetId> listAssets(String package, {String within}) { |
| 538 if (within != null) { | 544 if (within != null) { |
| 539 throw new UnimplementedError("Doesn't handle 'within' yet."); | 545 throw new UnimplementedError("Doesn't handle 'within' yet."); |
| 540 } | 546 } |
| 541 | 547 |
| 542 return _assets[package].map((asset) => asset.id); | 548 return _assets[package].map((asset) => asset.id); |
| 543 } | 549 } |
| 544 | 550 |
| 545 Future<Asset> getAsset(AssetId id) { | 551 Future<Asset> getAsset(AssetId id) { |
| 546 // Eagerly load the asset so we can test an asset's value changing between | 552 // Eagerly load the asset so we can test an asset's value changing between |
| 547 // when a load starts and when it finishes. | 553 // when a load starts and when it finishes. |
| 548 var assets = _assets[id.package]; | 554 var assets = _assets[id.package]; |
| 549 var asset; | 555 var asset; |
| 550 if (assets != null) asset = assets[id]; | 556 if (assets != null) asset = assets[id]; |
| 551 | 557 |
| 558 if (_syncErrors.contains(id)) throw new MockLoadException(id); |
| 552 var hasError = _errors.contains(id); | 559 var hasError = _errors.contains(id); |
| 553 | 560 |
| 554 var future; | 561 var future; |
| 555 if (_pauseCompleter != null) { | 562 if (_pauseCompleter != null) { |
| 556 future = _pauseCompleter.future; | 563 future = _pauseCompleter.future; |
| 557 } else { | 564 } else { |
| 558 future = new Future.value(); | 565 future = new Future.value(); |
| 559 } | 566 } |
| 560 | 567 |
| 561 return future.then((_) { | 568 return future.then((_) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 582 | 589 |
| 583 _MockAsset(this.id, this.contents); | 590 _MockAsset(this.id, this.contents); |
| 584 | 591 |
| 585 Future<String> readAsString({Encoding encoding}) => | 592 Future<String> readAsString({Encoding encoding}) => |
| 586 new Future.value(contents); | 593 new Future.value(contents); |
| 587 | 594 |
| 588 Stream<List<int>> read() => throw new UnimplementedError(); | 595 Stream<List<int>> read() => throw new UnimplementedError(); |
| 589 | 596 |
| 590 String toString() => "MockAsset $id $contents"; | 597 String toString() => "MockAsset $id $contents"; |
| 591 } | 598 } |
| OLD | NEW |