Friday, June 12, 2009

It IS possible to do this in an AIR Application...!

A couple hours ago, I posted a question regarding loading classes from a local SWF file. I poked around a bit more thinking that the problem had something to do with the ApplicationDomain. I took another look at the LiveDocs for ApplicationDomain and tried out the example on that screen without any luck. I even commented on the LiveDoc page. I was about to go file a bug but figured I would do one more Google search on the subject. I found this a post at that seemed to describe the same problem. One on the replies mentioned that he was able to work around the problem by putting a half second delay betweeen the loading of the swf and any attempt to access the classes. So, I took the ClassLoader example from the ApplicationDomain LiveDoc example and added a 500ms delay to the complete handler:

private function completeHandler(e:Event):void {
setTimeout(function(...params) {
params[0].dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
}, 1000, this);

After that small change I was able to load the classes from the external SWF. However, this doesn't solve the FlexUnit problem because internally, FlexUnit loads the test classes using getDefinitionByName(). Even if you pass the test cases in as Classes, it gets the qualified name and then calls getDefinitionByName() resulting in the error.

Is this possible in an AIR application?

I have what appears to be a common question regarding a requirement that has come up two of my current AIR application projects. Here is the basic requirement.
The ability for an installed AIR application, to access and load Classes from a separate SWF that is located on the file system
I consider this to be a "plugin" architecture. Let me give you a concrete example. I am working on various AIR projects at any one time. For each I write FlexUnit tests. Because my tests are used to test my AIR applications, they need access to the AIR specific libraries. Rather than compile a unique FlexUnitRunner for each set of tests, I would like to create a single FlexUnitRunner application that allows me to load and run the tests from any of my projects.

I want to be able to have my test projects get built automatically when I change a class in FlexBuilder and then be able to hit a "refresh" button on my FlexUnitRunner application to rerun the tests with the changes.

My initial thought was that I could package my unit tests in a SWC and then have my runner unzip the SWC, pull out the library.swf, load it using RSLItem, SWFLoader or Loader. Then get amd instantiate my TestSuite from that set of tests and run it. This seemed to work in AIR 1.1 but doesn't work in AIR 1.5. The result in 1.5 is always "the Variable PreSaveProcessorTest is not defined"

My second thought was to compile a Module and load that at run time. I can load the module, but I can't figure out how to use AIR specific classes File, FileStream, etc. in the module. There is no option to "include Adobe AIR Libraries" like there is when compiling an SWC.

If I put my tests in a SWC project and reference them from my Module, the module can't use any of my classes that reference File for example. This isn't necessarily needed for my FlexUnit problem.

But if I reference the SWC project from my Module and try to get an instance of my ModuleTestSuite class, I can instantiate it. However I then get a FlexUnit error when I try to my suite that says: Property metaData not found on ModuleTestSuite and there is no default value.

I have read many articles and posts regarding this, but none do exactly what I want:

Has anyone actually got a working example of how this kind of thing can be done in AIR 1.5?

This RSLItem method is exactly what I was doing in Klok, to allow installation of my 3rd party app connectors at runtime. However, that no longer seems to work.

If someone from Adobe can show a working example that would be really helpful. I imagine there are quite a number of people wondering how to create a "plugin" architecture.

UPDATE: I resolved the issue with the FlexUnit error but I am now back to "the Variable PreSaveProcessorTest is not defined". I think the issue is that FlexUnit is doing a getDefinitionByName() which seems to be the problem.