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 InplaceEditorNoModifyOnTextChangeContractStringEditorTest extends NbTestCase { 41 public InplaceEditorNoModifyOnTextChangeContractStringEditorTest(String name) { 42 super(name); 43 } 44 45 static Component edComp = null; 46 static PropertyEditor ped = null; 47 static InplaceEditor ied = null; 48 static ActionEvent [] events = new ActionEvent [10]; 49 static Object postSetValuePropertyEdValue=null; 50 static Object preSetValuePropertyEdValue=null; 51 static Object finalValuePropertyEdValue=null; 52 53 private static int idx=0; 54 private static InplaceEditorFactory factory = new InplaceEditorFactory(true, new ReusablePropertyEnv()); 55 56 static boolean canRun = ExtTestCase.canSafelyRunFocusTests(); 57 58 protected void setUp() throws Exception { 59 if (!canRun) { 60 return; 61 } 62 PropUtils.forceRadioButtons=false; 63 if (idx != 0) return; 64 tp = new TProperty("TProperty", true); 66 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 83 postSetValuePropertyEdValue=ped.getValue(); 84 85 edComp = ied.getComponent(); 86 JFrame jf = new JFrame (); 87 jf.getContentPane().add(edComp); 88 jf.setSize(200,200); 89 new ExtTestCase.WaitWindow(jf); 90 91 new ExtTestCase.WaitFocus(edComp); 92 93 ied.addActionListener(al); 94 sleep(); 95 96 final KeyEvent ke = new KeyEvent (edComp, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 97 SwingUtilities.invokeAndWait(new Runnable () { 98 public void run() { 99 edComp.dispatchEvent(ke); 100 } 101 }); 102 sleep(); 103 final KeyEvent ke2 = new KeyEvent (edComp, KeyEvent.KEY_RELEASED, System.currentTimeMillis(), 0, KeyEvent.VK_ENTER, (char) KeyEvent.VK_ENTER); 104 SwingUtilities.invokeAndWait(new Runnable () { 105 public void run() { 106 edComp.dispatchEvent(ke2); 107 } 108 }); 109 sleep(); 110 sleep(); 111 sleep(); 112 113 idx++; 114 finalValuePropertyEdValue = ped.getValue(); 115 jf.hide(); 116 jf.dispose(); 117 ied.removeActionListener(al); 118 ied.clear(); 119 } catch (Exception e) { 120 e.printStackTrace(); 121 fail("FAILED - Exception thrown "+e.getClass().toString()); 122 } 123 } 124 125 private void sleep() { 126 try { 127 ExtTestCase.sleep(); 128 } catch (Exception e) { 129 } 131 } 132 133 public void testInplaceEditorSetValueDidNotChangePropertyEditorValue() throws Exception { 134 if (!canRun) return; 135 assertTrue("PreSetValue value is " + preSetValuePropertyEdValue + " but post value is " + postSetValuePropertyEdValue, preSetValuePropertyEdValue == postSetValuePropertyEdValue); 136 } 137 138 public void testEnterTriggeredActionSuccess() { 139 if (!canRun) return; 140 try { 141 Thread.currentThread().sleep(2000); 142 } catch (Exception e) { 143 } 144 assertTrue("Enter keystroke did not produce an action event", events[0] != null); 145 assertTrue("Action command for faked Enter keystroke should be " + InplaceEditor.COMMAND_SUCCESS + " but is " + events[0].getActionCommand(), InplaceEditor.COMMAND_SUCCESS.equals(events[0].getActionCommand())); 146 } 147 148 public void testFinalPropertyValueIsUnchanged() { 149 if (!canRun) return; 150 assertTrue("Final value should be unchanged but is " + finalValuePropertyEdValue, "Value".equals(finalValuePropertyEdValue)); 151 } 152 153 public class TProperty extends PropertySupport { 155 private String myValue = "Value"; 156 public TProperty(String name, boolean isWriteable) { 158 super(name, String .class, name, "", true, isWriteable); 159 } 160 public Object getValue() { 162 return myValue; 163 } 164 public void setValue(Object value) throws IllegalArgumentException ,IllegalAccessException , InvocationTargetException { 166 Object oldVal = myValue; 167 myValue = value.toString(); 168 } 169 public PropertyEditor getPropertyEditor() { 171 return te; 172 } 173 } 174 175 public class TagsEditor extends PropertyEditorSupport implements ExPropertyEditor { 176 PropertyEnv env; 177 178 public TagsEditor() { 179 } 180 181 public void attachEnv(PropertyEnv env) { 182 this.env = env; 183 } 184 185 public boolean supportsCustomEditor() { 186 return false; 187 } 188 189 public void setValue(Object newValue) { 190 super.setValue(newValue); 191 } 192 } 193 194 private TProperty tp; 195 private TagsEditor te; 196 private String initEditorValue; 197 private String initPropertyValue; 198 private String postChangePropertyValue; 199 private String postChangeEditorValue; 200 } 201 | Popular Tags |