001: package abbot.script;
002:
003: import java.lang.reflect.Method;
004: import java.awt.*;
005: import java.awt.event.*;
006: import java.util.HashMap;
007: import javax.swing.JList;
008:
009: import junit.extensions.abbot.*;
010: import abbot.finder.Hierarchy;
011: import abbot.tester.*;
012:
013: public class ActionTest extends ResolverFixture {
014:
015: public void testMethodFixing() throws Throwable {
016: Action action = new Action(getResolver(), getName(), "Click",
017: new String[0]);
018: assertEquals("Method name should be fixed", "actionClick",
019: action.getMethodName());
020: }
021:
022: private class FakeRef extends ComponentReference {
023: public FakeRef(Resolver r) {
024: this (r, Component.class);
025: }
026:
027: public FakeRef(Resolver r, Class cls) {
028: super (r, cls, new HashMap());
029: }
030:
031: // Fake the lookup; we don't care about the result
032: public Component getComponent(Hierarchy h) {
033: return null;
034: }
035: }
036:
037: private class MyAction extends Action {
038: public MyAction(Resolver r, String method, String[] args,
039: Class cls) {
040: super (r, getName(), method, args, cls);
041: }
042:
043: public MyAction(Resolver r, String method, String[] args) {
044: this (r, method, args, Component.class);
045: }
046:
047: public Object[] getParameters() throws Exception {
048: return evaluateParameters(getMethod(), getArguments());
049: }
050: }
051:
052: public void testVKPlusModifier() throws Exception {
053: ComponentReference ref = new FakeRef(getResolver());
054: MyAction action = new MyAction(getResolver(),
055: "actionKeyStroke", new String[] { ref.getID(), "VK_A",
056: "SHIFT_MASK", });
057: Object[] params = action.getParameters();
058: assertEquals("Second parameter should be an integer",
059: new Integer(KeyEvent.VK_A), params[1]);
060: assertEquals("Third parameter should be an integer",
061: new Integer(KeyEvent.SHIFT_MASK), params[2]);
062: }
063:
064: public void testComponentPlusVK() throws Exception {
065: ComponentReference ref = new ComponentReference(getResolver(),
066: Component.class, new HashMap());
067: MyAction action = new MyAction(getResolver(),
068: "actionKeyStroke",
069: new String[] { ref.getID(), "VK_X", });
070: Method method = action.getMethod();
071: Class[] params = method.getParameterTypes();
072: assertEquals("First parameter should be a Component",
073: Component.class, params[0]);
074: assertEquals("Second parameter should be an integer",
075: int.class, params[1]);
076: }
077:
078: public void testMultipleModifiers() throws Exception {
079: MyAction action = new MyAction(getResolver(),
080: "actionKeyStroke", new String[] { "VK_B",
081: "SHIFT_MASK|ALT_MASK", });
082: Object[] params = action.getParameters();
083: assertEquals("First parameter should be an integer",
084: new Integer(KeyEvent.VK_B), params[0]);
085: assertEquals("Second parameter should be an integer",
086: new Integer(KeyEvent.SHIFT_MASK | KeyEvent.ALT_MASK),
087: params[1]);
088: }
089:
090: public void testVKArgument() throws Exception {
091: MyAction action = new MyAction(getResolver(),
092: "actionKeyStroke", new String[] { "VK_C", });
093: Object[] params = action.getParameters();
094: assertEquals("First parameter should be an integer",
095: new Integer(KeyEvent.VK_C), params[0]);
096: }
097:
098: public void testComponentLocationMethod() throws Throwable {
099: ComponentReference ref = new FakeRef(getResolver());
100: MyAction action = new MyAction(getResolver(), "Click",
101: new String[] { ref.getID(), "(0,0)" });
102: String[] args = action.getArguments();
103: assertEquals("Wrong component reference ID", ref.getID(),
104: args[0]);
105: Object[] params = action.getParameters();
106: assertEquals("Second parameter should be a ComponentLocation",
107: new ComponentLocation(new Point(0, 0)), params[1]);
108: }
109:
110: public void testComponentLocationWithSubstitution()
111: throws Exception {
112: final String PROPNAME = "user.name";
113: ComponentReference ref = new FakeRef(getResolver(), JList.class);
114: MyAction action = new MyAction(
115: getResolver(),
116: "Click",
117: new String[] { ref.getID(), "\"${" + PROPNAME + "}\"" },
118: JListTester.class);
119: String[] args = action.getArguments();
120: Object[] params = action.getParameters();
121: assertEquals("Wrong target class", JListTester.class, action
122: .getTargetClass());
123: assertEquals("Second parameter should be a ComponentLocation",
124: new JListLocation(System.getProperty(PROPNAME)),
125: params[1]);
126: }
127:
128: public ActionTest(String name) {
129: super (name);
130: }
131:
132: public static void main(String[] args) {
133: TestHelper.runTests(args, ActionTest.class);
134: }
135: }
|