1 19 20 package org.openide.explorer.propertysheet; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.beans.PropertyEditor ; 27 import java.beans.PropertyEditorSupport ; 28 import java.lang.reflect.InvocationTargetException ; 29 import javax.swing.JFrame ; 30 import javax.swing.SwingUtilities ; 31 import org.netbeans.junit.NbTestCase; 32 import org.openide.nodes.PropertySupport; 33 34 40 public class InplaceEditorNoModifyOnTextChangeContractComboEditorTest extends NbTestCase { 41 public InplaceEditorNoModifyOnTextChangeContractComboEditorTest(String name) { 42 super(name); 43 } 44 45 Component edComp = null; 46 PropertyEditor ped = null; 47 InplaceEditor ied = null; 48 ActionEvent [] events = new ActionEvent [10]; 49 Object postSetValuePropertyEdValue=null; 50 Object preSetValuePropertyEdValue=null; 51 Object finalValuePropertyEdValue=null; 52 Object finalInplaceEditorValue=null; 53 54 int i=0; 55 56 private static boolean canRun = ExtTestCase.canSafelyRunFocusTests(); 57 58 private static InplaceEditorFactory factory = new InplaceEditorFactory(true, new ReusablePropertyEnv()); 59 private int idx=0; 60 protected void setUp() throws Exception { 61 if (!canRun) { 62 return; 63 } 64 PropUtils.forceRadioButtons=false; 65 66 tp = new TProperty("TProperty", true); 67 te = new TagsEditor(); 68 69 ActionListener al = new ActionListener () { 70 public void actionPerformed(ActionEvent ae) { 71 events[idx] = ae; 72 } 73 }; 74 75 try { 76 ied = factory.getInplaceEditor(tp, false); 77 edComp = ied.getComponent(); 78 ped = ied.getPropertyEditor(); 79 80 preSetValuePropertyEdValue=ped.getValue(); 81 ied.setValue("newValue"); 82 sleep(); 83 84 postSetValuePropertyEdValue=ped.getValue(); 85 86 edComp = ied.getComponent(); 87 JFrame jf = new JFrame (); 88 jf.getContentPane().add(edComp); 89 new ExtTestCase.WaitWindow(jf); 90 91 new ExtTestCase.WaitFocus(edComp); 92 93 ied.addActionListener(al); 94 95 KeyEvent ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_UP, (char) KeyEvent.VK_UP); 96 dispatchEvent(ke, edComp); 97 sleep(); 98 99 ke = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_UP, (char) KeyEvent.VK_UP); 100 dispatchEvent(ke, edComp); 101 sleep(); 102 103 ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_UP, (char) KeyEvent.VK_UP); 104 dispatchEvent(ke, edComp); 105 sleep(); 106 ke = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_UP, (char) KeyEvent.VK_UP); 107 dispatchEvent(ke, edComp); 108 sleep(); 109 110 ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 111 dispatchEvent(ke, edComp); 112 sleep(); 113 114 ke = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 115 dispatchEvent(ke, edComp); 116 sleep(); 117 sleep(); 118 119 ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 120 dispatchEvent(ke, edComp); 121 sleep(); 122 ke = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 123 dispatchEvent(ke, edComp); 124 sleep(); 125 126 idx++; 127 128 ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, (char) KeyEvent.VK_ESCAPE); 129 dispatchEvent(ke, edComp); 130 sleep(); 131 ke = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, (char) KeyEvent.VK_ESCAPE); 132 dispatchEvent(ke, edComp); 133 sleep(); 134 135 finalInplaceEditorValue = ied.getValue(); 136 finalValuePropertyEdValue = ped.getValue(); 137 jf.hide(); 138 jf.dispose(); 139 ied.removeActionListener(al); 140 ied.clear(); 141 } catch (Exception e) { 142 e.printStackTrace(); 143 fail("FAILED - Exception thrown "+e.getClass().toString()); 144 } 145 } 146 147 private void sleep() throws Exception { 148 Thread.currentThread().sleep(100); 149 if (!SwingUtilities.isEventDispatchThread()) { 150 SwingUtilities.invokeAndWait(new Runnable () { 151 public void run() { 152 System.currentTimeMillis(); 153 } 154 }); 155 } 156 Thread.currentThread().sleep(100); 157 } 158 159 160 private void dispatchEvent(final KeyEvent ke, final Component comp) throws Exception { 161 if (!SwingUtilities.isEventDispatchThread()) { 162 SwingUtilities.invokeAndWait(new Runnable () { 163 public void run() { 164 comp.dispatchEvent(ke); 165 } 166 }); 167 168 } else { 169 comp.dispatchEvent(ke); 170 } 171 sleep(); 172 } 173 174 public void testInplaceEditorSetValueDidNotChangePropertyEditorValue() throws Exception { 175 if (ExtTestCase.canSafelyRunFocusTests()) { 176 assertTrue("PreSetValue value is " + preSetValuePropertyEdValue + " but post value is " + postSetValuePropertyEdValue, preSetValuePropertyEdValue == postSetValuePropertyEdValue); 177 } 178 } 179 180 public void testEnterTriggeredActionSuccess() { 181 if (ExtTestCase.canSafelyRunFocusTests()) { 182 assertTrue("Enter keystroke did not produce an action event", events[0] != null); 183 assertTrue("Action command for faked Enter keystroke should be " + InplaceEditor.COMMAND_SUCCESS + " but is " + events[0].getActionCommand(), InplaceEditor.COMMAND_SUCCESS.equals(events[0].getActionCommand())); 184 } 185 } 186 187 public void testFinalInplaceEditorValue() throws Exception { 188 if (ExtTestCase.canSafelyRunFocusTests()) { 189 assertTrue("Final inplace editor value should be \"c\" but is " + finalInplaceEditorValue, "c".equals(finalInplaceEditorValue)); 190 } 191 } 192 193 public void testEscTriggeredActionFailure() { 194 if (ExtTestCase.canSafelyRunFocusTests()) { 195 assertTrue("Escape keystroke did not produce an action event", events[1] != null); 196 assertTrue("Action command for faked Escape keystroke should be " + InplaceEditor.COMMAND_FAILURE + " but is " + events[1].getActionCommand(), InplaceEditor.COMMAND_FAILURE.equals(events[1].getActionCommand())); 197 } 198 } 199 200 public void testFinalPropertyValueIsUnchanged() { 201 if (ExtTestCase.canSafelyRunFocusTests()) { 202 assertTrue("Final value should be unchanged but is " + finalValuePropertyEdValue, "Value".equals(finalValuePropertyEdValue)); 203 } 204 } 205 206 public class TProperty extends PropertySupport { 208 private String myValue = "Value"; 209 public TProperty(String name, boolean isWriteable) { 211 super(name, String .class, name, "", true, isWriteable); 212 } 213 public Object getValue() { 215 return myValue; 216 } 217 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 219 Object oldVal = myValue; 220 myValue = value.toString(); 221 } 222 public PropertyEditor getPropertyEditor() { 224 return te; 225 } 226 } 227 228 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 229 PropertyEnv env; 230 231 public TagsEditor() { 232 } 233 234 public String [] getTags() { 235 return new String [] {"a","b","c","d","Value"}; 236 } 237 238 public void attachEnv(PropertyEnv env) { 239 this.env = env; 240 } 241 242 public boolean supportsCustomEditor() { 243 return false; 244 } 245 246 public void setValue(Object newValue) { 247 super.setValue(newValue); 248 } 249 } 250 251 private TProperty tp; 252 private TagsEditor te; 253 private String initEditorValue; 254 private String initPropertyValue; 255 private String postChangePropertyValue; 256 private String postChangeEditorValue; 257 } 258 | Popular Tags |