<?xml version="1.0"?>
<mx:Application name="lightspark_net_URLLoader_test"
	xmlns:mx="http://www.adobe.com/2006/mxml"
	layout="absolute"
	applicationComplete="appComplete();"
	backgroundColor="white">

<mx:Script>
	<![CDATA[
	import Tests;
	private var remoteLoader:URLLoader;
	private var remoteLoader2:URLLoader;
	private var localLoader:URLLoader;
	private var localLoader2:URLLoader;

	//This is the path for a local file.
	private var localFilePath:String = "test.data";
	//Should contain "Local data\n"

	//This shouldn't be allowed when we implement allowed domains, security policy files
	private var remoteFilePath:String = "http://lightspark.sourceforge.net/test.data";
	//Should contain "Remote data\n"

	private var received:uint = 0;
	private function appComplete():void
	{
		try
		{
			trace(Security.sandboxType);
			if(Security.sandboxType == Security.REMOTE || 
				Security.sandboxType == Security.LOCAL_WITH_NETWORK ||
				Security.sandboxType == Security.LOCAL_TRUSTED) 
			{
				Tests.info("Sandbox allows loading remote files");
				remoteLoader = new URLLoader();
				remoteLoader.addEventListener(IOErrorEvent.IO_ERROR, remoteErrorHandler);
				remoteLoader.addEventListener(Event.COMPLETE, remoteLoaderCompleteHandler);
				remoteLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,remoteSecurityError);
				remoteLoader.load(new URLRequest(remoteFilePath));
				remoteLoader2 = new URLLoader(new URLRequest(remoteFilePath));
				remoteLoader2.addEventListener(IOErrorEvent.IO_ERROR, remoteErrorHandler);
				remoteLoader2.addEventListener(Event.COMPLETE, remoteLoaderCompleteHandler2);
			}
			if(Security.sandboxType == Security.LOCAL_WITH_FILE ||
				Security.sandboxType == Security.LOCAL_TRUSTED)
			{
				Tests.info("Sandbox allows loading local files");
				localLoader = new URLLoader();
				localLoader.addEventListener(IOErrorEvent.IO_ERROR, localErrorHandler);
				localLoader.addEventListener(Event.COMPLETE, localLoaderCompleteHandler);
				localLoader.load(new URLRequest(localFilePath));
				localLoader2 = new URLLoader(new URLRequest(localFilePath));
				localLoader2.addEventListener(IOErrorEvent.IO_ERROR, localErrorHandler);
				localLoader2.addEventListener(Event.COMPLETE, localLoaderCompleteHandler2);
			}
			// Add a timer to let the test fail after a certain amount of time has passed.
			var timeout:Timer = new Timer(2000, 0);
			timeout.addEventListener(TimerEvent.TIMER, killScript);
			timeout.start();
		}
		catch(e:SecurityError)
		{
			Tests.assertDontReach("SecurityError thrown");
			Tests.info("Error:", e.toString());
			Tests.report(visual, this.name);
		}
	}
	private function remoteErrorHandler(e:Event):void
	{
		Tests.assertDontReach("IOErrorEvent.IO_ERROR: an error ocurred (remote)");
		received++
		finishUp();
	}
	private function localErrorHandler(e:Event):void
	{
		Tests.assertDontReach("IOErrorEvent.IO_ERROR: an error ocurred (local)");
		received++
		finishUp();
	}
	private function remoteLoaderCompleteHandler(e:Event):void
	{
		Tests.assertEquals("Remote data\n", remoteLoader.data, "Event.COMPLETE: data received");
		received++
		finishUp();
	}
	private function localLoaderCompleteHandler(e:Event):void
	{
		Tests.assertEquals("Local data\n", localLoader.data, "Event.COMPLETE: data received");
		received++
		finishUp();
	}
	private function remoteLoaderCompleteHandler2(e:Event):void
	{
		Tests.assertEquals("Remote data\n", remoteLoader2.data, "Event.COMPLETE: data received");
		received++
		finishUp();
	}
	private function localLoaderCompleteHandler2(e:Event):void
	{
		Tests.assertEquals("Local data\n", localLoader2.data, "Event.COMPLETE: data received");
		received++
		finishUp();
	}
	private function remoteSecurityError(e:Event):void
	{
		finishUp();
	}
	private function finishUp():void
	{
		if((Security.sandboxType == Security.LOCAL_TRUSTED && received == 4) || (Security.sandboxType != Security.LOCAL_TRUSTED && received == 2))
			Tests.report(visual, this.name);
		else if(Security.sandboxType == Security.LOCAL_TRUSTED && received > 4)
		{
			Tests.assertEquals(4, received);
			Tests.report(visual, this.name);
		}
		else if(Security.sandboxType != Security.LOCAL_TRUSTED && received > 2)
		{
			Tests.assertEquals(2, received);
			Tests.report(visual, this.name);
		}
	}
	private function killScript(event:TimerEvent):void
	{
		Tests.assertDontReach("Test timed out. Probably some request was never made.");
		Tests.report(visual, this.name);
	}
	]]>
</mx:Script>


<mx:UIComponent id="visual" />

</mx:Application>
