3 Replies Latest reply: Feb 29, 2012 10:12 AM by pup500 RSS

    how to access stage in flexunit

    Jessica Shizuku

      I've found in the following post (http://forums.adobe.com/message/2371850#2371850) some suggestions about this topic, but as I'm relatively new to as3, I'm still confused about how to make it work. My team has an as3 only project, and we refer to the stage very often this way:

       

      package clipper{

           ...

           public function init() {

                onInit();

                ...

           }

       

           private function onInit() {

                this.stage.frameRate = 24;

                this.stage.addChild(...);

                ...

           }

      }

       

      Everytime when my testcases call statements like this, it'll give error saying "null object reference", etc. As far as I've read in the other post, I should initiate a VisualTestEnvironment in the test runner, and use the UIImpersonator in the test cases, am I right? If I wanna write a testcase for function init as below, how shall I modify the codes? What shall I add in the mxml test runner? Thanks. I'm building and running the tests with ant.

       

      var _clip:clipper;

       

      [Before]

      public function setUp():void {

           _clip = new clipper();

           UIImpersonator.addChild(_clip);

      }

       

      [Test(async)]

      public function testInit():void{

           _clip.init();

           assert...

      }

        • 1. Re: how to access stage in flexunit
          Michael Labriola Community Member

          Not sure if you are using Flash Builder or another tool. First thing, when you are running your tests, you need to provide FlexUnit a reference to the stage:

           

          It would look something like this:

           

          core = new FlexUnitCore();

          core.visualDisplayRoot = this;

           

          /// listeners or other junk

           

          core.run( MyTests );

           

          In AS3 you can say this.stage inside of any visual class such as a movie clip or sprite that is on the stage, but only visual classes get this property. Since FlexUnit tests aren't visual, you don't get that benefit. The reason you are getting the null error is that the visualDisplayRoot property is not being set.

           

          Mike

          • 2. Re: how to access stage in flexunit
            Jessica Shizuku Community Member

            Thanks, Michael! I don't use FlashBuilder, just using some text editor, and I'm using the mxml test runner as in the official manual.

             

            Do I still need to use VisualTestEnvironmentBuilder.getInstance(this) anywhere in the runner like you said in the other post? And after I add core.visualDisplayRoot = this in the test runner, shall I add instance of my custom class to the stage in the test cases using UIImpersonator.addChild(_instance) or this.stage.addChild(_instance)? Currently I will get "Error #1034: Type Coercion failed: cannot convert org.fluint.uiImpersonation.actionScript::ActionScriptVisualTestEnvironment@6b60089 to mx.core.IUIComponent." if I use UIImpersonator.addChild(_instance).

            • 3. Re: how to access stage in flexunit
              pup500 Community Member

              I found this BDD project page, which includes a stage access example:

              Cuke4AS3 https://github.com/flashquartermaster/Cuke4AS3/

               

              [EDIT]

              After much research and complication, I ended up using a Singleton that provides a stage reference that was passed in from a `creationComplete` handler in my TestRunner.

               

              =======TestRunner.mxml==========================================

               

              <?xml version="1.0" encoding="utf-8"?>

              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                  creationComplete="onCreationComplete()"

                  xmlns:flexUnitUIRunner="http://www.adobe.com/2009/flexUnitUIRunner"

                  styleName="flexUnitApplication"

                  layout="absolute" height="800" width="1000">

                  <mx:Script>

                      <![CDATA[

                          import org.flexunit.runner.FlexUnitCore;

                          private var core:FlexUnitCore;

                          public function onCreationComplete():void {

                              core = new FlexUnitCore();

                              core.addListener( new TraceListener() );

                              core.addListener( new UIListener( uiListener ));

                              new StageLocator(systemManager.stage);

                              core.run(MyTestSuite);

                          }

                      ]]>

                  </mx:Script>

                  <mx:Style>

                      Application {

                             backgroundColor: #3872b2;

                             backgroundGradientColors: #3872b2, #0c1a3d;

                             backgroundGradientAlphas: 1, 1;

                             themeColor: #ffffff;

                             color: #444444;

                             fontFamily: "Myriad Pro Semibold";

                             fontSize: 12;

                          }

                  </mx:Style>

                  <flexUnitUIRunner:TestRunnerBase id="uiListener" width="100%" height="100%" />

              </mx:Application>

               

              =======StageLocator.as==========================================

               

              package {

              import flash.display.Stage;

              public class StageLocator {

                  public static var instance:StageLocator;

                  public static var stage:Stage;

                  public function StageLocator ($stage:Stage) {

                      instance = this;

                      stage = $stage;

                  }

              }

              }