Skip to main content
Participant
June 22, 2009
Question

FocusManager in FlexUnit4?

  • June 22, 2009
  • 2 replies
  • 4900 views

I'm trying to write just a simple test ensuring that a TextInput control has focus when a component is loaded. However focusManager is null even after I've used the UIImpersonator class to setup my component. Am I doing it wrong or is this a bug?

UIImpersonator.addChild(sc);

Assert.assertEquals(sc.itemInput.name, Object(sc.focusManager.getFocus()).name);

This topic has been closed for replies.

2 replies

mverteuil
Participant
October 14, 2009

I have found that injecting the Application.application FocusManager instance into the TestEnvironment container which UIImpersonator uses works well. Set up your test class as such:

[BeforeClass]

public static function setUpClass():void

{

     TestEnvironment.getInstance().focusManager = Application.application.focusManager;

}

[AfterClass]

public static function tearDownClass():void

{

     TestEnvironment.getInstance().focusManager = null

}

Then do everything as you normally do. This worked well for me, as I was having the same issue.

Participating Frequently
January 21, 2010

What happened to the TestEnvironment class in the lastest builds?

I use flexunit-4.0-rc-1 swc file in my project but there is no getInstance() method available for that class

Participating Frequently
January 22, 2010

It has just been renamed. However, if you are using the latest rc1, you should no longer have issues with the focusmanager as before.

If you want to see where this code went, take a look at the

IVisualEnvironmentBuilder interface

VisualTestEnvironmentBuilder

and either FlexEnvironmentBuilder or ActionScriptEnvironmentBuilder dependent upon if you are using this in an ASOnly or Flex environment

Finally, FlexVisualTestEnvironment and ActionScriptVisualTestEnvironment

The new setup is a little more complicated, but it now works with Flex and non-Flex projects to allow UIImpersonation. It also works better in Flex 2, 3 and 4.

Mike

Participating Frequently
June 22, 2009

Calling add child starts the process of creating your component, however, it will not be done (and hence the focusmanager will not exist) for some time. You need to now wait until the component either issues a creationComplete before you can begin testing for this.

Mike

Participant
June 23, 2009

I have a before-method added to the test which I thought would take care of it, however the focusManager is still null...

[Before(async, ui)]
public function setup():void {

            Async.proceedOnEvent(this, sc, FlexEvent.CREATION_COMPLETE, LONG_TIME );
            UIImpersonator.addChild(sc);

}

Participating Frequently
June 23, 2009

I will try to take a look at this tonight and give you a few pointers