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