Skip to main content
Participant
Jun 22, 2009 7:03 PM UTC
Question

FocusManager in FlexUnit4?

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 is closed to new replies. Start a new post to keep the conversation going.

15 replies

mverteuil
Participant
Oct 14, 2009 8:04 PM UTC

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.

Translate
Participating Frequently
Jan 21, 2010 5:15 PM UTC

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

Translate
Participating Frequently
Jan 22, 2010 2:17 AM UTC

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

Translate
Participating Frequently
Jun 22, 2009 7:28 PM UTC

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

Translate
Participant
Jun 23, 2009 5:42 PM UTC

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);

}

Translate
Participating Frequently
Jun 23, 2009 7:32 PM UTC

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

Translate