Skip to content

Commit

Permalink
Fixed bug where hasItem would return true even for items not yet loaded.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arthur Debert committed Sep 18, 2009
1 parent c7457ea commit 7790888
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 75 deletions.
2 changes: 1 addition & 1 deletion src/br/com/stimuli/loading/BulkLoader.as
Expand Up @@ -427,7 +427,7 @@ import flash.utils.*;
/** @private */
public static function _hasItemInBulkLoader(key : *, atLoader : BulkLoader) : Boolean{
var item : LoadingItem = atLoader.get(key);
if (item) {
if (item && item._isLoaded) {
return true;
}
return false;
Expand Down
Binary file modified tests/bin/TestSuite.swf
Binary file not shown.
149 changes: 75 additions & 74 deletions tests/src/br/com/stimuli/loading/tests/BulkLoaderTestSuite.as
@@ -1,82 +1,83 @@
package br.com.stimuli.loading.tests {
import br.com.stimuli.kisstest.TestSuite;
import flash.events.*;
import br.com.stimuli.loading.tests.*;
import flash.utils.*;
import flash.media.SoundMixer;

/**@private*/
public class BulkLoaderTestSuite extends TestSuite {
public var testClasses : Array = [
HostPriorityTest,
LoaderImageItemTest,
GuessTypeTest,
LoaderItemMovieTest,
LoadNowTest,
CollectionTestCase,
RemoveAllTest,
RemoveAllSimpleTest,
GetClassDefinitionTest,
RegisterNewTypeTest,
TwoItemsWithTheSameURLTest,
OnErrorTest,
ErrorResumeTest,
SmartURLTest,
VideoContentTest,
RemoveFailedItemTest,
OnCompleteTest,
AutoIdTest,
StringSubstituionTest,
LazyJSONLoaderTest,
LazyXMLLoaderTest,
LazyXMLInternalsTest,
LoaderItemAVM1MovieTest,
VideoContentPausedAtStartTestCase,
PauseAllResumeTest,
ClearNowTest,
StartPausedTest,
ProgressEventsTest,
ReloadTest,
import br.com.stimuli.kisstest.TestSuite;
import flash.events.*;
import br.com.stimuli.loading.tests.*;
import flash.utils.*;
import flash.media.SoundMixer;

ClearMemoryTest,
ResumeAllTest,
ResumeTest,
BulkStartTest,
XMLItemTest,
InstanceRetrivalTestCase,
URLItemTest,
AudioContentTest
];

public var testsRun : Object = {} ;
/**@private*/
public class BulkLoaderTestSuite extends TestSuite {
public var testClasses : Array = [
HasItemTest,
HostPriorityTest,
LoaderImageItemTest,
GuessTypeTest,
LoaderItemMovieTest,
LoadNowTest,
CollectionTestCase,
RemoveAllTest,
RemoveAllSimpleTest,
GetClassDefinitionTest,
RegisterNewTypeTest,
TwoItemsWithTheSameURLTest,
OnErrorTest,
ErrorResumeTest,
SmartURLTest,
VideoContentTest,
RemoveFailedItemTest,
OnCompleteTest,
AutoIdTest,
StringSubstituionTest,
LazyJSONLoaderTest,
LazyXMLLoaderTest,
LazyXMLInternalsTest,
LoaderItemAVM1MovieTest,
VideoContentPausedAtStartTestCase,
PauseAllResumeTest,
ClearNowTest,
StartPausedTest,
ProgressEventsTest,
ReloadTest,

ClearMemoryTest,
ResumeAllTest,
ResumeTest,
BulkStartTest,
XMLItemTest,
InstanceRetrivalTestCase,
URLItemTest,
AudioContentTest
];

public var testsRun : Object = {} ;
public static var LOADING_VERBOSE : Boolean = false;


public function BulkLoaderTestSuite() {
super();
SoundMixer.soundTransform.volume = 0;
super();
SoundMixer.soundTransform.volume = 0;
testClasses.forEach(function(cl : Class, ...rest):void{addTestCase(cl)})
for (var prop : String in testsRun){
//trace(prop.substring(6, prop.length -1) + " (" + testsRun[prop].length + ")", ":");
for each (var testName : String in testsRun[prop]){
//trace("\t",testName );
}
}
}

public function addTestsFromClass(klass : Class) : void{
for each (var name : String in describeType(klass).factory.method.@name){
if (name.substr(0, 4) == "test"){
addTest(new klass(name));
if(!testsRun[String(klass)]){
testsRun[String(klass)] = [];
}
testsRun[String(klass)].push(name);
}
}
}
for (var prop : String in testsRun){
//trace(prop.substring(6, prop.length -1) + " (" + testsRun[prop].length + ")", ":");
for each (var testName : String in testsRun[prop]){
//trace("\t",testName );
}
}
}

public function addTestsFromClass(klass : Class) : void{
for each (var name : String in describeType(klass).factory.method.@name){
if (name.substr(0, 4) == "test"){
addTest(new klass(name));
if(!testsRun[String(klass)]){
testsRun[String(klass)] = [];
}
testsRun[String(klass)].push(name);
}
}
}


}

}

}
101 changes: 101 additions & 0 deletions tests/src/br/com/stimuli/loading/tests/HasItemTest.as
@@ -0,0 +1,101 @@
package br.com.stimuli.loading.tests {
import flash.net.URLRequest;
import flash.net.*;
import flash.events.*;
import flash.utils.*;
import flash.display.*;
import br.com.stimuli.kisstest.TestCase;
import br.com.stimuli.loading.BulkLoader;
import br.com.stimuli.loading.loadingtypes.*;
/**@private*/
public class HasItemTest extends TestCase {

public var _bulkLoader : BulkLoader;
public var lastProgress : Number = 0;


public var ioError : Event;

public var dispatchedProgressAfterComplete : Boolean = false;
public var numHasItems : int = 0;
public var timer : Timer;
public function HasItemTest(name : String) : void {
super(name);
this.name = name;
}
public var items :Array = [
"http://www.emptywhite.com/bulkloader-assets/some-text.txt",
"http://www.emptywhite.com/bulkloader-assets/movie.flv" ,
"http://www.emptywhite.com/bulkloader-assets/samplexml.xml",
"http://www.emptywhite.com/bulkloader-assets/shoes.jpg",
"http://www.emptywhite.com/bulkloader-assets/chopin.mp3"
]
// Override the run method and begin the request for remote data
public override function setUp():void {
_bulkLoader = new BulkLoader(BulkLoader.getUniqueName())
for each(var url : String in items){_bulkLoader.add(url, {"preventCache":true});}
_bulkLoader.start();
_bulkLoader.addEventListener(BulkLoader.COMPLETE, completeHandler);
_bulkLoader.addEventListener(BulkLoader.PROGRESS, progressHandler);
for each(url in items){
if (_bulkLoader.hasItem(url)) numHasItems ++;
}
}

public function onIOError(evt : Event) : void{
ioError = evt;
// call the on complete manually
completeHandler(evt);
tearDown();
}

public function completeHandler(event:Event):void {
timer = new Timer(2000, 1);
timer.addEventListener( TimerEvent.TIMER_COMPLETE, runWrapper)
timer.start();
}

public function runWrapper(evt : Event) : void{
dispatchEvent(new Event(Event.INIT));
}

/** This also works as an assertion that event progress will never be NaN
*/
public function progressHandler(event:ProgressEvent):void {
//var evt : * = event as Object;
var current :Number = Math.floor((event as Object).percentLoaded * 100) /100;
var delta : Number = current - lastProgress;
if (current > lastProgress && delta > 0.099){
lastProgress = current;
if (BulkLoaderTestSuite.LOADING_VERBOSE) trace(current * 100 , "% loaded") ;
}
for each(var propName : String in ["percentLoaded", "weightPercent", "ratioLoaded"] ){
if (isNaN(event[propName]) ){
trace(propName, "is not a number" );
assertFalse(isNaN(event[propName]));
}
}
}



override public function tearDown():void {
_bulkLoader.clear();
BulkLoader.removeAllLoaders();
_bulkLoader = null;
}

public function testHasItemsIsFalseAtStart():void {
assertEquals(numHasItems, 0);
}


public function testHasItemsIsTrueOnLoad() : void{
numHasItems = 0;
for each(var url :String in items){
if (_bulkLoader.hasItem(url)) numHasItems ++;
}
assertEquals(numHasItems, items.length);
}
}
}

0 comments on commit 7790888

Please sign in to comment.