PageRenderTime 34ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/src/br/com/stimuli/loading/tests/AudioContentTest.as

http://github.com/arthur-debert/BulkLoader
ActionScript | 135 lines | 102 code | 23 blank | 10 comment | 8 complexity | 509a631f16bfc6379dfbf6cea547f97e MD5 | raw file
  1. package br.com.stimuli.loading.tests {
  2. import br.com.stimuli.kisstest.TestCase
  3. import br.com.stimuli.loading.BulkLoader;
  4. import br.com.stimuli.loading.loadingtypes.*;
  5. import flash.events.*;
  6. import flash.media.Sound;
  7. /**@private*/
  8. public class AudioContentTest extends TestCase { public var _bulkLoader : BulkLoader;
  9. public var lastProgress : Number = 0;
  10. public var sound : Sound;
  11. public var sound1 : Sound;
  12. public var ioError : Event;
  13. public function AudioContentTest(name : String) : void {
  14. super(name);
  15. this.name = name;
  16. }
  17. // Override the run method and begin the request for remote data
  18. public override function setUp():void {
  19. _bulkLoader = new BulkLoader(BulkLoader.getUniqueName());
  20. var goodSoundURL : String = "http://www.emptywhite.com/bulkloader-assets/sound-short.mp3";
  21. var badSoundURL : String = "http://www.emptywhite.com/bulkloader-assets/badchopin.mp3"
  22. var theURL : String = goodSoundURL;
  23. if (this.name.indexOf('testIOError') > -1){
  24. theURL = badSoundURL;
  25. }
  26. var item : LoadingItem = _bulkLoader.add(theURL, {id:"the-sound"});
  27. item.addEventListener(BulkLoader.OPEN, onAudioStartLoading);
  28. if (this.name != "testIOErrorOnBulkLoader"){
  29. item.addEventListener(BulkLoader.ERROR, onIOError);
  30. }else{
  31. _bulkLoader.addEventListener(BulkLoader.ERROR, onIOError);
  32. }
  33. //_bulkLoader.add("http://www.emptywhite.com/bulkloader-assets/movie.flv", {id:"the-movie", pausedAtStart:true});
  34. _bulkLoader.start();
  35. _bulkLoader.addEventListener(BulkLoader.COMPLETE, completeHandler);
  36. _bulkLoader.addEventListener(BulkLoader.PROGRESS, progressHandler);
  37. }
  38. public function onIOError(evt : Event) : void{
  39. ioError = evt;
  40. // call the on complete manually
  41. completeHandler(evt);
  42. }
  43. public function completeHandler(event:Event):void {
  44. _bulkLoader.removeEventListener(BulkLoader.COMPLETE, completeHandler);
  45. _bulkLoader.removeEventListener(BulkLoader.PROGRESS, progressHandler);
  46. dispatchEvent(new Event(Event.INIT));
  47. }
  48. /** This also works as an assertion that event progress will never be NaN
  49. */
  50. public function progressHandler(event:ProgressEvent):void {
  51. //var evt : * = event as Object;
  52. var current : Number = Math.floor((event as Object).percentLoaded * 100) /100;
  53. if (current > lastProgress){
  54. lastProgress = current;
  55. if (BulkLoaderTestSuite.LOADING_VERBOSE) trace(current * 100 , "% loaded") ;
  56. }
  57. for each(var propName : String in ["percentLoaded", "weightPercent", "ratioLoaded"] ){
  58. if (isNaN(event[propName]) ){
  59. trace(propName, "is not a number" );
  60. assertFalse(isNaN(event[propName]));
  61. }
  62. }
  63. //trace("event", (event as Object).percentLoaded, current);
  64. }
  65. override public function tearDown():void {
  66. // destroy the class under test instance
  67. _bulkLoader.clear();
  68. BulkLoader.removeAllLoaders();
  69. _bulkLoader = null;
  70. }
  71. public function onAudioStartLoading(evt : Event) : void {
  72. sound = evt.target.content;
  73. sound1 = _bulkLoader.getSound("the-sound");
  74. }
  75. public function testOpenEventWorksBothWays() : void{
  76. assertNotNull(sound);
  77. assertTrue(sound is Sound);
  78. assertEquals(sound, sound1)
  79. }
  80. public function testSoundContent():void {
  81. var soundItem : * = _bulkLoader.getSound("the-sound");
  82. assertNotNull(soundItem);
  83. }
  84. public function testGetHTTPStatusFromLoader() :void{
  85. assertEquals(_bulkLoader.getHttpStatus("the-sound"), -1);
  86. }
  87. public function testClearMemoryRemovesItem(): void{
  88. var soundItem : Sound = _bulkLoader.getSound("the-sound", true);
  89. assertNotNull(soundItem);
  90. // now try again
  91. soundItem = _bulkLoader.getSound("the-sound");
  92. assertNull(soundItem);
  93. }
  94. public function testIOError() : void{
  95. assertNotNull(ioError);
  96. }
  97. public function testIOErrorOnBulkLoader() : void{
  98. assertNotNull(ioError);
  99. assertNotNull( _bulkLoader.get("the-sound").errorEvent);
  100. assertTrue( _bulkLoader.get("the-sound").errorEvent is ErrorEvent);
  101. }
  102. public function testItemIsLoaded() : void{
  103. assertTrue(_bulkLoader.get("the-sound")._isLoaded);
  104. }
  105. public function testSaneBytesReport() : void{
  106. var item : LoadingItem = _bulkLoader.get("the-sound");
  107. assertFalse(item.bytesTotal == 0);
  108. assertEquals(item.bytesTotal ,item.bytesLoaded);
  109. assertEquals(item.bytesRemaining, 0);
  110. }
  111. }
  112. }