How to exit an air application from an actionscript library?
I am trying the following but every once in awhile the nativeApp is not defined.
var nativeApp:Object = getDefinitionByName("flash.desktop.NativeApplication");
nativeApp.nativeApplication.exit();
I am confused why sometimes getDefinitionByName("flash.desktop.NativeApplication") resolves and other times it does not.
I am trying to resolve this problem to address the following issue in flexcover - code.google.com/p开发者_如何学Python/flexcover/issues/detail?id=33
Update - here is the class I am attempting to fix: http://code.google.com/p/flexcover/source/browse/trunk/CoverageAgent/src/com/allurent/coverage/runtime/AbstractCoverageAgent.as CoverageAgent.swc is an actionscript library called by the unit tests to exit the flexcover air application used to determine the code coverage of the unit tests. The flexcover air application only exits about the half the time and it is causing problems for our maven builds to execute successfully.
NativeApplication.nativeApplication.exit();
In regards to FlexCover - the reason you are seeing it work sometimes and not others is the CoverageAgent is designed to exit the Unit Tests it does not communicate back to the CoverageViewer. I have created my own FlexCoverListener that sends an exit message over local connection to the CoverageViewer. Below is the code.
package org.flexunit.listeners
{
import flash.events.EventDispatcher;
import org.flexunit.listeners.closer.FlexCoverCloser;
import org.flexunit.runner.IDescription;
import org.flexunit.runner.Result;
import org.flexunit.runner.notification.Failure;
import org.flexunit.runner.notification.IAsyncStartupRunListener;
import org.flexunit.runner.notification.ITemporalRunListener;
public class FlexCoverListener extends EventDispatcher implements IAsyncStartupRunListener,
ITemporalRunListener
{
import com.allurent.coverage.runtime.CoverageManager;
public function FlexCoverListener()
{
}
public function get ready():Boolean
{
return true;
}
public function testTimed( description:IDescription, runTime:Number ):void
{
}
public function testRunFinished( result:Result ):void
{
CoverageManager.agent.recordCoverage("SR_TESTS_COMPLETE");
}
public function testFinished( description:IDescription ):void {}
public function testRunStarted( description:IDescription ):void {}
public function testStarted( description:IDescription ):void{}
public function testFailure( failure:Failure ):void{}
public function testAssumptionFailure( failure:Failure ):void{}
public function testIgnored( description:IDescription ):void{}
}
}
You can add the above listener to your tests by doing the following in your TestRunner:
core.addListener(new FlexCoverListener());
var core : FlexUnitCore = new FlexUnitCore();
Last but most importantly I changed the recordCoverage
method in the AbstractCoverageAgent
to look like the following:
/**
* Record the execution of a single coverage key; called by
* the global coverage() function.
*/
public function recordCoverage(key:String):void
{
if(key == "SR_TESTS_COMPLETE")
{
exit();
}
else if (isNaN(coverageMap[key]++))
{
// The map must not have contained this key yet, so enter an
// execution count of 1. Subsequent calls will autoincrement without
// returning NaN.
//
coverageMap[key] = 1;
}
}
nativeAppilcation
is a static field. It does not need to be called on an object. So you do not have to call getDefinitionByName("flash.desktop.NativeApplication")
.
Just invoke exit as follows:
NativeApplication.nativeApplication.exit();
Flash Builder or Flash Pro will include the library for you.
If you are not using the IDE, import the library:
import flash.desktop.NativeApplication;
精彩评论