カスタム コントロールをテストする前に、カスタム コントロールの ActionScript でオートメーション サポート(オートメーションの委譲)を実装し、テスト アプリケーションにコンパイルします。
以下の手順では、Flex のカスタム Spinner コントロールを使用して、カスタム コントロールのオートメーション サポートの実装方法を示します。Spinner カスタム コントロールは、以下のグラフィックに示すように、2 つのボタンと 1 つのテキスト フィールドを含んでいます。
ユーザーは、Down をクリックしてテキスト フィールドに表示されている値を 1 減分させ、Up をクリックしてテキスト フィールドの値を 1 増分させることができます。
package customcontrols
{
import flash.display.DisplayObject;
import mx.automation.Automation;
import customcontrols.SpinnerEvent;
import mx.automation.delegates.containers.BoxAutomationImpl;
import flash.events.Event;
import mx.automation.IAutomationObjectHelper;
import mx.events.FlexEvent;
import flash.events.IEventDispatcher;
import mx.preloaders.DownloadProgressBar;
import flash.events.MouseEvent;
import mx.core.EventPriority;
[Mixin]
public class SpinnerAutomationDelegate extends BoxAutomationImpl
{
public static function init(root:DisplayObject) : void {
// register delegate for the automation
Automation.registerDelegateClass(Spinner, SpinnerAutomationDelegate);
}
public function SpinnerAutomationDelegate(obj:Spinner) {
super(obj);
// listen to the events of interest (for recording)
obj.addEventListener(SpinnerEvent.DECREMENT, decrementHandler);
obj.addEventListener(SpinnerEvent.INCREMENT, incrementHandler);
}
protected function decrementHandler(event : SpinnerEvent) : void {
recordAutomatableEvent(event);
}
protected function incrementHandler(event : SpinnerEvent) : void {
recordAutomatableEvent(event);
}
protected function get spinner() : Spinner {
return uiComponent as Spinner;
}
//----------------------------------
// override functions
//----------------------------------
override public function get automationValue():Array {
return [ spinner.Value.toString() ];
}
private function replayClicks(button : IEventDispatcher, steps : int) : Boolean {
var helper : IAutomationObjectHelper = Automation.automationObjectHelper;
var result : Boolean;
for(var i:int; i < steps; i++) {
helper.replayClick(button);
}
return result;
}
override public function replayAutomatableEvent(event:Event):Boolean {
if(event is SpinnerEvent) {
var spinnerEvent : SpinnerEvent = event as SpinnerEvent;
if(event.type == SpinnerEvent.INCREMENT) {
return replayClicks(spinner.upButton, spinnerEvent.steps);
}
else if(event.type == SpinnerEvent.DECREMENT) {
return replayClicks(spinner.downButton, spinnerEvent.steps);
}
else {
return false;
}
}
else {
return super.replayAutomatableEvent(event);
}
}
// do not expose the child controls (i.e the buttons and the textfield) as individual controls
override public function get numAutomationChildren():int {
return 0;
}
}
}
<?xml version="1.0" encoding="UTF-8"?> <TypeInformation> <ClassInfo Name="FlexSpinner" Extends="FlexBox"> <Implementation Class="customcontrols.Spinner" /> <Events> <Event Name="Decrement"> <Implementation Class="customcontrols.SpinnerEvent" Type="decrement" /> <Property Name="steps"> <PropertyType Type="integer" /> </Property> </Event> <Event Name="Increment"> <Implementation Class="customcontrols.SpinnerEvent" Type="increment" /> <Property Name="steps"> <PropertyType Type="integer" /> </Property> </Event> </Events> <Properties> <Property Name="lowerBound" accessType="read"> <PropertyType Type="integer" /> </Property> <Property Name="upperBound" accessType="read"> <PropertyType Type="integer" /> </Property> <!-- expose read and write access for the Value property --> <Property Name="Value" accessType="both"> <PropertyType Type="integer" /> </Property> <Property Name="stepSize" accessType="read"> <PropertyType Type="integer" /> </Property> </Properties> </ClassInfo> </TypeInformation>